From e849959b6b1083f8f3b451a91b0e9c418cca8e19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Fri, 16 Oct 2020 22:37:48 +0100 Subject: [PATCH] First draft implementation of distribution --- z/calendar.go | 36 +++++++++++++++++++++++++++++++++++- z/stats.go | 2 ++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/z/calendar.go b/z/calendar.go index 44d3068..60f7d64 100644 --- a/z/calendar.go +++ b/z/calendar.go @@ -27,11 +27,15 @@ type Month struct { type Calendar struct { Months [12]Month + Distribution map[string]Statistic + TotalHours decimal.Decimal } func NewCalendar(entries []Entry) (Calendar, error) { cal := Calendar{} + cal.Distribution = make(map[string]Statistic) + for _, entry := range entries { endOfBeginDay := now.With(entry.Begin).EndOfDay() sameDayHours := decimal.NewFromInt(0) @@ -88,7 +92,7 @@ func NewCalendar(entries []Entry) (Calendar, error) { stat := Statistic{ Hours: nextDayHours, Project: entry.Project, - Color: color.FgCyan.Render, + Color: color.FgCyan.Render, // TODO: Make configurable } if cal.Months[month0].Weeks[weeknumber0].Statistics == nil { @@ -97,6 +101,17 @@ func NewCalendar(entries []Entry) (Calendar, error) { cal.Months[month0].Weeks[weeknumber0].Statistics[weekdayName] = append(cal.Months[month0].Weeks[weeknumber0].Statistics[weekdayName], stat) } + + var dist = cal.Distribution[entry.Project] + dist.Project = entry.Project + dist.Hours = dist.Hours.Add(sameDayHours) + dist.Hours = dist.Hours.Add(nextDayHours) + dist.Color = color.FgCyan.Render // TODO: Make configurable + cal.Distribution[entry.Project] = dist + + // fmt.Printf("Same Day: %s \n Next Day: %s \n Project Hours: %s\n", sameDayHours.String(), nextDayHours.String(), dist.Hours.String()) + cal.TotalHours = cal.TotalHours.Add(sameDayHours) + cal.TotalHours = cal.TotalHours.Add(nextDayHours) } return cal, nil @@ -133,3 +148,22 @@ func (calendar *Calendar) GetOutputForWeekCalendar(date time.Time, month int, we return output } + +func (calendar *Calendar) GetOutputForDistribution() (string) { + var output string = "" + + output = fmt.Sprintf("DISTRIBUTION\n\n"); + output = fmt.Sprintf("%s████████████████████████████████████████████████████████████████████████████████\n\n", output) + + // fmt.Printf("%s\n", calendar.TotalHours.String()) + + for _, stat := range calendar.Distribution { + divided := stat.Hours.Div(calendar.TotalHours) + percentage := divided.Mul(decimal.NewFromInt(100)) + hoursStr := stat.Hours.StringFixed(2) + percentageStr := percentage.StringFixed(2) + output = fmt.Sprintf("%s%s%*s H / %*s %%\n", output, stat.Project, (68 - len(stat.Project)), hoursStr, 5, percentageStr) + } + + return output +} diff --git a/z/stats.go b/z/stats.go index e887cb0..9e0b580 100644 --- a/z/stats.go +++ b/z/stats.go @@ -38,6 +38,8 @@ var statsCmd = &cobra.Command{ fmt.Printf("%s\n", OutputAppendRight(thisWeek, previousWeek, 16)) + fmt.Printf("%s\n", cal.GetOutputForDistribution()) + return }, }