From 8252abcbda406fa51420067591a935082c7b8ca8 Mon Sep 17 00:00:00 2001 From: Mark Raynsford Date: Thu, 28 Nov 2024 19:40:46 +0000 Subject: [PATCH] Increase time tracking debugging. Avoid strict exceptions. --- .../time/tracking/TimeTrackingDebugging.kt | 34 +++++++++++++++---- .../books/time/tracking/TimeTrackingMerge.kt | 19 +++++++++-- .../books/time/tracking/TimeTrackingSender.kt | 7 ++-- .../time/tracking/TimeTrackingService.kt | 1 + .../time_tracking/TimeTrackingMergeTest.kt | 2 ++ 5 files changed, 50 insertions(+), 13 deletions(-) diff --git a/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingDebugging.kt b/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingDebugging.kt index 156091db5..2041a7a1e 100644 --- a/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingDebugging.kt +++ b/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingDebugging.kt @@ -6,7 +6,8 @@ import java.io.File import java.io.FileOutputStream import java.io.PrintWriter import java.io.StringWriter -import java.time.OffsetTime +import java.time.OffsetDateTime +import java.time.ZoneOffset.UTC import java.util.Properties import java.util.concurrent.locks.ReentrantLock @@ -58,7 +59,7 @@ object TimeTrackingDebugging { ) { val p = Properties() p.setProperty("Operation", "TimeTrackingStarted") - p.setProperty("Time", OffsetTime.now().toString()) + p.setProperty("Time", OffsetDateTime.now(UTC).toString()) p.setProperty("LibraryID", libraryId) p.setProperty("BookID", bookId) this.writeLocked(timeTrackingDebugDirectory, p) @@ -71,12 +72,31 @@ object TimeTrackingDebugging { ) { val p = Properties() p.setProperty("Operation", "TimeTrackingStopped") - p.setProperty("Time", OffsetTime.now().toString()) + p.setProperty("Time", OffsetDateTime.now(UTC).toString()) p.setProperty("LibraryID", libraryId) p.setProperty("BookID", bookId) this.writeLocked(timeTrackingDebugDirectory, p) } + fun onTimeTrackingEntryCreated( + timeTrackingDebugDirectory: File, + libraryId: String, + bookId: String, + entryId: String, + duringMinute: String, + seconds: Int, + ) { + val p = Properties() + p.setProperty("Operation", "TimeTrackingEntryCreated") + p.setProperty("Time", OffsetDateTime.now(UTC).toString()) + p.setProperty("EntryID", entryId) + p.setProperty("LibraryID", libraryId) + p.setProperty("BookID", bookId) + p.setProperty("DuringMinute", duringMinute) + p.setProperty("Seconds", seconds.toString()) + this.writeLocked(timeTrackingDebugDirectory, p) + } + fun onTimeTrackingSendAttempt( timeTrackingDebugDirectory: File, libraryId: String, @@ -86,7 +106,7 @@ object TimeTrackingDebugging { ) { val p = Properties() p.setProperty("Operation", "TimeTrackingSendAttempt") - p.setProperty("Time", OffsetTime.now().toString()) + p.setProperty("Time", OffsetDateTime.now(UTC).toString()) p.setProperty("LibraryID", libraryId) p.setProperty("BookID", bookId) p.setProperty("EntryID", entryId) @@ -102,7 +122,7 @@ object TimeTrackingDebugging { ) { val p = Properties() p.setProperty("Operation", "TimeTrackingSendAttemptSucceeded") - p.setProperty("Time", OffsetTime.now().toString()) + p.setProperty("Time", OffsetDateTime.now(UTC).toString()) p.setProperty("LibraryID", libraryId) p.setProperty("BookID", bookId) p.setProperty("EntryID", entryId) @@ -118,7 +138,7 @@ object TimeTrackingDebugging { ) { val p = Properties() p.setProperty("Operation", "TimeTrackingSendAttemptFailedExceptionally") - p.setProperty("Time", OffsetTime.now().toString()) + p.setProperty("Time", OffsetDateTime.now(UTC).toString()) p.setProperty("LibraryID", libraryId) p.setProperty("BookID", bookId) p.setProperty("EntryID", entryId) @@ -146,7 +166,7 @@ object TimeTrackingDebugging { ) { val p = Properties() p.setProperty("Operation", "TimeTrackingSendAttemptFailed") - p.setProperty("Time", OffsetTime.now().toString()) + p.setProperty("Time", OffsetDateTime.now(UTC).toString()) p.setProperty("LibraryID", libraryId) p.setProperty("BookID", bookId) p.setProperty("EntryID", entryId) diff --git a/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingMerge.kt b/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingMerge.kt index f0a74a73b..7567f7722 100644 --- a/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingMerge.kt +++ b/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingMerge.kt @@ -32,6 +32,7 @@ import java.util.stream.Collectors class TimeTrackingMerge private constructor( private val clock: () -> OffsetDateTime, + private val debugDirectory: Path, private val inboxDirectory: Path, private val outboxDirectory: Path, private val frequency: Duration, @@ -104,9 +105,10 @@ class TimeTrackingMerge private constructor( */ val spanFiles: List = - Files.list(this.inboxDirectory) - .filter { p -> isSpanFileSuitable(timeOldest, p) } - .collect(Collectors.toList()) + Files.list(this.inboxDirectory).use { inboxStream -> + inboxStream.filter { p -> isSpanFileSuitable(timeOldest, p) } + .collect(Collectors.toList()) + } val spans = mutableListOf() for (file in spanFiles) { @@ -165,6 +167,15 @@ class TimeTrackingMerge private constructor( StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING ) + + TimeTrackingDebugging.onTimeTrackingEntryCreated( + timeTrackingDebugDirectory = this.debugDirectory.toFile(), + libraryId = entry.libraryID.toString(), + bookId = entry.bookID.value, + entryId = entry.timeEntry.id, + duringMinute = entry.timeEntry.duringMinute, + seconds = entry.timeEntry.secondsPlayed + ) } companion object { @@ -262,12 +273,14 @@ class TimeTrackingMerge private constructor( clock: () -> OffsetDateTime, frequency: Duration, inputDirectory: Path, + debugDirectory: Path, outputDirectory: Path, ): TimeTrackingMergeServiceType { return TimeTrackingMerge( clock = clock, frequency = frequency, inboxDirectory = inputDirectory, + debugDirectory = debugDirectory, outboxDirectory = outputDirectory ) } diff --git a/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingSender.kt b/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingSender.kt index b01a4bbbf..6901d64ed 100644 --- a/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingSender.kt +++ b/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingSender.kt @@ -79,9 +79,10 @@ class TimeTrackingSender private constructor( MDC.put("TimeLoss", "false") val entryFiles: List = - Files.list(this.inputDirectory) - .filter { p -> this.isFileSuitable(p) } - .collect(Collectors.toList()) + Files.list(this.inputDirectory).use { inputStream -> + inputStream.filter { p -> this.isFileSuitable(p) } + .collect(Collectors.toList()) + } val entries = mutableListOf() for (entryFile in entryFiles) { diff --git a/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingService.kt b/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingService.kt index dc3f1e2f6..1930ff25a 100644 --- a/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingService.kt +++ b/simplified-books-time-tracking/src/main/java/org/nypl/simplified/books/time/tracking/TimeTrackingService.kt @@ -60,6 +60,7 @@ class TimeTrackingService private constructor( clock = clock, frequency = Duration.ofSeconds(30L), inputDirectory = collectorDirectory, + debugDirectory = debugDirectory, outputDirectory = senderDirectory ), sender = TimeTrackingSender.create( diff --git a/simplified-tests/src/test/java/org/nypl/simplified/tests/books/time_tracking/TimeTrackingMergeTest.kt b/simplified-tests/src/test/java/org/nypl/simplified/tests/books/time_tracking/TimeTrackingMergeTest.kt index 24f05d8bd..e689d7458 100644 --- a/simplified-tests/src/test/java/org/nypl/simplified/tests/books/time_tracking/TimeTrackingMergeTest.kt +++ b/simplified-tests/src/test/java/org/nypl/simplified/tests/books/time_tracking/TimeTrackingMergeTest.kt @@ -53,6 +53,7 @@ class TimeTrackingMergeTest { @BeforeEach fun setup( @TempDir outboxDirectory: Path, + @TempDir debugDirectory: Path, @TempDir inboxDirectory: Path ) { this.timeNow = OffsetDateTime.now() @@ -65,6 +66,7 @@ class TimeTrackingMergeTest { this.merge = TimeTrackingMerge.create( outputDirectory = outboxDirectory, + debugDirectory = debugDirectory, inputDirectory = inboxDirectory, clock = this.clock, frequency = Duration.ofMillis(100L)