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] 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 { func FormatTime(t time.Time) string {
return t.Format("2006-01-02 15:04:05") 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" { if url == "http://:0" {
url = "" url = ""
} }
lastSeen := "never"
if !jsonRepr.LastSeen.IsZero() {
lastSeen = strutils.FormatTime(jsonRepr.LastSeen)
}
return json.Marshal(map[string]any{ return json.Marshal(map[string]any{
"name": jsonRepr.Name, "name": jsonRepr.Name,
"config": jsonRepr.Config, "config": jsonRepr.Config,
@ -43,7 +39,7 @@ func (jsonRepr *JSONRepresentation) MarshalJSON() ([]byte, error) {
"latency": jsonRepr.Latency.Seconds(), "latency": jsonRepr.Latency.Seconds(),
"latencyStr": strconv.Itoa(int(jsonRepr.Latency.Milliseconds())) + " ms", "latencyStr": strconv.Itoa(int(jsonRepr.Latency.Milliseconds())) + " ms",
"lastSeen": jsonRepr.LastSeen.Unix(), "lastSeen": jsonRepr.LastSeen.Unix(),
"lastSeenStr": lastSeen, "lastSeenStr": strutils.FormatLastSeen(jsonRepr.LastSeen),
"detail": jsonRepr.Detail, "detail": jsonRepr.Detail,
"url": url, "url": url,
"extra": jsonRepr.Extra, "extra": jsonRepr.Extra,

View file

@ -189,7 +189,7 @@ func (mon *monitor) checkUpdateHealth() error {
if result.Healthy != (mon.status.Swap(status) == health.StatusHealthy) { if result.Healthy != (mon.status.Swap(status) == health.StatusHealthy) {
extras := map[string]any{ extras := map[string]any{
"Service Name": mon.service, "Service Name": mon.service,
"Last Seen": strutils.FormatTime(mon.lastSeen), "Last Seen": strutils.FormatLastSeen(mon.lastSeen),
} }
if !mon.url.Load().Nil() { if !mon.url.Load().Nil() {
extras["Service URL"] = mon.url.Load().String() extras["Service URL"] = mon.url.Load().String()