sessions: the overview's active hours keep only the hours with time

Each range statsFor keeps holds an Active for every day, agent and
folder, and each had a 24-int64 Hours (192 bytes), mostly zeros: 4.5 MB
of statsOfRange's 13.5 MB in the idle heap on the owner's real cache
(18,331 files). Active.Hours is now the same sparse hours a day keeps
(hour<<58|seconds), and the act sums in statsOfRange use it too. Its JSON
is the 24 numbers of seconds it was, which the Usage page's heatmap reads
(a.hours.forEach).

Run on mote at this head:
- go test ./internal/sessions: ok
- GOOS=darwin/linux/windows go vet ./internal/sessions: ok; gofmt clean
- TestStatsHoursAndSessions now reads the hours through JSON, as the
  page does. Broken by hand (ms not turned into seconds): red, "the 20th
  by hour: [... 180000]".
- Real-account copy (~/r2278/acct), idle heap at 180s: inuse 110.65 MB
  (v0.1.3402's tree) -> 100.21 MB at this head (sampled; 4.5 MB of it
  was the hours' own lists).
This commit is contained in:
yetone
2026-11-09 19:22:48 +08:00
parent 7000b23b88
commit 36ddd57619
2 changed files with 15 additions and 11 deletions
+9 -10
View File
@@ -179,8 +179,10 @@ type Active struct {
Agent string `json:"agent"`
Cwd string `json:"cwd"`
Seconds int64 `json:"seconds"`
// Hours is Seconds again by the local hour of the day (24, or none)
Hours []int64 `json:"hours,omitempty"`
// Hours is Seconds again by the local hour of the day (24 in JSON, or
// none), only the hours with time held: each range statsFor keeps has
// one for every day, agent and folder, ~4.5 MB of mostly zeros at 24
Hours hours `json:"hours,omitempty"`
}
// StatsFor reads the usage of the last days days up to today, today
@@ -353,7 +355,7 @@ func statsOfRange(days int, today time.Time, files []file, copies map[string]str
type ak struct{ date, agent, cwd string }
type act0 struct {
ms int64
hours []int64
hours hours
}
use := map[uk]Tokens{}
act := map[ak]*act0{}
@@ -423,11 +425,8 @@ func statsOfRange(days int, today time.Time, files []file, copies map[string]str
}
a.ms += d.Active
if d.Hours != nil {
if a.hours == nil {
a.hours = make([]int64, 24)
}
for _, e := range d.Hours {
a.hours[hourOf(e)] += msOf(e)
a.hours.add(hourOf(e), msOf(e))
}
}
dates[date] = true
@@ -508,9 +507,9 @@ func statsOfRange(days int, today time.Time, files []file, copies map[string]str
for k, a := range act {
v := Active{Agent: k.agent, Cwd: k.cwd, Seconds: a.ms / 1000}
if a.hours != nil {
v.Hours = make([]int64, 24)
for h, ms := range a.hours {
v.Hours[h] = ms / 1000
v.Hours = make(hours, len(a.hours))
for i, e := range a.hours {
v.Hours[i] = hourEntry(hourOf(e), msOf(e)/1000)
}
}
byDate[k.date].Active = append(byDate[k.date].Active, v)
+6 -1
View File
@@ -1,6 +1,7 @@
package sessions
import (
"encoding/json"
"os"
"path/filepath"
"slices"
@@ -201,11 +202,15 @@ func TestStatsHoursAndSessions(t *testing.T) {
ccMsg("m2", "2026-09-20T15:59:00.000Z", 200, 20)+ // 23:59
ccMsg("m3", "2026-09-20T16:03:00.000Z", 400, 40)), 0o644) // 00:03
// as the page reads them: 24 numbers of seconds
hours := func(s Stats, date string) []int64 {
for _, d := range s.Days {
for _, a := range d.Active {
if d.Date == date && a.Cwd == "/work/night" {
return a.Hours
var all struct{ Hours []int64 }
b, _ := json.Marshal(a)
json.Unmarshal(b, &all)
return all.Hours
}
}
}