zeit/z/statsCmd.go

62 lines
1.8 KiB
Go
Raw Normal View History

2020-10-16 15:36:44 +00:00
package z
import (
"os"
"fmt"
"time"
2020-10-17 00:33:21 +00:00
"strings"
2020-10-16 15:36:44 +00:00
"github.com/spf13/cobra"
// "github.com/shopspring/decimal"
// "github.com/gookit/color"
2020-10-16 15:36:44 +00:00
)
var statsCmd = &cobra.Command{
Use: "stats",
Short: "Display activity statistics",
Long: "Display statistics on all tracked activities.",
Run: func(cmd *cobra.Command, args []string) {
user := GetCurrentUser()
2020-10-16 15:36:44 +00:00
entries, err := database.ListEntries(user)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
2020-10-16 15:36:44 +00:00
cal, _ := NewCalendar(entries)
2020-10-16 15:36:44 +00:00
weekMinus0 := time.Now()
monthMinus0, weeknumberMinus0 := GetISOWeekInMonth(weekMinus0)
monthMinus00 := monthMinus0 - 1
weeknumberMinus00 := weeknumberMinus0 - 1
thisWeek := cal.GetOutputForWeekCalendar(weekMinus0, monthMinus00, weeknumberMinus00)
weekMinus1 := weekMinus0.AddDate(0, 0, -7)
monthMinus1, weeknumberMinus1 := GetISOWeekInMonth(weekMinus1)
monthMinus10 := monthMinus1 - 1
weeknumberMinus10 := weeknumberMinus1 - 1
previousWeek := cal.GetOutputForWeekCalendar(weekMinus1, monthMinus10, weeknumberMinus10)
if monthMinus00 == monthMinus10 {
fmt.Printf("\n%s\n\n", strings.ToUpper(weekMinus0.Month().String()))
} else {
fmt.Printf("\n%s / %s\n\n", strings.ToUpper(weekMinus0.Month().String()), strings.ToUpper(weekMinus1.Month().String()))
}
2020-10-17 00:33:21 +00:00
fmt.Printf("%s\n\n\n", OutputAppendRight(thisWeek, previousWeek, 16))
fmt.Printf("%s\n", cal.GetOutputForDistribution())
2020-10-16 15:36:44 +00:00
return
},
}
func init() {
rootCmd.AddCommand(statsCmd)
statsCmd.Flags().BoolVar(&fractional, "decimal", false, "Show fractional hours in decimal format instead of minutes")
2020-10-16 15:36:44 +00:00
var err error
database, err = InitDatabase()
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}