-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
76 lines (65 loc) · 1.69 KB
/
report.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"io"
"sort"
"strings"
"text/tabwriter"
"time"
"github.com/dgsb/tt/internal/db"
)
func sameDate(t1, t2 time.Time) bool {
year1, month1, day1 := t1.UTC().Date()
year2, month2, day2 := t2.UTC().Date()
return year1 == year2 && month1 == month2 && day1 == day2
}
func FlatReport(tas []db.TaggedInterval, out io.Writer) error {
if !sort.SliceIsSorted(tas, func(i, j int) bool {
return tas[i].Interval.StartTimestamp.Unix() < tas[j].Interval.StartTimestamp.Unix()
}) {
return fmt.Errorf("%w: input tagged interval is not sorted", errInvalidParameter)
}
tab := tabwriter.NewWriter(out, 16, 4, 0, ' ', 0)
var prevStartTime time.Time
var totalDuration time.Duration
var err error
twrite := func(s string) {
if err != nil {
return
}
_, err = tab.Write([]byte(s))
}
for i := 0; i < len(tas) && err == nil; i++ {
ta := tas[i]
if !sameDate(prevStartTime, ta.Interval.StartTimestamp) {
twrite(ta.Interval.StartTimestamp.Format("2006-01-02"))
}
twrite("\t")
twrite(ta.Interval.ID)
twrite("\t")
twrite(ta.Interval.StartTimestamp.Format("15:04:05"))
twrite("\t")
twrite(ta.Interval.StopTimestamp.Format("15:04:05"))
twrite("\t")
if ta.Interval.StopTimestamp.IsZero() {
ta.Interval.StopTimestamp = time.Now().Truncate(time.Second)
}
duration := ta.Interval.StopTimestamp.Sub(ta.Interval.StartTimestamp)
totalDuration += duration
twrite(duration.String())
twrite("\t")
twrite(strings.Join(ta.Tags, ","))
twrite("\t")
twrite("\n")
prevStartTime = ta.Interval.StartTimestamp
}
twrite("\n")
twrite("Total time")
twrite("\t\t\t\t")
twrite(totalDuration.String())
twrite("\n")
if err == nil {
err = tab.Flush()
}
return err
}