Laravel Rust✓ Human-reviewed

Gap-filled daily trends in Laravel Rust

Replace one UTC daily-count chart with a scoped aggregate and explicit zero filling, with bounded half-open ranges and production-driver timezone checks.

Approved 2026-12-04T13:20:39.31825Z · Submission 48

Gap-filled daily trends in Laravel Rust

A bounded analytics query

Replace one chart's UTC daily count over a bounded window. Preserve the existing authorization scope and response shape, return one ascending YYYY-MM-DD bucket per day, and fill missing counts with zero. Exclude arbitrary aggregate functions, client-selected columns, local-time/DST charts, weeks, averages, and cross-database abstraction. This guidance has not been exercised in your app.

Check get-recipe's current port catalog; at the time of writing it listed a community Rust port. Offer it as an alternative and verify the app's framework/database compatibility; a listing does not establish parity. A direct query is appropriate when the caller needs only this small subset.

Important source details

The reference Trend accepts an Eloquent builder, applies whereBetween to its date column, aggregates in SQL through a driver adapter, then merges a Carbon-generated sequence of zero placeholders. whereBetween includes both endpoints. An authorized base query must not be replaced by a fresh unscoped table query.

Do not copy interval formatting blindly. At this baseline the PostgreSQL hour SQL label includes seconds while the Carbon hour placeholder label does not; PostgreSQL weeks use ISO year whereas Carbon's placeholder uses calendar year. SQLite/MySQL week formats also differ. The source's ExampleTest.php is only a trivial smoke test, not evidence that these boundaries agree. This recipe avoids those intervals and defines UTC daily behavior explicitly.

Adapt the query and merge

  1. Inventory the actual chart's model scopes, selected date column/type, time zone, filters, aggregate meaning, response shape, and inclusive end behavior. If clients rely on local days or sums/averages, do not silently replace them with this UTC count contract.
  2. Parse input into calendar dates at the boundary. Use UTC midnight start inclusive and the midnight after the last displayed date as end_exclusive. Bound the window, for example to an app-chosen maximum of 366 buckets. This half-open interval is an intentional change from the reference's inclusive-end API; translate existing callers explicitly.
  3. Reuse the installed Laravel Rust database builder or existing app repository, with tenant and soft-delete predicates intact. Aggregate once in SQL rather than loading every record or issuing one query per day. Bind start/end and tenant values; do not interpolate request strings into SQL, aliases, aggregate names, or sort expressions.
  4. Select one fixed day expression for the actual driver/column. For PostgreSQL timestamptz, to_char(created_at AT TIME ZONE 'UTC', 'YYYY-MM-DD') gives UTC day labels. For a timestamp without timezone whose values are already UTC, omit that conversion. MySQL timestamp session settings and SQLite stored text conventions need their own verified expression; they are not interchangeable. Apply the time range to the timestamp column, not to a formatted string.
  5. Read each aggregate as a checked integer count. Initialize a BTreeMap<String, i64> with every expected day label and zero, then assign returned counts by exact label. Reject unexpected/duplicate labels rather than appending them. Generate labels with the app's pinned calendar-date API and calendar-day stepping, not approximate month durations. Match Rust/date-library formats to the SQL labels exactly.
  6. Serialize an explicit ordered vector of {date, aggregate} objects, retaining numeric types expected by the client. An empty result still contains the requested zero buckets. If caching is required, include tenant, filters, window, UTC zone, and aggregate in its key; never cache a scoped chart under a global key.

Illustrative SQL for PostgreSQL with a timestamptz column only; use the app's parameter API, not string replacement:

SELECT to_char(created_at AT TIME ZONE 'UTC', 'YYYY-MM-DD') AS date,
       count(*) AS aggregate
FROM orders
WHERE tenant_id = $1 AND deleted_at IS NULL
  AND created_at >= $2 AND created_at < $3
GROUP BY 1 ORDER BY 1;

The table, columns, deletion policy, and parameter types must match the real model; this SQL is not a framework method or a portable query.

Acceptance checks

Use a three-day window starting 2026-01-30: two authorized rows on day one, none on day two, and one exactly at day-three midnight. Add a foreign tenant row and a row exactly at the exclusive end. Expect counts [2, 0, 1], three unique ascending labels, and no other rows.

  • Empty and one-day windows produce the declared number of buckets; reversed, malformed, and oversized windows fail without a query explosion.
  • End-of-month, leap-day, year boundary, and timestamps around UTC midnight align SQL and placeholder labels exactly.
  • Change the database session time zone away from UTC: the chart remains UTC or explicitly rejects an unsupported configuration.
  • Soft-deleted/unauthorized rows remain excluded. Two tenant requests cannot share cached results.
  • Query output uses integer counts and the exact original chart envelope; no duplicate bucket appears when a database count replaces a zero.

Run formatting, strict Clippy, chart/authorization tests, and the same cases against the production database driver. Record actual framework/date-library/driver versions and any deliberate endpoint changes with the change; a SQLite-only test cannot validate PostgreSQL timezone semantics.

Sources

  • MIT reference: flowframe/laravel-trend v0.5.0, src/Trend.php, all src/Adapters/*, TrendValue.php, and tests/ExampleTest.php. Preserve the upstream LICENSE.md for adapted source.
  • Laravel Rust 0.1.3 baseline; inspect actual locked database and calendar-date APIs.
  • Port options: check get-recipe's current port catalog (sourced from Package Port) at implementation time.