@altertable/data-app-examples 0.76.0 → 0.78.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/package.json +1 -1
  2. package/templates/starter-data-app/index.tsx +686 -136
  3. package/templates/starter-finding-data-app/index.tsx +530 -188
  4. package/templates/starter-local-data-app/AGENTS.md +9 -9
  5. package/templates/starter-local-data-app/README.md +14 -5
  6. package/templates/starter-local-data-app/app.ts +256 -21
  7. package/templates/starter-local-data-app/package.json +5 -5
  8. package/templates/starter-local-data-app/src/App.tsx +276 -5
  9. package/templates/starter-local-data-app/src/data-context.ts +28 -14
  10. package/templates/starter-local-data-app/src/filters.ts +35 -0
  11. package/templates/starter-local-data-app/src/main.tsx +3 -3
  12. package/templates/starter-local-data-app/src/operations.ts +129 -4
  13. package/templates/starter-local-data-app/src/server.ts +1 -2
  14. package/templates/starter-local-finding-data-app/AGENTS.md +9 -9
  15. package/templates/starter-local-finding-data-app/README.md +3 -4
  16. package/templates/starter-local-finding-data-app/app.ts +63 -34
  17. package/templates/starter-local-finding-data-app/package.json +5 -5
  18. package/templates/starter-local-finding-data-app/src/App.tsx +308 -126
  19. package/templates/starter-local-finding-data-app/src/data-context.ts +19 -4
  20. package/templates/starter-local-finding-data-app/src/filters.ts +66 -0
  21. package/templates/starter-local-finding-data-app/src/main.tsx +3 -3
  22. package/templates/starter-local-finding-data-app/src/operations.ts +101 -37
  23. package/templates/starter-local-finding-data-app/src/server.ts +4 -2
  24. package/templates/starter-local-metric-data-app/AGENTS.md +9 -9
  25. package/templates/starter-local-metric-data-app/README.md +3 -4
  26. package/templates/starter-local-metric-data-app/app.ts +5 -2
  27. package/templates/starter-local-metric-data-app/package.json +5 -5
  28. package/templates/starter-local-metric-data-app/src/App.tsx +69 -71
  29. package/templates/starter-local-metric-data-app/src/data-context.ts +9 -5
  30. package/templates/starter-local-metric-data-app/src/filters.ts +18 -0
  31. package/templates/starter-local-metric-data-app/src/main.tsx +3 -3
  32. package/templates/starter-local-metric-data-app/src/operations.ts +34 -41
  33. package/templates/starter-local-metric-data-app/src/server.ts +4 -2
  34. package/templates/starter-metric-data-app/index.tsx +125 -122
@@ -1,108 +1,150 @@
1
1
  import {
2
- choiceVariable,
3
2
  createDataHooks,
4
3
  DataApp,
5
4
  DataSection,
6
- MetricWidget,
5
+ Button,
6
+ Grid,
7
+ GridItem,
8
+ Notice,
7
9
  Stack,
8
10
  TableWidget,
9
11
  VisualizationWidget,
12
+ Ranking,
13
+ Comparison,
10
14
  } from '@altertable/data-app-react';
11
- import { BarChart, ComposedChart } from '@altertable/data-app-react/ui';
12
- import { createDataClient } from '@altertable/data-app/client';
15
+ import {
16
+ Tabs,
17
+ TabList,
18
+ Tab,
19
+ TabPanels,
20
+ TabPanel,
21
+ useViewTab,
22
+ ComposedChart,
23
+ useAppFilters,
24
+ } from '@altertable/data-app-react/ui';
25
+ import {
26
+ formatCount,
27
+ formatNumber,
28
+ formatPercent,
29
+ } from '@altertable/data-app/format';
13
30
 
14
31
  import { sampleDataContext } from '#app/data-context.ts';
15
- import type { operations } from '#app/operations.ts';
16
- import { period } from '#config';
17
- const { defineTimeView } =
18
- createDataHooks(createDataClient<typeof operations>());
19
- const samplesView = defineTimeView({
32
+ import {
33
+ findingPeriodLabel,
34
+ periodLabel,
35
+ periodFilter,
36
+ groupName,
37
+ grain,
38
+ breakdown,
39
+ } from '#app/filters.ts';
40
+ import { operations } from '#app/operations.ts';
41
+ const { defineView } = createDataHooks(operations.createDataClient());
42
+ const count = { kind: 'count' } as const;
43
+ const emptyFallback = {
44
+ title: 'No samples in this period',
45
+ description: 'No observations are available for this period.',
46
+ };
47
+ const totalsView = defineView({
20
48
  dataContext: sampleDataContext,
21
- operation: 'samples',
22
- time: period,
23
- variables: {
24
- grain: choiceVariable({
25
- key: 'grain',
26
- label: 'Grain',
27
- defaultValue: 'day',
28
- options: [
29
- { id: 'day', label: 'Day' },
30
- { id: 'week', label: 'Week' },
31
- ],
32
- presentation: { group: 'display', control: 'widget' },
33
- }),
34
- breakdown: choiceVariable({
35
- key: 'breakdown',
36
- label: 'Breakdown',
37
- defaultValue: 'segment',
38
- options: [
39
- { id: 'segment', label: 'Segment' },
40
- { id: 'group', label: 'Group' },
41
- ],
42
- presentation: { group: 'display', control: 'widget' },
43
- }),
44
- },
49
+ operation: 'sampleTotals',
45
50
  isEmpty: rows => rows.length === 0,
46
- emptyFallback: {
47
- title: 'No samples in this period',
48
- description: 'No observations are available for this period.',
49
- },
51
+ emptyFallback,
50
52
  });
51
- type SampleRow = { sampleCount: number; selected: boolean };
52
- function total(rows: readonly SampleRow[]) {
53
- return rows.reduce((sum, row) => sum + row.sampleCount, 0);
54
- }
55
- function totalsBy<Row extends SampleRow>(
56
- rows: readonly Row[],
57
- key: (row: Row) => string
58
- ) {
59
- const totals = new Map<string, number>();
60
- for (const row of rows)
61
- totals.set(key(row), (totals.get(key(row)) ?? 0) + row.sampleCount);
62
- return [...totals].map(([id, sampleCount]) => ({ id, sampleCount }));
63
- }
64
- const count = { kind: 'count' } as const;
65
- const totalSamples = samplesView.metric(
66
- {
67
- id: 'total-samples',
68
- label: 'Samples',
69
- glossaryId: 'sampleCount',
70
- format: count,
53
+ const totals = totalsView.dataset({
54
+ name: 'Period totals',
55
+ select: rows => rows,
56
+ rowKey: () => 'total',
57
+ columns: {
58
+ currentCount: {
59
+ label: 'Selected period',
60
+ value: row => row.currentCount,
61
+ format: count,
62
+ },
63
+ previousCount: {
64
+ label: 'Previous period',
65
+ value: row => row.previousCount,
66
+ format: count,
67
+ },
71
68
  },
72
- (rows, input) => ({
73
- current: total(rows.filter(row => row.selected)),
74
- ...(input.period.comparison && {
75
- previous: total(rows.filter(row => !row.selected)),
76
- }),
77
- })
78
- );
79
- const trend = samplesView.dataset({
69
+ evidence: { id: 'period-totals', glossaryIds: ['sampleCount'] },
70
+ });
71
+ const trendView = defineView({
72
+ dataContext: sampleDataContext,
73
+ operation: 'samplesOverTime',
74
+ isEmpty: rows => rows.length === 0,
75
+ emptyFallback,
76
+ });
77
+ const trend = trendView.dataset({
80
78
  name: 'Samples over time',
81
- select: rows => totalsBy(rows, row => row.periodStart),
82
- rowKey: row => row.id,
79
+ select: rows => rows,
80
+ rowKey: row => row.periodStart,
83
81
  columns: {
84
- id: { label: 'Period start', value: row => row.id },
82
+ periodStart: { label: 'Period start', value: row => row.periodStart },
85
83
  sampleCount: { value: row => row.sampleCount, format: count },
86
84
  },
87
85
  evidence: { id: 'samples-over-time', glossaryIds: ['sampleCount'] },
88
86
  });
89
- const breakdown = samplesView.dataset({
90
- name: 'Samples by member',
91
- select: rows =>
92
- totalsBy(
93
- rows.filter(row => row.selected),
94
- row => row.member
95
- ),
96
- rowKey: row => row.id,
87
+ const memberView = defineView({
88
+ dataContext: sampleDataContext,
89
+ operation: 'samplesByMember',
90
+ isEmpty: rows => rows.length === 0,
91
+ emptyFallback,
92
+ });
93
+ const byMember = memberView.dataset({
94
+ name: 'Member comparison',
95
+ select: rows => rows,
96
+ rowKey: row => row.member,
97
97
  columns: {
98
- id: { label: 'Member', value: row => row.id },
99
- sampleCount: { value: row => row.sampleCount, format: count },
98
+ member: { label: 'Member', value: row => row.member },
99
+ currentCount: {
100
+ label: 'Selected period',
101
+ value: row => row.currentCount,
102
+ format: count,
103
+ },
104
+ previousCount: {
105
+ label: 'Previous period',
106
+ value: row => row.previousCount,
107
+ format: count,
108
+ },
109
+ change: {
110
+ label: 'Change',
111
+ value: row => row.change,
112
+ format: row => formatNumber(row.change, { signDisplay: 'exceptZero' }),
113
+ },
114
+ changeRate: {
115
+ label: 'Change %',
116
+ value: row => row.changeRate,
117
+ format: row => formatPercent(row.changeRate),
118
+ },
119
+ },
120
+ evidence: {
121
+ id: 'samples-by-member',
122
+ glossaryIds: ['sampleCount', 'sampleChange'],
100
123
  },
101
- evidence: { id: 'samples-by-member', glossaryIds: ['sampleCount'] },
102
124
  });
103
- const sampleRows = samplesView.dataset({
125
+ const selectedMembers = memberView.dataset({
126
+ name: 'Selected-period samples',
127
+ select: rows => rows,
128
+ rowKey: row => row.member,
129
+ columns: {
130
+ member: { label: 'Member', value: row => row.member },
131
+ currentCount: {
132
+ label: 'Samples',
133
+ value: row => row.currentCount,
134
+ format: count,
135
+ },
136
+ },
137
+ evidence: { id: 'selected-members', glossaryIds: ['sampleCount'] },
138
+ });
139
+ const rowsView = defineView({
140
+ dataContext: sampleDataContext,
141
+ operation: 'sampleRows',
142
+ isEmpty: rows => rows.length === 0,
143
+ emptyFallback,
144
+ });
145
+ const sampleRows = rowsView.dataset({
104
146
  name: 'Samples',
105
- select: rows => rows.filter(row => row.selected),
147
+ select: rows => rows,
106
148
  rowKey: row => `${row.periodStart}:${row.member}`,
107
149
  columns: {
108
150
  periodStart: { label: 'Period start', value: row => row.periodStart },
@@ -111,55 +153,195 @@ const sampleRows = samplesView.dataset({
111
153
  },
112
154
  evidence: { id: 'samples', glossaryIds: ['sampleCount'] },
113
155
  });
114
- // The Finding's text is shown above the app; keep content to charts and captions.
115
- const sampleContent = samplesView.content(result => (
116
- <Stack>
117
- <MetricWidget metric={totalSamples} source={result} />
118
- <VisualizationWidget
119
- dataset={trend}
120
- source={result}
121
- title="Samples over time"
122
- description="Includes the previous period when comparison is on."
123
- breakdown={samplesView.breakdown('grain')}
124
- >
125
- {rows => (
126
- <ComposedChart
127
- ariaLabel="Samples over time"
128
- data={rows}
129
- valueFormat={count}
130
- >
131
- <ComposedChart.XAxis dataKey="id" />
132
- <ComposedChart.YAxis />
133
- <ComposedChart.Line dataKey="sampleCount" name="Samples" />
134
- <ComposedChart.Tooltip />
135
- </ComposedChart>
136
- )}
137
- </VisualizationWidget>
138
- <VisualizationWidget
139
- dataset={breakdown}
140
- source={result}
141
- title="Samples by member"
142
- breakdown={samplesView.breakdown('breakdown')}
143
- >
144
- {rows => (
145
- <BarChart
146
- ariaLabel="Samples by member"
147
- unit="samples"
148
- items={rows.map(row => ({
149
- ...row,
150
- label: row.id,
151
- value: row.sampleCount,
152
- }))}
156
+ // Compare current and previous periods from the same displayed result.
157
+ const totalsContent = totalsView.content(result => (
158
+ <VisualizationWidget
159
+ dataset={totals}
160
+ source={result}
161
+ title={
162
+ result.loading || result.input.period.comparison
163
+ ? 'Period comparison'
164
+ : 'Selected period'
165
+ }
166
+ >
167
+ {rows =>
168
+ result.loading ? null : result.input.period.comparison ? (
169
+ <Comparison
170
+ label="Samples"
171
+ current={{
172
+ value: rows[0]!.currentCount,
173
+ formattedValue: formatCount(rows[0]!.currentCount),
174
+ periodLabel: periodLabel(result.input.period.range),
175
+ }}
176
+ previous={{
177
+ value: rows[0]!.previousCount,
178
+ formattedValue: formatCount(rows[0]!.previousCount, {
179
+ missing: 'Not available',
180
+ }),
181
+ periodLabel: periodLabel(result.input.period.comparison),
182
+ }}
183
+ />
184
+ ) : (
185
+ <Ranking
186
+ items={[
187
+ {
188
+ id: 'selected',
189
+ label: periodLabel(result.input.period.range),
190
+ value: rows[0]!.currentCount,
191
+ },
192
+ ]}
153
193
  />
154
- )}
155
- </VisualizationWidget>
156
- <TableWidget dataset={sampleRows} source={result} title="Samples" />
157
- </Stack>
194
+ )
195
+ }
196
+ </VisualizationWidget>
197
+ ));
198
+ const trendContent = trendView.content(result => (
199
+ <VisualizationWidget
200
+ dataset={trend}
201
+ source={result}
202
+ title="Samples over time"
203
+ breakdown={trendView.breakdown('grain')}
204
+ >
205
+ {rows => (
206
+ <ComposedChart
207
+ ariaLabel="Samples over time"
208
+ data={rows}
209
+ valueFormat={count}
210
+ >
211
+ <ComposedChart.XAxis dataKey="periodStart" />
212
+ <ComposedChart.YAxis />
213
+ <ComposedChart.Line dataKey="sampleCount" name="Samples" />
214
+ <ComposedChart.Tooltip />
215
+ </ComposedChart>
216
+ )}
217
+ </VisualizationWidget>
218
+ ));
219
+ const memberContent = memberView.content(result => (
220
+ <VisualizationWidget
221
+ dataset={byMember}
222
+ source={result}
223
+ title={
224
+ result.loading || result.input.period.comparison
225
+ ? 'Where volume changed'
226
+ : 'Samples by member'
227
+ }
228
+ breakdown={memberView.breakdown('breakdown')}
229
+ >
230
+ {rows => (
231
+ <ComposedChart
232
+ ariaLabel="Samples by member"
233
+ data={rows}
234
+ valueFormat={count}
235
+ >
236
+ <ComposedChart.XAxis dataKey="member" />
237
+ <ComposedChart.YAxis />
238
+ {!result.loading && result.input.period.comparison && (
239
+ <ComposedChart.Bar
240
+ dataKey="previousCount"
241
+ name="Previous period"
242
+ fill="var(--atbl-muted)"
243
+ fillOpacity={0.45}
244
+ />
245
+ )}
246
+ <ComposedChart.Bar dataKey="currentCount" name="Selected period" />
247
+ <ComposedChart.Tooltip />
248
+ <ComposedChart.Legend />
249
+ </ComposedChart>
250
+ )}
251
+ </VisualizationWidget>
252
+ ));
253
+ const memberTableContent = memberView.content(result => (
254
+ <TableWidget
255
+ dataset={
256
+ result.loading || result.input.period.comparison
257
+ ? byMember
258
+ : selectedMembers
259
+ }
260
+ source={result}
261
+ title={
262
+ result.loading || result.input.period.comparison
263
+ ? 'Change by member'
264
+ : 'Samples by member'
265
+ }
266
+ />
158
267
  ));
268
+ const rowsContent = rowsView.content(result => (
269
+ <TableWidget dataset={sampleRows} source={result} title="Sample details" />
270
+ ));
271
+ // Show this notice when scope or display choices differ from the original finding.
272
+ // Back to finding restores both scope filters and display choices to their defaults.
273
+ function FindingScope() {
274
+ const filters = useAppFilters({
275
+ period: periodFilter,
276
+ groupName,
277
+ grain,
278
+ breakdown,
279
+ });
280
+ const scopeChanged =
281
+ !periodFilter.same(filters.values.period, periodFilter.defaultValue) ||
282
+ !groupName.same(filters.values.groupName, groupName.defaultValue);
283
+ const displayChanged =
284
+ !grain.same(filters.values.grain, grain.defaultValue) ||
285
+ !breakdown.same(filters.values.breakdown, breakdown.defaultValue);
286
+ if (!scopeChanged && !displayChanged) return null;
287
+ return (
288
+ <Notice
289
+ title={
290
+ scopeChanged
291
+ ? 'Exploring a different scope'
292
+ : 'Viewing a different breakdown'
293
+ }
294
+ description={
295
+ <>
296
+ Original finding · {findingPeriodLabel} {periodFilter.policy.timeZone}{' '}
297
+ vs previous period ·{' '}
298
+ {groupName.describe?.(groupName.defaultValue) ??
299
+ groupName.defaultValue}
300
+ </>
301
+ }
302
+ action={
303
+ <Button size="compact" onClick={() => filters.resetAll()}>
304
+ Back to finding
305
+ </Button>
306
+ }
307
+ />
308
+ );
309
+ }
310
+ const views = ['overview', 'data'] as const;
159
311
  export function App() {
312
+ const [view, setView] = useViewTab(views, 'overview');
160
313
  return (
161
- <DataApp view={samplesView} showToolbar={false}>
162
- <DataSection content={sampleContent} />
314
+ <DataApp view={trendView} showToolbar={false}>
315
+ <Stack>
316
+ <FindingScope />
317
+ <Tabs selectedKey={view} onSelectionChange={setView}>
318
+ <TabList aria-label="Finding views">
319
+ <Tab id="overview">Overview</Tab>
320
+ <Tab id="data">Data</Tab>
321
+ </TabList>
322
+ <TabPanels>
323
+ <TabPanel id="overview">
324
+ <Stack>
325
+ <Grid columns={3}>
326
+ <GridItem span={2}>
327
+ <DataSection content={trendContent} />
328
+ </GridItem>
329
+ <GridItem>
330
+ <DataSection content={totalsContent} />
331
+ </GridItem>
332
+ </Grid>
333
+ <DataSection content={memberContent} />
334
+ </Stack>
335
+ </TabPanel>
336
+ <TabPanel id="data">
337
+ <Stack>
338
+ <DataSection content={memberTableContent} />
339
+ <DataSection content={rowsContent} />
340
+ </Stack>
341
+ </TabPanel>
342
+ </TabPanels>
343
+ </Tabs>
344
+ </Stack>
163
345
  </DataApp>
164
346
  );
165
347
  }
@@ -1,13 +1,28 @@
1
1
  import { createDataContext } from '@altertable/data-app-react';
2
- const queryNames = { samples: 'samples' } as const;
2
+
3
+ import { operations } from '#app/operations.ts';
4
+
5
+ const queryNames = {
6
+ ...operations.sampleTotals.queryNames,
7
+ ...operations.samplesOverTime.queryNames,
8
+ ...operations.samplesByMember.queryNames,
9
+ ...operations.sampleRows.queryNames,
10
+ };
3
11
  export const sampleDataContext = createDataContext(queryNames)({
4
12
  description:
5
- 'Fictional daily samples from starter_demo.daily_samples. Replace this evidence with the actual Finding queries.',
13
+ 'Fictional daily sample counts by group and segment. Comparisons use equal-length periods in UTC.',
6
14
  glossary: {
15
+ sampleChange: {
16
+ term: 'Sample change',
17
+ definition:
18
+ 'Selected-period samples minus the previous equal-length period in the same scope. Missing observations remain unavailable; they are not measured zeroes.',
19
+ queryNames: ['samplesByMember'],
20
+ },
7
21
  sampleCount: {
8
22
  term: 'Sample count',
9
- definition: 'Fixture samples recorded per day, group, and segment.',
10
- queryNames: [queryNames.samples],
23
+ definition:
24
+ 'Number of samples recorded for each day, group, and segment.',
25
+ queryNames: Object.values(queryNames),
11
26
  },
12
27
  },
13
28
  });
@@ -0,0 +1,66 @@
1
+ import {
2
+ choiceFilter,
3
+ dateTimeRangeFilter,
4
+ displayChoice,
5
+ } from '@altertable/data-app/contract';
6
+ import { formatDateRange } from '@altertable/data-app/format';
7
+
8
+ // Replace this fictional finding period with the period supported by its evidence.
9
+ const findingPeriod = {
10
+ from: new Date('2026-03-09T00:00:00Z'),
11
+ to: new Date('2026-03-16T00:00:00Z'),
12
+ };
13
+ export function periodLabel(range: { from: Date; to: Date }) {
14
+ return formatDateRange({
15
+ start: range.from.toISOString().slice(0, 10),
16
+ end: new Date(range.to.getTime() - 1).toISOString().slice(0, 10),
17
+ });
18
+ }
19
+ export const findingPeriodLabel = periodLabel(findingPeriod);
20
+ export const periodFilter = dateTimeRangeFilter({
21
+ key: 'period',
22
+ defaultValue: findingPeriod,
23
+ comparison: 'on',
24
+ // These bounds match the seeded demo history. Use the real source coverage.
25
+ minDate: '2026-01-05',
26
+ maxDate: '2026-03-29',
27
+ maxRangeDays: 28,
28
+ presets: [{ label: 'Finding period', range: findingPeriod }],
29
+ });
30
+ export const groupName = choiceFilter({
31
+ key: 'group',
32
+ label: 'Group',
33
+ defaultValue: 'Alpha',
34
+ options: [
35
+ { id: 'all', label: 'All groups' },
36
+ { id: 'Alpha', label: 'Alpha' },
37
+ { id: 'Beta', label: 'Beta' },
38
+ ],
39
+ });
40
+ export const grain = displayChoice({
41
+ key: 'grain',
42
+ label: 'Grain',
43
+ defaultValue: 'day',
44
+ options: [
45
+ { id: 'day', label: 'Day' },
46
+ { id: 'week', label: 'Week' },
47
+ ],
48
+ presentation: { control: 'widget' },
49
+ });
50
+ export const breakdown = displayChoice({
51
+ key: 'breakdown',
52
+ label: 'Breakdown',
53
+ defaultValue: 'segment',
54
+ options: [
55
+ { id: 'segment', label: 'Segment' },
56
+ { id: 'group', label: 'Group' },
57
+ ],
58
+ presentation: { control: 'widget' },
59
+ });
60
+
61
+ export const operationFilters = {
62
+ sampleTotals: { period: periodFilter, groupName },
63
+ samplesOverTime: { period: periodFilter, groupName, grain },
64
+ samplesByMember: { period: periodFilter, groupName, breakdown },
65
+ sampleRows: { period: periodFilter, groupName, grain, breakdown },
66
+ };
@@ -1,8 +1,8 @@
1
- import { injectDataAppStyles, mountDataApp } from '@altertable/data-app-react';
1
+ import { mountDataApp } from '@altertable/data-app-react';
2
+ import { installLocalQueryLakehouse } from '@altertable/data-app/client';
2
3
 
3
4
  import { App } from '#app/App.tsx';
4
5
  import { dataApp } from '#config';
5
6
 
6
- injectDataAppStyles();
7
-
7
+ installLocalQueryLakehouse();
8
8
  mountDataApp({ app: dataApp, component: App });