fix(charts): unify timeline labels across graphs

This commit is contained in:
2026-03-29 21:24:06 +03:00
parent e15bcc91c5
commit 8ffdebe047

View File

@@ -72,29 +72,36 @@ func (r *metricsRing) snapshot() ([]float64, []string) {
defer r.mu.Unlock() defer r.mu.Unlock()
v := make([]float64, len(r.vals)) v := make([]float64, len(r.vals))
copy(v, r.vals) copy(v, r.vals)
now := time.Now()
labels := make([]string, len(r.times)) labels := make([]string, len(r.times))
if len(r.times) == 0 {
return v, labels
}
sameDay := timestampsSameLocalDay(r.times)
for i, t := range r.times { for i, t := range r.times {
labels[i] = relAgeLabel(now.Sub(t)) labels[i] = formatTimelineLabel(t.Local(), sameDay)
} }
return v, labels return v, labels
} }
func relAgeLabel(age time.Duration) string { func timestampsSameLocalDay(times []time.Time) bool {
if age <= 0 { if len(times) == 0 {
return "0" return true
} }
if age < time.Hour { first := times[0].Local()
m := int(age.Minutes()) for _, t := range times[1:] {
if m == 0 { local := t.Local()
return "-1m" if local.Year() != first.Year() || local.YearDay() != first.YearDay() {
return false
} }
return fmt.Sprintf("-%dm", m)
} }
if age < 24*time.Hour { return true
return fmt.Sprintf("-%dh", int(age.Hours())) }
func formatTimelineLabel(ts time.Time, sameDay bool) string {
if sameDay {
return ts.Format("15:04")
} }
return fmt.Sprintf("-%dd", int(age.Hours()/24)) return ts.Format("01-02 15:04")
} }
// gpuRings holds per-GPU ring buffers. // gpuRings holds per-GPU ring buffers.
@@ -792,22 +799,13 @@ func sampleTimeLabels(samples []platform.LiveMetricSample) []string {
if len(samples) == 0 { if len(samples) == 0 {
return labels return labels
} }
sameDay := true times := make([]time.Time, len(samples))
first := samples[0].Timestamp.Local()
for _, s := range samples {
ts := s.Timestamp.Local()
if ts.Year() != first.Year() || ts.YearDay() != first.YearDay() {
sameDay = false
break
}
}
for i, s := range samples { for i, s := range samples {
ts := s.Timestamp.Local() times[i] = s.Timestamp
if sameDay { }
labels[i] = ts.Format("15:04") sameDay := timestampsSameLocalDay(times)
} else { for i, s := range samples {
labels[i] = ts.Format("01-02 15:04") labels[i] = formatTimelineLabel(s.Timestamp.Local(), sameDay)
}
} }
return labels return labels
} }