Skip to content

Commit 285ad8d

Browse files
fix(core): chart compare-to shifts {current_iso_week} to the compared week (#36416)
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} --> _Requested by **Steedos** · [project thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbD5gxSWeWGswbZYVzNvtqNM)_ Fixes #35395 Clause-②: no Metadata-protocol: none ## 维护者速读 - 仪表盘作者在图表筛选里用「本 ISO 周」并开启环比时,对比曲线从「同一周」改为「上一周」(同比则为去年同一天所在周),趋势标签显示「较上周」。 - 修复图表环比/同比功能里的一处遗漏,替用周主数据做报表的作者解决对比失真。 - 出错时只影响同时用到这个周占位符和对比功能的图表,对比数值不对;可直接撤回本改动。 - 无 ## What changes `shiftFilterByCompareTo` (`ui/packages/core/src/utils/compare-to.ts`) renamed date macros but left the `{current_iso_week}` period key as written, so the server resolved it to the current week and the comparison series filtered the same week as the main one. - `previousPeriod`: `{current_iso_week}` / `${current_iso_week}` is replaced by the literal previous ISO week key (`2026-W22` becomes `2026-W21`). Week 1 steps back to the previous ISO year's last week: W53 when that year starts on a Thursday (or a Wednesday in a leap year), else W52. The literal key passes through every resolver unchanged. No new public token is added. - `previousYear`: the period key is resolved against the same shifted day the date macros already use (`resolvePeriodKeyTokens`), so it is the ISO week of the same day one year earlier. - `compareToTrendLabelKey`: a filter on `{current_iso_week}` now reads `vsLastWeek` instead of falling through to `vsPreviousPeriod`. Other period-key placeholders that share the gap: none. `PERIOD_KEY_TOKENS` in `@objectstack/spec/data` has one member, `current_iso_week`. ## Tests New block `compare-to on the {current_iso_week} period key` in `ui/packages/core/src/utils/__tests__/compare-to.test.ts`: previousPeriod mid-year (W22 to W21, both spellings), week 1 to 2026-W53 and to 2025-W52, previousYear (2025-W21; Fri 2027-01-01 = 2026-W53 becomes 2026-W01), and the label. <details><summary>Run on a2b9aa6</summary> - `pnpm exec vitest run packages/core/src/utils/ --maxWorkers=1` (from `ui/`, under the verify lock): Test Files 124 passed (124), Tests 2214 passed (2214). `pnpm --filter @object-ui/core type-check`: exit 0. Lock: `VERDICT command-exit 0`. - Ablation, one-off (fix committed first): deleted the `current_iso_week` substitution line with `scripts/ablation-replace.mjs`, `grep -c` of the anchor = 0 on disk. compare-to.test.ts: Tests 2 failed / 33 passed. Both previousPeriod pins went red, and the received value was `'{current_iso_week}'`, which is the bug. The test imports the source by relative path, so there is no dist leg. Restored with `git checkout HEAD --`: `git diff HEAD` is empty and the blob hash matches HEAD (`d276c311`). </details> ## Acceptance notes - ADR-0021 D3 dataset path: `service-analytics` `dataset-executor.ts` shifts only the chosen time dimension's `dateRange` (`shiftRange`). A selection `filter` is held constant across the compare pass, whether it holds a date macro or a period key. That is the executor's existing design, not a period-key regression. Noted here, not filed: read from code, no public-door measurement. ## Verification Read first: `compare-to.ts` `previousIsoWeekKey` (the year-edge arithmetic is the risky hunk), then its call in `shiftFilterByCompareTo`. Two-minute check: `cd ui && pnpm exec vitest run packages/core/src/utils/__tests__/compare-to.test.ts`. Expect 35 passed. --- _Generated by [Claude Code](https://claude.ai/code/session_01QsR2jREBdDtYvSLn38xUCg)_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01QsR2jREBdDtYvSLn38xUCg Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5345905 commit 285ad8d

3 files changed

Lines changed: 64 additions & 4 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@object-ui/core': patch
3+
---
4+
5+
fix(core): a chart's compare-to no longer repeats the current week when its filter uses `{current_iso_week}`
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
`previousPeriod` now compares with last week's ISO key (week 1 steps back to the previous ISO year's W52/W53), and `previousYear` with the key of the same day one year earlier. The trend label reads "vs last week".

‎ui/packages/core/src/utils/__tests__/compare-to.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,32 @@ describe('compareToTrendLabelKey', () => {
303303
});
304304
});
305305
});
306+
307+
// objectstack#35395 — `{current_iso_week}` is a period key, not a date macro;
308+
// left as written the comparison series filtered the same week as the current one.
309+
describe('compare-to on the {current_iso_week} period key', () => {
310+
const filter = { cost_week: '{current_iso_week}', alt: '${current_iso_week}' };
311+
const run = (kind: 'previousPeriod' | 'previousYear', now: Date) =>
312+
shiftFilterByCompareTo(filter, { kind }, now);
313+
314+
it('previousPeriod compares with last week’s key, never this week’s', () => {
315+
const out = run('previousPeriod', new Date(2026, 4, 25, 12)); // Mon, 2026-W22
316+
expect(out).toEqual({ cost_week: '2026-W21', alt: '2026-W21' });
317+
});
318+
319+
it('previousPeriod steps week 1 back into the previous ISO year’s last week', () => {
320+
// 2026 starts on a Thursday → 53 weeks; 2025 starts on a Wednesday, not leap → 52.
321+
expect(run('previousPeriod', new Date(2027, 0, 4, 12)).cost_week).toBe('2026-W53');
322+
expect(run('previousPeriod', new Date(2026, 0, 1, 12)).cost_week).toBe('2025-W52');
323+
});
324+
325+
it('previousYear resolves the key of the same day one year earlier', () => {
326+
expect(run('previousYear', new Date(2026, 4, 25, 12)).cost_week).toBe('2025-W21');
327+
// Fri 2027-01-01 is 2026-W53; one year earlier, Thu 2026-01-01, is 2026-W01.
328+
expect(run('previousYear', new Date(2027, 0, 1, 12)).cost_week).toBe('2026-W01');
329+
});
330+
331+
it('labels the previousPeriod delta as vs last week', () => {
332+
expect(compareToTrendLabelKey({ kind: 'previousPeriod' }, filter)).toBe('vsLastWeek');
333+
});
334+
});

‎ui/packages/core/src/utils/compare-to.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
import { resolveDateMacros } from './date-macros.js';
9+
import { resolveDateMacros, resolvePeriodKeyTokens } from './date-macros.js';
1010

1111
/**
1212
* Period-over-period comparison config for dashboard widgets.
@@ -133,6 +133,24 @@ function lastPeriodBefore(token: string, now: Date): string | undefined {
133133
return isoShift(lastStart, { years: -1 });
134134
}
135135

136+
/**
137+
* The ISO week key just before the one `{current_iso_week}` resolves to at
138+
* `now` (objectstack#35395). A period key is not a date macro, so no token
139+
* rename moves it, and left as written the server resolves it to THIS week:
140+
* the comparison series filtered the same week as the current one. It is
141+
* written as the literal key instead, which every resolver passes through.
142+
* Week 1 steps back into the previous ISO year's last week, W53 when that
143+
* year starts on a Thursday (or a Wednesday in a leap year), else W52.
144+
*/
145+
function previousIsoWeekKey(now: Date): string {
146+
const [year, week] = resolvePeriodKeyTokens('{current_iso_week}', now).split('-W').map(Number);
147+
if (week > 1) return `${year}-W${String(week - 1).padStart(2, '0')}`;
148+
const prev = year - 1;
149+
const jan1 = new Date(Date.UTC(prev, 0, 1)).getUTCDay();
150+
const leap = (prev % 4 === 0 && prev % 100 !== 0) || prev % 400 === 0;
151+
return `${prev}-W${jan1 === 4 || (leap && jan1 === 3) ? 53 : 52}`;
152+
}
153+
136154
const WHOLE_TOKEN_RE = /^\$?\{([a-zA-Z0-9_]+)\}$/;
137155
const ROLLING_DAYS_RE = /^(\d+)_days_(ago|from_now)$/;
138156

@@ -246,7 +264,8 @@ function substituteTokens<T>(value: T, replace: (token: string) => string | unde
246264
* (objectstack#30140), and `{N_days_ago}` … `{today}` becomes
247265
* `{(2N+1)_days_ago}` … `{(N+1)_days_ago}` (objectstack#30197). Then
248266
* `next_month_*` / `next_quarter_*` become `current_*`, and `{yesterday}` /
249-
* `last_*` become the period before them ({@link lastPeriodBefore}). Each
267+
* `last_*` become the period before them ({@link lastPeriodBefore}), and
268+
* `{current_iso_week}` becomes last week's key ({@link previousIsoWeekKey}). Each
250269
* token is substituted once, so no window moves back twice.
251270
*/
252271
export function shiftFilterByCompareTo<T = any>(
@@ -266,7 +285,8 @@ export function shiftFilterByCompareTo<T = any>(
266285
now.getMinutes(),
267286
now.getSeconds(),
268287
);
269-
return resolveDateMacros(filter, shifted);
288+
// The period key too: the same ISO week of the shifted day, not this one.
289+
return resolvePeriodKeyTokens(resolveDateMacros(filter, shifted), shifted);
270290
}
271291
// previousPeriod. The rolling pair goes first, so it claims its `{today}`
272292
// before the generic `today` → `yesterday` swap can; the `{K_day(s)_ago}`
@@ -278,6 +298,7 @@ export function shiftFilterByCompareTo<T = any>(
278298
const substituted = substituteTokens(rolled, (tok) => {
279299
const renamed = PREVIOUS_PERIOD_TOKENS[tok];
280300
if (renamed) return `{${renamed}}`;
301+
if (tok === 'current_iso_week') return previousIsoWeekKey(now);
281302
return lastPeriodBefore(tok, now);
282303
});
283304
return resolveDateMacros(substituted, now);
@@ -313,7 +334,7 @@ export function compareToTrendLabelKey(
313334
if (/current_quarter_|quarter_start|quarter_end/.test(json)) return 'vsLastQuarter';
314335
if (/next_month_(start|end)/.test(json)) return 'vsThisMonth';
315336
if (/current_month_|month_start|month_end/.test(json)) return 'vsLastMonth';
316-
if (/current_week_|week_start|week_end/.test(json)) return 'vsLastWeek';
337+
if (/current_week_|week_start|week_end|current_iso_week/.test(json)) return 'vsLastWeek';
317338
if (hasRollingWindow(filter)) return 'vsPreviousPeriod';
318339
if (/\btoday\b/.test(json)) return 'vsYesterday';
319340
return 'vsPreviousPeriod';

0 commit comments

Comments
 (0)