@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,25 +1,25 @@
1
1
  import { defineDataApp } from '@altertable/data-app';
2
- import {
3
- createDataContext,
4
- injectDataAppStyles,
5
- mountDataApp,
6
- } from '@altertable/data-app-react';
2
+ import { createDataContext, mountDataApp } from '@altertable/data-app-react';
7
3
  import {
8
4
  createDataHooks,
9
5
  DataApp,
10
6
  DataSection,
11
7
  VisualizationWidget,
12
- Stack,
13
- choiceVariable,
8
+ Ranking,
9
+ Grid,
10
+ GridItem,
14
11
  } from '@altertable/data-app-react';
15
12
  import { BarChart } from '@altertable/data-app-react/ui';
16
- import { createDataClient } from '@altertable/data-app/client';
17
- import { parseCount } from '@altertable/data-app/contract';
18
- const dataApp = defineDataApp({
13
+ import { choiceFilter, parseCount } from '@altertable/data-app/contract';
14
+ import { formatCount } from '@altertable/data-app/format';
15
+ export const dataApp = defineDataApp({
16
+ // Replace the demo title, description, and scope labels with the app's identity.
19
17
  title: 'Sample metric',
20
- description: 'Fixture counts by group; replace with the requested metric.',
18
+ description: 'Explore sample volume across groups and segments.',
21
19
  scope: { organization: 'demo', environment: 'sample' },
22
20
  appearance: { theme: 'system' },
21
+ // starter_sample_counts is the seeded semantic view for fictional sample counts.
22
+ // Replace these queries with verified sources before publishing.
23
23
  queries: {
24
24
  sampleCountsBySegment: {
25
25
  projections: {
@@ -30,7 +30,7 @@ const dataApp = defineDataApp({
30
30
  SELECT segment_name, sample_count FROM SEMANTIC_VIEW(
31
31
  starter_sample_counts DIMENSIONS samples.segment_name METRICS samples.sample_count
32
32
  WHERE $groupName = '' OR samples.group_name = $groupName
33
- ) ORDER BY segment_name LIMIT $limit`,
33
+ ) ORDER BY sample_count DESC, segment_name LIMIT $limit`,
34
34
  params: {
35
35
  groupName: { type: 'string', defaultValue: '' },
36
36
  limit: { type: 'number', defaultValue: 10 },
@@ -53,61 +53,67 @@ SELECT group_name, sample_count FROM SEMANTIC_VIEW(
53
53
  },
54
54
  },
55
55
  });
56
- const operations = {
56
+ // Reuse this group filter in both operations so one selection updates both charts.
57
+ const groupName = choiceFilter({
58
+ key: 'group',
59
+ label: 'Group',
60
+ defaultValue: 'all',
61
+ options: [
62
+ { id: 'all', label: 'All groups' },
63
+ { id: 'Alpha', label: 'Alpha' },
64
+ { id: 'Beta', label: 'Beta' },
65
+ ],
66
+ });
67
+ // The sample SQL uses '' for all groups; the filter control uses 'all'.
68
+ function queryGroup(groupName: string) {
69
+ return groupName === 'all' ? '' : groupName;
70
+ }
71
+ export const operations = dataApp.defineOperations({
57
72
  sampleCountsByGroup: dataApp.defineOperation({
58
- input: value => {
59
- if (
60
- !value ||
61
- typeof value !== 'object' ||
62
- !('groupName' in value) ||
63
- typeof value.groupName !== 'string' ||
64
- !['all', 'Alpha', 'Beta'].includes(value.groupName)
65
- )
66
- throw new Error('Expected a fixture group.');
67
- return { groupName: value.groupName };
73
+ filters: { groupName },
74
+ policy: { maxQueryRows: 10, maxDurationMs: 15000 },
75
+ async run({ query }, { groupName }) {
76
+ const queryResult = await query('sampleCountsByGroup', {
77
+ groupName: queryGroup(groupName),
78
+ });
79
+ return queryResult.rows.map(
80
+ (row): { groupName: 'Alpha' | 'Beta'; sampleCount: number } => {
81
+ if (row.group_name !== 'Alpha' && row.group_name !== 'Beta')
82
+ throw new Error('Unexpected fixture group.');
83
+ return {
84
+ groupName: row.group_name,
85
+ sampleCount: parseCount(row.sample_count),
86
+ };
87
+ }
88
+ );
68
89
  },
69
- checks: [
70
- { groupName: 'all' },
71
- { groupName: 'Alpha' },
72
- { groupName: 'Beta' },
73
- ],
90
+ }),
91
+ sampleCountsBySegment: dataApp.defineOperation({
92
+ filters: { groupName },
74
93
  policy: { maxQueryRows: 10, maxDurationMs: 15000 },
75
94
  async run({ query }, { groupName }) {
76
- const [groups, segments] = await Promise.all([
77
- query('sampleCountsByGroup', {
78
- groupName: groupName === 'all' ? '' : groupName,
79
- }),
80
- query('sampleCountsBySegment', {
81
- groupName: groupName === 'all' ? '' : groupName,
82
- }),
83
- ]);
84
- return {
85
- groups: groups.rows.map(
86
- (row): { groupName: 'Alpha' | 'Beta'; sampleCount: number } => {
87
- if (row.group_name !== 'Alpha' && row.group_name !== 'Beta')
88
- throw new Error('Unexpected fixture group.');
89
- return {
90
- groupName: row.group_name,
91
- sampleCount: parseCount(row.sample_count),
92
- };
93
- }
94
- ),
95
- segments: segments.rows.map(row => ({
96
- segmentName: row.segment_name,
97
- sampleCount: parseCount(row.sample_count),
98
- })),
99
- };
95
+ const queryResult = await query('sampleCountsBySegment', {
96
+ groupName: queryGroup(groupName),
97
+ });
98
+ return queryResult.rows.map(row => ({
99
+ segmentName: row.segment_name,
100
+ sampleCount: parseCount(row.sample_count),
101
+ }));
100
102
  },
101
103
  }),
104
+ });
105
+ const queryNames = {
106
+ ...operations.sampleCountsByGroup.queryNames,
107
+ ...operations.sampleCountsBySegment.queryNames,
102
108
  };
103
- const queryNames = operations.sampleCountsByGroup.queryNames;
104
109
  const sampleDataContext = createDataContext(queryNames)({
105
110
  description:
106
- 'The starter_sample_counts semantic view measures fictional samples by group and segment. Replace it with the requested semantic metric before publishing.',
111
+ 'The starter_sample_counts semantic view measures fictional sample volume by group and segment. The source has no time dimension.',
107
112
  glossary: {
108
113
  sampleCount: {
109
114
  term: 'Sample count',
110
- definition: 'A fixture value: Alpha is 2,200,000 and Beta is 600,000.',
115
+ definition:
116
+ 'Sum of recorded sample counts in the selected group scope. Additive across groups and segments.',
111
117
  queryNames: [
112
118
  queryNames.sampleCountsByGroup,
113
119
  queryNames.sampleCountsBySegment,
@@ -115,25 +121,11 @@ const sampleDataContext = createDataContext(queryNames)({
115
121
  },
116
122
  },
117
123
  });
118
- const { defineDataView } = createDataHooks(createDataClient({ operations }));
119
- const sampleCountsView = defineDataView({
124
+ const { defineView } = createDataHooks(operations.createDataClient());
125
+ const sampleCountsView = defineView({
120
126
  dataContext: sampleDataContext,
121
127
  operation: 'sampleCountsByGroup',
122
- variables: {
123
- groupName: choiceVariable({
124
- key: 'group',
125
- label: 'Group',
126
- defaultValue: 'all',
127
- options: [
128
- { id: 'all', label: 'All groups' },
129
- { id: 'Alpha', label: 'Alpha' },
130
- { id: 'Beta', label: 'Beta' },
131
- ],
132
- }),
133
- },
134
- describeInput: ({ groupName }) =>
135
- groupName === 'all' ? 'all groups' : groupName,
136
- isEmpty: result => result.groups.length === 0,
128
+ isEmpty: rows => rows.length === 0,
137
129
  emptyFallback: {
138
130
  title: 'No matching groups',
139
131
  description: 'No observations are available for this query.',
@@ -141,7 +133,7 @@ const sampleCountsView = defineDataView({
141
133
  });
142
134
  const sampleCounts = sampleCountsView.dataset({
143
135
  name: 'Sample counts',
144
- select: result => result.groups,
136
+ select: rows => rows,
145
137
  rowKey: row => row.groupName,
146
138
  columns: {
147
139
  groupName: { label: 'Group', value: row => row.groupName },
@@ -152,9 +144,18 @@ const sampleCounts = sampleCountsView.dataset({
152
144
  glossaryIds: ['sampleCount'],
153
145
  },
154
146
  });
155
- const segments = sampleCountsView.dataset({
147
+ const segmentCountsView = defineView({
148
+ dataContext: sampleDataContext,
149
+ operation: 'sampleCountsBySegment',
150
+ isEmpty: rows => rows.length === 0,
151
+ emptyFallback: {
152
+ title: 'No matching segments',
153
+ description: 'No observations are available for this query.',
154
+ },
155
+ });
156
+ const segmentCounts = segmentCountsView.dataset({
156
157
  name: 'Counts by segment',
157
- select: result => result.segments,
158
+ select: rows => rows,
158
159
  rowKey: row => row.segmentName,
159
160
  columns: {
160
161
  segmentName: { label: 'Segment', value: row => row.segmentName },
@@ -162,6 +163,7 @@ const segments = sampleCountsView.dataset({
162
163
  },
163
164
  evidence: { id: 'counts-by-segment', glossaryIds: ['sampleCount'] },
164
165
  });
166
+ // Selecting a group applies the shared group filter to both charts.
165
167
  const groupActions = sampleCounts.actions(
166
168
  action => ({
167
169
  drill: action.filter({
@@ -172,58 +174,59 @@ const groupActions = sampleCounts.actions(
172
174
  }),
173
175
  { primary: 'drill' }
174
176
  );
175
- // Keep only the breakdowns and interactions relevant to the requested metric.
176
- const sampleContent = sampleCountsView.content(result => (
177
- <Stack>
178
- <VisualizationWidget
179
- dataset={sampleCounts}
180
- source={result}
181
- title="Sample count"
182
- description="Select a group to filter both charts; choose All groups to reset."
183
- actions={groupActions}
184
- >
185
- {(rows, actions) => (
186
- <BarChart
187
- ariaLabel="Sample counts by group"
188
- unit="samples"
189
- items={rows.map(row => ({
190
- ...row,
191
- id: row.groupName,
192
- label: row.groupName,
193
- value: row.sampleCount,
194
- }))}
195
- itemActions={actions}
196
- />
197
- )}
198
- </VisualizationWidget>
199
- <VisualizationWidget
200
- dataset={segments}
201
- source={result}
202
- title="Samples by segment"
203
- description="Segment breakdown for the selected group scope."
204
- >
205
- {(rows, actions) => (
206
- <BarChart
207
- ariaLabel="Sample counts by segment"
208
- unit="samples"
209
- items={rows.map(row => ({
210
- ...row,
211
- id: row.segmentName,
212
- label: row.segmentName,
213
- value: row.sampleCount,
214
- }))}
215
- itemActions={actions}
216
- />
217
- )}
218
- </VisualizationWidget>
219
- </Stack>
177
+ const groupContent = sampleCountsView.content(result => (
178
+ <VisualizationWidget
179
+ dataset={sampleCounts}
180
+ source={result}
181
+ title="Samples by group"
182
+ actions={groupActions}
183
+ >
184
+ {(rows, actions) => (
185
+ <BarChart
186
+ ariaLabel="Sample counts by group"
187
+ unit="samples"
188
+ items={rows.map(row => ({
189
+ ...row,
190
+ id: row.groupName,
191
+ label: `${row.groupName} · ${formatCount(row.sampleCount, { compact: true })}`,
192
+ value: row.sampleCount,
193
+ }))}
194
+ horizontalGrid={false}
195
+ itemActions={actions}
196
+ />
197
+ )}
198
+ </VisualizationWidget>
199
+ ));
200
+ const segmentContent = segmentCountsView.content(result => (
201
+ <VisualizationWidget
202
+ dataset={segmentCounts}
203
+ source={result}
204
+ title="Samples by segment"
205
+ >
206
+ {rows => (
207
+ <Ranking
208
+ aria-label="Sample counts by segment"
209
+ items={rows.map(row => ({
210
+ id: row.segmentName,
211
+ label: row.segmentName,
212
+ value: row.sampleCount,
213
+ }))}
214
+ />
215
+ )}
216
+ </VisualizationWidget>
220
217
  ));
221
218
  function App() {
222
219
  return (
223
220
  <DataApp view={sampleCountsView} showToolbar={false}>
224
- <DataSection content={sampleContent} />
221
+ <Grid columns={3}>
222
+ <GridItem span={2}>
223
+ <DataSection content={groupContent} />
224
+ </GridItem>
225
+ <GridItem>
226
+ <DataSection content={segmentContent} />
227
+ </GridItem>
228
+ </Grid>
225
229
  </DataApp>
226
230
  );
227
231
  }
228
- injectDataAppStyles();
229
232
  mountDataApp({ app: dataApp, component: App });