@altertable/data-app-examples 0.74.0 → 0.76.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 (52) hide show
  1. package/README.md +8 -4
  2. package/dist/index.js +38 -11
  3. package/dist/index.js.map +1 -1
  4. package/dist/types/index.d.ts +20 -0
  5. package/package.json +13 -1
  6. package/templates/starter-finding-data-app/LICENSE +21 -0
  7. package/templates/starter-finding-data-app/index.tsx +282 -0
  8. package/templates/starter-local-data-app/AGENTS.md +9 -9
  9. package/templates/starter-local-data-app/README.md +4 -4
  10. package/templates/starter-local-data-app/package.json +3 -3
  11. package/templates/starter-local-finding-data-app/.bun-version +1 -0
  12. package/templates/starter-local-finding-data-app/.node-version +1 -0
  13. package/templates/starter-local-finding-data-app/.oxfmtrc.json +11 -0
  14. package/templates/starter-local-finding-data-app/.oxlintrc.json +33 -0
  15. package/templates/starter-local-finding-data-app/AGENTS.md +18 -0
  16. package/templates/starter-local-finding-data-app/LICENSE +21 -0
  17. package/templates/starter-local-finding-data-app/README.md +43 -0
  18. package/templates/starter-local-finding-data-app/_gitignore +3 -0
  19. package/templates/starter-local-finding-data-app/app.ts +59 -0
  20. package/templates/starter-local-finding-data-app/bunfig.toml +2 -0
  21. package/templates/starter-local-finding-data-app/package.json +36 -0
  22. package/templates/starter-local-finding-data-app/src/App.tsx +165 -0
  23. package/templates/starter-local-finding-data-app/src/assets.d.ts +1 -0
  24. package/templates/starter-local-finding-data-app/src/data-context.ts +13 -0
  25. package/templates/starter-local-finding-data-app/src/favicon.svg +13 -0
  26. package/templates/starter-local-finding-data-app/src/index.html +14 -0
  27. package/templates/starter-local-finding-data-app/src/main.tsx +8 -0
  28. package/templates/starter-local-finding-data-app/src/operations.ts +50 -0
  29. package/templates/starter-local-finding-data-app/src/server.ts +7 -0
  30. package/templates/starter-local-finding-data-app/tsconfig.json +21 -0
  31. package/templates/starter-local-metric-data-app/.bun-version +1 -0
  32. package/templates/starter-local-metric-data-app/.node-version +1 -0
  33. package/templates/starter-local-metric-data-app/.oxfmtrc.json +11 -0
  34. package/templates/starter-local-metric-data-app/.oxlintrc.json +33 -0
  35. package/templates/starter-local-metric-data-app/AGENTS.md +18 -0
  36. package/templates/starter-local-metric-data-app/LICENSE +21 -0
  37. package/templates/starter-local-metric-data-app/README.md +43 -0
  38. package/templates/starter-local-metric-data-app/_gitignore +3 -0
  39. package/templates/starter-local-metric-data-app/app.ts +39 -0
  40. package/templates/starter-local-metric-data-app/bunfig.toml +2 -0
  41. package/templates/starter-local-metric-data-app/package.json +36 -0
  42. package/templates/starter-local-metric-data-app/src/App.tsx +124 -0
  43. package/templates/starter-local-metric-data-app/src/assets.d.ts +1 -0
  44. package/templates/starter-local-metric-data-app/src/data-context.ts +19 -0
  45. package/templates/starter-local-metric-data-app/src/favicon.svg +13 -0
  46. package/templates/starter-local-metric-data-app/src/index.html +14 -0
  47. package/templates/starter-local-metric-data-app/src/main.tsx +8 -0
  48. package/templates/starter-local-metric-data-app/src/operations.ts +50 -0
  49. package/templates/starter-local-metric-data-app/src/server.ts +7 -0
  50. package/templates/starter-local-metric-data-app/tsconfig.json +21 -0
  51. package/templates/starter-metric-data-app/LICENSE +21 -0
  52. package/templates/starter-metric-data-app/index.tsx +229 -0
@@ -0,0 +1,124 @@
1
+ import {
2
+ createDataHooks,
3
+ DataApp,
4
+ DataSection,
5
+ VisualizationWidget,
6
+ Stack,
7
+ choiceVariable,
8
+ } from '@altertable/data-app-react';
9
+ import { BarChart } from '@altertable/data-app-react/ui';
10
+ import { createDataClient } from '@altertable/data-app/client';
11
+
12
+ import { sampleDataContext } from '#app/data-context.ts';
13
+ import type { operations } from '#app/operations.ts';
14
+ const { defineDataView } =
15
+ createDataHooks(createDataClient<typeof operations>());
16
+ const sampleCountsView = defineDataView({
17
+ dataContext: sampleDataContext,
18
+ operation: 'sampleCountsByGroup',
19
+ variables: {
20
+ groupName: choiceVariable({
21
+ key: 'group',
22
+ label: 'Group',
23
+ defaultValue: 'all',
24
+ options: [
25
+ { id: 'all', label: 'All groups' },
26
+ { id: 'Alpha', label: 'Alpha' },
27
+ { id: 'Beta', label: 'Beta' },
28
+ ],
29
+ }),
30
+ },
31
+ describeInput: ({ groupName }) =>
32
+ groupName === 'all' ? 'all groups' : groupName,
33
+ isEmpty: result => result.groups.length === 0,
34
+ emptyFallback: {
35
+ title: 'No matching groups',
36
+ description: 'No observations are available for this query.',
37
+ },
38
+ });
39
+ const sampleCounts = sampleCountsView.dataset({
40
+ name: 'Sample counts',
41
+ select: result => result.groups,
42
+ rowKey: row => row.groupName,
43
+ columns: {
44
+ groupName: { label: 'Group', value: row => row.groupName },
45
+ sampleCount: { value: row => row.sampleCount, format: { kind: 'count' } },
46
+ },
47
+ evidence: {
48
+ id: 'counts-by-group',
49
+ glossaryIds: ['sampleCount'],
50
+ },
51
+ });
52
+ const segments = sampleCountsView.dataset({
53
+ name: 'Counts by segment',
54
+ select: result => result.segments,
55
+ rowKey: row => row.segmentName,
56
+ columns: {
57
+ segmentName: { label: 'Segment', value: row => row.segmentName },
58
+ sampleCount: { value: row => row.sampleCount, format: { kind: 'count' } },
59
+ },
60
+ evidence: { id: 'counts-by-segment', glossaryIds: ['sampleCount'] },
61
+ });
62
+ const groupActions = sampleCounts.actions(
63
+ action => ({
64
+ drill: action.filter({
65
+ label: 'Drill into group',
66
+ target: sampleCountsView.filter(),
67
+ values: row => ({ groupName: row.groupName }),
68
+ }),
69
+ }),
70
+ { primary: 'drill' }
71
+ );
72
+ // Keep only the breakdowns and interactions relevant to the requested metric.
73
+ const sampleContent = sampleCountsView.content(result => (
74
+ <Stack>
75
+ <VisualizationWidget
76
+ dataset={sampleCounts}
77
+ source={result}
78
+ title="Sample count"
79
+ description="Select a group to filter both charts; choose All groups to reset."
80
+ actions={groupActions}
81
+ >
82
+ {(rows, actions) => (
83
+ <BarChart
84
+ ariaLabel="Sample counts by group"
85
+ unit="samples"
86
+ items={rows.map(row => ({
87
+ ...row,
88
+ id: row.groupName,
89
+ label: row.groupName,
90
+ value: row.sampleCount,
91
+ }))}
92
+ itemActions={actions}
93
+ />
94
+ )}
95
+ </VisualizationWidget>
96
+ <VisualizationWidget
97
+ dataset={segments}
98
+ source={result}
99
+ title="Samples by segment"
100
+ description="Segment breakdown for the selected group scope."
101
+ >
102
+ {(rows, actions) => (
103
+ <BarChart
104
+ ariaLabel="Sample counts by segment"
105
+ unit="samples"
106
+ items={rows.map(row => ({
107
+ ...row,
108
+ id: row.segmentName,
109
+ label: row.segmentName,
110
+ value: row.sampleCount,
111
+ }))}
112
+ itemActions={actions}
113
+ />
114
+ )}
115
+ </VisualizationWidget>
116
+ </Stack>
117
+ ));
118
+ export function App() {
119
+ return (
120
+ <DataApp view={sampleCountsView} showToolbar={false}>
121
+ <DataSection content={sampleContent} />
122
+ </DataApp>
123
+ );
124
+ }
@@ -0,0 +1 @@
1
+ declare module '*.css';
@@ -0,0 +1,19 @@
1
+ import { createDataContext } from '@altertable/data-app-react';
2
+ const queryNames = {
3
+ sampleCountsByGroup: 'sampleCountsByGroup',
4
+ sampleCountsBySegment: 'sampleCountsBySegment',
5
+ } as const;
6
+ export const sampleDataContext = createDataContext(queryNames)({
7
+ description:
8
+ 'The starter_sample_counts semantic view measures fictional samples by group and segment. Replace it with the requested semantic metric before publishing.',
9
+ glossary: {
10
+ sampleCount: {
11
+ term: 'Sample count',
12
+ definition: 'A fixture value: Alpha is 2,200,000 and Beta is 600,000.',
13
+ queryNames: [
14
+ queryNames.sampleCountsByGroup,
15
+ queryNames.sampleCountsBySegment,
16
+ ],
17
+ },
18
+ },
19
+ });
@@ -0,0 +1,13 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 51 44">
2
+ <style>
3
+ path { fill: #01050b; }
4
+ @media (prefers-color-scheme: dark) {
5
+ path { fill: #fff; }
6
+ }
7
+ </style>
8
+ <path
9
+ fill-rule="evenodd"
10
+ clip-rule="evenodd"
11
+ d="M40.3274 28.9788L35.9663 28.8378L34.9541 28.6881L18.1066 10.301L24.1073 0L26.8738 0.545413L41.5212 25.6848L40.3274 28.9788ZM20.5319 20.1112L18.1894 15.6731L14.6154 16.2904L0 41.378L0.894584 44H12.9453L20.5769 20.4134L20.5319 20.1112ZM44.9804 31.6253L20.3808 36.9197L17.5838 41.2974L19.9001 44H49.2036L51 41.9551L44.9804 31.6253Z"
12
+ />
13
+ </svg>
@@ -0,0 +1,14 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <meta name="theme-color" content="#ffffff" />
7
+ <title>Altertable app</title>
8
+ <link rel="icon" href="./favicon.svg" type="image/svg+xml" />
9
+ <script type="module" src="./main.tsx"></script>
10
+ </head>
11
+ <body>
12
+ <div id="root"></div>
13
+ </body>
14
+ </html>
@@ -0,0 +1,8 @@
1
+ import { injectDataAppStyles, mountDataApp } from '@altertable/data-app-react';
2
+
3
+ import { App } from '#app/App.tsx';
4
+ import { dataApp } from '#config';
5
+
6
+ injectDataAppStyles();
7
+
8
+ mountDataApp({ app: dataApp, component: App });
@@ -0,0 +1,50 @@
1
+ import { parseCount } from '@altertable/data-app/contract';
2
+
3
+ import { dataApp } from '#config';
4
+ export const operations = {
5
+ 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 };
16
+ },
17
+ checks: [
18
+ { groupName: 'all' },
19
+ { groupName: 'Alpha' },
20
+ { groupName: 'Beta' },
21
+ ],
22
+ policy: { maxQueryRows: 10, maxDurationMs: 15000 },
23
+ 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
+ };
48
+ },
49
+ }),
50
+ };
@@ -0,0 +1,7 @@
1
+ import { serveLocalApp } from '@altertable/data-app-bun';
2
+
3
+ import page from '#app/index.html';
4
+ import { operations } from '#app/operations.ts';
5
+ import { dataApp } from '#config';
6
+
7
+ serveLocalApp({ page, operations, title: dataApp.title });
@@ -0,0 +1,21 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "Preserve",
5
+ "moduleResolution": "bundler",
6
+ "allowImportingTsExtensions": true,
7
+ "resolveJsonModule": true,
8
+ "jsx": "react-jsx",
9
+ "strict": true,
10
+ "noUncheckedIndexedAccess": true,
11
+ "noEmit": true,
12
+ "skipLibCheck": true,
13
+ "types": ["bun"],
14
+ "noImplicitReturns": true,
15
+ "noFallthroughCasesInSwitch": true,
16
+ "noImplicitOverride": true,
17
+ "allowUnreachableCode": false,
18
+ "noUncheckedSideEffectImports": true
19
+ },
20
+ "include": ["src", "app.ts"]
21
+ }
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Altertable
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,229 @@
1
+ import { defineDataApp } from '@altertable/data-app';
2
+ import {
3
+ createDataContext,
4
+ injectDataAppStyles,
5
+ mountDataApp,
6
+ } from '@altertable/data-app-react';
7
+ import {
8
+ createDataHooks,
9
+ DataApp,
10
+ DataSection,
11
+ VisualizationWidget,
12
+ Stack,
13
+ choiceVariable,
14
+ } from '@altertable/data-app-react';
15
+ 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({
19
+ title: 'Sample metric',
20
+ description: 'Fixture counts by group; replace with the requested metric.',
21
+ scope: { organization: 'demo', environment: 'sample' },
22
+ appearance: { theme: 'system' },
23
+ queries: {
24
+ sampleCountsBySegment: {
25
+ projections: {
26
+ segment_name: { type: 'string' },
27
+ sample_count: { type: 'number' },
28
+ },
29
+ statement: `
30
+ SELECT segment_name, sample_count FROM SEMANTIC_VIEW(
31
+ starter_sample_counts DIMENSIONS samples.segment_name METRICS samples.sample_count
32
+ WHERE $groupName = '' OR samples.group_name = $groupName
33
+ ) ORDER BY segment_name LIMIT $limit`,
34
+ params: {
35
+ groupName: { type: 'string', defaultValue: '' },
36
+ limit: { type: 'number', defaultValue: 10 },
37
+ },
38
+ },
39
+ sampleCountsByGroup: {
40
+ projections: {
41
+ group_name: { type: 'string' },
42
+ sample_count: { type: 'number' },
43
+ },
44
+ statement: `
45
+ SELECT group_name, sample_count FROM SEMANTIC_VIEW(
46
+ starter_sample_counts DIMENSIONS samples.group_name METRICS samples.sample_count
47
+ WHERE $groupName = '' OR samples.group_name = $groupName
48
+ ) ORDER BY group_name LIMIT $limit`,
49
+ params: {
50
+ groupName: { type: 'string', defaultValue: '' },
51
+ limit: { type: 'number', defaultValue: 10 },
52
+ },
53
+ },
54
+ },
55
+ });
56
+ const operations = {
57
+ 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 };
68
+ },
69
+ checks: [
70
+ { groupName: 'all' },
71
+ { groupName: 'Alpha' },
72
+ { groupName: 'Beta' },
73
+ ],
74
+ policy: { maxQueryRows: 10, maxDurationMs: 15000 },
75
+ 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
+ };
100
+ },
101
+ }),
102
+ };
103
+ const queryNames = operations.sampleCountsByGroup.queryNames;
104
+ const sampleDataContext = createDataContext(queryNames)({
105
+ description:
106
+ 'The starter_sample_counts semantic view measures fictional samples by group and segment. Replace it with the requested semantic metric before publishing.',
107
+ glossary: {
108
+ sampleCount: {
109
+ term: 'Sample count',
110
+ definition: 'A fixture value: Alpha is 2,200,000 and Beta is 600,000.',
111
+ queryNames: [
112
+ queryNames.sampleCountsByGroup,
113
+ queryNames.sampleCountsBySegment,
114
+ ],
115
+ },
116
+ },
117
+ });
118
+ const { defineDataView } = createDataHooks(createDataClient({ operations }));
119
+ const sampleCountsView = defineDataView({
120
+ dataContext: sampleDataContext,
121
+ 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,
137
+ emptyFallback: {
138
+ title: 'No matching groups',
139
+ description: 'No observations are available for this query.',
140
+ },
141
+ });
142
+ const sampleCounts = sampleCountsView.dataset({
143
+ name: 'Sample counts',
144
+ select: result => result.groups,
145
+ rowKey: row => row.groupName,
146
+ columns: {
147
+ groupName: { label: 'Group', value: row => row.groupName },
148
+ sampleCount: { value: row => row.sampleCount, format: { kind: 'count' } },
149
+ },
150
+ evidence: {
151
+ id: 'counts-by-group',
152
+ glossaryIds: ['sampleCount'],
153
+ },
154
+ });
155
+ const segments = sampleCountsView.dataset({
156
+ name: 'Counts by segment',
157
+ select: result => result.segments,
158
+ rowKey: row => row.segmentName,
159
+ columns: {
160
+ segmentName: { label: 'Segment', value: row => row.segmentName },
161
+ sampleCount: { value: row => row.sampleCount, format: { kind: 'count' } },
162
+ },
163
+ evidence: { id: 'counts-by-segment', glossaryIds: ['sampleCount'] },
164
+ });
165
+ const groupActions = sampleCounts.actions(
166
+ action => ({
167
+ drill: action.filter({
168
+ label: 'Drill into group',
169
+ target: sampleCountsView.filter(),
170
+ values: row => ({ groupName: row.groupName }),
171
+ }),
172
+ }),
173
+ { primary: 'drill' }
174
+ );
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>
220
+ ));
221
+ function App() {
222
+ return (
223
+ <DataApp view={sampleCountsView} showToolbar={false}>
224
+ <DataSection content={sampleContent} />
225
+ </DataApp>
226
+ );
227
+ }
228
+ injectDataAppStyles();
229
+ mountDataApp({ app: dataApp, component: App });