fix(charts): unify timeline labels across graphs
This commit is contained in:
@@ -72,29 +72,36 @@ func (r *metricsRing) snapshot() ([]float64, []string) {
|
||||
defer r.mu.Unlock()
|
||||
v := make([]float64, len(r.vals))
|
||||
copy(v, r.vals)
|
||||
now := time.Now()
|
||||
labels := make([]string, len(r.times))
|
||||
if len(r.times) == 0 {
|
||||
return v, labels
|
||||
}
|
||||
sameDay := timestampsSameLocalDay(r.times)
|
||||
for i, t := range r.times {
|
||||
labels[i] = relAgeLabel(now.Sub(t))
|
||||
labels[i] = formatTimelineLabel(t.Local(), sameDay)
|
||||
}
|
||||
return v, labels
|
||||
}
|
||||
|
||||
func relAgeLabel(age time.Duration) string {
|
||||
if age <= 0 {
|
||||
return "0"
|
||||
func timestampsSameLocalDay(times []time.Time) bool {
|
||||
if len(times) == 0 {
|
||||
return true
|
||||
}
|
||||
if age < time.Hour {
|
||||
m := int(age.Minutes())
|
||||
if m == 0 {
|
||||
return "-1m"
|
||||
first := times[0].Local()
|
||||
for _, t := range times[1:] {
|
||||
local := t.Local()
|
||||
if local.Year() != first.Year() || local.YearDay() != first.YearDay() {
|
||||
return false
|
||||
}
|
||||
return fmt.Sprintf("-%dm", m)
|
||||
}
|
||||
if age < 24*time.Hour {
|
||||
return fmt.Sprintf("-%dh", int(age.Hours()))
|
||||
return true
|
||||
}
|
||||
|
||||
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.
|
||||
@@ -792,22 +799,13 @@ func sampleTimeLabels(samples []platform.LiveMetricSample) []string {
|
||||
if len(samples) == 0 {
|
||||
return labels
|
||||
}
|
||||
sameDay := true
|
||||
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
|
||||
}
|
||||
}
|
||||
times := make([]time.Time, len(samples))
|
||||
for i, s := range samples {
|
||||
ts := s.Timestamp.Local()
|
||||
if sameDay {
|
||||
labels[i] = ts.Format("15:04")
|
||||
} else {
|
||||
labels[i] = ts.Format("01-02 15:04")
|
||||
}
|
||||
times[i] = s.Timestamp
|
||||
}
|
||||
sameDay := timestampsSameLocalDay(times)
|
||||
for i, s := range samples {
|
||||
labels[i] = formatTimelineLabel(s.Timestamp.Local(), sameDay)
|
||||
}
|
||||
return labels
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user