Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/26893-time-series-empty-buckets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@objectstack/service-analytics': patch
---

fix(service-analytics): a date-dimension series answers every bucket of its window, the empty ones included (#26893)

Clause-②: no — no input is refused or newly accepted and no export or type changes; a dataset answer gains the rows for the time buckets no record fell in, which it used to leave out
Metadata-protocol: none

A grouped query answers one row per bucket that holds records, so a month with no closed-won deal was not a zero in a 12-month revenue trend: it was missing, and a chart's category axis drew the months on either side of it as neighbours. `queryDataset` (and `POST /api/v1/analytics/dataset/query`, which the dashboard's dataset widget calls) now adds those buckets in the dataset executor:

- **Which buckets.** When the grid's time dimension carries a `timeDimensions[].dateRange` (a preset or an explicit pair), every bucket of that window, in order. With no `dateRange`, only the gaps between the first and last bucket that came back are filled: a window stated only in `runtimeFilter`, or a one-bound filter such as `{ close_date: { $gte: '{12_months_ago}' } }`, does not bound the edges. No window and no rows still answer no rows.
- **Their values.** The same values an empty group already reads: `0` for `count` and `sum`; `avg`, `min` and `max` stay unset (the column is absent from the row, which a chart reads as no value), never `0`. A derived measure is computed over them as over any row.
- **Other dimensions.** With a second grid dimension (month × owner), each combination that already holds a row gets every bucket; a combination no row holds is not added.
- **Keys.** The added keys are spelled by the same bucket labeller the grouping uses, in the selection's timezone, so they sort and merge with the real ones (`2026-05`, `2026-W01`, `2026-10-04`).
- **Not filled.** A selection with a `limit` or `offset`, an `order` on a measure, or a `compareTo`, and a grid with two bucketed time dimensions, answer exactly as before. A fill that would leave more than 2,000 rows in the grid (a `day` series over ten years) does not run at all, so the answer is never filled part-way.
4 changes: 3 additions & 1 deletion packages/qa/dogfood/test/analytics-timezone.dogfood.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ describe('dogfood: org timezone drives analytics date bucketing (#1982/#2018)',
// The regression: before #2018 this stayed on 2024-03-01 (tz dropped) or
// returned cnt 0. The org timezone must move it to 2024-02-29.
expect(la['2024-02-29']).toBe(N_LEADS);
expect(la['2024-03-01']).toBeUndefined();
// 2024-03-01 now lies between the boundary leads and the seed's own, so it
// comes back as an empty bucket (#26893): present, and no lead counted.
expect(la['2024-03-01']).toBe(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,9 @@ describe('#16737 negative controls — aggregation only, and only the incoherent
});

it('⭐ a `datetime` used as a DIMENSION is untouched — date-range filtering', async () => {
const { svc, calls } = makeBucketService([{ submitted: '2026-05', row_count: 3 }]);
// Keyed by the FIELD, as the engine answers: the strategy maps it onto the
// `submitted` dimension, so the bucket is a real May rather than the null one.
const { svc, calls } = makeBucketService([{ submitted_at: '2026-05', row_count: 3 }]);
const result: any = await svc.queryDataset(
dataset(
[{ name: 'row_count', aggregate: 'count' }],
Expand All @@ -415,7 +417,9 @@ describe('#16737 negative controls — aggregation only, and only the incoherent
timeDimensions: [{ dimension: 'submitted', dateRange: ['2026-01-01', '2026-12-31'] }],
},
);
expect(result.rows.length).toBe(1);
// #26893 — every month of the window comes back, the empty ones at 0.
expect(result.rows.length).toBe(12);
expect(result.rows.filter((r: any) => r.row_count > 0)).toEqual([{ submitted: '2026-05', row_count: 3 }]);
// The window reached the engine as a resolved bound on the datetime column
// — the dimension path this card must not have touched.
expect(JSON.stringify(calls[0])).toContain('2026-01-01');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,15 @@ describe('[#20867] a draft preview keys 0050-06-15 as the published path does',
},
});

// The buckets that hold rows. Between them the executor fills the empty
// ones (#26893) — up to its bound, so the year axis gains 1,975 empty years
// and the finer ones are left as grouped; either way the keys pinned here are
// the two real ones.
const keysOf = (rows: Record<string, unknown>[]) =>
rows.map((r) => [r.spent_on, r.expense_count]).sort((a, b) => String(a[0]).localeCompare(String(b[0])));
rows
.filter((r) => Number(r.expense_count) > 0)
.map((r) => [r.spent_on, r.expense_count])
.sort((a, b) => String(a[0]).localeCompare(String(b[0])));

it.each([
['year', '0050', '2026'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ describe('#3916 — matrix date columns arrive chronological end-to-end', () =>
{ dimensions: ['closed_quarter'], measures: ['task_count'] },
CTX,
);
expect(result.rows.map((r) => r.closed_quarter)).toEqual(['2025-Q4', '2026-Q1', '2026-Q3']);
// 2026-Q2 holds no row and is filled in its place (#26893).
expect(result.rows.map((r) => r.closed_quarter)).toEqual(['2025-Q4', '2026-Q1', '2026-Q2', '2026-Q3']);
});

it('emits a real ORDER BY when native SQL serves the query', async () => {
Expand Down
Loading
Loading