✨ Export entries, days and weeks as CSV
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
package tracking
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
)
|
||||
|
||||
const (
|
||||
dayKeyMin = "0000-01-01"
|
||||
dayKeyMax = "9999-12-31"
|
||||
weekKeyMin = "0000-W01"
|
||||
weekKeyMax = "9999-W53"
|
||||
msPerHour = 3_600_000
|
||||
fileDateFmt = "20060102"
|
||||
)
|
||||
|
||||
func handleExportEntries(e *core.RequestEvent) error {
|
||||
from, to := keyRange(e, dayKeyMin, dayKeyMax)
|
||||
rows, err := e.App.FindRecordsByFilter("entries",
|
||||
liveFilter+" && day_key >= {:from} && day_key <= {:to}",
|
||||
"start_time", 0, 0,
|
||||
dbx.Params{"owner": e.Auth.Id, "from": from, "to": to},
|
||||
)
|
||||
if err != nil {
|
||||
return writeError(e, http.StatusInternalServerError, err)
|
||||
}
|
||||
lines := make([][]string, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
start, end := r.GetInt64("start_time"), r.GetInt64("end_time")
|
||||
endStr, duration := "", "0"
|
||||
if end != 0 {
|
||||
endStr = formatUTC(end)
|
||||
duration = strconv.FormatInt(end-start, 10)
|
||||
}
|
||||
lines = append(lines, []string{
|
||||
r.Id,
|
||||
r.GetString("day_key"),
|
||||
formatUTC(start),
|
||||
endStr,
|
||||
duration,
|
||||
r.GetString("note"),
|
||||
strconv.FormatBool(r.GetBool("auto_stopped")),
|
||||
})
|
||||
}
|
||||
writeCSV(e, "entries", []string{"id", "day_key", "start_time", "end_time", "duration_ms", "note", "auto_stopped"}, lines)
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleExportDays(e *core.RequestEvent) error {
|
||||
from, to := keyRange(e, dayKeyMin, dayKeyMax)
|
||||
rows, err := e.App.FindRecordsByFilter("closed_days",
|
||||
liveFilter+" && day_key >= {:from} && day_key <= {:to}",
|
||||
"day_key", 0, 0,
|
||||
dbx.Params{"owner": e.Auth.Id, "from": from, "to": to},
|
||||
)
|
||||
if err != nil {
|
||||
return writeError(e, http.StatusInternalServerError, err)
|
||||
}
|
||||
lines := make([][]string, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
worked := r.GetInt64("worked_ms")
|
||||
lines = append(lines, []string{
|
||||
r.GetString("day_key"),
|
||||
r.GetString("kind"),
|
||||
strconv.FormatInt(worked, 10),
|
||||
hours(worked),
|
||||
})
|
||||
}
|
||||
writeCSV(e, "days", []string{"day_key", "kind", "worked_ms", "worked_hours"}, lines)
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleExportWeeks(e *core.RequestEvent) error {
|
||||
from, to := keyRange(e, weekKeyMin, weekKeyMax)
|
||||
rows, err := e.App.FindRecordsByFilter("closed_weeks",
|
||||
liveFilter+" && week_key >= {:from} && week_key <= {:to}",
|
||||
"week_key", 0, 0,
|
||||
dbx.Params{"owner": e.Auth.Id, "from": from, "to": to},
|
||||
)
|
||||
if err != nil {
|
||||
return writeError(e, http.StatusInternalServerError, err)
|
||||
}
|
||||
lines := make([][]string, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
expected, worked, delta := r.GetInt64("expected_ms"), r.GetInt64("worked_ms"), r.GetInt64("delta_ms")
|
||||
lines = append(lines, []string{
|
||||
r.GetString("week_key"),
|
||||
strconv.FormatInt(expected, 10),
|
||||
strconv.FormatInt(worked, 10),
|
||||
strconv.FormatInt(delta, 10),
|
||||
hours(expected),
|
||||
hours(worked),
|
||||
hours(delta),
|
||||
})
|
||||
}
|
||||
writeCSV(e, "weeks", []string{"week_key", "expected_ms", "worked_ms", "delta_ms", "expected_h", "worked_h", "delta_h"}, lines)
|
||||
return nil
|
||||
}
|
||||
|
||||
func keyRange(e *core.RequestEvent, defaultFrom, defaultTo string) (string, string) {
|
||||
query := e.Request.URL.Query()
|
||||
from, to := query.Get("from"), query.Get("to")
|
||||
if from == "" {
|
||||
from = defaultFrom
|
||||
}
|
||||
if to == "" {
|
||||
to = defaultTo
|
||||
}
|
||||
return from, to
|
||||
}
|
||||
|
||||
func formatUTC(ms int64) string {
|
||||
return time.UnixMilli(ms).UTC().Format(time.RFC3339)
|
||||
}
|
||||
|
||||
func hours(ms int64) string {
|
||||
return fmt.Sprintf("%.2f", float64(ms)/msPerHour)
|
||||
}
|
||||
|
||||
func writeCSV(e *core.RequestEvent, name string, header []string, lines [][]string) {
|
||||
e.Response.Header().Set("Content-Type", "text/csv")
|
||||
e.Response.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s-%s.csv"`, name, time.Now().Format(fileDateFmt)))
|
||||
e.Response.WriteHeader(http.StatusOK)
|
||||
w := csv.NewWriter(e.Response)
|
||||
w.Write(header)
|
||||
for _, line := range lines {
|
||||
w.Write(line)
|
||||
}
|
||||
w.Flush()
|
||||
if err := w.Error(); err != nil {
|
||||
e.App.Logger().Error("csv export failed", "export", name, "error", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user