fix health lastSeen format

This commit is contained in:
yusing 2024-12-18 10:49:33 +08:00
parent 57a7c04a4c
commit 34d5edd6b9
3 changed files with 9 additions and 6 deletions

View file

@ -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")
}

View file

@ -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,

View file

@ -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()