diff --git a/internal/utils/strutils/format.go b/internal/utils/strutils/format.go index a0fe28d..a604e99 100644 --- a/internal/utils/strutils/format.go +++ b/internal/utils/strutils/format.go @@ -45,6 +45,13 @@ func FormatDuration(d time.Duration) string { return strings.Join(parts[:len(parts)-1], ", ") + " and " + parts[len(parts)-1] } +func FormatLastSeen(t time.Time) string { + if t.IsZero() { + return "never" + } + return FormatTime(t) +} + func FormatTime(t time.Time) string { return t.Format("2006-01-02 15:04:05") } diff --git a/internal/watcher/health/monitor/json.go b/internal/watcher/health/monitor/json.go index 1bbe03a..b43dc9f 100644 --- a/internal/watcher/health/monitor/json.go +++ b/internal/watcher/health/monitor/json.go @@ -28,10 +28,6 @@ func (jsonRepr *JSONRepresentation) MarshalJSON() ([]byte, error) { if url == "http://:0" { url = "" } - lastSeen := "never" - if !jsonRepr.LastSeen.IsZero() { - lastSeen = strutils.FormatTime(jsonRepr.LastSeen) - } return json.Marshal(map[string]any{ "name": jsonRepr.Name, "config": jsonRepr.Config, @@ -43,7 +39,7 @@ func (jsonRepr *JSONRepresentation) MarshalJSON() ([]byte, error) { "latency": jsonRepr.Latency.Seconds(), "latencyStr": strconv.Itoa(int(jsonRepr.Latency.Milliseconds())) + " ms", "lastSeen": jsonRepr.LastSeen.Unix(), - "lastSeenStr": lastSeen, + "lastSeenStr": strutils.FormatLastSeen(jsonRepr.LastSeen), "detail": jsonRepr.Detail, "url": url, "extra": jsonRepr.Extra, diff --git a/internal/watcher/health/monitor/monitor.go b/internal/watcher/health/monitor/monitor.go index 9f6ed15..49cd071 100644 --- a/internal/watcher/health/monitor/monitor.go +++ b/internal/watcher/health/monitor/monitor.go @@ -189,7 +189,7 @@ func (mon *monitor) checkUpdateHealth() error { if result.Healthy != (mon.status.Swap(status) == health.StatusHealthy) { extras := map[string]any{ "Service Name": mon.service, - "Last Seen": strutils.FormatTime(mon.lastSeen), + "Last Seen": strutils.FormatLastSeen(mon.lastSeen), } if !mon.url.Load().Nil() { extras["Service URL"] = mon.url.Load().String()