diff --git a/server/.idea/misc.xml b/server/.idea/misc.xml index 90190a5..5c824b3 100644 --- a/server/.idea/misc.xml +++ b/server/.idea/misc.xml @@ -1,4 +1,7 @@ + + \ No newline at end of file diff --git a/server/src/it/scala/com/jackpf/locationhistory/server/AdminTest.scala b/server/src/it/scala/com/jackpf/locationhistory/server/AdminTest.scala index 055faf8..eef7bd4 100644 --- a/server/src/it/scala/com/jackpf/locationhistory/server/AdminTest.scala +++ b/server/src/it/scala/com/jackpf/locationhistory/server/AdminTest.scala @@ -6,6 +6,7 @@ import com.jackpf.locationhistory.admin_service.{ DeleteDeviceRequest, DeleteDeviceResponse, ListDevicesRequest, + ListLocationsRequest, LoginRequest, SendNotificationRequest, SendNotificationResponse @@ -15,9 +16,11 @@ import com.jackpf.locationhistory.beacon_service.{ RegisterDeviceRequest, RegisterDeviceResponse, RegisterPushHandlerRequest, - RegisterPushHandlerResponse + RegisterPushHandlerResponse, + SetLocationRequest, + SetLocationResponse } -import com.jackpf.locationhistory.common.{Device, PushHandler} +import com.jackpf.locationhistory.common.{Device, Location, PushHandler, StoredLocation} import com.jackpf.locationhistory.server.testutil.{GrpcMatchers, IntegrationTest, TestServer} import io.grpc.Status.Code import org.mockito.ArgumentMatchers.{any, anyString} @@ -108,12 +111,10 @@ class AdminTest extends IntegrationTest with GrpcMatchers { } "fail to approve already approved device" >> in(new RegisteredDeviceContext {}) { context => - // First approval should succeed context.adminClient.approveDevice( ApproveDeviceRequest(deviceId = context.device.id) ) === ApproveDeviceResponse(success = true) - // Second approval should fail context.adminClient.approveDevice( ApproveDeviceRequest(deviceId = context.device.id) ) must throwAGrpcRuntimeException( @@ -200,5 +201,66 @@ class AdminTest extends IntegrationTest with GrpcMatchers { ) } } + + "list locations endpoint" >> { + trait ApprovedDeviceContext extends RegisteredDeviceContext { + adminClient.approveDevice( + ApproveDeviceRequest(deviceId = device.id) + ) === ApproveDeviceResponse(success = true) + } + + "list locations with metadata fields" >> in(new ApprovedDeviceContext {}) { context => + val timestamp = System.currentTimeMillis() + val location = Location(lat = 51.5007, lon = -0.1246, accuracy = 10.0) + + context.client.setLocation( + SetLocationRequest( + timestamp = timestamp, + deviceId = context.device.id, + location = Some(location) + ) + ) === SetLocationResponse(success = true) + + val response = context.adminClient.listLocations( + ListLocationsRequest(deviceId = context.device.id) + ) + + response.locations must haveSize(1) + val storedLocation = response.locations.head + storedLocation.location must beSome(location) + storedLocation.startTimestamp === timestamp + storedLocation.endTimestamp === timestamp + storedLocation.count === 1L + } + + "list locations with updated metadata after duplicates" >> in(new ApprovedDeviceContext {}) { + context => + context.client.setLocation( + SetLocationRequest( + timestamp = 1000L, + deviceId = context.device.id, + location = Some(Location(lat = 51.5007, lon = -0.1246, accuracy = 10.0)) + ) + ) === SetLocationResponse(success = true) + + context.client.setLocation( + SetLocationRequest( + timestamp = 2000L, + deviceId = context.device.id, + location = Some(Location(lat = 51.5007, lon = -0.1246, accuracy = 10.0)) + ) + ) === SetLocationResponse(success = true) + + val response = context.adminClient.listLocations( + ListLocationsRequest(deviceId = context.device.id) + ) + + response.locations must haveSize(1) + val storedLocation = response.locations.head + storedLocation.startTimestamp === 1000L + storedLocation.endTimestamp === 2000L + storedLocation.count === 2L + } + } } } diff --git a/server/src/it/scala/com/jackpf/locationhistory/server/LocationTest.scala b/server/src/it/scala/com/jackpf/locationhistory/server/LocationTest.scala index e0427dc..188909d 100644 --- a/server/src/it/scala/com/jackpf/locationhistory/server/LocationTest.scala +++ b/server/src/it/scala/com/jackpf/locationhistory/server/LocationTest.scala @@ -103,7 +103,9 @@ class LocationTest extends IntegrationTest with GrpcMatchers { listLocationsResponse.locations must haveSize(1) listLocationsResponse.locations.head === StoredLocation( location = Some(location), - timestamp = timestamp + startTimestamp = timestamp, + endTimestamp = timestamp, + count = 1L ) } @@ -134,11 +136,15 @@ class LocationTest extends IntegrationTest with GrpcMatchers { listLocationsResponse.locations === Seq( StoredLocation( location = Some(Location(lat = 51.500800, lon = -0.124500, accuracy = 0.2)), - timestamp = 3L + startTimestamp = 1L, + endTimestamp = 3L, + count = 3L ), StoredLocation( location = Some(Location(lat = 35.659500, lon = 139.700500, accuracy = 0.1)), - timestamp = 4L + startTimestamp = 4L, + endTimestamp = 4L, + count = 1L ) ) } diff --git a/server/src/main/scala/com/jackpf/locationhistory/server/model/StoredLocation.scala b/server/src/main/scala/com/jackpf/locationhistory/server/model/StoredLocation.scala index 36bac72..bc4414c 100644 --- a/server/src/main/scala/com/jackpf/locationhistory/server/model/StoredLocation.scala +++ b/server/src/main/scala/com/jackpf/locationhistory/server/model/StoredLocation.scala @@ -3,13 +3,42 @@ package com.jackpf.locationhistory.server.model import com.jackpf.locationhistory.common.StoredLocation as ProtoStoredLocation object StoredLocation { - def fromLocation(location: Location, id: Long, timestamp: Long): StoredLocation = - StoredLocation(id, location, timestamp) + object Metadata { + def initial(timestamp: Long): Metadata = + Metadata(startTimestamp = timestamp, endTimestamp = timestamp, count = 1L) + } + + case class Metadata( + startTimestamp: Long, + endTimestamp: Long, + count: Long + ) { + def updated(newTimestamp: Long): Metadata = { + // Handle mismatched newTimestamp (e.g. out-of-order events) + val newStartTimestamp = math.min(startTimestamp, newTimestamp) + val newEndTimestamp = math.max(endTimestamp, newTimestamp) + + copy(startTimestamp = newStartTimestamp, endTimestamp = newEndTimestamp, count = count + 1) + } + } + + def fromLocation( + location: Location, + id: Long, + metadata: Metadata + ): StoredLocation = + StoredLocation(id, location, metadata) } -case class StoredLocation(id: Long, location: Location, timestamp: Long) { +case class StoredLocation( + id: Long, + location: Location, + metadata: StoredLocation.Metadata +) { def toProto: ProtoStoredLocation = ProtoStoredLocation( location = Some(location.toProto), - timestamp = timestamp + startTimestamp = metadata.startTimestamp, + endTimestamp = metadata.endTimestamp, + count = metadata.count ) } diff --git a/server/src/main/scala/com/jackpf/locationhistory/server/repo/InMemoryLocationRepo.scala b/server/src/main/scala/com/jackpf/locationhistory/server/repo/InMemoryLocationRepo.scala index 5d52b3c..5657171 100644 --- a/server/src/main/scala/com/jackpf/locationhistory/server/repo/InMemoryLocationRepo.scala +++ b/server/src/main/scala/com/jackpf/locationhistory/server/repo/InMemoryLocationRepo.scala @@ -28,10 +28,10 @@ class InMemoryLocationRepo(maxItemsPerDevice: Long = DefaultMaxItemsPerDevice) override def storeDeviceLocation( deviceId: DeviceId.Type, location: Location, - timestamp: Long + metadata: StoredLocation.Metadata ): Future[Try[Unit]] = Future.successful { val storedLocation = - StoredLocation.fromLocation(location, id = generateId(), timestamp = timestamp) + StoredLocation.fromLocation(location, id = generateId(), metadata = metadata) storedLocations.updateWith(deviceId) { case Some(existingLocations) => diff --git a/server/src/main/scala/com/jackpf/locationhistory/server/repo/LocationRepo.scala b/server/src/main/scala/com/jackpf/locationhistory/server/repo/LocationRepo.scala index 2689a13..f2b92bb 100644 --- a/server/src/main/scala/com/jackpf/locationhistory/server/repo/LocationRepo.scala +++ b/server/src/main/scala/com/jackpf/locationhistory/server/repo/LocationRepo.scala @@ -11,7 +11,7 @@ trait LocationRepo extends LocationRepoExtensions { def storeDeviceLocation( deviceId: DeviceId.Type, location: Location, - timestamp: Long + metadata: StoredLocation.Metadata ): Future[Try[Unit]] def getForDevice(deviceId: DeviceId.Type, limit: Option[Int]): Future[Vector[StoredLocation]] diff --git a/server/src/main/scala/com/jackpf/locationhistory/server/repo/LocationRepoExtensions.scala b/server/src/main/scala/com/jackpf/locationhistory/server/repo/LocationRepoExtensions.scala index 9ca1f42..935e019 100644 --- a/server/src/main/scala/com/jackpf/locationhistory/server/repo/LocationRepoExtensions.scala +++ b/server/src/main/scala/com/jackpf/locationhistory/server/repo/LocationRepoExtensions.scala @@ -11,14 +11,13 @@ object LocationRepoExtensions { } trait LocationRepoExtensions { self: LocationRepo => - // TODO We might want to update an endTimestamp and count so we don't lose info of when the location was first seen private def updatePreviousLocation( newLocation: Location, newTimestamp: Long, storedLocation: StoredLocation ): StoredLocation = storedLocation.copy( location = newLocation, - timestamp = newTimestamp + metadata = storedLocation.metadata.updated(newTimestamp) ) /** Note that this is a "best effort" approach and not strictly thread safe: @@ -43,7 +42,7 @@ trait LocationRepoExtensions { self: LocationRepo => storedLocation => updatePreviousLocation(location, timestamp, storedLocation) ) case _ => - storeDeviceLocation(deviceId, location, timestamp) + storeDeviceLocation(deviceId, location, StoredLocation.Metadata.initial(timestamp)) } } diff --git a/server/src/main/scala/com/jackpf/locationhistory/server/repo/SQLiteLocationRepo.scala b/server/src/main/scala/com/jackpf/locationhistory/server/repo/SQLiteLocationRepo.scala index 761d5e5..e1134fe 100644 --- a/server/src/main/scala/com/jackpf/locationhistory/server/repo/SQLiteLocationRepo.scala +++ b/server/src/main/scala/com/jackpf/locationhistory/server/repo/SQLiteLocationRepo.scala @@ -19,13 +19,19 @@ private case class StoredLocationRow( lat: Double, lon: Double, accuracy: Double, - timestamp: Long, + startTimestamp: Long, + endTimestamp: Long, + count: Long, metadata: JsonColumn[Map[String, String]] ) { def toStoredLocation: StoredLocation = StoredLocation( id = id, location = Location(lat = lat, lon = lon, accuracy = accuracy, metadata.value), - timestamp = timestamp + metadata = StoredLocation.Metadata( + startTimestamp = startTimestamp, + endTimestamp = endTimestamp, + count = count + ) ) } private object StoredLocationTable extends SimpleTable[StoredLocationRow] @@ -42,12 +48,14 @@ class SQLiteLocationRepo(db: DbClient.DataSource)(using executionContext: Execut lat DOUBLE, lon DOUBLE, accuracy DOUBLE, - timestamp UNSIGNED BIG INT, + start_timestamp UNSIGNED BIG INT, + end_timestamp UNSIGNED BIG INT, + count UNSIGNED BIG INT, metadata TEXT );""" ) val _ = db.updateRaw( - """CREATE INDEX IF NOT EXISTS idx_device_time ON stored_location_table (device_id, timestamp);""" + """CREATE INDEX IF NOT EXISTS idx_device_time ON stored_location_table (device_id, end_timestamp);""" ) } } @@ -56,7 +64,7 @@ class SQLiteLocationRepo(db: DbClient.DataSource)(using executionContext: Execut override def storeDeviceLocation( deviceId: DeviceId.Type, location: Location, - timestamp: Long + metadata: StoredLocation.Metadata ): Future[Try[Unit]] = Future { db.transaction { implicit db => Try { @@ -67,8 +75,10 @@ class SQLiteLocationRepo(db: DbClient.DataSource)(using executionContext: Execut _.lat := location.lat, _.lon := location.lon, _.accuracy := location.accuracy, - _.timestamp := timestamp, - _.metadata := JsonColumn(location.metadata) + _.metadata := JsonColumn(location.metadata), + _.startTimestamp := metadata.startTimestamp, + _.endTimestamp := metadata.endTimestamp, + _.count := metadata.count ) ) () @@ -87,7 +97,7 @@ class SQLiteLocationRepo(db: DbClient.DataSource)(using executionContext: Execut { val q = StoredLocationTable.select .filter(_.deviceId === deviceId.toString) - .sortBy(_.timestamp) + .sortBy(_.endTimestamp) .desc limit match { @@ -129,8 +139,10 @@ class SQLiteLocationRepo(db: DbClient.DataSource)(using executionContext: Execut _.lat := updatedStoredDevice.location.lat, _.lon := updatedStoredDevice.location.lon, _.accuracy := updatedStoredDevice.location.accuracy, - _.timestamp := updatedStoredDevice.timestamp, - _.metadata := JsonColumn(updatedStoredDevice.location.metadata) + _.metadata := JsonColumn(updatedStoredDevice.location.metadata), + _.startTimestamp := updatedStoredDevice.metadata.startTimestamp, + _.endTimestamp := updatedStoredDevice.metadata.endTimestamp, + _.count := updatedStoredDevice.metadata.count ) ) } @@ -175,7 +187,7 @@ class SQLiteLocationRepo(db: DbClient.DataSource)(using executionContext: Execut val result = db.runSql[StoredLocationRow](sql""" SELECT * FROM ( SELECT *, - ROW_NUMBER() OVER (PARTITION BY device_id ORDER BY timestamp DESC) as rn + ROW_NUMBER() OVER (PARTITION BY device_id ORDER BY end_timestamp DESC) as rn FROM stored_location_table WHERE device_id IN ($deviceIds) ) diff --git a/server/src/test/scala/com/jackpf/locationhistory/server/grpc/AdminServiceImplTest.scala b/server/src/test/scala/com/jackpf/locationhistory/server/grpc/AdminServiceImplTest.scala index 6b80dc0..3c93b9f 100644 --- a/server/src/test/scala/com/jackpf/locationhistory/server/grpc/AdminServiceImplTest.scala +++ b/server/src/test/scala/com/jackpf/locationhistory/server/grpc/AdminServiceImplTest.scala @@ -239,13 +239,13 @@ class AdminServiceImplTest(implicit ee: ExecutionEnv) 1L, MockModels .location(lat = 0.1, lon = 0.2, accuracy = 0.3, metadata = Map("k1" -> "v1")), - timestamp = 1L + model.StoredLocation.Metadata(startTimestamp = 1L, endTimestamp = 2L, count = 3L) ), MockModels.storedLocation( 2L, MockModels .location(lat = 0.4, lon = 0.5, accuracy = 0.6, metadata = Map("k2" -> "v2")), - timestamp = 2L + model.StoredLocation.Metadata.initial(2L) ) ) ) @@ -255,11 +255,15 @@ class AdminServiceImplTest(implicit ee: ExecutionEnv) Seq( StoredLocation( Some(Location(lat = 0.1, lon = 0.2, accuracy = 0.3, metadata = Map("k1" -> "v1"))), - timestamp = 1L + startTimestamp = 1L, + endTimestamp = 2L, + count = 3L ), StoredLocation( Some(Location(lat = 0.4, lon = 0.5, accuracy = 0.6, metadata = Map("k2" -> "v2"))), - timestamp = 2L + startTimestamp = 2L, + endTimestamp = 2L, + count = 1L ) ) ) diff --git a/server/src/test/scala/com/jackpf/locationhistory/server/repo/InMemoryLocationRepoTest.scala b/server/src/test/scala/com/jackpf/locationhistory/server/repo/InMemoryLocationRepoTest.scala index 295945e..ec8bb6e 100644 --- a/server/src/test/scala/com/jackpf/locationhistory/server/repo/InMemoryLocationRepoTest.scala +++ b/server/src/test/scala/com/jackpf/locationhistory/server/repo/InMemoryLocationRepoTest.scala @@ -1,6 +1,6 @@ package com.jackpf.locationhistory.server.repo -import com.jackpf.locationhistory.server.model.DeviceId +import com.jackpf.locationhistory.server.model.{DeviceId, StoredLocation} import com.jackpf.locationhistory.server.testutil.MockModels import org.specs2.concurrent.ExecutionEnv @@ -21,7 +21,7 @@ class InMemoryLocationRepoTest(implicit ee: ExecutionEnv) extends LocationRepoTe context.locationRepo.storeDeviceLocation( deviceId, MockModels.location(), - ts + StoredLocation.Metadata.initial(ts) ) { @@ -35,7 +35,7 @@ class InMemoryLocationRepoTest(implicit ee: ExecutionEnv) extends LocationRepoTe locations <- context.locationRepo.getForDevice(deviceId, limit = None) } yield { locations must haveSize(4) - locations.map(_.timestamp) must beEqualTo( + locations.map(_.metadata.startTimestamp) must beEqualTo( Seq( 3L, 4L, diff --git a/server/src/test/scala/com/jackpf/locationhistory/server/repo/LocationRepoExtensionsTest.scala b/server/src/test/scala/com/jackpf/locationhistory/server/repo/LocationRepoExtensionsTest.scala index d2ea690..ce4a3b8 100644 --- a/server/src/test/scala/com/jackpf/locationhistory/server/repo/LocationRepoExtensionsTest.scala +++ b/server/src/test/scala/com/jackpf/locationhistory/server/repo/LocationRepoExtensionsTest.scala @@ -44,8 +44,13 @@ class LocationRepoExtensionsTest(using ee: ExecutionEnv) extends DefaultSpecific when(repository.getForDevice(deviceId, limit = Some(1))).thenReturn( Future.successful(Vector.empty) ) - when(repository.storeDeviceLocation(deviceId, newLocation, newTimestamp)) - .thenReturn(Future.successful(Success(()))) + when( + repository.storeDeviceLocation( + deviceId, + newLocation, + StoredLocation.Metadata.initial(newTimestamp) + ) + ).thenReturn(Future.successful(Success(()))) } trait UpdatePreviousLocationContext extends Context { @@ -67,7 +72,11 @@ class LocationRepoExtensionsTest(using ee: ExecutionEnv) extends DefaultSpecific context.result must beSuccessfulTry.await verify(context.repository, Times(1)) - .storeDeviceLocation(context.deviceId, context.newLocation, context.newTimestamp) + .storeDeviceLocation( + context.deviceId, + context.newLocation, + StoredLocation.Metadata.initial(context.newTimestamp) + ) ok } diff --git a/server/src/test/scala/com/jackpf/locationhistory/server/repo/LocationRepoTest.scala b/server/src/test/scala/com/jackpf/locationhistory/server/repo/LocationRepoTest.scala index b8cdb7e..9a20487 100644 --- a/server/src/test/scala/com/jackpf/locationhistory/server/repo/LocationRepoTest.scala +++ b/server/src/test/scala/com/jackpf/locationhistory/server/repo/LocationRepoTest.scala @@ -35,7 +35,7 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) acc.flatMap { case Success(_) => val (d, l, t) = item - locationRepo.storeDeviceLocation(d, l, t) + locationRepo.storeDeviceLocation(d, l, StoredLocation.Metadata.initial(t)) case failure => Future.successful(failure) @@ -54,7 +54,13 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) "get locations by device" >> in(new StoredLocationContext {}) { context => context.locationRepo .getForDevice(DeviceId("123"), limit = None) must beEqualTo( - Seq(MockModels.storedLocation(1L, context.locations.head._2, context.locations.head._3)) + Seq( + MockModels.storedLocation( + 1L, + context.locations.head._2, + StoredLocation.Metadata.initial(context.locations.head._3) + ) + ) ).await } @@ -68,8 +74,16 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) context.locationRepo .getForDevice(DeviceId("123"), limit = Some(2)) must beEqualTo( Seq( - MockModels.storedLocation(2L, context.locations(1)._2, context.locations(1)._3), - MockModels.storedLocation(3L, context.locations(2)._2, context.locations(2)._3) + MockModels.storedLocation( + 2L, + context.locations(1)._2, + StoredLocation.Metadata.initial(context.locations(1)._3) + ), + MockModels.storedLocation( + 3L, + context.locations(2)._2, + StoredLocation.Metadata.initial(context.locations(2)._3) + ) ) ).await } @@ -91,7 +105,13 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) context.locationRepo .getForDevice(DeviceId("123"), limit = None) must beEqualTo( - Seq(MockModels.storedLocation(1L, context.locations.head._2, context.locations.head._3)) + Seq( + MockModels.storedLocation( + 1L, + context.locations.head._2, + StoredLocation.Metadata.initial(context.locations.head._3) + ) + ) ).await context.locationRepo .getForDevice(DeviceId("456"), limit = None) must beEmpty[Seq[StoredLocation]].await @@ -119,14 +139,18 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) }) { context => { for { - _ <- context.locationRepo.update(DeviceId("123"), 1L, _.copy(timestamp = 999)) + _ <- context.locationRepo.update( + DeviceId("123"), + 1L, + sl => sl.copy(metadata = sl.metadata.copy(startTimestamp = 999)) + ) updated <- context.locationRepo.getForDevice(DeviceId("123"), limit = None) } yield updated must beEqualTo( Seq( MockModels.storedLocation( 1L, MockModels.location(lat = 0.1, lon = 0.2, accuracy = 0.3), - 999 + StoredLocation.Metadata(startTimestamp = 999, endTimestamp = 123L, count = 1L) ) ) ) @@ -142,7 +166,7 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) context.locationRepo.update( DeviceId("123"), 999L, - _.copy(timestamp = 999) + sl => sl.copy(metadata = sl.metadata.copy(startTimestamp = 999)) ) must beEqualTo[Try[Unit]](Failure(LocationNotFoundException(DeviceId("123"), 999L))).await } @@ -156,7 +180,7 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) context.locationRepo.update( DeviceId("123"), 2L, - _.copy(timestamp = 999) + sl => sl.copy(metadata = sl.metadata.copy(startTimestamp = 999)) ) must beEqualTo[Try[Unit]](Failure(LocationNotFoundException(DeviceId("123"), 2L))).await } } diff --git a/server/src/test/scala/com/jackpf/locationhistory/server/testutil/MockModels.scala b/server/src/test/scala/com/jackpf/locationhistory/server/testutil/MockModels.scala index 26ee661..ac591f1 100644 --- a/server/src/test/scala/com/jackpf/locationhistory/server/testutil/MockModels.scala +++ b/server/src/test/scala/com/jackpf/locationhistory/server/testutil/MockModels.scala @@ -38,6 +38,10 @@ object MockModels { def storedLocation( id: Long = 1, location: Location = location(), - timestamp: Long = 123L - ): StoredLocation = StoredLocation(id = id, location = location, timestamp = timestamp) + metadata: StoredLocation.Metadata = StoredLocation.Metadata.initial(123L) + ): StoredLocation = StoredLocation( + id = id, + location = location, + metadata = metadata + ) } diff --git a/shared/src/main/protobuf/common.proto b/shared/src/main/protobuf/common.proto index 0639e9e..4d5e39b 100644 --- a/shared/src/main/protobuf/common.proto +++ b/shared/src/main/protobuf/common.proto @@ -30,7 +30,9 @@ message Location { message StoredLocation { Location location = 1; - int64 timestamp = 2; + int64 start_timestamp = 2; + int64 end_timestamp = 3; + int64 count = 4; } message PushHandler { diff --git a/ui/package-lock.json b/ui/package-lock.json index 454f6f5..cd267e3 100644 --- a/ui/package-lock.json +++ b/ui/package-lock.json @@ -160,7 +160,6 @@ "integrity": "sha512-H3mcG6ZDLTlYfaSNi0iOKkigqMFvkTKlGUYlD8GW7nNOYRrevuA46iTypPyv+06V3fEmvvazfntkBU34L0azAw==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@babel/code-frame": "^7.28.6", "@babel/generator": "^7.28.6", @@ -2476,7 +2475,6 @@ "integrity": "sha512-ne4A0IpG3+2ETuREInjPNhUGis1SFjv1d5asp8MzEAGtOZeTeHVDOYqOgqfhvseqg/iXty2hjBf1zAOb7RNiNw==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "undici-types": "~7.16.0" } @@ -2487,7 +2485,6 @@ "integrity": "sha512-Lpo8kgb/igvMIPeNV2rsYKTgaORYdO1XGVZ4Qz3akwOj0ySGYMPlQWa8BaLn0G63D1aSaAQ5ldR06wCpChQCjA==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "csstype": "^3.2.2" } @@ -2556,7 +2553,6 @@ "integrity": "sha512-nm3cvFN9SqZGXjmw5bZ6cGmvJSyJPn0wU9gHAZZHDnZl2wF9PhHv78Xf06E0MaNk4zLVHL8hb2/c32XvyJOLQg==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@typescript-eslint/scope-manager": "8.53.1", "@typescript-eslint/types": "8.53.1", @@ -2874,7 +2870,6 @@ "integrity": "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==", "dev": true, "license": "MIT", - "peer": true, "bin": { "acorn": "bin/acorn" }, @@ -3062,7 +3057,6 @@ } ], "license": "MIT", - "peer": true, "dependencies": { "baseline-browser-mapping": "^2.9.0", "caniuse-lite": "^1.0.30001759", @@ -3254,7 +3248,6 @@ "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-4.1.0.tgz", "integrity": "sha512-Ukq0owbQXxa/U3EGtsdVBkR1w7KOQ5gIBqdH2hkvknzZPYvBxb/aa6E8L7tmjFtkwZBu3UXBbjIgPo/Ez4xaNg==", "license": "MIT", - "peer": true, "funding": { "type": "github", "url": "https://github.com/sponsors/kossnocorp" @@ -3264,8 +3257,7 @@ "version": "1.11.19", "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.19.tgz", "integrity": "sha512-t5EcLVS6QPBNqM2z8fakk/NKel+Xzshgt8FFKAn+qwlD1pzZWxh0nVCrvFK7ZDb6XucZeF9z8C7CBWTRIVApAw==", - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/debug": { "version": "4.4.3", @@ -3399,7 +3391,6 @@ "integrity": "sha512-LEyamqS7W5HB3ujJyvi0HQK/dtVINZvd5mAAp9eT5S/ujByGjiZLCzPcHVzuXbpJDJF/cxwHlfceVUDZ2lnSTw==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@eslint-community/eslint-utils": "^4.8.0", "@eslint-community/regexpp": "^4.12.1", @@ -4059,7 +4050,6 @@ "resolved": "https://registry.npmjs.org/maplibre-gl/-/maplibre-gl-5.16.0.tgz", "integrity": "sha512-/VDY89nr4jgLJyzmhy325cG6VUI02WkZ/UfVuDbG/piXzo6ODnM+omDFIwWY8tsEsBG26DNDmNMn3Y2ikHsBiA==", "license": "BSD-3-Clause", - "peer": true, "dependencies": { "@mapbox/geojson-rewind": "^0.5.2", "@mapbox/jsonlint-lines-primitives": "^2.0.2", @@ -4399,7 +4389,6 @@ "resolved": "https://registry.npmjs.org/react/-/react-19.2.3.tgz", "integrity": "sha512-Ku/hhYbVjOQnXDZFv2+RibmLFGwFdeeKHFcOTlrt7xplBnya5OGn/hIRDsqDiSUcfORsDC7MPxwork8jBwsIWA==", "license": "MIT", - "peer": true, "engines": { "node": ">=0.10.0" } @@ -4409,7 +4398,6 @@ "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.2.3.tgz", "integrity": "sha512-yELu4WmLPw5Mr/lmeEpox5rw3RETacE++JgHqQzd2dg+YbJuat3jH4ingc+WPZhxaoFzdv9y33G+F7Nl5O0GBg==", "license": "MIT", - "peer": true, "dependencies": { "scheduler": "^0.27.0" }, @@ -4876,7 +4864,6 @@ "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", "dev": true, "license": "Apache-2.0", - "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" @@ -5006,7 +4993,6 @@ "integrity": "sha512-w+N7Hifpc3gRjZ63vYBXA56dvvRlNWRczTdmCBBa+CotUzAPf5b7YMdMR/8CQoeYE5LX3W4wj6RYTgonm1b9DA==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "esbuild": "^0.27.0", "fdir": "^6.5.0", @@ -5222,7 +5208,6 @@ "integrity": "sha512-k7Nwx6vuWx1IJ9Bjuf4Zt1PEllcwe7cls3VNzm4CQ1/hgtFUK2bRNG3rvnpPUhFjmqJKAKtjV576KnUkHocg/g==", "dev": true, "license": "MIT", - "peer": true, "funding": { "url": "https://github.com/sponsors/colinhacks" } diff --git a/ui/src/components/DeviceList.tsx b/ui/src/components/DeviceList.tsx index b2b9875..4a72f1b 100644 --- a/ui/src/components/DeviceList.tsx +++ b/ui/src/components/DeviceList.tsx @@ -189,7 +189,7 @@ export const DeviceList: React.FC = ({
Last seen {lastLocation ? formatDistanceToNow(new Date(lastLocation.timestamp), {addSuffix: true}) : "never"} + className="detail-value">{lastLocation ? formatDistanceToNow(new Date(lastLocation.endTimestamp), {addSuffix: true}) : "never"}
{storedDevice.pushHandler &&
diff --git a/ui/src/components/MLMap.tsx b/ui/src/components/MLMap.tsx index 57fb581..62912c5 100644 --- a/ui/src/components/MLMap.tsx +++ b/ui/src/components/MLMap.tsx @@ -1,7 +1,7 @@ import React, {useEffect, useMemo, useState} from "react"; import Map, {Layer, NavigationControl, Popup, Source} from "react-map-gl/maplibre"; import "maplibre-gl/dist/maplibre-gl.css"; -import {format, formatDistanceToNow} from "date-fns"; +import {format, formatDistance, formatDistanceToNow} from "date-fns"; import type {StoredLocation} from "../gen/common.ts"; import type {MapGeoJSONFeature, StyleSpecification} from "maplibre-gl"; import type {Feature, FeatureCollection, LineString, Point} from "geojson"; @@ -9,7 +9,15 @@ import {Segmented} from "antd"; import {useLocalStorage} from "../hooks/use-local-storage.ts"; import styles from "./MLMap.module.css"; import {accuracyCircleStyle, circlePoint, lineStyle, pointStyle} from "./MLMapStyles.tsx"; -import {DEFAULT_CENTER, DEFAULT_ZOOM, getMapUrl, mapStyleOptions, MapType, POINT_LIMIT} from "./MLMapConfig.tsx"; +import { + DEFAULT_CENTER, + DEFAULT_DATE_FORMAT, + DEFAULT_ZOOM, + getMapUrl, + mapStyleOptions, + MapType, + POINT_LIMIT +} from "./MLMapConfig.tsx"; import {MapUpdater} from "./MLMapUpdater.tsx"; import {MAP_TYPE} from "../config/config.ts"; @@ -55,7 +63,9 @@ export const MLMap: React.FC = ({history, selectedDeviceId, forceRec lat: h.location!.lat, lon: h.location!.lon, accuracy: h.location!.accuracy, - time: h.timestamp, + startTime: h.startTimestamp, + endTime: h.endTimestamp, + count: h.count, metadata: h.location!.metadata }, geometry: { @@ -93,8 +103,8 @@ export const MLMap: React.FC = ({history, selectedDeviceId, forceRec // Calculate cutoff ratio for faded-out lines let cutoffRatio = 0; if (history.length > 0) { - const startTime = history[0].timestamp; - const endTime = history[history.length - 1].timestamp; + const startTime = history[0].startTimestamp; + const endTime = history[history.length - 1].endTimestamp; const totalDuration = endTime - startTime; const twentyFourHoursAgo = currentTime - (24 * 60 * 60 * 1000); cutoffRatio = totalDuration > 0 ? (twentyFourHoursAgo - startTime) / totalDuration : 0; @@ -129,7 +139,7 @@ export const MLMap: React.FC = ({history, selectedDeviceId, forceRec Points: {history.length}
Updated: {lastLocation - ? formatDistanceToNow(new Date(lastLocation.timestamp), {addSuffix: true}) + ? formatDistanceToNow(new Date(lastLocation.endTimestamp), {addSuffix: true}) : "never"}
@@ -197,9 +207,15 @@ export const MLMap: React.FC = ({history, selectedDeviceId, forceRec Latitude: {popupInfo.properties.lat}
Longitude: {popupInfo.properties.lon}
Accuracy: {popupInfo.properties.accuracy}m
- Time: {format(new Date(popupInfo.properties.time), "yyyy-MM-dd HH:mm:ss")} - {popupMetadata &&

Metadata:
} + Start + Time: {format(new Date(popupInfo.properties.startTime), DEFAULT_DATE_FORMAT)}
+ End + Time: {format(new Date(popupInfo.properties.endTime), DEFAULT_DATE_FORMAT)}
+ Duration: {formatDistance(new Date(popupInfo.properties.endTime), new Date(popupInfo.properties.startTime))}
+ Count: {popupInfo.properties.count} + {Object.entries(popupMetadata).length > 0 &&

Metadata:
} {popupMetadata && Object.entries(popupMetadata) + .filter(([key]) => key !== 'displayName') // Filter since we display it above .sort(([k1], [k2]) => k1.localeCompare(k2)) .map(([key, value]) => { return ( diff --git a/ui/src/components/MLMapConfig.tsx b/ui/src/components/MLMapConfig.tsx index 103c0c4..1983561 100644 --- a/ui/src/components/MLMapConfig.tsx +++ b/ui/src/components/MLMapConfig.tsx @@ -1,6 +1,7 @@ import {MAPTILER_API_KEY} from "../config/config.ts"; import {GlobalOutlined, MoonFilled, SunOutlined} from "@ant-design/icons"; +export const DEFAULT_DATE_FORMAT: string = "yyyy-MM-dd HH:mm:ss"; export const DEFAULT_CENTER: [number, number] = [40, 0]; export const DEFAULT_ZOOM = 2; export const DEFAULT_ZOOM_IN = 15;