|
1 | 1 | package z
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 |
| - "time" |
6 |
| - "strings" |
7 |
| - "github.com/shopspring/decimal" |
8 |
| - "github.com/jinzhu/now" |
9 |
| - // "github.com/gookit/color" |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/jinzhu/now" |
| 9 | + "github.com/shopspring/decimal" |
| 10 | + // "github.com/gookit/color" |
10 | 11 | )
|
11 | 12 |
|
12 | 13 | type Statistic struct {
|
13 |
| - Hours decimal.Decimal |
14 |
| - Project string |
15 |
| - Color (func(...interface {}) string) |
| 14 | + Hours decimal.Decimal |
| 15 | + Project string |
| 16 | + Color (func(...interface{}) string) |
16 | 17 | }
|
17 | 18 |
|
18 | 19 | type WeekStatistics map[string][]Statistic
|
19 | 20 |
|
20 | 21 | type Week struct {
|
21 |
| - Statistics WeekStatistics |
| 22 | + Statistics WeekStatistics |
22 | 23 | }
|
23 | 24 |
|
24 | 25 | type Month struct {
|
25 |
| - Name string |
26 |
| - Weeks [5]Week |
| 26 | + Name string |
| 27 | + Weeks [5]Week |
27 | 28 | }
|
28 | 29 |
|
29 | 30 | type Calendar struct {
|
30 |
| - Months [12]Month |
31 |
| - Distribution map[string]Statistic |
32 |
| - TotalHours decimal.Decimal |
| 31 | + Months [12]Month |
| 32 | + Distribution map[string]Statistic |
| 33 | + TotalHours decimal.Decimal |
33 | 34 | }
|
34 | 35 |
|
35 | 36 | func NewCalendar(entries []Entry) (Calendar, error) {
|
36 |
| - cal := Calendar{} |
37 |
| - |
38 |
| - cal.Distribution = make(map[string]Statistic) |
39 |
| - |
40 |
| - projects := make(map[string]Project) |
41 |
| - |
42 |
| - for _, entry := range entries { |
43 |
| - var entryFinish time.Time |
44 |
| - endOfBeginDay := now.With(entry.Begin).EndOfDay() |
45 |
| - sameDayHours := decimal.NewFromInt(0) |
46 |
| - nextDayHours := decimal.NewFromInt(0) |
47 |
| - |
48 |
| - projectId := GetIdFromName(entry.Project) |
49 |
| - |
50 |
| - if projects[projectId].Name == "" { |
51 |
| - project, err := database.GetProject(entry.User, entry.Project) |
52 |
| - if err != nil { |
53 |
| - return cal, err |
54 |
| - } |
55 |
| - |
56 |
| - projects[projectId] = project |
57 |
| - } |
58 |
| - |
59 |
| - if entry.Finish.IsZero() { |
60 |
| - entryFinish = time.Now() |
61 |
| - } else { |
62 |
| - entryFinish = entry.Finish |
63 |
| - } |
64 |
| - |
65 |
| - /* |
66 |
| - * Apparently the activity end is on a new day. |
67 |
| - * This means we have to split the activity across two days. |
68 |
| - */ |
69 |
| - if endOfBeginDay.Before(entryFinish) == true { |
70 |
| - startOfFinishDay := now.With(entryFinish).BeginningOfDay() |
71 |
| - |
72 |
| - sameDayDuration := endOfBeginDay.Sub(entry.Begin) |
73 |
| - sameDay := sameDayDuration.Hours() |
74 |
| - sameDayHours = decimal.NewFromFloat(sameDay) |
75 |
| - |
76 |
| - nextDayDuration := entryFinish.Sub(startOfFinishDay) |
77 |
| - nextDay := nextDayDuration.Hours() |
78 |
| - nextDayHours = decimal.NewFromFloat(nextDay) |
79 |
| - |
80 |
| - } else { |
81 |
| - sameDayDuration := entryFinish.Sub(entry.Begin) |
82 |
| - sameDay := sameDayDuration.Hours() |
83 |
| - sameDayHours = decimal.NewFromFloat(sameDay) |
84 |
| - } |
85 |
| - |
86 |
| - if sameDayHours.GreaterThan(decimal.NewFromInt(0)) { |
87 |
| - month, weeknumber := GetISOWeekInMonth(entry.Begin) |
88 |
| - month0 := month - 1 |
89 |
| - weeknumber0 := weeknumber - 1 |
90 |
| - weekday := entry.Begin.Weekday() |
91 |
| - weekdayName := weekday.String()[:2] |
92 |
| - |
93 |
| - stat := Statistic{ |
94 |
| - Hours: sameDayHours, |
95 |
| - Project: entry.Project, |
96 |
| - Color: GetColorFnFromHex(projects[projectId].Color), |
97 |
| - } |
98 |
| - |
99 |
| - if cal.Months[month0].Weeks[weeknumber0].Statistics == nil { |
100 |
| - cal.Months[month0].Weeks[weeknumber0].Statistics = make(WeekStatistics) |
101 |
| - } |
102 |
| - |
103 |
| - cal.Months[month0].Weeks[weeknumber0].Statistics[weekdayName] = append(cal.Months[month0].Weeks[weeknumber0].Statistics[weekdayName], stat) |
104 |
| - } |
105 |
| - |
106 |
| - if nextDayHours.GreaterThan(decimal.NewFromInt(0)) { |
107 |
| - month, weeknumber := GetISOWeekInMonth(entryFinish) |
108 |
| - month0 := month - 1 |
109 |
| - weeknumber0 := weeknumber - 1 |
110 |
| - weekday := entry.Begin.Weekday() |
111 |
| - weekdayName := weekday.String()[:2] |
112 |
| - |
113 |
| - stat := Statistic{ |
114 |
| - Hours: nextDayHours, |
115 |
| - Project: entry.Project, |
116 |
| - Color: GetColorFnFromHex(projects[projectId].Color), |
117 |
| - } |
118 |
| - |
119 |
| - if cal.Months[month0].Weeks[weeknumber0].Statistics == nil { |
120 |
| - cal.Months[month0].Weeks[weeknumber0].Statistics = make(WeekStatistics) |
121 |
| - } |
122 |
| - |
123 |
| - cal.Months[month0].Weeks[weeknumber0].Statistics[weekdayName] = append(cal.Months[month0].Weeks[weeknumber0].Statistics[weekdayName], stat) |
124 |
| - } |
125 |
| - |
126 |
| - var dist = cal.Distribution[entry.Project] |
127 |
| - dist.Project = entry.Project |
128 |
| - dist.Hours = dist.Hours.Add(sameDayHours) |
129 |
| - dist.Hours = dist.Hours.Add(nextDayHours) |
130 |
| - dist.Color = GetColorFnFromHex(projects[projectId].Color) |
131 |
| - cal.Distribution[entry.Project] = dist |
132 |
| - |
133 |
| - // fmt.Printf("Same Day: %s \n Next Day: %s \n Project Hours: %s\n", sameDayHours.String(), nextDayHours.String(), dist.Hours.String()) |
134 |
| - cal.TotalHours = cal.TotalHours.Add(sameDayHours) |
135 |
| - cal.TotalHours = cal.TotalHours.Add(nextDayHours) |
136 |
| - } |
137 |
| - |
138 |
| - return cal, nil |
| 37 | + cal := Calendar{} |
| 38 | + |
| 39 | + cal.Distribution = make(map[string]Statistic) |
| 40 | + |
| 41 | + projects := make(map[string]Project) |
| 42 | + |
| 43 | + for _, entry := range entries { |
| 44 | + var entryFinish time.Time |
| 45 | + endOfBeginDay := now.With(entry.Begin).EndOfDay() |
| 46 | + sameDayHours := decimal.NewFromInt(0) |
| 47 | + nextDayHours := decimal.NewFromInt(0) |
| 48 | + |
| 49 | + projectId := GetIdFromName(entry.Project) |
| 50 | + |
| 51 | + if projects[projectId].Name == "" { |
| 52 | + project, err := database.GetProject(entry.User, entry.Project) |
| 53 | + if err != nil { |
| 54 | + return cal, err |
| 55 | + } |
| 56 | + |
| 57 | + projects[projectId] = project |
| 58 | + } |
| 59 | + |
| 60 | + if entry.Finish.IsZero() { |
| 61 | + entryFinish = time.Now() |
| 62 | + } else { |
| 63 | + entryFinish = entry.Finish |
| 64 | + } |
| 65 | + |
| 66 | + /* |
| 67 | + * Apparently the activity end is on a new day. |
| 68 | + * This means we have to split the activity across two days. |
| 69 | + */ |
| 70 | + if endOfBeginDay.Before(entryFinish) == true { |
| 71 | + startOfFinishDay := now.With(entryFinish).BeginningOfDay() |
| 72 | + |
| 73 | + sameDayDuration := endOfBeginDay.Sub(entry.Begin) |
| 74 | + sameDay := sameDayDuration.Hours() |
| 75 | + sameDayHours = decimal.NewFromFloat(sameDay) |
| 76 | + |
| 77 | + nextDayDuration := entryFinish.Sub(startOfFinishDay) |
| 78 | + nextDay := nextDayDuration.Hours() |
| 79 | + nextDayHours = decimal.NewFromFloat(nextDay) |
| 80 | + |
| 81 | + } else { |
| 82 | + sameDayDuration := entryFinish.Sub(entry.Begin) |
| 83 | + sameDay := sameDayDuration.Hours() |
| 84 | + sameDayHours = decimal.NewFromFloat(sameDay) |
| 85 | + } |
| 86 | + |
| 87 | + if sameDayHours.GreaterThan(decimal.NewFromInt(0)) { |
| 88 | + month, weeknumber := GetISOWeekInMonth(entry.Begin) |
| 89 | + month0 := month - 1 |
| 90 | + weeknumber0 := weeknumber - 1 |
| 91 | + weekday := entry.Begin.Weekday() |
| 92 | + weekdayName := weekday.String()[:2] |
| 93 | + |
| 94 | + stat := Statistic{ |
| 95 | + Hours: sameDayHours, |
| 96 | + Project: entry.Project, |
| 97 | + Color: GetColorFnFromHex(projects[projectId].Color), |
| 98 | + } |
| 99 | + |
| 100 | + if cal.Months[month0].Weeks[weeknumber0].Statistics == nil { |
| 101 | + cal.Months[month0].Weeks[weeknumber0].Statistics = make(WeekStatistics) |
| 102 | + } |
| 103 | + |
| 104 | + cal.Months[month0].Weeks[weeknumber0].Statistics[weekdayName] = append(cal.Months[month0].Weeks[weeknumber0].Statistics[weekdayName], stat) |
| 105 | + } |
| 106 | + |
| 107 | + if nextDayHours.GreaterThan(decimal.NewFromInt(0)) { |
| 108 | + month, weeknumber := GetISOWeekInMonth(entryFinish) |
| 109 | + month0 := month - 1 |
| 110 | + weeknumber0 := weeknumber - 1 |
| 111 | + weekday := entry.Begin.Weekday() |
| 112 | + weekdayName := weekday.String()[:2] |
| 113 | + |
| 114 | + stat := Statistic{ |
| 115 | + Hours: nextDayHours, |
| 116 | + Project: entry.Project, |
| 117 | + Color: GetColorFnFromHex(projects[projectId].Color), |
| 118 | + } |
| 119 | + |
| 120 | + if cal.Months[month0].Weeks[weeknumber0].Statistics == nil { |
| 121 | + cal.Months[month0].Weeks[weeknumber0].Statistics = make(WeekStatistics) |
| 122 | + } |
| 123 | + |
| 124 | + cal.Months[month0].Weeks[weeknumber0].Statistics[weekdayName] = append(cal.Months[month0].Weeks[weeknumber0].Statistics[weekdayName], stat) |
| 125 | + } |
| 126 | + |
| 127 | + dist := cal.Distribution[entry.Project] |
| 128 | + dist.Project = entry.Project |
| 129 | + dist.Hours = dist.Hours.Add(sameDayHours) |
| 130 | + dist.Hours = dist.Hours.Add(nextDayHours) |
| 131 | + dist.Color = GetColorFnFromHex(projects[projectId].Color) |
| 132 | + cal.Distribution[entry.Project] = dist |
| 133 | + |
| 134 | + // fmt.Printf("Same Day: %s \n Next Day: %s \n Project Hours: %s\n", sameDayHours.String(), nextDayHours.String(), dist.Hours.String()) |
| 135 | + cal.TotalHours = cal.TotalHours.Add(sameDayHours) |
| 136 | + cal.TotalHours = cal.TotalHours.Add(nextDayHours) |
| 137 | + } |
| 138 | + |
| 139 | + return cal, nil |
139 | 140 | }
|
140 | 141 |
|
141 |
| -func (calendar *Calendar) GetOutputForWeekCalendar(date time.Time, month int, week int) (string) { |
142 |
| - var output string = "" |
143 |
| - var bars [][]string |
144 |
| - var totalHours = decimal.NewFromInt(0) |
145 |
| - |
146 |
| - var days = []string{"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"} |
147 |
| - for _, day := range days { |
148 |
| - var dayHours = decimal.NewFromInt(0) |
149 |
| - |
150 |
| - for _, stat := range calendar.Months[month].Weeks[week].Statistics[day] { |
151 |
| - dayHours = dayHours.Add(stat.Hours) |
152 |
| - totalHours = totalHours.Add(stat.Hours) |
153 |
| - } |
154 |
| - |
155 |
| - if dayHours.GreaterThan(decimal.NewFromInt(24)) { |
156 |
| - fmt.Printf("%s %s of week %d in month %d has more than 24h tracked; cutting at 24h now\n", CharError, day, (month+1), (week+1)) |
157 |
| - dayHours = decimal.NewFromInt(24) |
158 |
| - } |
159 |
| - |
160 |
| - bar := GetOutputBarForHours(dayHours, calendar.Months[month].Weeks[week].Statistics[day]) |
161 |
| - bars = append(bars, bar) |
162 |
| - } |
163 |
| - |
164 |
| - output = fmt.Sprintf("CW %02d %s H\n", GetISOCalendarWeek(date), fmtHours(totalHours)) |
165 |
| - for row := 0; row < len(bars[0]); row++ { |
166 |
| - output = fmt.Sprintf("%s%2d │", output, ((6 - row) * 4)) |
167 |
| - for col := 0; col < len(bars); col++ { |
168 |
| - output = fmt.Sprintf("%s%s", output, bars[col][row]) |
169 |
| - } |
170 |
| - output = fmt.Sprintf("%s\n", output) |
171 |
| - } |
172 |
| - output = fmt.Sprintf("%s └────────────────────────────\n %s %s %s %s %s %s %s\n", |
173 |
| - output, days[0], days[1], days[2], days[3], days[4], days[5], days[6]) |
174 |
| - |
175 |
| - return output |
| 142 | +func (calendar *Calendar) GetOutputForWeekCalendar(date time.Time, month int, week int) string { |
| 143 | + var output string = "" |
| 144 | + var bars [][]string |
| 145 | + totalHours := decimal.NewFromInt(0) |
| 146 | + |
| 147 | + days := []string{"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"} |
| 148 | + for _, day := range days { |
| 149 | + dayHours := decimal.NewFromInt(0) |
| 150 | + |
| 151 | + for _, stat := range calendar.Months[month].Weeks[week].Statistics[day] { |
| 152 | + dayHours = dayHours.Add(stat.Hours) |
| 153 | + totalHours = totalHours.Add(stat.Hours) |
| 154 | + } |
| 155 | + |
| 156 | + if dayHours.GreaterThan(decimal.NewFromInt(24)) { |
| 157 | + fmt.Printf("%s %s of week %d in month %d has more than 24h tracked; cutting at 24h now\n", CharError, day, (month + 1), (week + 1)) |
| 158 | + dayHours = decimal.NewFromInt(24) |
| 159 | + } |
| 160 | + |
| 161 | + bar := GetOutputBarForHours(dayHours, calendar.Months[month].Weeks[week].Statistics[day]) |
| 162 | + bars = append(bars, bar) |
| 163 | + } |
| 164 | + |
| 165 | + output = fmt.Sprintf("CW %02d %s H\n", GetISOCalendarWeek(date), fmtHours(totalHours)) |
| 166 | + for row := 0; row < len(bars[0]); row++ { |
| 167 | + output = fmt.Sprintf("%s%2d │", output, ((6 - row) * 4)) |
| 168 | + for col := 0; col < len(bars); col++ { |
| 169 | + output = fmt.Sprintf("%s%s", output, bars[col][row]) |
| 170 | + } |
| 171 | + output = fmt.Sprintf("%s\n", output) |
| 172 | + } |
| 173 | + output = fmt.Sprintf("%s └────────────────────────────\n %s %s %s %s %s %s %s\n", |
| 174 | + output, days[0], days[1], days[2], days[3], days[4], days[5], days[6]) |
| 175 | + |
| 176 | + return output |
176 | 177 | }
|
177 | 178 |
|
178 |
| -func (calendar *Calendar) GetOutputForDistribution() (string) { |
179 |
| - var output string = "" |
| 179 | +func (calendar *Calendar) GetOutputForDistribution() string { |
| 180 | + var output string = "" |
180 | 181 |
|
181 |
| - // fmt.Printf("%s\n", calendar.TotalHours.String()) |
| 182 | + // fmt.Printf("%s\n", calendar.TotalHours.String()) |
182 | 183 |
|
183 |
| - var bar string = "" |
184 |
| - for _, stat := range calendar.Distribution { |
185 |
| - divided := stat.Hours.Div(calendar.TotalHours) |
186 |
| - percentage := divided.Mul(decimal.NewFromInt(100)) |
187 |
| - hoursStr := fmtHours(stat.Hours) |
188 |
| - percentageStr := percentage.StringFixed(2) |
| 184 | + var bar string = "" |
| 185 | + for _, stat := range calendar.Distribution { |
| 186 | + divided := stat.Hours.Div(calendar.TotalHours) |
| 187 | + percentage := divided.Mul(decimal.NewFromInt(100)) |
| 188 | + hoursStr := fmtHours(stat.Hours) |
| 189 | + percentageStr := percentage.StringFixed(2) |
189 | 190 |
|
190 |
| - dividedByBarLength := percentage.Div(decimal.NewFromInt(100)) |
191 |
| - percentageForBar := dividedByBarLength.Mul(decimal.NewFromInt(80)) |
192 |
| - percentageForBarInt := int(percentageForBar.Round(0).IntPart()) |
| 191 | + dividedByBarLength := percentage.Div(decimal.NewFromInt(100)) |
| 192 | + percentageForBar := dividedByBarLength.Mul(decimal.NewFromInt(80)) |
| 193 | + percentageForBarInt := int(percentageForBar.Round(0).IntPart()) |
193 | 194 |
|
194 |
| - bar = fmt.Sprintf("%s%s", bar, stat.Color(strings.Repeat("█", percentageForBarInt))) |
| 195 | + bar = fmt.Sprintf("%s%s", bar, stat.Color(strings.Repeat("█", percentageForBarInt))) |
195 | 196 |
|
196 |
| - output = fmt.Sprintf("%s%s%*s H / %*s %%\n", output, stat.Color(stat.Project), (68 - len(stat.Project)), hoursStr, 5, percentageStr) |
197 |
| - } |
| 197 | + output = fmt.Sprintf("%s%s%*s H / %*s %%\n", output, stat.Color(stat.Project), (68 - len(stat.Project)), hoursStr, 5, percentageStr) |
| 198 | + } |
198 | 199 |
|
199 |
| - output = fmt.Sprintf("DISTRIBUTION\n\n%s\n\n%s\n", bar, output) |
200 |
| - return output |
| 200 | + output = fmt.Sprintf("DISTRIBUTION\n\n%s\n\n%s\n", bar, output) |
| 201 | + return output |
201 | 202 | }
|
0 commit comments