Gap-filled daily trends in Laravel Go
Scope and source contract
Build one authorized chart of UTC daily integer counts, with a bounded date window, ascending day labels, and zeroes for missing days. Check get-recipe's current port catalog before implementation; at the time of writing it listed no Go port for Trend-style aggregation/filling. A framework GroupBy does not itself fill gaps. This is a focused app-owned query, not a full API port; verify it in your app with the trial below.
The PHP reference keeps an Eloquent base builder, uses inclusive whereBetween, chooses a database-specific date format, aggregates once in SQL, then merges counts with Carbon period placeholders. Preserve tenant/global/soft-delete scopes. At the cited baseline, hour labels differ between PostgreSQL SQL and Carbon placeholders, and week-year conventions differ too. Do not generalize these formats into a Go helper. The package's example test is tautological; it does not establish boundary correctness.
Implement only UTC daily count
- Inventory the chart's real date column/type, time zone, filters, authorization, aggregation, pagination/envelope, and inclusive-end behavior. Retain the package path or expand the reviewed scope if the app requires local-day DST handling, weeks, arbitrary intervals, sums, or nullable averages.
- Parse a date-only range at the request boundary. Normalize both bounds to UTC midnight, compute the day after the last displayed date as
endExclusive, and bound the number of days. Reject reversed/malformed/excessive ranges. Moving from an inclusive timestamp API to a half-open range is an intentional compatibility change; adapt callers explicitly. - Start the aggregate from the existing authorized query and its context-bound database connection. Use fixed columns and bound range/tenant values. Aggregate once, not with one query per day and not by fetching every row into Go. Keep timestamp range predicates on the raw indexed column.
- Choose a fixed expression for the production driver's actual column type. PostgreSQL
timestamptz:to_char(created_at AT TIME ZONE 'UTC', 'YYYY-MM-DD'). A UTC-valued timestamp without timezone needs no timezone conversion. MySQLTIMESTAMPdepends on session zone; SQLite must use a verified canonical UTC storage representation. Do not interpolate a zone/column/aggregate supplied by the user intoSelectRaw/WhereRaw. - Decode counts into
int64, check label uniqueness and that each belongs to the requested window. Fill exactly one output point per expected day. Reuse the application's response type or define the two-field transport shape where the chart owns it, not a generic analytics DSL.
The following standard-library filling fragment assumes validated UTC-midnight bounds, a bounded window, a map[string]int64 of verified aggregate labels, and an app-owned point type. Missing map entries intentionally mean zero:
for day := start; day.Before(endExclusive); day = day.AddDate(0, 0, 1) {
label := day.Format("2006-01-02")
points = append(points, Point{Date: label, Aggregate: counts[label]})
}
Point, start, and the query result are application values, not Laravel Go APIs. Use calendar stepping; do not add fixed durations if adapting this to a local time zone later. An average with no samples is not automatically equivalent to count zero, so this fill rule must not be generalized to every aggregate.
- Return the original chart envelope with numeric counts and deterministic ordering. Cache only if needed, using tenant/filter/window/zone/aggregate-aware keys. A scoped query with a global cache key still leaks results.
Trial fixtures and expected results
For a three-day window beginning 2026-01-30, create two visible records on day one, none on day two, and one at day-three UTC midnight. Add another tenant's record, a deleted record, and a record at the exclusive end. Expect exactly three ascending labels and counts [2, 0, 1].
- Empty input yields all zero buckets; a one-day window yields exactly one bucket. Invalid or excessive windows fail before expensive work.
- Cross month/year boundaries and a leap day. Compare SQL labels with Go's labels byte for byte.
- Change database session timezone and exercise timestamps immediately around UTC midnight; verify UTC results remain stable.
- Test duplicate/out-of-range aggregate labels and count decoding failures as errors, not extra output points.
- Confirm authorization, deletion policy, response number types, and cached isolation for two tenants.
Run gofmt, go vet ./..., go test ./..., and production-driver chart tests. A race test does not validate timezone SQL; a SQLite pass does not validate PostgreSQL or MySQL. Record Go/framework/database versions, query count, and intentional compatibility changes with the change.
Sources
- MIT reference: flowframe/laravel-trend v0.5.0,
src/Trend.php,Adapters/PgsqlAdapter.php,MySqlAdapter.php,SqliteAdapter.php, andtests/ExampleTest.php. Keep the upstreamLICENSE.mdfor copied/adapted code. - Laravel Go baseline, module
go.laravel.com. Check the app's locked query/result-scanning APIs rather than inventing driver-neutral methods. - Port options: check
get-recipe's current port catalog (sourced from Package Port) at implementation time.