@altertable/data-app-examples 0.75.0 → 0.77.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.
@@ -1,50 +1,43 @@
1
1
  import { parseCount } from '@altertable/data-app/contract';
2
2
 
3
+ import { operationFilters } from '#app/filters.ts';
3
4
  import { dataApp } from '#config';
5
+
6
+ /** The fixture queries read an empty group name as every group. */
7
+ function queryGroup(groupName: string) {
8
+ return groupName === 'all' ? '' : groupName;
9
+ }
4
10
  export const operations = {
5
11
  sampleCountsByGroup: dataApp.defineOperation({
6
- input: value => {
7
- if (
8
- !value ||
9
- typeof value !== 'object' ||
10
- !('groupName' in value) ||
11
- typeof value.groupName !== 'string' ||
12
- !['all', 'Alpha', 'Beta'].includes(value.groupName)
13
- )
14
- throw new Error('Expected a fixture group.');
15
- return { groupName: value.groupName };
12
+ filters: operationFilters.sampleCountsByGroup,
13
+ policy: { maxQueryRows: 10, maxDurationMs: 15000 },
14
+ async run({ query }, { groupName }) {
15
+ const queryResult = await query('sampleCountsByGroup', {
16
+ groupName: queryGroup(groupName),
17
+ });
18
+ return queryResult.rows.map(
19
+ (row): { groupName: 'Alpha' | 'Beta'; sampleCount: number } => {
20
+ if (row.group_name !== 'Alpha' && row.group_name !== 'Beta')
21
+ throw new Error('Unexpected fixture group.');
22
+ return {
23
+ groupName: row.group_name,
24
+ sampleCount: parseCount(row.sample_count),
25
+ };
26
+ }
27
+ );
16
28
  },
17
- checks: [
18
- { groupName: 'all' },
19
- { groupName: 'Alpha' },
20
- { groupName: 'Beta' },
21
- ],
29
+ }),
30
+ sampleCountsBySegment: dataApp.defineOperation({
31
+ filters: operationFilters.sampleCountsBySegment,
22
32
  policy: { maxQueryRows: 10, maxDurationMs: 15000 },
23
33
  async run({ query }, { groupName }) {
24
- const [groups, segments] = await Promise.all([
25
- query('sampleCountsByGroup', {
26
- groupName: groupName === 'all' ? '' : groupName,
27
- }),
28
- query('sampleCountsBySegment', {
29
- groupName: groupName === 'all' ? '' : groupName,
30
- }),
31
- ]);
32
- return {
33
- groups: groups.rows.map(
34
- (row): { groupName: 'Alpha' | 'Beta'; sampleCount: number } => {
35
- if (row.group_name !== 'Alpha' && row.group_name !== 'Beta')
36
- throw new Error('Unexpected fixture group.');
37
- return {
38
- groupName: row.group_name,
39
- sampleCount: parseCount(row.sample_count),
40
- };
41
- }
42
- ),
43
- segments: segments.rows.map(row => ({
44
- segmentName: row.segment_name,
45
- sampleCount: parseCount(row.sample_count),
46
- })),
47
- };
34
+ const queryResult = await query('sampleCountsBySegment', {
35
+ groupName: queryGroup(groupName),
36
+ });
37
+ return queryResult.rows.map(row => ({
38
+ segmentName: row.segment_name,
39
+ sampleCount: parseCount(row.sample_count),
40
+ }));
48
41
  },
49
42
  }),
50
43
  };
@@ -10,11 +10,10 @@ import {
10
10
  DataSection,
11
11
  VisualizationWidget,
12
12
  Stack,
13
- choiceVariable,
14
13
  } from '@altertable/data-app-react';
15
14
  import { BarChart } from '@altertable/data-app-react/ui';
16
15
  import { createDataClient } from '@altertable/data-app/client';
17
- import { parseCount } from '@altertable/data-app/contract';
16
+ import { choiceFilter, parseCount } from '@altertable/data-app/contract';
18
17
  const dataApp = defineDataApp({
19
18
  title: 'Sample metric',
20
19
  description: 'Fixture counts by group; replace with the requested metric.',
@@ -53,54 +52,60 @@ SELECT group_name, sample_count FROM SEMANTIC_VIEW(
53
52
  },
54
53
  },
55
54
  });
55
+ // Declare each filter once; operations list the filters they read, and views,
56
+ // controls, and scope derive from them.
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 fixture queries read an empty group name as every group. */
68
+ function queryGroup(groupName: string) {
69
+ return groupName === 'all' ? '' : groupName;
70
+ }
56
71
  const operations = {
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
  }),
102
104
  };
103
- const queryNames = operations.sampleCountsByGroup.queryNames;
105
+ const queryNames = {
106
+ ...operations.sampleCountsByGroup.queryNames,
107
+ ...operations.sampleCountsBySegment.queryNames,
108
+ };
104
109
  const sampleDataContext = createDataContext(queryNames)({
105
110
  description:
106
111
  'The starter_sample_counts semantic view measures fictional samples by group and segment. Replace it with the requested semantic metric before publishing.',
@@ -115,25 +120,11 @@ const sampleDataContext = createDataContext(queryNames)({
115
120
  },
116
121
  },
117
122
  });
118
- const { defineDataView } = createDataHooks(createDataClient({ operations }));
119
- const sampleCountsView = defineDataView({
123
+ const { defineView } = createDataHooks(createDataClient({ operations }));
124
+ const sampleCountsView = defineView({
120
125
  dataContext: sampleDataContext,
121
126
  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,
127
+ isEmpty: rows => rows.length === 0,
137
128
  emptyFallback: {
138
129
  title: 'No matching groups',
139
130
  description: 'No observations are available for this query.',
@@ -141,7 +132,7 @@ const sampleCountsView = defineDataView({
141
132
  });
142
133
  const sampleCounts = sampleCountsView.dataset({
143
134
  name: 'Sample counts',
144
- select: result => result.groups,
135
+ select: rows => rows,
145
136
  rowKey: row => row.groupName,
146
137
  columns: {
147
138
  groupName: { label: 'Group', value: row => row.groupName },
@@ -152,9 +143,18 @@ const sampleCounts = sampleCountsView.dataset({
152
143
  glossaryIds: ['sampleCount'],
153
144
  },
154
145
  });
155
- const segments = sampleCountsView.dataset({
146
+ const segmentCountsView = defineView({
147
+ dataContext: sampleDataContext,
148
+ operation: 'sampleCountsBySegment',
149
+ isEmpty: rows => rows.length === 0,
150
+ emptyFallback: {
151
+ title: 'No matching segments',
152
+ description: 'No observations are available for this query.',
153
+ },
154
+ });
155
+ const segmentCounts = segmentCountsView.dataset({
156
156
  name: 'Counts by segment',
157
- select: result => result.segments,
157
+ select: rows => rows,
158
158
  rowKey: row => row.segmentName,
159
159
  columns: {
160
160
  segmentName: { label: 'Segment', value: row => row.segmentName },
@@ -173,55 +173,58 @@ const groupActions = sampleCounts.actions(
173
173
  { primary: 'drill' }
174
174
  );
175
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>
176
+ const groupContent = sampleCountsView.content(result => (
177
+ <VisualizationWidget
178
+ dataset={sampleCounts}
179
+ source={result}
180
+ title="Sample count"
181
+ description="Select a group to filter both charts; choose All groups to reset."
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,
192
+ value: row.sampleCount,
193
+ }))}
194
+ itemActions={actions}
195
+ />
196
+ )}
197
+ </VisualizationWidget>
198
+ ));
199
+ const segmentContent = segmentCountsView.content(result => (
200
+ <VisualizationWidget
201
+ dataset={segmentCounts}
202
+ source={result}
203
+ title="Samples by segment"
204
+ description="Segment breakdown for the selected group scope."
205
+ >
206
+ {(rows, actions) => (
207
+ <BarChart
208
+ ariaLabel="Sample counts by segment"
209
+ unit="samples"
210
+ items={rows.map(row => ({
211
+ ...row,
212
+ id: row.segmentName,
213
+ label: row.segmentName,
214
+ value: row.sampleCount,
215
+ }))}
216
+ itemActions={actions}
217
+ />
218
+ )}
219
+ </VisualizationWidget>
220
220
  ));
221
221
  function App() {
222
222
  return (
223
223
  <DataApp view={sampleCountsView} showToolbar={false}>
224
- <DataSection content={sampleContent} />
224
+ <Stack>
225
+ <DataSection content={groupContent} label="Sample count" />
226
+ <DataSection content={segmentContent} label="Samples by segment" />
227
+ </Stack>
225
228
  </DataApp>
226
229
  );
227
230
  }