From c6a29f95f3690b5a44eda40ca8f272b60395a929 Mon Sep 17 00:00:00 2001 From: Jack Farrelly Date: Fri, 23 Jan 2026 02:42:49 +0100 Subject: [PATCH 01/10] Start & end location timestamps --- server/.idea/misc.xml | 3 + .../locationhistory/server/AdminTest.scala | 77 ++++++++++++++++++- .../locationhistory/server/LocationTest.scala | 12 ++- .../server/model/StoredLocation.scala | 22 +++++- .../server/repo/InMemoryLocationRepo.scala | 12 ++- .../server/repo/LocationRepo.scala | 4 +- .../server/repo/LocationRepoExtensions.scala | 6 +- .../server/repo/SQLiteLocationRepo.scala | 36 ++++++--- .../server/grpc/AdminServiceImplTest.scala | 16 +++- .../repo/InMemoryLocationRepoTest.scala | 6 +- .../repo/LocationRepoExtensionsTest.scala | 19 ++++- .../server/repo/LocationRepoTest.scala | 38 ++++++--- .../server/testutil/MockModels.scala | 12 ++- shared/src/main/protobuf/common.proto | 4 +- 14 files changed, 219 insertions(+), 48 deletions(-) diff --git a/server/.idea/misc.xml b/server/.idea/misc.xml index 90190a5d..5c824b34 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 055faf89..9cd876a2 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} @@ -200,5 +203,75 @@ 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) + + // Set a location + context.client.setLocation( + SetLocationRequest( + timestamp = timestamp, + deviceId = context.device.id, + location = Some(location) + ) + ) === SetLocationResponse(success = true) + + // Retrieve locations via admin endpoint + val response = context.adminClient.listLocations( + ListLocationsRequest(deviceId = context.device.id) + ) + + response.locations must haveSize(1) + val storedLocation = response.locations.head + + // Verify the new metadata fields are present + storedLocation.location must beSome(location) + storedLocation.startTimestamp === timestamp + storedLocation.endTimestamp must beNone + storedLocation.count === 1L + } + + "list locations with updated metadata after duplicates" >> in(new ApprovedDeviceContext {}) { + context => + // Insert initial location + 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) + + // Insert duplicate location (same coordinates, different timestamp) + 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) + + // Retrieve locations via admin endpoint + val response = context.adminClient.listLocations( + ListLocationsRequest(deviceId = context.device.id) + ) + + response.locations must haveSize(1) + val storedLocation = response.locations.head + + // Verify metadata reflects the duplicate handling + storedLocation.startTimestamp === 1000L + storedLocation.endTimestamp must beSome(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 e0427dc1..c746b1fc 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 = None, + 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 = Some(3L), + count = 3L ), StoredLocation( location = Some(Location(lat = 35.659500, lon = 139.700500, accuracy = 0.1)), - timestamp = 4L + startTimestamp = 4L, + endTimestamp = None, + 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 36bac72e..d97bab20 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,27 @@ 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) + def fromLocation( + location: Location, + id: Long, + startTimestamp: Long, + endTimestamp: Option[Long], + count: Long + ): StoredLocation = + StoredLocation(id, location, startTimestamp, endTimestamp, count) } -case class StoredLocation(id: Long, location: Location, timestamp: Long) { +case class StoredLocation( + id: Long, + location: Location, + startTimestamp: Long, + endTimestamp: Option[Long], + count: Long +) { def toProto: ProtoStoredLocation = ProtoStoredLocation( location = Some(location.toProto), - timestamp = timestamp + startTimestamp = startTimestamp, + endTimestamp = endTimestamp, + count = 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 5d52b3ce..56e2c1ba 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,18 @@ class InMemoryLocationRepo(maxItemsPerDevice: Long = DefaultMaxItemsPerDevice) override def storeDeviceLocation( deviceId: DeviceId.Type, location: Location, - timestamp: Long + startTimestamp: Long, + endTimestamp: Option[Long], + count: Long ): Future[Try[Unit]] = Future.successful { val storedLocation = - StoredLocation.fromLocation(location, id = generateId(), timestamp = timestamp) + StoredLocation.fromLocation( + location, + id = generateId(), + startTimestamp = startTimestamp, + endTimestamp = endTimestamp, + count = count + ) 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 2689a134..8b7fa0ae 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,9 @@ trait LocationRepo extends LocationRepoExtensions { def storeDeviceLocation( deviceId: DeviceId.Type, location: Location, - timestamp: Long + startTimestamp: Long, + endTimestamp: Option[Long], + count: Long ): 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 9ca1f427..477e2f8b 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,14 @@ 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 + endTimestamp = Some(newTimestamp), + count = storedLocation.count + 1 ) /** Note that this is a "best effort" approach and not strictly thread safe: @@ -43,7 +43,7 @@ trait LocationRepoExtensions { self: LocationRepo => storedLocation => updatePreviousLocation(location, timestamp, storedLocation) ) case _ => - storeDeviceLocation(deviceId, location, timestamp) + storeDeviceLocation(deviceId, location, timestamp, None, 1L) } } 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 761d5e53..c88dbb4c 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,17 @@ private case class StoredLocationRow( lat: Double, lon: Double, accuracy: Double, - timestamp: Long, - metadata: JsonColumn[Map[String, String]] + metadata: JsonColumn[Map[String, String]], + startTimestamp: Long, + endTimestamp: Option[Long], + count: Long ) { def toStoredLocation: StoredLocation = StoredLocation( id = id, location = Location(lat = lat, lon = lon, accuracy = accuracy, metadata.value), - timestamp = timestamp + startTimestamp = startTimestamp, + endTimestamp = endTimestamp, + count = count ) } private object StoredLocationTable extends SimpleTable[StoredLocationRow] @@ -42,12 +46,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, start_timestamp);""" ) } } @@ -56,7 +62,9 @@ class SQLiteLocationRepo(db: DbClient.DataSource)(using executionContext: Execut override def storeDeviceLocation( deviceId: DeviceId.Type, location: Location, - timestamp: Long + startTimestamp: Long, + endTimestamp: Option[Long], + count: Long ): 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 := startTimestamp, + _.endTimestamp := endTimestamp, + _.count := count ) ) () @@ -87,7 +97,7 @@ class SQLiteLocationRepo(db: DbClient.DataSource)(using executionContext: Execut { val q = StoredLocationTable.select .filter(_.deviceId === deviceId.toString) - .sortBy(_.timestamp) + .sortBy(r => r.endTimestamp.getOrElse(r.startTimestamp)) .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.startTimestamp, + _.endTimestamp := updatedStoredDevice.endTimestamp, + _.count := updatedStoredDevice.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 COALESCE(end_timestamp, start_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 6b80dc01..487e4a21 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,17 @@ class AdminServiceImplTest(implicit ee: ExecutionEnv) 1L, MockModels .location(lat = 0.1, lon = 0.2, accuracy = 0.3, metadata = Map("k1" -> "v1")), - timestamp = 1L + startTimestamp = 1L, + endTimestamp = Some(2L), + count = 3L ), MockModels.storedLocation( 2L, MockModels .location(lat = 0.4, lon = 0.5, accuracy = 0.6, metadata = Map("k2" -> "v2")), - timestamp = 2L + startTimestamp = 2L, + endTimestamp = None, + count = 1L ) ) ) @@ -255,11 +259,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 = Some(2L), + count = 3L ), StoredLocation( Some(Location(lat = 0.4, lon = 0.5, accuracy = 0.6, metadata = Map("k2" -> "v2"))), - timestamp = 2L + startTimestamp = 2L, + endTimestamp = None, + 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 295945e7..92607ebc 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 @@ -21,7 +21,9 @@ class InMemoryLocationRepoTest(implicit ee: ExecutionEnv) extends LocationRepoTe context.locationRepo.storeDeviceLocation( deviceId, MockModels.location(), - ts + startTimestamp = ts, + endTimestamp = None, + count = 1L ) { @@ -35,7 +37,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(_.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 d2ea690d..3ce77856 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,15 @@ 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, + newTimestamp, + endTimestamp = None, + count = 1L + ) + ).thenReturn(Future.successful(Success(()))) } trait UpdatePreviousLocationContext extends Context { @@ -67,7 +74,13 @@ 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, + context.newTimestamp, + endTimestamp = None, + count = 1L + ) 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 b8cdb7ef..acaa4ba8 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, t, None, 1L) 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, + startTimestamp = 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, + startTimestamp = context.locations(1)._3 + ), + MockModels.storedLocation( + 3L, + context.locations(2)._2, + startTimestamp = 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, + startTimestamp = context.locations.head._3 + ) + ) ).await context.locationRepo .getForDevice(DeviceId("456"), limit = None) must beEmpty[Seq[StoredLocation]].await @@ -119,14 +139,14 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) }) { context => { for { - _ <- context.locationRepo.update(DeviceId("123"), 1L, _.copy(timestamp = 999)) + _ <- context.locationRepo.update(DeviceId("123"), 1L, _.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 + startTimestamp = 999 ) ) ) @@ -142,7 +162,7 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) context.locationRepo.update( DeviceId("123"), 999L, - _.copy(timestamp = 999) + _.copy(startTimestamp = 999) ) must beEqualTo[Try[Unit]](Failure(LocationNotFoundException(DeviceId("123"), 999L))).await } @@ -156,7 +176,7 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) context.locationRepo.update( DeviceId("123"), 2L, - _.copy(timestamp = 999) + _.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 26ee661d..08dfc224 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,14 @@ object MockModels { def storedLocation( id: Long = 1, location: Location = location(), - timestamp: Long = 123L - ): StoredLocation = StoredLocation(id = id, location = location, timestamp = timestamp) + startTimestamp: Long = 123L, + endTimestamp: Option[Long] = None, + count: Long = 1L + ): StoredLocation = StoredLocation( + id = id, + location = location, + startTimestamp = startTimestamp, + endTimestamp = endTimestamp, + count = count + ) } diff --git a/shared/src/main/protobuf/common.proto b/shared/src/main/protobuf/common.proto index 0639e9e1..2fccb1bc 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; + optional int64 end_timestamp = 3; + int64 count = 4; } message PushHandler { From 5ff9b3e6b3cab1adc2e0bb30908ff5f843d57950 Mon Sep 17 00:00:00 2001 From: Jack Farrelly Date: Fri, 23 Jan 2026 02:44:45 +0100 Subject: [PATCH 02/10] Remove comments --- .../com/jackpf/locationhistory/server/AdminTest.scala | 11 ----------- 1 file changed, 11 deletions(-) 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 9cd876a2..7eba32c4 100644 --- a/server/src/it/scala/com/jackpf/locationhistory/server/AdminTest.scala +++ b/server/src/it/scala/com/jackpf/locationhistory/server/AdminTest.scala @@ -111,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( @@ -215,7 +213,6 @@ class AdminTest extends IntegrationTest with GrpcMatchers { val timestamp = System.currentTimeMillis() val location = Location(lat = 51.5007, lon = -0.1246, accuracy = 10.0) - // Set a location context.client.setLocation( SetLocationRequest( timestamp = timestamp, @@ -224,15 +221,12 @@ class AdminTest extends IntegrationTest with GrpcMatchers { ) ) === SetLocationResponse(success = true) - // Retrieve locations via admin endpoint val response = context.adminClient.listLocations( ListLocationsRequest(deviceId = context.device.id) ) response.locations must haveSize(1) val storedLocation = response.locations.head - - // Verify the new metadata fields are present storedLocation.location must beSome(location) storedLocation.startTimestamp === timestamp storedLocation.endTimestamp must beNone @@ -241,7 +235,6 @@ class AdminTest extends IntegrationTest with GrpcMatchers { "list locations with updated metadata after duplicates" >> in(new ApprovedDeviceContext {}) { context => - // Insert initial location context.client.setLocation( SetLocationRequest( timestamp = 1000L, @@ -250,7 +243,6 @@ class AdminTest extends IntegrationTest with GrpcMatchers { ) ) === SetLocationResponse(success = true) - // Insert duplicate location (same coordinates, different timestamp) context.client.setLocation( SetLocationRequest( timestamp = 2000L, @@ -259,15 +251,12 @@ class AdminTest extends IntegrationTest with GrpcMatchers { ) ) === SetLocationResponse(success = true) - // Retrieve locations via admin endpoint val response = context.adminClient.listLocations( ListLocationsRequest(deviceId = context.device.id) ) response.locations must haveSize(1) val storedLocation = response.locations.head - - // Verify metadata reflects the duplicate handling storedLocation.startTimestamp === 1000L storedLocation.endTimestamp must beSome(2000L) storedLocation.count === 2L From 0946b0e12c66fc9851c2136f70052f279e5d4e02 Mon Sep 17 00:00:00 2001 From: Jack Farrelly Date: Fri, 23 Jan 2026 03:01:12 +0100 Subject: [PATCH 03/10] endTimestamp -> non-optional --- .../locationhistory/server/AdminTest.scala | 4 ++-- .../locationhistory/server/LocationTest.scala | 6 +++--- .../server/model/StoredLocation.scala | 4 ++-- .../server/repo/InMemoryLocationRepo.scala | 2 +- .../server/repo/LocationRepo.scala | 2 +- .../server/repo/LocationRepoExtensions.scala | 4 ++-- .../server/repo/SQLiteLocationRepo.scala | 8 ++++---- .../server/grpc/AdminServiceImplTest.scala | 8 ++++---- .../server/repo/InMemoryLocationRepoTest.scala | 6 +++--- .../repo/LocationRepoExtensionsTest.scala | 8 ++++---- .../server/repo/LocationRepoTest.scala | 17 +++++++++++------ .../server/testutil/MockModels.scala | 2 +- shared/src/main/protobuf/common.proto | 2 +- ui/package-lock.json | 17 +---------------- ui/src/components/DeviceList.tsx | 2 +- 15 files changed, 41 insertions(+), 51 deletions(-) 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 7eba32c4..eef7bd4e 100644 --- a/server/src/it/scala/com/jackpf/locationhistory/server/AdminTest.scala +++ b/server/src/it/scala/com/jackpf/locationhistory/server/AdminTest.scala @@ -229,7 +229,7 @@ class AdminTest extends IntegrationTest with GrpcMatchers { val storedLocation = response.locations.head storedLocation.location must beSome(location) storedLocation.startTimestamp === timestamp - storedLocation.endTimestamp must beNone + storedLocation.endTimestamp === timestamp storedLocation.count === 1L } @@ -258,7 +258,7 @@ class AdminTest extends IntegrationTest with GrpcMatchers { response.locations must haveSize(1) val storedLocation = response.locations.head storedLocation.startTimestamp === 1000L - storedLocation.endTimestamp must beSome(2000L) + 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 c746b1fc..188909d1 100644 --- a/server/src/it/scala/com/jackpf/locationhistory/server/LocationTest.scala +++ b/server/src/it/scala/com/jackpf/locationhistory/server/LocationTest.scala @@ -104,7 +104,7 @@ class LocationTest extends IntegrationTest with GrpcMatchers { listLocationsResponse.locations.head === StoredLocation( location = Some(location), startTimestamp = timestamp, - endTimestamp = None, + endTimestamp = timestamp, count = 1L ) } @@ -137,13 +137,13 @@ class LocationTest extends IntegrationTest with GrpcMatchers { StoredLocation( location = Some(Location(lat = 51.500800, lon = -0.124500, accuracy = 0.2)), startTimestamp = 1L, - endTimestamp = Some(3L), + endTimestamp = 3L, count = 3L ), StoredLocation( location = Some(Location(lat = 35.659500, lon = 139.700500, accuracy = 0.1)), startTimestamp = 4L, - endTimestamp = None, + 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 d97bab20..5f098e1c 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 @@ -7,7 +7,7 @@ object StoredLocation { location: Location, id: Long, startTimestamp: Long, - endTimestamp: Option[Long], + endTimestamp: Long, count: Long ): StoredLocation = StoredLocation(id, location, startTimestamp, endTimestamp, count) @@ -17,7 +17,7 @@ case class StoredLocation( id: Long, location: Location, startTimestamp: Long, - endTimestamp: Option[Long], + endTimestamp: Long, count: Long ) { def toProto: ProtoStoredLocation = ProtoStoredLocation( 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 56e2c1ba..d5aea24d 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 @@ -29,7 +29,7 @@ class InMemoryLocationRepo(maxItemsPerDevice: Long = DefaultMaxItemsPerDevice) deviceId: DeviceId.Type, location: Location, startTimestamp: Long, - endTimestamp: Option[Long], + endTimestamp: Long, count: Long ): Future[Try[Unit]] = Future.successful { val storedLocation = 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 8b7fa0ae..da94ea25 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 @@ -12,7 +12,7 @@ trait LocationRepo extends LocationRepoExtensions { deviceId: DeviceId.Type, location: Location, startTimestamp: Long, - endTimestamp: Option[Long], + endTimestamp: Long, count: Long ): Future[Try[Unit]] 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 477e2f8b..c61643ef 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 @@ -17,7 +17,7 @@ trait LocationRepoExtensions { self: LocationRepo => storedLocation: StoredLocation ): StoredLocation = storedLocation.copy( location = newLocation, - endTimestamp = Some(newTimestamp), + endTimestamp = newTimestamp, count = storedLocation.count + 1 ) @@ -43,7 +43,7 @@ trait LocationRepoExtensions { self: LocationRepo => storedLocation => updatePreviousLocation(location, timestamp, storedLocation) ) case _ => - storeDeviceLocation(deviceId, location, timestamp, None, 1L) + storeDeviceLocation(deviceId, location, timestamp, timestamp, 1L) } } 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 c88dbb4c..eb56e955 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 @@ -21,7 +21,7 @@ private case class StoredLocationRow( accuracy: Double, metadata: JsonColumn[Map[String, String]], startTimestamp: Long, - endTimestamp: Option[Long], + endTimestamp: Long, count: Long ) { def toStoredLocation: StoredLocation = StoredLocation( @@ -63,7 +63,7 @@ class SQLiteLocationRepo(db: DbClient.DataSource)(using executionContext: Execut deviceId: DeviceId.Type, location: Location, startTimestamp: Long, - endTimestamp: Option[Long], + endTimestamp: Long, count: Long ): Future[Try[Unit]] = Future { db.transaction { implicit db => @@ -97,7 +97,7 @@ class SQLiteLocationRepo(db: DbClient.DataSource)(using executionContext: Execut { val q = StoredLocationTable.select .filter(_.deviceId === deviceId.toString) - .sortBy(r => r.endTimestamp.getOrElse(r.startTimestamp)) + .sortBy(_.endTimestamp) .desc limit match { @@ -187,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 COALESCE(end_timestamp, start_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 487e4a21..13c6b846 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 @@ -240,7 +240,7 @@ class AdminServiceImplTest(implicit ee: ExecutionEnv) MockModels .location(lat = 0.1, lon = 0.2, accuracy = 0.3, metadata = Map("k1" -> "v1")), startTimestamp = 1L, - endTimestamp = Some(2L), + endTimestamp = 2L, count = 3L ), MockModels.storedLocation( @@ -248,7 +248,7 @@ class AdminServiceImplTest(implicit ee: ExecutionEnv) MockModels .location(lat = 0.4, lon = 0.5, accuracy = 0.6, metadata = Map("k2" -> "v2")), startTimestamp = 2L, - endTimestamp = None, + endTimestamp = 2L, count = 1L ) ) @@ -260,13 +260,13 @@ class AdminServiceImplTest(implicit ee: ExecutionEnv) StoredLocation( Some(Location(lat = 0.1, lon = 0.2, accuracy = 0.3, metadata = Map("k1" -> "v1"))), startTimestamp = 1L, - endTimestamp = Some(2L), + endTimestamp = 2L, count = 3L ), StoredLocation( Some(Location(lat = 0.4, lon = 0.5, accuracy = 0.6, metadata = Map("k2" -> "v2"))), startTimestamp = 2L, - endTimestamp = None, + 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 92607ebc..8bde9f82 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 @@ -21,9 +21,9 @@ class InMemoryLocationRepoTest(implicit ee: ExecutionEnv) extends LocationRepoTe context.locationRepo.storeDeviceLocation( deviceId, MockModels.location(), - startTimestamp = ts, - endTimestamp = None, - count = 1L + ts, + ts, + 1L ) { 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 3ce77856..4cfd2562 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 @@ -49,8 +49,8 @@ class LocationRepoExtensionsTest(using ee: ExecutionEnv) extends DefaultSpecific deviceId, newLocation, newTimestamp, - endTimestamp = None, - count = 1L + newTimestamp, + 1L ) ).thenReturn(Future.successful(Success(()))) } @@ -78,8 +78,8 @@ class LocationRepoExtensionsTest(using ee: ExecutionEnv) extends DefaultSpecific context.deviceId, context.newLocation, context.newTimestamp, - endTimestamp = None, - count = 1L + context.newTimestamp, + 1L ) 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 acaa4ba8..6ee69d93 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, None, 1L) + locationRepo.storeDeviceLocation(d, l, t, t, 1L) case failure => Future.successful(failure) @@ -58,7 +58,8 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) MockModels.storedLocation( 1L, context.locations.head._2, - startTimestamp = context.locations.head._3 + startTimestamp = context.locations.head._3, + endTimestamp = context.locations.head._3 ) ) ).await @@ -77,12 +78,14 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) MockModels.storedLocation( 2L, context.locations(1)._2, - startTimestamp = context.locations(1)._3 + startTimestamp = context.locations(1)._3, + endTimestamp = context.locations(1)._3 ), MockModels.storedLocation( 3L, context.locations(2)._2, - startTimestamp = context.locations(2)._3 + startTimestamp = context.locations(2)._3, + endTimestamp = context.locations(2)._3 ) ) ).await @@ -109,7 +112,8 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) MockModels.storedLocation( 1L, context.locations.head._2, - startTimestamp = context.locations.head._3 + startTimestamp = context.locations.head._3, + endTimestamp = context.locations.head._3 ) ) ).await @@ -146,7 +150,8 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) MockModels.storedLocation( 1L, MockModels.location(lat = 0.1, lon = 0.2, accuracy = 0.3), - startTimestamp = 999 + startTimestamp = 999, + endTimestamp = 123L ) ) ) 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 08dfc224..5d98c326 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 @@ -39,7 +39,7 @@ object MockModels { id: Long = 1, location: Location = location(), startTimestamp: Long = 123L, - endTimestamp: Option[Long] = None, + endTimestamp: Long = 123L, count: Long = 1L ): StoredLocation = StoredLocation( id = id, diff --git a/shared/src/main/protobuf/common.proto b/shared/src/main/protobuf/common.proto index 2fccb1bc..4d5e39bd 100644 --- a/shared/src/main/protobuf/common.proto +++ b/shared/src/main/protobuf/common.proto @@ -31,7 +31,7 @@ message Location { message StoredLocation { Location location = 1; int64 start_timestamp = 2; - optional int64 end_timestamp = 3; + int64 end_timestamp = 3; int64 count = 4; } diff --git a/ui/package-lock.json b/ui/package-lock.json index 454f6f52..cd267e3d 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 b2b98757..4a72f1bf 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 &&
From ccd9a01e0f95a870533e36162f81cb22189c4449 Mon Sep 17 00:00:00 2001 From: Jack Farrelly Date: Fri, 23 Jan 2026 03:14:41 +0100 Subject: [PATCH 04/10] StoredLocation Metadata --- .../server/model/StoredLocation.scala | 30 ++++++++++++------- .../server/repo/InMemoryLocationRepo.scala | 12 ++------ .../server/repo/LocationRepo.scala | 4 +-- .../server/repo/LocationRepoExtensions.scala | 5 ++-- .../server/repo/SQLiteLocationRepo.scala | 24 +++++++-------- .../server/grpc/AdminServiceImplTest.scala | 8 ++--- .../repo/InMemoryLocationRepoTest.scala | 8 ++--- .../repo/LocationRepoExtensionsTest.scala | 8 ++--- .../server/repo/LocationRepoTest.scala | 27 ++++++++--------- .../server/testutil/MockModels.scala | 8 ++--- 10 files changed, 59 insertions(+), 75 deletions(-) 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 5f098e1c..0f6c8b28 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,27 +3,37 @@ package com.jackpf.locationhistory.server.model import com.jackpf.locationhistory.common.StoredLocation as ProtoStoredLocation object StoredLocation { - def fromLocation( - location: Location, - id: Long, + case class Metadata( startTimestamp: Long, endTimestamp: Long, count: Long + ) { + def updated(newTimestamp: Long): Metadata = + copy(endTimestamp = newTimestamp, count = count + 1) + } + + object Metadata { + def initial(timestamp: Long): Metadata = + Metadata(startTimestamp = timestamp, endTimestamp = timestamp, count = 1L) + } + + def fromLocation( + location: Location, + id: Long, + metadata: Metadata ): StoredLocation = - StoredLocation(id, location, startTimestamp, endTimestamp, count) + StoredLocation(id, location, metadata) } case class StoredLocation( id: Long, location: Location, - startTimestamp: Long, - endTimestamp: Long, - count: Long + metadata: StoredLocation.Metadata ) { def toProto: ProtoStoredLocation = ProtoStoredLocation( location = Some(location.toProto), - startTimestamp = startTimestamp, - endTimestamp = endTimestamp, - count = count + 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 d5aea24d..56571714 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,18 +28,10 @@ class InMemoryLocationRepo(maxItemsPerDevice: Long = DefaultMaxItemsPerDevice) override def storeDeviceLocation( deviceId: DeviceId.Type, location: Location, - startTimestamp: Long, - endTimestamp: Long, - count: Long + metadata: StoredLocation.Metadata ): Future[Try[Unit]] = Future.successful { val storedLocation = - StoredLocation.fromLocation( - location, - id = generateId(), - startTimestamp = startTimestamp, - endTimestamp = endTimestamp, - count = count - ) + 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 da94ea25..f2b92bb1 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,9 +11,7 @@ trait LocationRepo extends LocationRepoExtensions { def storeDeviceLocation( deviceId: DeviceId.Type, location: Location, - startTimestamp: Long, - endTimestamp: Long, - count: 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 c61643ef..935e019a 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 @@ -17,8 +17,7 @@ trait LocationRepoExtensions { self: LocationRepo => storedLocation: StoredLocation ): StoredLocation = storedLocation.copy( location = newLocation, - endTimestamp = newTimestamp, - count = storedLocation.count + 1 + 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, timestamp, 1L) + 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 eb56e955..a83c2a41 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 @@ -27,9 +27,11 @@ private case class StoredLocationRow( def toStoredLocation: StoredLocation = StoredLocation( id = id, location = Location(lat = lat, lon = lon, accuracy = accuracy, metadata.value), - startTimestamp = startTimestamp, - endTimestamp = endTimestamp, - count = count + metadata = StoredLocation.Metadata( + startTimestamp = startTimestamp, + endTimestamp = endTimestamp, + count = count + ) ) } private object StoredLocationTable extends SimpleTable[StoredLocationRow] @@ -62,9 +64,7 @@ class SQLiteLocationRepo(db: DbClient.DataSource)(using executionContext: Execut override def storeDeviceLocation( deviceId: DeviceId.Type, location: Location, - startTimestamp: Long, - endTimestamp: Long, - count: Long + metadata: StoredLocation.Metadata ): Future[Try[Unit]] = Future { db.transaction { implicit db => Try { @@ -76,9 +76,9 @@ class SQLiteLocationRepo(db: DbClient.DataSource)(using executionContext: Execut _.lon := location.lon, _.accuracy := location.accuracy, _.metadata := JsonColumn(location.metadata), - _.startTimestamp := startTimestamp, - _.endTimestamp := endTimestamp, - _.count := count + _.startTimestamp := metadata.startTimestamp, + _.endTimestamp := metadata.endTimestamp, + _.count := metadata.count ) ) () @@ -140,9 +140,9 @@ class SQLiteLocationRepo(db: DbClient.DataSource)(using executionContext: Execut _.lon := updatedStoredDevice.location.lon, _.accuracy := updatedStoredDevice.location.accuracy, _.metadata := JsonColumn(updatedStoredDevice.location.metadata), - _.startTimestamp := updatedStoredDevice.startTimestamp, - _.endTimestamp := updatedStoredDevice.endTimestamp, - _.count := updatedStoredDevice.count + _.startTimestamp := updatedStoredDevice.metadata.startTimestamp, + _.endTimestamp := updatedStoredDevice.metadata.endTimestamp, + _.count := updatedStoredDevice.metadata.count ) ) } 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 13c6b846..3c93b9fb 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,17 +239,13 @@ class AdminServiceImplTest(implicit ee: ExecutionEnv) 1L, MockModels .location(lat = 0.1, lon = 0.2, accuracy = 0.3, metadata = Map("k1" -> "v1")), - startTimestamp = 1L, - endTimestamp = 2L, - count = 3L + 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")), - startTimestamp = 2L, - endTimestamp = 2L, - count = 1L + model.StoredLocation.Metadata.initial(2L) ) ) ) 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 8bde9f82..ec8bb6e9 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,9 +21,7 @@ class InMemoryLocationRepoTest(implicit ee: ExecutionEnv) extends LocationRepoTe context.locationRepo.storeDeviceLocation( deviceId, MockModels.location(), - ts, - ts, - 1L + StoredLocation.Metadata.initial(ts) ) { @@ -37,7 +35,7 @@ class InMemoryLocationRepoTest(implicit ee: ExecutionEnv) extends LocationRepoTe locations <- context.locationRepo.getForDevice(deviceId, limit = None) } yield { locations must haveSize(4) - locations.map(_.startTimestamp) 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 4cfd2562..ce4a3b81 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 @@ -48,9 +48,7 @@ class LocationRepoExtensionsTest(using ee: ExecutionEnv) extends DefaultSpecific repository.storeDeviceLocation( deviceId, newLocation, - newTimestamp, - newTimestamp, - 1L + StoredLocation.Metadata.initial(newTimestamp) ) ).thenReturn(Future.successful(Success(()))) } @@ -77,9 +75,7 @@ class LocationRepoExtensionsTest(using ee: ExecutionEnv) extends DefaultSpecific .storeDeviceLocation( context.deviceId, context.newLocation, - context.newTimestamp, - context.newTimestamp, - 1L + 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 6ee69d93..9a204874 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, t, 1L) + locationRepo.storeDeviceLocation(d, l, StoredLocation.Metadata.initial(t)) case failure => Future.successful(failure) @@ -58,8 +58,7 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) MockModels.storedLocation( 1L, context.locations.head._2, - startTimestamp = context.locations.head._3, - endTimestamp = context.locations.head._3 + StoredLocation.Metadata.initial(context.locations.head._3) ) ) ).await @@ -78,14 +77,12 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) MockModels.storedLocation( 2L, context.locations(1)._2, - startTimestamp = context.locations(1)._3, - endTimestamp = context.locations(1)._3 + StoredLocation.Metadata.initial(context.locations(1)._3) ), MockModels.storedLocation( 3L, context.locations(2)._2, - startTimestamp = context.locations(2)._3, - endTimestamp = context.locations(2)._3 + StoredLocation.Metadata.initial(context.locations(2)._3) ) ) ).await @@ -112,8 +109,7 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) MockModels.storedLocation( 1L, context.locations.head._2, - startTimestamp = context.locations.head._3, - endTimestamp = context.locations.head._3 + StoredLocation.Metadata.initial(context.locations.head._3) ) ) ).await @@ -143,15 +139,18 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) }) { context => { for { - _ <- context.locationRepo.update(DeviceId("123"), 1L, _.copy(startTimestamp = 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), - startTimestamp = 999, - endTimestamp = 123L + StoredLocation.Metadata(startTimestamp = 999, endTimestamp = 123L, count = 1L) ) ) ) @@ -167,7 +166,7 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) context.locationRepo.update( DeviceId("123"), 999L, - _.copy(startTimestamp = 999) + sl => sl.copy(metadata = sl.metadata.copy(startTimestamp = 999)) ) must beEqualTo[Try[Unit]](Failure(LocationNotFoundException(DeviceId("123"), 999L))).await } @@ -181,7 +180,7 @@ abstract class LocationRepoTest(implicit ee: ExecutionEnv) context.locationRepo.update( DeviceId("123"), 2L, - _.copy(startTimestamp = 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 5d98c326..ac591f12 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,14 +38,10 @@ object MockModels { def storedLocation( id: Long = 1, location: Location = location(), - startTimestamp: Long = 123L, - endTimestamp: Long = 123L, - count: Long = 1L + metadata: StoredLocation.Metadata = StoredLocation.Metadata.initial(123L) ): StoredLocation = StoredLocation( id = id, location = location, - startTimestamp = startTimestamp, - endTimestamp = endTimestamp, - count = count + metadata = metadata ) } From b6f7103b5aa036f87d20719e7df0402b19e99aee Mon Sep 17 00:00:00 2001 From: Jack Farrelly Date: Fri, 23 Jan 2026 03:15:32 +0100 Subject: [PATCH 05/10] Small refactor --- .../locationhistory/server/model/StoredLocation.scala | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 0f6c8b28..faf23284 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,6 +3,11 @@ package com.jackpf.locationhistory.server.model import com.jackpf.locationhistory.common.StoredLocation as ProtoStoredLocation object StoredLocation { + object Metadata { + def initial(timestamp: Long): Metadata = + Metadata(startTimestamp = timestamp, endTimestamp = timestamp, count = 1L) + } + case class Metadata( startTimestamp: Long, endTimestamp: Long, @@ -12,11 +17,6 @@ object StoredLocation { copy(endTimestamp = newTimestamp, count = count + 1) } - object Metadata { - def initial(timestamp: Long): Metadata = - Metadata(startTimestamp = timestamp, endTimestamp = timestamp, count = 1L) - } - def fromLocation( location: Location, id: Long, From 7c0dce89b347bcc91de2483cb70e08f63c13ec91 Mon Sep 17 00:00:00 2001 From: Jack Farrelly Date: Fri, 23 Jan 2026 03:35:42 +0100 Subject: [PATCH 06/10] Fixes --- .../server/repo/SQLiteLocationRepo.scala | 4 +-- ui/src/components/MLMap.tsx | 28 ++++++++++++++----- ui/src/components/MLMapConfig.tsx | 1 + 3 files changed, 24 insertions(+), 9 deletions(-) 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 a83c2a41..07548b43 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,10 +19,10 @@ private case class StoredLocationRow( lat: Double, lon: Double, accuracy: Double, - metadata: JsonColumn[Map[String, String]], startTimestamp: Long, endTimestamp: Long, - count: Long + count: Long, + metadata: JsonColumn[Map[String, String]] ) { def toStoredLocation: StoredLocation = StoredLocation( id = id, diff --git a/ui/src/components/MLMap.tsx b/ui/src/components/MLMap.tsx index 57fb5818..79f6998b 100644 --- a/ui/src/components/MLMap.tsx +++ b/ui/src/components/MLMap.tsx @@ -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,8 +207,12 @@ 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.startTime), DEFAULT_DATE_FORMAT)}
+ Count: {popupInfo.properties.count} + {Object.entries(popupMetadata).length > 0 &&

Metadata:
} {popupMetadata && Object.entries(popupMetadata) .sort(([k1], [k2]) => k1.localeCompare(k2)) .map(([key, value]) => { diff --git a/ui/src/components/MLMapConfig.tsx b/ui/src/components/MLMapConfig.tsx index 103c0c48..19835616 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; From d7227fc57e27fa33694142b8d0b061dd57278e12 Mon Sep 17 00:00:00 2001 From: Jack Farrelly Date: Fri, 23 Jan 2026 03:38:54 +0100 Subject: [PATCH 07/10] Fixes --- ui/src/components/MLMap.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ui/src/components/MLMap.tsx b/ui/src/components/MLMap.tsx index 79f6998b..806c3edf 100644 --- a/ui/src/components/MLMap.tsx +++ b/ui/src/components/MLMap.tsx @@ -207,10 +207,10 @@ export const MLMap: React.FC = ({history, selectedDeviceId, forceRec Latitude: {popupInfo.properties.lat}
Longitude: {popupInfo.properties.lon}
Accuracy: {popupInfo.properties.accuracy}m
- Start Time: - {format(new Date(popupInfo.properties.startTime), DEFAULT_DATE_FORMAT)}
- End Time: - {format(new Date(popupInfo.properties.startTime), DEFAULT_DATE_FORMAT)}
+ Start + Time: {format(new Date(popupInfo.properties.startTime), DEFAULT_DATE_FORMAT)}
+ End + Time: {format(new Date(popupInfo.properties.endTime), DEFAULT_DATE_FORMAT)}
Count: {popupInfo.properties.count} {Object.entries(popupMetadata).length > 0 &&

Metadata:
} {popupMetadata && Object.entries(popupMetadata) From 2639db8a94e380393f66fe1e7caba4ceca9fa904 Mon Sep 17 00:00:00 2001 From: Jack Farrelly Date: Fri, 23 Jan 2026 23:04:34 +0100 Subject: [PATCH 08/10] Small UI fixes --- ui/src/components/MLMap.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ui/src/components/MLMap.tsx b/ui/src/components/MLMap.tsx index 806c3edf..62912c5f 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"; @@ -211,9 +211,11 @@ export const MLMap: React.FC = ({history, selectedDeviceId, forceRec 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 ( From 3a225ace7785e7e042bb0e555e8a94564cbe848b Mon Sep 17 00:00:00 2001 From: Jack Farrelly Date: Fri, 23 Jan 2026 23:15:56 +0100 Subject: [PATCH 09/10] Handle out of order timestamps --- .../locationhistory/server/model/StoredLocation.scala | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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 faf23284..bc4414cc 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 @@ -13,8 +13,13 @@ object StoredLocation { endTimestamp: Long, count: Long ) { - def updated(newTimestamp: Long): Metadata = - copy(endTimestamp = newTimestamp, count = count + 1) + 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( From 1e71f18c87dbc7e241922fa7926a4e07cabfc429 Mon Sep 17 00:00:00 2001 From: Jack Farrelly Date: Fri, 23 Jan 2026 23:23:22 +0100 Subject: [PATCH 10/10] Fix index --- .../jackpf/locationhistory/server/repo/SQLiteLocationRepo.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 07548b43..e1134fe8 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 @@ -55,7 +55,7 @@ class SQLiteLocationRepo(db: DbClient.DataSource)(using executionContext: Execut );""" ) val _ = db.updateRaw( - """CREATE INDEX IF NOT EXISTS idx_device_time ON stored_location_table (device_id, start_timestamp);""" + """CREATE INDEX IF NOT EXISTS idx_device_time ON stored_location_table (device_id, end_timestamp);""" ) } }