Skip to content

Commit

Permalink
Increase time tracking debugging. Avoid strict exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
io7m committed Nov 28, 2024
1 parent 4ab0b45 commit 8252abc
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -104,9 +105,10 @@ class TimeTrackingMerge private constructor(
*/

val spanFiles: List<Path> =
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<TimeTrackingReceivedSpan>()
for (file in spanFiles) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ class TimeTrackingSender private constructor(
MDC.put("TimeLoss", "false")

val entryFiles: List<Path> =
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<TimeTrackingEntryOutgoing>()
for (entryFile in entryFiles) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class TimeTrackingService private constructor(
clock = clock,
frequency = Duration.ofSeconds(30L),
inputDirectory = collectorDirectory,
debugDirectory = debugDirectory,
outputDirectory = senderDirectory
),
sender = TimeTrackingSender.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class TimeTrackingMergeTest {
@BeforeEach
fun setup(
@TempDir outboxDirectory: Path,
@TempDir debugDirectory: Path,
@TempDir inboxDirectory: Path
) {
this.timeNow = OffsetDateTime.now()
Expand All @@ -65,6 +66,7 @@ class TimeTrackingMergeTest {
this.merge =
TimeTrackingMerge.create(
outputDirectory = outboxDirectory,
debugDirectory = debugDirectory,
inputDirectory = inboxDirectory,
clock = this.clock,
frequency = Duration.ofMillis(100L)
Expand Down

0 comments on commit 8252abc

Please sign in to comment.