Skip to content

Commit a7308d6

Browse files
committed
fix: calculate peak hour based on time spent instead of event count
1 parent 4b1091c commit a7308d6

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

core/stats.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func CalculateStats(db *sql.DB, todayOnly bool) (Stats, error) {
1414
Projects: make(map[string]ProjectStat),
1515
Languages: make(map[string]LangStat),
1616
DailyActivity: make(map[string]DailyStat),
17-
HourlyActivity: make(map[int]int),
17+
HourlyActivity: make(map[int]int64),
1818
TopFiles: []FileStat{},
1919
WeeklyHeatmap: []HeatmapDay{},
2020
Sessions: []Session{},
@@ -168,9 +168,9 @@ func CalculateStats(db *sql.DB, todayOnly bool) (Stats, error) {
168168
ds.Files++
169169
stats.DailyActivity[date] = ds
170170

171-
// Hourly activity
171+
// Hourly activity (track TIME, not count)
172172
hour := timestamp.Hour()
173-
stats.HourlyActivity[hour]++
173+
stats.HourlyActivity[hour] += sessionDelta
174174

175175
// Aggregate totals
176176
stats.TotalLines += lines
@@ -277,7 +277,7 @@ func filterTodayOnly(db *sql.DB, stats Stats, today string) Stats {
277277
todayProjects := make(map[string]ProjectStat)
278278
todayLanguages := make(map[string]LangStat)
279279
todayTopFiles := []FileStat{}
280-
todayHourlyActivity := make(map[int]int)
280+
todayHourlyActivity := make(map[int]int64)
281281

282282
todayQuery := `SELECT timestamp, file, language, project, lines FROM heartbeats
283283
WHERE substr(timestamp, 1, 10) = '` + today + `' ORDER BY timestamp`
@@ -344,9 +344,9 @@ func filterTodayOnly(db *sql.DB, stats Stats, today string) Stats {
344344
todayFileStats[file].Lines += lines
345345
todayFileStats[file].Time += sessionDelta
346346

347-
// Hourly activity (today only)
347+
// Hourly activity (today only - track TIME, not count)
348348
hour := timestamp.Hour()
349-
todayHourlyActivity[hour]++
349+
todayHourlyActivity[hour] += sessionDelta
350350

351351
todayTotalLines += lines
352352
}
@@ -437,13 +437,13 @@ func calculateLongestStreak(daily map[string]DailyStat) int {
437437
return longest
438438
}
439439

440-
func calculateMostActiveHour(hourly map[int]int) int {
440+
func calculateMostActiveHour(hourly map[int]int64) int {
441441
maxHour := 0
442-
maxCount := 0
442+
var maxTime int64
443443

444-
for hour, count := range hourly {
445-
if count > maxCount {
446-
maxCount = count
444+
for hour, time := range hourly {
445+
if time > maxTime {
446+
maxTime = time
447447
maxHour = hour
448448
}
449449
}

core/storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type Stats struct {
6464
Languages map[string]LangStat `json:"languages"`
6565
TopFiles []FileStat `json:"top_files"`
6666
DailyActivity map[string]DailyStat `json:"daily_activity"`
67-
HourlyActivity map[int]int `json:"hourly_activity"`
67+
HourlyActivity map[int]int64 `json:"hourly_activity"` // hour -> time in seconds
6868

6969
// Weekly heatmap (last 12 weeks, for GitHub-style contribution grid)
7070
// Keys are dates in "2006-01-02" format, values are activity levels 0-4

0 commit comments

Comments
 (0)