From 9489952842cfa480921206db7a95b93cf380ada4 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sun, 29 Nov 2026 20:24:54 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Export=20entries,=20days=20and=20we?= =?UTF-8?q?eks=20as=20CSV?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/tracking/export.go | 139 ++++++++++++++++++++++++++++++++++ internal/tracking/tracking.go | 3 + 2 files changed, 142 insertions(+) create mode 100644 internal/tracking/export.go diff --git a/internal/tracking/export.go b/internal/tracking/export.go new file mode 100644 index 0000000..665ad33 --- /dev/null +++ b/internal/tracking/export.go @@ -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) + } +} diff --git a/internal/tracking/tracking.go b/internal/tracking/tracking.go index f8a307a..3d6a49d 100644 --- a/internal/tracking/tracking.go +++ b/internal/tracking/tracking.go @@ -39,6 +39,9 @@ func Register(app core.App) { g.DELETE("/days/{dayKey}/close", handleReopenDay) g.POST("/weeks/{weekKey}/close", handleCloseWeek) g.DELETE("/weeks/{weekKey}/close", handleReopenWeek) + g.GET("/export/entries.csv", handleExportEntries) + g.GET("/export/days.csv", handleExportDays) + g.GET("/export/weeks.csv", handleExportWeeks) return se.Next() }) app.OnRecordAfterCreateSuccess("users").BindFunc(seedDefaultSettings)