Skip to content

Commit b527cb0

Browse files
committed
fix: Improve the aggregations
1 parent 66d4c45 commit b527cb0

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed

aggregated_file.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package pulse
22

3+
import "cmp"
4+
35
// AggregatedFile represents a file that has been aggregated
46
// for a given time period (day, week, month, year).
57
type AggregatedFile struct {
@@ -11,6 +13,10 @@ type AggregatedFile struct {
1113

1214
// merge takes two AggregatedFile, merges them, and returns the result.
1315
func (a AggregatedFile) merge(b AggregatedFile) AggregatedFile {
14-
a.DurationMs += b.DurationMs
15-
return a
16+
return AggregatedFile{
17+
Name: cmp.Or(a.Name, b.Name),
18+
Path: cmp.Or(a.Path, b.Path),
19+
Filetype: cmp.Or(a.Filetype, b.Filetype),
20+
DurationMs: a.DurationMs + b.DurationMs,
21+
}
1622
}

aggregated_files.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ func (a AggregatedFiles) merge(b AggregatedFiles) AggregatedFiles {
1818
aFileMap := createPathFileMap(a)
1919
bFileMap := createPathFileMap(b)
2020

21-
allPaths := make(map[string]bool)
21+
allPaths := make(map[string]struct{})
2222
for path := range aFileMap {
23-
allPaths[path] = true
23+
allPaths[path] = struct{}{}
2424
}
2525
for path := range bFileMap {
26-
allPaths[path] = true
26+
allPaths[path] = struct{}{}
2727
}
2828

2929
mergedFiles := make([]AggregatedFile, 0, len(allPaths))

aggregated_session.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package pulse
22

3+
import "cmp"
4+
35
// AggregatedSession represents a session that has been aggregated
46
// for a given time period (day, week, month, year).
57
type AggregatedSession struct {
@@ -12,11 +14,11 @@ type AggregatedSession struct {
1214
}
1315

1416
// merge takes two aggregated sessions, merges them, and returns the result.
15-
func (a AggregatedSession) merge(b AggregatedSession, date int64, timePeriod Period) AggregatedSession {
17+
func (a AggregatedSession) merge(b AggregatedSession, epochDateMs int64, timePeriod Period) AggregatedSession {
1618
mergedSession := AggregatedSession{
1719
Period: timePeriod,
18-
EpochDateMs: date,
19-
DateString: a.DateString,
20+
EpochDateMs: epochDateMs,
21+
DateString: cmp.Or(a.DateString, b.DateString),
2022
TotalTimeMs: a.TotalTimeMs + b.TotalTimeMs,
2123
Repositories: a.Repositories.merge(b.Repositories),
2224
}

buffer_stack.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,13 @@ func (b *bufferStack) files() Files {
3030
sortOrder := make([]string, 0)
3131
pathFile := make(map[string]File)
3232

33-
// Merge the buffers by filepath.
33+
// Turn the buffers into files and merge them by filepath.
3434
for _, buffer := range b.buffers {
35-
file, exists := pathFile[buffer.Filepath]
36-
if !exists {
35+
file, ok := pathFile[buffer.Filepath]
36+
pathFile[buffer.Filepath] = file.merge(fileFromBuffer(buffer))
37+
if !ok {
3738
sortOrder = append(sortOrder, buffer.Filepath)
38-
pathFile[buffer.Filepath] = fileFromBuffer(buffer)
39-
continue
4039
}
41-
file.Duration += buffer.Duration()
42-
pathFile[buffer.Filepath] = file
4340
}
4441

4542
// Return the buffers in the original order.

file.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package pulse
22

3-
import "time"
3+
import (
4+
"cmp"
5+
"time"
6+
)
47

58
// File represents a file that has been opened during a coding session.
69
type File struct {
@@ -11,6 +14,16 @@ type File struct {
1114
Duration time.Duration `json:"duration"`
1215
}
1316

17+
func (f File) merge(b File) File {
18+
return File{
19+
Name: cmp.Or(f.Name, b.Name),
20+
Path: cmp.Or(f.Path, b.Path),
21+
Repository: cmp.Or(f.Repository, b.Repository),
22+
Filetype: cmp.Or(f.Filetype, b.Filetype),
23+
Duration: f.Duration + b.Duration,
24+
}
25+
}
26+
1427
// fileFromBuffer turns a code buffer into a file.
1528
func fileFromBuffer(b Buffer) File {
1629
return File{

repository.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package pulse
22

3+
import "cmp"
4+
35
// Repository represents a git repository. A coding session
46
// might open files across any number of repos. The files of
57
// the coding session are later grouped by repository.
@@ -12,7 +14,7 @@ type Repository struct {
1214
// merge takes two repositories, merges them, and returns the result.
1315
func (r Repository) merge(b Repository) Repository {
1416
return Repository{
15-
Name: r.Name,
17+
Name: cmp.Or(r.Name, b.Name),
1618
Files: r.Files.merge(b.Files),
1719
DurationMs: r.DurationMs + b.DurationMs,
1820
}

0 commit comments

Comments
 (0)