@altertable/data-app-examples 0.76.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@altertable/data-app-examples",
3
- "version": "0.76.0",
3
+ "version": "0.77.0",
4
4
  "description": "Canonical starter templates for Altertable data apps.",
5
5
  "keywords": [
6
6
  "altertable",
@@ -6,16 +6,14 @@ import {
6
6
  DataSection,
7
7
  DataValue,
8
8
  TableWidget,
9
- Grid,
10
9
  Stack,
11
10
  TextContent,
12
11
  injectDataAppStyles,
13
12
  mountDataApp,
14
13
  MetricWidget,
15
- searchVariable,
16
14
  } from '@altertable/data-app-react';
17
15
  import { createDataClient } from '@altertable/data-app/client';
18
- import { parseCount } from '@altertable/data-app/contract';
16
+ import { parseCount, searchFilter } from '@altertable/data-app/contract';
19
17
  import { formatMetric } from '@altertable/data-app/format';
20
18
 
21
19
  const dataApp = defineDataApp({
@@ -39,27 +37,22 @@ ORDER BY group_name LIMIT $limit`,
39
37
  limit: { type: 'number', defaultValue: 10 },
40
38
  },
41
39
  },
40
+ sampleTotal: {
41
+ projections: { sample_count: { type: 'number' } },
42
+ statement: `
43
+ WITH sample_counts(group_name, sample_count) AS (VALUES ('Alpha', 2200000), ('Beta', 0))
44
+ SELECT CAST(COALESCE(SUM(sample_count), 0) AS BIGINT) AS sample_count FROM sample_counts
45
+ WHERE $groupName = '' OR group_name = $groupName`,
46
+ params: { groupName: { type: 'string', defaultValue: '' } },
47
+ },
42
48
  },
43
49
  });
44
- function parseSampleCountFilter(value: unknown) {
45
- if (
46
- !value ||
47
- typeof value !== 'object' ||
48
- !('groupName' in value) ||
49
- typeof value.groupName !== 'string' ||
50
- value.groupName.length > 40
51
- )
52
- throw new Error('Expected a group name of at most 40 characters.');
53
- return { groupName: value.groupName };
54
- }
50
+ // Declare each filter once; operations list the filters they read, and views,
51
+ // controls, and scope derive from them.
52
+ const groupName = searchFilter({ key: 'group', label: 'Group' });
55
53
  const operations = {
56
54
  sampleCountsByGroup: dataApp.defineOperation({
57
- input: parseSampleCountFilter,
58
- checks: [
59
- { groupName: '' },
60
- { groupName: 'Alpha' },
61
- { groupName: 'missing' },
62
- ],
55
+ filters: { groupName },
63
56
  policy: { maxQueryRows: 10, maxDurationMs: 15000 },
64
57
  async run({ query }, { groupName }) {
65
58
  const queryResult = await query('sampleCountsByGroup', { groupName });
@@ -69,8 +62,19 @@ const operations = {
69
62
  }));
70
63
  },
71
64
  }),
65
+ sampleTotal: dataApp.defineOperation({
66
+ filters: { groupName },
67
+ policy: { maxQueryRows: 1, maxDurationMs: 15000 },
68
+ async run({ query }, { groupName }) {
69
+ const queryResult = await query('sampleTotal', { groupName });
70
+ return { sampleCount: parseCount(queryResult.rows[0]?.sample_count) };
71
+ },
72
+ }),
73
+ };
74
+ const queryNames = {
75
+ ...operations.sampleCountsByGroup.queryNames,
76
+ ...operations.sampleTotal.queryNames,
72
77
  };
73
- const queryNames = operations.sampleCountsByGroup.queryNames;
74
78
  const sampleDataContext = createDataContext(queryNames)({
75
79
  description:
76
80
  'Two SQL VALUES rows demonstrate the host query path. Replace them with inspected source data before publishing findings.',
@@ -79,23 +83,14 @@ const sampleDataContext = createDataContext(queryNames)({
79
83
  term: 'Sample count',
80
84
  definition:
81
85
  'A fixture value: Alpha is 2,200,000 and Beta is a measured zero.',
82
- queryNames: [queryNames.sampleCountsByGroup],
86
+ queryNames: [queryNames.sampleCountsByGroup, queryNames.sampleTotal],
83
87
  },
84
88
  },
85
89
  });
86
- const { defineDataView } = createDataHooks(createDataClient({ operations }));
87
- const sampleCountsView = defineDataView({
90
+ const { defineView } = createDataHooks(createDataClient({ operations }));
91
+ const sampleCountsView = defineView({
88
92
  dataContext: sampleDataContext,
89
93
  operation: 'sampleCountsByGroup',
90
- variables: {
91
- groupName: searchVariable({
92
- key: 'group',
93
- label: 'Group',
94
- defaultValue: '',
95
- }),
96
- },
97
- describeInput: ({ groupName }) =>
98
- groupName ? `group ${groupName}` : 'all groups',
99
94
  isEmpty: sampleCounts => sampleCounts.length === 0,
100
95
  emptyFallback: {
101
96
  title: 'No matching groups',
@@ -115,52 +110,59 @@ const sampleCounts = sampleCountsView.dataset({
115
110
  glossaryIds: ['sampleCount'],
116
111
  },
117
112
  });
118
- const totalSamples = sampleCountsView.metric(
113
+ const sampleTotalView = defineView({
114
+ dataContext: sampleDataContext,
115
+ operation: 'sampleTotal',
116
+ });
117
+ const totalSamples = sampleTotalView.metric(
119
118
  {
120
119
  id: 'total-samples',
121
120
  glossaryId: 'sampleCount',
122
121
  label: 'Total samples',
123
122
  format: { kind: 'count', compact: true },
124
123
  },
125
- rows => ({ current: rows.reduce((sum, row) => sum + row.sampleCount, 0) })
124
+ total => ({ current: total.sampleCount })
126
125
  );
127
- const sampleContent = sampleCountsView.content(result => (
128
- <Stack aria-label="Sample results">
126
+ const totalContent = sampleTotalView.content(result => (
127
+ <Stack>
129
128
  <TextContent>
130
129
  <p>
131
130
  Showing <DataValue scope={result.scope} />
132
131
  </p>
133
132
  </TextContent>
134
- <Grid columns={2}>
135
- <MetricWidget
136
- metric={totalSamples}
137
- source={result}
138
- description="Sum of the fixture counts in the selected groups."
139
- />
140
- <TableWidget
141
- dataset={sampleCounts}
142
- source={result}
143
- title="Counts by group"
144
- description="Alpha and Beta demonstrate measured values, including zero."
145
- pagination={false}
146
- />
147
- </Grid>
133
+ <MetricWidget
134
+ metric={totalSamples}
135
+ source={result}
136
+ description="Sum of the fixture counts in the selected groups."
137
+ />
148
138
  </Stack>
149
139
  ));
140
+ const countsContent = sampleCountsView.content(result => (
141
+ <TableWidget
142
+ dataset={sampleCounts}
143
+ source={result}
144
+ title="Counts by group"
145
+ description="Alpha and Beta demonstrate measured values, including zero."
146
+ pagination={false}
147
+ />
148
+ ));
150
149
  function App() {
151
150
  return (
152
151
  <DataApp
153
152
  view={sampleCountsView}
153
+ summaryViews={[sampleTotalView]}
154
154
  datasets={[sampleCounts]}
155
- story={snapshot => {
156
- const total = totalSamples.read(snapshot);
157
- const scope = sampleCountsView.scope(snapshot);
155
+ story={(countsSnapshot, totalSnapshot) => {
156
+ const total = totalSamples.read(totalSnapshot);
157
+ const scope = sampleCountsView.scope(countsSnapshot);
158
158
  return [
159
159
  {
160
160
  id: 'total-samples',
161
161
  headline: `Total samples: ${formatMetric(total.value.current, totalSamples.definition.format)}`,
162
162
  context: `Demonstration values for ${scope}.`,
163
- visual: <MetricWidget metric={totalSamples} source={snapshot} />,
163
+ visual: (
164
+ <MetricWidget metric={totalSamples} source={totalSnapshot} />
165
+ ),
164
166
  visualKind: 'metric',
165
167
  evidence: totalSamples,
166
168
  },
@@ -171,7 +173,7 @@ function App() {
171
173
  visual: (
172
174
  <TableWidget
173
175
  dataset={sampleCounts}
174
- source={snapshot}
176
+ source={countsSnapshot}
175
177
  pagination={false}
176
178
  />
177
179
  ),
@@ -184,7 +186,8 @@ function App() {
184
186
  <TextContent>
185
187
  <h2>Sample counts</h2>
186
188
  </TextContent>
187
- <DataSection content={sampleContent} />
189
+ <DataSection content={totalContent} label="Sample total" />
190
+ <DataSection content={countsContent} label="Group counts" />
188
191
  </Stack>
189
192
  </DataApp>
190
193
  );