trello-bot/daily-headmap.go

117 lines
2.3 KiB
Go

package main
import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"github.com/nikolaydubina/calendarheatmap/charts"
"github.com/urfave/cli/v2"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"image/color"
"os"
"strings"
"time"
_ "github.com/nikolaydubina/calendarheatmap/charts"
_ "golang.org/x/image/font"
_ "golang.org/x/image/font/opentype"
)
//go:embed assets/fonts/Sunflower-Medium.ttf
var defaultFontFaceBytes []byte
//go:embed assets/colorscales/green-blue-9.csv
var defaultColorScaleBytes []byte
func dailyHabitImage(cCtx *cli.Context) error {
folder := DATAPATH
files, err := os.ReadDir(folder)
if err != nil {
return err
}
counts := make(map[string]int)
for _, f := range files {
if f.IsDir() {
continue
}
day := strings.TrimSuffix(f.Name(), ".json")
content, err := os.ReadFile(fmt.Sprintf("%v/%v", folder, f.Name()))
if err != nil {
return err
}
var data []archivCard
err = json.Unmarshal(content, &data)
if err != nil {
return err
}
inToDo := 0
all := 0
for _, archivCard := range data {
if archivCard.List == "ToDo" {
inToDo++
}
all++
}
var percent float64
percent = float64(all-inToDo) / float64(all) * 100
counts[day] = int(percent)
}
colorscale, err := charts.NewBasicColorscaleFromCSV(bytes.NewBuffer(defaultColorScaleBytes))
if err != nil {
return err
}
fontFace, err := charts.LoadFontFace(defaultFontFaceBytes, opentype.FaceOptions{
Size: 26,
DPI: 280,
Hinting: font.HintingNone,
})
if err != nil {
return err
}
conf := charts.HeatmapConfig{
Counts: counts,
ColorScale: colorscale,
DrawMonthSeparator: false,
DrawLabels: true,
Margin: 30,
BoxSize: 150,
MonthSeparatorWidth: 5,
MonthLabelYOffset: 50,
TextWidthLeft: 300,
TextHeightTop: 200,
TextColor: color.RGBA{100, 100, 100, 255},
BorderColor: color.RGBA{200, 200, 200, 255},
Locale: "en_US",
Format: "png",
FontFace: fontFace,
ShowWeekdays: map[time.Weekday]bool{
time.Monday: true,
time.Wednesday: true,
time.Friday: true,
},
}
f, err := os.OpenFile("/tmp/headmap.png", os.O_WRONLY|os.O_CREATE, 0644)
defer f.Close()
err = charts.WriteHeatmap(conf, f)
if err != nil {
return err
}
return nil
}