package report import ( "bytes" _ "embed" "fmt" "html/template" "math" "time" ) //go:embed assets/style.css var style template.CSS //go:embed assets/tailwind.js var tailwind template.JS type tmpler struct { Report Tailwind template.JS Style template.CSS } func (r Report) ToHTML() (html string, err error) { tmpl := template.Must( template.New("report").Funcs(template.FuncMap{ "md": mdToHTML, "oh": offsetHTags, "price": r.applyRate, "time": fmtDateTime, "date": fmtDate, "duration": fmtDuration, }).Parse(r.template)) buf := new(bytes.Buffer) err = tmpl.Execute(buf, tmpler{r, tailwind, style}) if err != nil { return html, err } html = buf.String() return html, err } func (r Report) applyRate(dur time.Duration) string { cost := dur.Hours() * r.Rate cost = math.Round(cost/0.05) * 0.05 return fmt.Sprintf("%.2f", cost) } func fmtDateTime(t time.Time) string { return t.Format("02.08.2006 15:04") } func fmtDate(t time.Time) string { return t.Format("02.08.2006") } func fmtDuration(d time.Duration) string { return fmt.Sprintf("%.2f h", d.Hours()) } func (r Report) Total() time.Duration { dur := time.Duration(0) for _, i := range r.Issues { dur += i.Duration } return dur }