Skip to content

Commit

Permalink
#43: add support to timestamps without date, nicer display for config…
Browse files Browse the repository at this point in the history
…uration dialog
  • Loading branch information
rladstaetter committed Aug 8, 2024
1 parent c140893 commit 6f6361c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
18 changes: 13 additions & 5 deletions app/src/main/scala/app/logorrr/model/LogEntryInstantFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package app.logorrr.model

import app.logorrr.util.CanLog
import app.logorrr.views.settings.timestamp.SimpleRange
import pureconfig.{ConfigReader, ConfigWriter}
import pureconfig.generic.semiauto.{deriveReader, deriveWriter}
import pureconfig.{ConfigReader, ConfigWriter}

import java.time._
import java.time.format.DateTimeFormatter
import java.time.{Instant, LocalDateTime, ZoneId, ZoneOffset}
import scala.util.{Failure, Success, Try}

object LogEntryInstantFormat extends CanLog {
Expand All @@ -21,14 +21,22 @@ object LogEntryInstantFormat extends CanLog {
def parseInstant(line: String, entrySetting: LogEntryInstantFormat): Option[Instant] =
if (line.length >= entrySetting.endCol) {
val dateTimeAsString = line.substring(entrySetting.startCol, entrySetting.endCol)
val dtf: DateTimeFormatter = entrySetting.dateTimeFormatter
Try {
val dtf: DateTimeFormatter = entrySetting.dateTimeFormatter
LocalDateTime.parse(dateTimeAsString, dtf).toInstant(ZoneOffset.of(entrySetting.zoneOffset))
} match {
case Success(value) => Option(value)
case Failure(_) =>
logTrace(s"Could not parse '$dateTimeAsString' at pos (${entrySetting.startCol}/${entrySetting.endCol}) with pattern '$${entrySetting.dateTimePattern}'")
None
// retrying with localtime as fallback for entries which don't have any
// date information (for example: '08:34:33' representing today morning)
Try {
LocalDateTime.of(LocalDate.now(), LocalTime.parse(dateTimeAsString, dtf)).toInstant(ZoneOffset.of(entrySetting.zoneOffset))
} match {
case Success(value) => Option(value)
case Failure(exception) =>
logTrace(s"Could not parse '$dateTimeAsString' at pos (${entrySetting.startCol}/${entrySetting.endCol}) with pattern '${entrySetting.dateTimePattern}': ${exception.getMessage}")
None
}
}
} else {
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import app.logorrr.views.block.ChunkListView
import javafx.beans.binding.Bindings
import javafx.beans.property.SimpleObjectProperty
import javafx.collections.ObservableList
import javafx.css.PseudoClass
import javafx.scene.control._
import javafx.scene.layout.{BorderPane, VBox}
import javafx.scene.layout.BorderPane

import java.time.{Duration, Instant}

Expand Down Expand Up @@ -45,45 +46,20 @@ class TimestampSettingsBorderPane(settings: MutLogFileSettings
case None => (new SimpleObjectProperty[java.lang.Integer](), new SimpleObjectProperty[java.lang.Integer]())
}

private val rangeTextBinding = Bindings.createStringBinding(() => {
(Option(getStartCol), Option(getEndCol)) match {
case (Some(start), Some(end)) => s"Range: (${start.toString}/${end.toString})"
case (Some(start), None) => s"Range: (${start.toString}/not set)"
case (None, Some(end)) => s"select start col: (not set/${end.toString})"
case (None, None) => "Select range."
}

}, startColProperty, endColProperty)

private val rangeColLabel = {
val l = new Label()
l.textProperty().bind(rangeTextBinding)
l
}

private val selectedRangeLabel = new Label("Selected: ")
private val (timeFormatLabel, timeFormatTf) = TimestampSettingsBorderPane.mkTf("time format", Option("<enter time format>"), Option(LogEntryInstantFormat.DefaultPattern), 30)

private val timerSettingsLogTextView = {
val tslv = new TimerSettingsLogView(settings, logEntries)
startColProperty.bind(tslv.startColProperty)
endColProperty.bind(tslv.endColProperty)
tslv
}

/**
* if ok button is clicked, log definition will be written, settings stage will be closed, associated logfile
* definition will be updated
* */
private val okButton = {
val b = new Button("set new format")

b.setOnAction(_ => {
val leif: LogEntryInstantFormat = LogEntryInstantFormat(SimpleRange(getStartCol, getEndCol), timeFormatTf.getText.trim)
settings.setLogEntryInstantFormat(leif)
LogoRRRGlobals.persist()
// we have to deactivate this listener otherwise
chunkListView.removeInvalidationListener()
var someFirstEntryTimestamp : Option[Instant] = None
var someFirstEntryTimestamp: Option[Instant] = None
for (i <- 0 until logEntries.size()) {
val e = logEntries.get(i)
val someInstant = LogEntryInstantFormat.parseInstant(e.value, leif)
Expand All @@ -106,12 +82,40 @@ class TimestampSettingsBorderPane(settings: MutLogFileSettings
b
}

private val rangeTextBinding = Bindings.createStringBinding(() => {
(Option(getStartCol), Option(getEndCol)) match {
case (Some(start), Some(end)) =>
okButton.pseudoClassStateChanged(PseudoClass.getPseudoClass("hover"), true)
okButton.requestFocus()
s"Range: (${start.toString}/${end.toString})"
case (Some(start), None) => s"Range: (${start.toString}/not set)"
case (None, Some(end)) => s"select start col: (not set/${end.toString})"
case (None, None) => "Select range."
}

}, startColProperty, endColProperty)

private val rangeColLabel = {
val l = new Label()
l.setPrefWidth(200)
l.textProperty().bind(rangeTextBinding)
l
}

private val (timeFormatLabel, timeFormatTf) = TimestampSettingsBorderPane.mkTf("time format", Option("<enter time format>"), Option(LogEntryInstantFormat.DefaultPattern), 30)

private val timerSettingsLogTextView = {
val tslv = new TimerSettingsLogView(settings, logEntries)
startColProperty.bind(tslv.startColProperty)
endColProperty.bind(tslv.endColProperty)
tslv
}




private val hyperlink: Hyperlink = TimestampSettingsBorderPane.dateTimeFormatterLink.mkHyperLink()
private val selectedBar = new ToolBar(selectedRangeLabel)
private val timeFormatBar = new ToolBar(timeFormatLabel, timeFormatTf, hyperlink)
private val bar = new ToolBar(rangeColLabel, okButton)
private val vbox = new VBox(selectedBar, timeFormatBar, bar)
private val timeFormatBar = new ToolBar(rangeColLabel, timeFormatLabel, timeFormatTf, hyperlink, okButton)

init()

Expand All @@ -133,7 +137,7 @@ class TimestampSettingsBorderPane(settings: MutLogFileSettings
}

setCenter(timerSettingsLogTextView)
setBottom(vbox)
setBottom(timeFormatBar)

}
}

0 comments on commit 6f6361c

Please sign in to comment.