@altertable/data-app-examples 0.77.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 +681 -134
  3. package/templates/starter-finding-data-app/index.tsx +382 -98
  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 +1 -1
  15. package/templates/starter-local-finding-data-app/README.md +3 -4
  16. package/templates/starter-local-finding-data-app/app.ts +29 -11
  17. package/templates/starter-local-finding-data-app/package.json +5 -5
  18. package/templates/starter-local-finding-data-app/src/App.tsx +241 -44
  19. package/templates/starter-local-finding-data-app/src/data-context.ts +17 -8
  20. package/templates/starter-local-finding-data-app/src/filters.ts +29 -11
  21. package/templates/starter-local-finding-data-app/src/main.tsx +3 -3
  22. package/templates/starter-local-finding-data-app/src/operations.ts +80 -34
  23. package/templates/starter-local-finding-data-app/src/server.ts +4 -2
  24. package/templates/starter-local-metric-data-app/AGENTS.md +1 -1
  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 +21 -22
  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 +1 -3
  31. package/templates/starter-local-metric-data-app/src/main.tsx +3 -3
  32. package/templates/starter-local-metric-data-app/src/operations.ts +3 -3
  33. package/templates/starter-local-metric-data-app/src/server.ts +4 -2
  34. package/templates/starter-metric-data-app/index.tsx +34 -34
@@ -1,196 +1,743 @@
1
1
  import { defineDataApp } from '@altertable/data-app';
2
2
  import {
3
3
  createDataContext,
4
+ mountDataApp,
4
5
  createDataHooks,
5
6
  DataApp,
6
7
  DataSection,
7
- DataValue,
8
+ Grid,
9
+ MetricWidget,
8
10
  TableWidget,
11
+ VisualizationWidget,
9
12
  Stack,
10
- TextContent,
11
- injectDataAppStyles,
12
- mountDataApp,
13
- MetricWidget,
14
13
  } from '@altertable/data-app-react';
15
- import { createDataClient } from '@altertable/data-app/client';
16
- import { parseCount, searchFilter } from '@altertable/data-app/contract';
17
- import { formatMetric } from '@altertable/data-app/format';
14
+ import { BarChart, ComposedChart } from '@altertable/data-app-react/ui';
15
+ import {
16
+ parseCount,
17
+ choiceFilter,
18
+ displayChoice,
19
+ } from '@altertable/data-app/contract';
20
+ import { formatDateRange } from '@altertable/data-app/format';
18
21
 
19
- const dataApp = defineDataApp({
20
- title: 'Sample counts',
21
- description: 'Explore sample counts by group.',
22
+ export const dataApp = defineDataApp({
23
+ // Replace the demo title, description, and scope labels with the app's identity.
24
+ title: 'Infrastructure costs',
25
+ description: 'USD · Fictional weekly allocations',
22
26
  scope: { organization: 'demo', environment: 'sample' },
23
27
  appearance: { theme: 'system' },
24
28
  queries: {
25
- sampleCountsByGroup: {
29
+ costBreakdown: {
30
+ projections: {
31
+ member_name: { type: 'string' },
32
+ cost_cents: { type: 'number' },
33
+ },
34
+ statement: `
35
+ WITH infrastructure_costs(month, service_name, customer_name, week_1, week_2, week_3, week_4) AS (
36
+ -- Fictional allocation batches in integer cents.
37
+ -- Replace this CTE with verified billing data.
38
+ VALUES
39
+ ('2026-09', 'Compute', 'Atlas', 84000, 126000, 84000, 126000),
40
+ ('2026-09', 'Compute', 'Beacon', 42000, 63000, 42000, 63000),
41
+ ('2026-09', 'Compute', 'Cedar', 18000, 27000, 18000, 27000),
42
+ ('2026-09', 'Storage', 'Atlas', 24000, 16000, 24000, 16000),
43
+ ('2026-09', 'Storage', 'Beacon', 84000, 56000, 84000, 56000),
44
+ ('2026-09', 'Storage', 'Cedar', 36000, 24000, 36000, 24000),
45
+ ('2026-09', 'Networking', 'Atlas', 3000, 6000, 9000, 12000),
46
+ ('2026-09', 'Networking', 'Beacon', 6000, 12000, 18000, 24000),
47
+ ('2026-09', 'Networking', 'Cedar', 3000, 6000, 9000, 12000),
48
+ ('2026-09', 'Logging', 'Atlas', 0, 0, 0, 0),
49
+ ('2026-09', 'Logging', 'Beacon', 0, 0, 0, 0),
50
+ ('2026-09', 'Logging', 'Cedar', 0, 0, 0, 0),
51
+ ('2026-08', 'Compute', 'Atlas', 76000, 114000, 76000, 114000),
52
+ ('2026-08', 'Compute', 'Beacon', 40000, 60000, 40000, 60000),
53
+ ('2026-08', 'Compute', 'Cedar', 16000, 24000, 16000, 24000),
54
+ ('2026-08', 'Storage', 'Atlas', 24000, 16000, 24000, 16000),
55
+ ('2026-08', 'Storage', 'Beacon', 75000, 50000, 75000, 50000),
56
+ ('2026-08', 'Storage', 'Cedar', 33000, 22000, 33000, 22000),
57
+ ('2026-08', 'Networking', 'Atlas', 2000, 4000, 6000, 8000),
58
+ ('2026-08', 'Networking', 'Beacon', 5000, 10000, 15000, 20000),
59
+ ('2026-08', 'Networking', 'Cedar', 3000, 6000, 9000, 12000),
60
+ ('2026-08', 'Logging', 'Atlas', 0, 0, 0, 0),
61
+ ('2026-08', 'Logging', 'Beacon', 0, 0, 0, 0),
62
+ ('2026-08', 'Logging', 'Cedar', 0, 0, 0, 0)
63
+ ),
64
+ selected_costs AS (
65
+ SELECT service_name, customer_name,
66
+ week_1 + week_2 + week_3 + week_4 AS cost_cents
67
+ FROM infrastructure_costs
68
+ WHERE month = $month AND ($customerName = '' OR customer_name = $customerName)
69
+ )
70
+ SELECT CASE WHEN $breakdown = 'service' THEN service_name ELSE customer_name END AS member_name,
71
+ CAST(SUM(cost_cents) AS BIGINT) AS cost_cents
72
+ FROM selected_costs GROUP BY member_name
73
+ ORDER BY cost_cents DESC, member_name LIMIT $limit`,
74
+ params: {
75
+ month: { type: 'string', defaultValue: '2026-09' },
76
+ customerName: { type: 'string', defaultValue: '' },
77
+ breakdown: { type: 'string', defaultValue: 'service' },
78
+ limit: { type: 'number', defaultValue: 10 },
79
+ },
80
+ },
81
+ totalCost: {
82
+ projections: {
83
+ cost_cents: { type: 'number' },
84
+ customer_count: { type: 'number' },
85
+ },
86
+ statement: `
87
+ WITH infrastructure_costs(month, service_name, customer_name, week_1, week_2, week_3, week_4) AS (
88
+ -- Fictional allocation batches in integer cents.
89
+ -- Replace this CTE with verified billing data.
90
+ VALUES
91
+ ('2026-09', 'Compute', 'Atlas', 84000, 126000, 84000, 126000),
92
+ ('2026-09', 'Compute', 'Beacon', 42000, 63000, 42000, 63000),
93
+ ('2026-09', 'Compute', 'Cedar', 18000, 27000, 18000, 27000),
94
+ ('2026-09', 'Storage', 'Atlas', 24000, 16000, 24000, 16000),
95
+ ('2026-09', 'Storage', 'Beacon', 84000, 56000, 84000, 56000),
96
+ ('2026-09', 'Storage', 'Cedar', 36000, 24000, 36000, 24000),
97
+ ('2026-09', 'Networking', 'Atlas', 3000, 6000, 9000, 12000),
98
+ ('2026-09', 'Networking', 'Beacon', 6000, 12000, 18000, 24000),
99
+ ('2026-09', 'Networking', 'Cedar', 3000, 6000, 9000, 12000),
100
+ ('2026-09', 'Logging', 'Atlas', 0, 0, 0, 0),
101
+ ('2026-09', 'Logging', 'Beacon', 0, 0, 0, 0),
102
+ ('2026-09', 'Logging', 'Cedar', 0, 0, 0, 0),
103
+ ('2026-08', 'Compute', 'Atlas', 76000, 114000, 76000, 114000),
104
+ ('2026-08', 'Compute', 'Beacon', 40000, 60000, 40000, 60000),
105
+ ('2026-08', 'Compute', 'Cedar', 16000, 24000, 16000, 24000),
106
+ ('2026-08', 'Storage', 'Atlas', 24000, 16000, 24000, 16000),
107
+ ('2026-08', 'Storage', 'Beacon', 75000, 50000, 75000, 50000),
108
+ ('2026-08', 'Storage', 'Cedar', 33000, 22000, 33000, 22000),
109
+ ('2026-08', 'Networking', 'Atlas', 2000, 4000, 6000, 8000),
110
+ ('2026-08', 'Networking', 'Beacon', 5000, 10000, 15000, 20000),
111
+ ('2026-08', 'Networking', 'Cedar', 3000, 6000, 9000, 12000),
112
+ ('2026-08', 'Logging', 'Atlas', 0, 0, 0, 0),
113
+ ('2026-08', 'Logging', 'Beacon', 0, 0, 0, 0),
114
+ ('2026-08', 'Logging', 'Cedar', 0, 0, 0, 0)
115
+ ),
116
+ selected_costs AS (
117
+ SELECT service_name, customer_name,
118
+ week_1 + week_2 + week_3 + week_4 AS cost_cents
119
+ FROM infrastructure_costs
120
+ WHERE month = $month AND ($customerName = '' OR customer_name = $customerName)
121
+ )
122
+ SELECT CAST(SUM(cost_cents) AS BIGINT) AS cost_cents,
123
+ COUNT(DISTINCT customer_name) AS customer_count
124
+ FROM selected_costs HAVING COUNT(*) > 0`,
125
+ params: {
126
+ month: { type: 'string', defaultValue: '2026-09' },
127
+ customerName: { type: 'string', defaultValue: '' },
128
+ },
129
+ },
130
+ costsByCustomer: {
131
+ projections: {
132
+ customer_name: { type: 'string' },
133
+ cost_cents: { type: 'number' },
134
+ },
135
+ statement: `
136
+ WITH infrastructure_costs(month, service_name, customer_name, week_1, week_2, week_3, week_4) AS (
137
+ -- Fictional allocation batches in integer cents.
138
+ -- Replace this CTE with verified billing data.
139
+ VALUES
140
+ ('2026-09', 'Compute', 'Atlas', 84000, 126000, 84000, 126000),
141
+ ('2026-09', 'Compute', 'Beacon', 42000, 63000, 42000, 63000),
142
+ ('2026-09', 'Compute', 'Cedar', 18000, 27000, 18000, 27000),
143
+ ('2026-09', 'Storage', 'Atlas', 24000, 16000, 24000, 16000),
144
+ ('2026-09', 'Storage', 'Beacon', 84000, 56000, 84000, 56000),
145
+ ('2026-09', 'Storage', 'Cedar', 36000, 24000, 36000, 24000),
146
+ ('2026-09', 'Networking', 'Atlas', 3000, 6000, 9000, 12000),
147
+ ('2026-09', 'Networking', 'Beacon', 6000, 12000, 18000, 24000),
148
+ ('2026-09', 'Networking', 'Cedar', 3000, 6000, 9000, 12000),
149
+ ('2026-09', 'Logging', 'Atlas', 0, 0, 0, 0),
150
+ ('2026-09', 'Logging', 'Beacon', 0, 0, 0, 0),
151
+ ('2026-09', 'Logging', 'Cedar', 0, 0, 0, 0),
152
+ ('2026-08', 'Compute', 'Atlas', 76000, 114000, 76000, 114000),
153
+ ('2026-08', 'Compute', 'Beacon', 40000, 60000, 40000, 60000),
154
+ ('2026-08', 'Compute', 'Cedar', 16000, 24000, 16000, 24000),
155
+ ('2026-08', 'Storage', 'Atlas', 24000, 16000, 24000, 16000),
156
+ ('2026-08', 'Storage', 'Beacon', 75000, 50000, 75000, 50000),
157
+ ('2026-08', 'Storage', 'Cedar', 33000, 22000, 33000, 22000),
158
+ ('2026-08', 'Networking', 'Atlas', 2000, 4000, 6000, 8000),
159
+ ('2026-08', 'Networking', 'Beacon', 5000, 10000, 15000, 20000),
160
+ ('2026-08', 'Networking', 'Cedar', 3000, 6000, 9000, 12000),
161
+ ('2026-08', 'Logging', 'Atlas', 0, 0, 0, 0),
162
+ ('2026-08', 'Logging', 'Beacon', 0, 0, 0, 0),
163
+ ('2026-08', 'Logging', 'Cedar', 0, 0, 0, 0)
164
+ ),
165
+ selected_costs AS (
166
+ SELECT service_name, customer_name,
167
+ week_1 + week_2 + week_3 + week_4 AS cost_cents
168
+ FROM infrastructure_costs
169
+ WHERE month = $month AND ($customerName = '' OR customer_name = $customerName)
170
+ )
171
+ SELECT customer_name, CAST(SUM(cost_cents) AS BIGINT) AS cost_cents
172
+ FROM selected_costs WHERE service_name = $serviceName
173
+ GROUP BY customer_name ORDER BY cost_cents DESC, customer_name LIMIT $limit`,
174
+ params: {
175
+ month: { type: 'string', defaultValue: '2026-09' },
176
+ customerName: { type: 'string', defaultValue: '' },
177
+ serviceName: { type: 'string' },
178
+ limit: { type: 'number', defaultValue: 10 },
179
+ },
180
+ },
181
+ costTrend: {
26
182
  projections: {
27
- group_name: { type: 'string' },
28
- sample_count: { type: 'number' },
183
+ period_start: { type: 'string' },
184
+ cost_cents: { type: 'number' },
29
185
  },
30
186
  statement: `
31
- WITH sample_counts(group_name, sample_count) AS (VALUES ('Alpha', 2200000), ('Beta', 0))
32
- SELECT group_name, sample_count FROM sample_counts
33
- WHERE $groupName = '' OR group_name = $groupName
34
- ORDER BY group_name LIMIT $limit`,
187
+ WITH infrastructure_costs(month, service_name, customer_name, week_1, week_2, week_3, week_4) AS (
188
+ -- Fictional allocation batches in integer cents.
189
+ -- Replace this CTE with verified billing data.
190
+ VALUES
191
+ ('2026-09', 'Compute', 'Atlas', 84000, 126000, 84000, 126000),
192
+ ('2026-09', 'Compute', 'Beacon', 42000, 63000, 42000, 63000),
193
+ ('2026-09', 'Compute', 'Cedar', 18000, 27000, 18000, 27000),
194
+ ('2026-09', 'Storage', 'Atlas', 24000, 16000, 24000, 16000),
195
+ ('2026-09', 'Storage', 'Beacon', 84000, 56000, 84000, 56000),
196
+ ('2026-09', 'Storage', 'Cedar', 36000, 24000, 36000, 24000),
197
+ ('2026-09', 'Networking', 'Atlas', 3000, 6000, 9000, 12000),
198
+ ('2026-09', 'Networking', 'Beacon', 6000, 12000, 18000, 24000),
199
+ ('2026-09', 'Networking', 'Cedar', 3000, 6000, 9000, 12000),
200
+ ('2026-09', 'Logging', 'Atlas', 0, 0, 0, 0),
201
+ ('2026-09', 'Logging', 'Beacon', 0, 0, 0, 0),
202
+ ('2026-09', 'Logging', 'Cedar', 0, 0, 0, 0),
203
+ ('2026-08', 'Compute', 'Atlas', 76000, 114000, 76000, 114000),
204
+ ('2026-08', 'Compute', 'Beacon', 40000, 60000, 40000, 60000),
205
+ ('2026-08', 'Compute', 'Cedar', 16000, 24000, 16000, 24000),
206
+ ('2026-08', 'Storage', 'Atlas', 24000, 16000, 24000, 16000),
207
+ ('2026-08', 'Storage', 'Beacon', 75000, 50000, 75000, 50000),
208
+ ('2026-08', 'Storage', 'Cedar', 33000, 22000, 33000, 22000),
209
+ ('2026-08', 'Networking', 'Atlas', 2000, 4000, 6000, 8000),
210
+ ('2026-08', 'Networking', 'Beacon', 5000, 10000, 15000, 20000),
211
+ ('2026-08', 'Networking', 'Cedar', 3000, 6000, 9000, 12000),
212
+ ('2026-08', 'Logging', 'Atlas', 0, 0, 0, 0),
213
+ ('2026-08', 'Logging', 'Beacon', 0, 0, 0, 0),
214
+ ('2026-08', 'Logging', 'Cedar', 0, 0, 0, 0)
215
+ ),
216
+ -- The four fictional batch dates match the week_1 through week_4 columns.
217
+ weeks(day) AS (VALUES ('01'), ('08'), ('15'), ('22'))
218
+ SELECT $month || '-' || day AS period_start,
219
+ CAST(SUM(CASE day WHEN '01' THEN week_1 WHEN '08' THEN week_2
220
+ WHEN '15' THEN week_3 ELSE week_4 END) AS BIGINT) AS cost_cents
221
+ FROM infrastructure_costs CROSS JOIN weeks
222
+ WHERE month = $month AND ($customerName = '' OR customer_name = $customerName)
223
+ GROUP BY day ORDER BY day LIMIT $limit`,
35
224
  params: {
36
- groupName: { type: 'string', defaultValue: '' },
225
+ month: { type: 'string', defaultValue: '2026-09' },
226
+ customerName: { type: 'string', defaultValue: '' },
37
227
  limit: { type: 'number', defaultValue: 10 },
38
228
  },
39
229
  },
40
- sampleTotal: {
41
- projections: { sample_count: { type: 'number' } },
230
+ customerServiceMix: {
231
+ projections: {
232
+ customer_name: { type: 'string' },
233
+ service_name: { type: 'string' },
234
+ cost_cents: { type: 'number' },
235
+ },
42
236
  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: '' } },
237
+ WITH infrastructure_costs(month, service_name, customer_name, week_1, week_2, week_3, week_4) AS (
238
+ -- Fictional allocation batches in integer cents.
239
+ -- Replace this CTE with verified billing data.
240
+ VALUES
241
+ ('2026-09', 'Compute', 'Atlas', 84000, 126000, 84000, 126000),
242
+ ('2026-09', 'Compute', 'Beacon', 42000, 63000, 42000, 63000),
243
+ ('2026-09', 'Compute', 'Cedar', 18000, 27000, 18000, 27000),
244
+ ('2026-09', 'Storage', 'Atlas', 24000, 16000, 24000, 16000),
245
+ ('2026-09', 'Storage', 'Beacon', 84000, 56000, 84000, 56000),
246
+ ('2026-09', 'Storage', 'Cedar', 36000, 24000, 36000, 24000),
247
+ ('2026-09', 'Networking', 'Atlas', 3000, 6000, 9000, 12000),
248
+ ('2026-09', 'Networking', 'Beacon', 6000, 12000, 18000, 24000),
249
+ ('2026-09', 'Networking', 'Cedar', 3000, 6000, 9000, 12000),
250
+ ('2026-09', 'Logging', 'Atlas', 0, 0, 0, 0),
251
+ ('2026-09', 'Logging', 'Beacon', 0, 0, 0, 0),
252
+ ('2026-09', 'Logging', 'Cedar', 0, 0, 0, 0),
253
+ ('2026-08', 'Compute', 'Atlas', 76000, 114000, 76000, 114000),
254
+ ('2026-08', 'Compute', 'Beacon', 40000, 60000, 40000, 60000),
255
+ ('2026-08', 'Compute', 'Cedar', 16000, 24000, 16000, 24000),
256
+ ('2026-08', 'Storage', 'Atlas', 24000, 16000, 24000, 16000),
257
+ ('2026-08', 'Storage', 'Beacon', 75000, 50000, 75000, 50000),
258
+ ('2026-08', 'Storage', 'Cedar', 33000, 22000, 33000, 22000),
259
+ ('2026-08', 'Networking', 'Atlas', 2000, 4000, 6000, 8000),
260
+ ('2026-08', 'Networking', 'Beacon', 5000, 10000, 15000, 20000),
261
+ ('2026-08', 'Networking', 'Cedar', 3000, 6000, 9000, 12000),
262
+ ('2026-08', 'Logging', 'Atlas', 0, 0, 0, 0),
263
+ ('2026-08', 'Logging', 'Beacon', 0, 0, 0, 0),
264
+ ('2026-08', 'Logging', 'Cedar', 0, 0, 0, 0)
265
+ ),
266
+ selected_costs AS (
267
+ SELECT service_name, customer_name,
268
+ week_1 + week_2 + week_3 + week_4 AS cost_cents
269
+ FROM infrastructure_costs
270
+ WHERE month = $month AND ($customerName = '' OR customer_name = $customerName)
271
+ )
272
+ SELECT customer_name, service_name, CAST(SUM(cost_cents) AS BIGINT) AS cost_cents
273
+ FROM selected_costs GROUP BY customer_name, service_name
274
+ ORDER BY customer_name, service_name LIMIT $limit`,
275
+ params: {
276
+ month: { type: 'string', defaultValue: '2026-09' },
277
+ customerName: { type: 'string', defaultValue: '' },
278
+ limit: { type: 'number', defaultValue: 20 },
279
+ },
47
280
  },
48
281
  },
49
282
  });
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' });
53
- const operations = {
54
- sampleCountsByGroup: dataApp.defineOperation({
55
- filters: { groupName },
283
+
284
+ // These months match the fictional SQL rows.
285
+ // Replace them with months covered by the billing source.
286
+ const month = choiceFilter({
287
+ key: 'month',
288
+ label: 'Month',
289
+ defaultValue: '2026-09',
290
+ options: [
291
+ { id: '2026-09', label: 'September 2026' },
292
+ { id: '2026-08', label: 'August 2026' },
293
+ ],
294
+ });
295
+ // These customers match the fictional SQL rows.
296
+ // Replace them with customers from the billing source.
297
+ const customer = choiceFilter({
298
+ key: 'customer',
299
+ label: 'Customer',
300
+ defaultValue: 'all',
301
+ options: [
302
+ { id: 'all', label: 'All customers' },
303
+ { id: 'Atlas', label: 'Atlas' },
304
+ { id: 'Beacon', label: 'Beacon' },
305
+ { id: 'Cedar', label: 'Cedar' },
306
+ ],
307
+ });
308
+ const breakdown = displayChoice({
309
+ key: 'breakdown',
310
+ label: 'Breakdown',
311
+ defaultValue: 'service',
312
+ options: [
313
+ { id: 'service', label: 'Service' },
314
+ { id: 'customer', label: 'Customer' },
315
+ ],
316
+ });
317
+
318
+ function parseService(value: unknown) {
319
+ if (
320
+ !value ||
321
+ typeof value !== 'object' ||
322
+ Array.isArray(value) ||
323
+ !('serviceName' in value) ||
324
+ typeof value.serviceName !== 'string' ||
325
+ value.serviceName.trim().length === 0 ||
326
+ value.serviceName.length > 100
327
+ )
328
+ throw new Error('Choose a service to inspect its customer costs.');
329
+ return { serviceName: value.serviceName };
330
+ }
331
+ function queryScope(input: { month: string; customer: string }) {
332
+ return {
333
+ month: input.month,
334
+ customerName: input.customer === 'all' ? '' : input.customer,
335
+ };
336
+ }
337
+ export const operations = dataApp.defineOperations({
338
+ costBreakdown: dataApp.defineOperation({
339
+ filters: { month, customer, breakdown },
56
340
  policy: { maxQueryRows: 10, maxDurationMs: 15000 },
57
- async run({ query }, { groupName }) {
58
- const queryResult = await query('sampleCountsByGroup', { groupName });
59
- return queryResult.rows.map(row => ({
60
- groupName: row.group_name,
61
- sampleCount: parseCount(row.sample_count),
341
+ async run({ query }, input) {
342
+ const result = await query('costBreakdown', {
343
+ ...queryScope(input),
344
+ breakdown: input.breakdown,
345
+ });
346
+ return result.rows.map(row => ({
347
+ memberName: row.member_name,
348
+ dimension: input.breakdown,
349
+ cost: parseCount(row.cost_cents) / 100,
62
350
  }));
63
351
  },
64
352
  }),
65
- sampleTotal: dataApp.defineOperation({
66
- filters: { groupName },
353
+ totalCost: dataApp.defineOperation({
354
+ filters: { month, customer },
67
355
  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) };
356
+ async run({ query }, input) {
357
+ const result = await query('totalCost', queryScope(input));
358
+ const row = result.rows[0];
359
+ if (!row) return null;
360
+ const cost = parseCount(row.cost_cents) / 100;
361
+ const customerCount = parseCount(row.customer_count);
362
+ if (customerCount === 0)
363
+ throw new Error('Allocations must identify at least one customer.');
364
+ return {
365
+ cost,
366
+ customerCount,
367
+ averageCost: cost / customerCount,
368
+ };
71
369
  },
72
370
  }),
73
- };
371
+ costsByCustomer: dataApp.defineOperation({
372
+ filters: { month, customer },
373
+ input: parseService,
374
+ checks: [{ serviceName: 'Compute' }],
375
+ policy: { maxQueryRows: 10, maxDurationMs: 15000 },
376
+ async run({ query }, input) {
377
+ const result = await query('costsByCustomer', {
378
+ ...queryScope(input),
379
+ serviceName: input.serviceName,
380
+ });
381
+ return result.rows.map(row => ({
382
+ customerName: row.customer_name,
383
+ cost: parseCount(row.cost_cents) / 100,
384
+ }));
385
+ },
386
+ }),
387
+ costTrend: dataApp.defineOperation({
388
+ filters: { month, customer },
389
+ policy: { maxQueryRows: 10, maxDurationMs: 15000 },
390
+ async run({ query }, input) {
391
+ const result = await query('costTrend', queryScope(input));
392
+ return result.rows.map(row => ({
393
+ periodStart: row.period_start,
394
+ cost: parseCount(row.cost_cents) / 100,
395
+ }));
396
+ },
397
+ }),
398
+ customerServiceMix: dataApp.defineOperation({
399
+ filters: { month, customer },
400
+ policy: { maxQueryRows: 20, maxDurationMs: 15000 },
401
+ async run({ query }, input) {
402
+ const result = await query('customerServiceMix', queryScope(input));
403
+ // These service names match the fictional SQL rows and chart series.
404
+ // Update the pivot and chart series together when changing the services.
405
+ const customers = new Map<
406
+ string,
407
+ {
408
+ customerName: string;
409
+ Compute: number;
410
+ Storage: number;
411
+ Networking: number;
412
+ Logging: number;
413
+ total: number;
414
+ }
415
+ >();
416
+ for (const row of result.rows) {
417
+ const service = row.service_name;
418
+ if (
419
+ service !== 'Compute' &&
420
+ service !== 'Storage' &&
421
+ service !== 'Networking' &&
422
+ service !== 'Logging'
423
+ )
424
+ throw new Error('Unexpected fixture service.');
425
+ const entry = customers.get(row.customer_name) ?? {
426
+ customerName: row.customer_name,
427
+ Compute: 0,
428
+ Storage: 0,
429
+ Networking: 0,
430
+ Logging: 0,
431
+ total: 0,
432
+ };
433
+ const cost = parseCount(row.cost_cents) / 100;
434
+ entry[service] += cost;
435
+ entry.total += cost;
436
+ customers.set(row.customer_name, entry);
437
+ }
438
+ return [...customers.values()].sort(
439
+ (a, b) =>
440
+ b.total - a.total || a.customerName.localeCompare(b.customerName)
441
+ );
442
+ },
443
+ }),
444
+ });
445
+
74
446
  const queryNames = {
75
- ...operations.sampleCountsByGroup.queryNames,
76
- ...operations.sampleTotal.queryNames,
447
+ ...operations.costBreakdown.queryNames,
448
+ ...operations.totalCost.queryNames,
449
+ ...operations.costsByCustomer.queryNames,
450
+ ...operations.costTrend.queryNames,
451
+ ...operations.customerServiceMix.queryNames,
77
452
  };
78
- const sampleDataContext = createDataContext(queryNames)({
453
+ // Replace these example definitions and coverage with verified source metadata.
454
+ const costDataContext = createDataContext(queryNames)({
79
455
  description:
80
- 'Two SQL VALUES rows demonstrate the host query path. Replace them with inspected source data before publishing findings.',
456
+ 'Fictional weekly customer allocations for August and September 2026. Scope filters apply to every chart and its contributing records.',
81
457
  glossary: {
82
- sampleCount: {
83
- term: 'Sample count',
458
+ allocatedCost: {
459
+ term: 'Allocated cost',
460
+ definition:
461
+ 'USD charges in the selected billing month and customer scope. Four recorded allocation batches, dated the 1st, 8th, 15th and 22nd, sum to each monthly allocation. Customers sum to services; services sum to the total. Zero is a recorded charge of zero.',
462
+ queryNames: Object.values(queryNames),
463
+ },
464
+ customerCount: {
465
+ term: 'Customers',
84
466
  definition:
85
- 'A fixture value: Alpha is 2,200,000 and Beta is a measured zero.',
86
- queryNames: [queryNames.sampleCountsByGroup, queryNames.sampleTotal],
467
+ 'Distinct customers with recorded allocations in the selected scope, including recorded zero charges.',
468
+ queryNames: [queryNames.totalCost],
469
+ },
470
+ averageCustomerCost: {
471
+ term: 'Average per customer',
472
+ definition:
473
+ 'Total allocated cost divided by the number of distinct customers in the same selected scope, including customers with zero charges.',
474
+ queryNames: [queryNames.totalCost],
87
475
  },
88
476
  },
89
477
  });
90
- const { defineView } = createDataHooks(createDataClient({ operations }));
91
- const sampleCountsView = defineView({
92
- dataContext: sampleDataContext,
93
- operation: 'sampleCountsByGroup',
94
- isEmpty: sampleCounts => sampleCounts.length === 0,
95
- emptyFallback: {
96
- title: 'No matching groups',
97
- description: 'Try Alpha, Beta, or clear the group filter.',
98
- },
478
+
479
+ // Fictional charges use USD. Match this format to the real billing currency.
480
+ const costFormat = { kind: 'currency', currency: 'USD' } as const;
481
+ const { defineView, defineDetailView } = createDataHooks(
482
+ operations.createDataClient()
483
+ );
484
+ const emptyFallback = {
485
+ title: 'No allocations',
486
+ description: 'Choose another month or customer.',
487
+ };
488
+ const servicesView = defineView({
489
+ dataContext: costDataContext,
490
+ operation: 'costBreakdown',
491
+ isEmpty: rows => rows.length === 0,
492
+ emptyFallback,
99
493
  });
100
- const sampleCounts = sampleCountsView.dataset({
101
- name: 'Sample counts',
494
+ const serviceCosts = servicesView.dataset({
495
+ name: 'Cost breakdown',
102
496
  select: rows => rows,
103
- rowKey: row => row.groupName,
497
+ rowKey: row => row.memberName,
104
498
  columns: {
105
- groupName: { label: 'Group', value: row => row.groupName },
106
- sampleCount: { value: row => row.sampleCount, format: { kind: 'count' } },
107
- },
108
- evidence: {
109
- id: 'counts-by-group',
110
- glossaryIds: ['sampleCount'],
499
+ memberName: { label: 'Member', value: row => row.memberName },
500
+ cost: { label: 'Cost', value: row => row.cost, format: costFormat },
111
501
  },
502
+ evidence: { id: 'cost-breakdown', glossaryIds: ['allocatedCost'] },
112
503
  });
113
- const sampleTotalView = defineView({
114
- dataContext: sampleDataContext,
115
- operation: 'sampleTotal',
504
+ const totalView = defineView({
505
+ dataContext: costDataContext,
506
+ operation: 'totalCost',
507
+ isEmpty: total => total === null,
508
+ emptyFallback: { title: 'No total available' },
116
509
  });
117
- const totalSamples = sampleTotalView.metric(
510
+ const totalCost = totalView.metric(
118
511
  {
119
- id: 'total-samples',
120
- glossaryId: 'sampleCount',
121
- label: 'Total samples',
122
- format: { kind: 'count', compact: true },
512
+ id: 'total-cost',
513
+ glossaryId: 'allocatedCost',
514
+ label: 'Total cost',
515
+ format: costFormat,
123
516
  },
124
- total => ({ current: total.sampleCount })
517
+ total => {
518
+ if (total === null)
519
+ throw new Error('No allocated total for this selection.');
520
+ return { current: total.cost };
521
+ }
125
522
  );
126
- const totalContent = sampleTotalView.content(result => (
127
- <Stack>
128
- <TextContent>
129
- <p>
130
- Showing <DataValue scope={result.scope} />
131
- </p>
132
- </TextContent>
133
- <MetricWidget
134
- metric={totalSamples}
135
- source={result}
136
- description="Sum of the fixture counts in the selected groups."
137
- />
138
- </Stack>
523
+ const customerCount = totalView.metric(
524
+ {
525
+ id: 'customer-count',
526
+ glossaryId: 'customerCount',
527
+ label: 'Customers',
528
+ format: { kind: 'count' },
529
+ },
530
+ total => {
531
+ if (total === null)
532
+ throw new Error('No customer count for this selection.');
533
+ return { current: total.customerCount };
534
+ }
535
+ );
536
+ const averageCost = totalView.metric(
537
+ {
538
+ id: 'average-cost',
539
+ glossaryId: 'averageCustomerCost',
540
+ label: 'Average per customer',
541
+ format: costFormat,
542
+ },
543
+ total => {
544
+ if (total === null) throw new Error('No average cost for this selection.');
545
+ return { current: total.averageCost };
546
+ }
547
+ );
548
+ const totalContent = totalView.content(result => (
549
+ <Grid columns={3}>
550
+ <MetricWidget metric={totalCost} source={result} />
551
+ <MetricWidget metric={customerCount} source={result} />
552
+ <MetricWidget metric={averageCost} source={result} />
553
+ </Grid>
139
554
  ));
140
- const countsContent = sampleCountsView.content(result => (
555
+ const customersView = defineDetailView({
556
+ id: 'service-customers',
557
+ title: 'Customer costs',
558
+ operation: 'costsByCustomer',
559
+ dataContext: costDataContext,
560
+ parseInput: operations.costsByCustomer.input,
561
+ describeInput: input =>
562
+ `${input.serviceName} · ${month.describe?.(input.month)} · USD`,
563
+ isEmpty: rows => rows.length === 0,
564
+ emptyFallback: {
565
+ title: 'No customer allocations',
566
+ description:
567
+ 'No allocations are recorded for this service in the selected month and customer scope.',
568
+ },
569
+ });
570
+ const customerCosts = customersView.dataset({
571
+ name: 'Customer allocations',
572
+ select: rows => rows,
573
+ rowKey: row => row.customerName,
574
+ columns: {
575
+ customerName: { label: 'Customer', value: row => row.customerName },
576
+ cost: { label: 'Cost', value: row => row.cost, format: costFormat },
577
+ },
578
+ evidence: { id: 'costs-by-customer', glossaryIds: ['allocatedCost'] },
579
+ });
580
+ const customersDetail = customersView.content(result => (
141
581
  <TableWidget
142
- dataset={sampleCounts}
582
+ dataset={customerCosts}
143
583
  source={result}
144
- title="Counts by group"
145
- description="Alpha and Beta demonstrate measured values, including zero."
584
+ title="Cost by customer"
146
585
  pagination={false}
147
586
  />
148
587
  ));
149
- function App() {
588
+ const serviceActions = serviceCosts.actions(
589
+ action => ({
590
+ customers: action.detail({
591
+ label: 'View customers',
592
+ target: customersView,
593
+ when: row => row.dimension === 'service',
594
+ input: row => ({ serviceName: row.memberName }),
595
+ }),
596
+ }),
597
+ { primary: 'customers' }
598
+ );
599
+ const breakdownContent = servicesView.content(result => {
600
+ const byService = result.loading || result.input.breakdown === 'service';
150
601
  return (
151
- <DataApp
152
- view={sampleCountsView}
153
- summaryViews={[sampleTotalView]}
154
- datasets={[sampleCounts]}
155
- story={(countsSnapshot, totalSnapshot) => {
156
- const total = totalSamples.read(totalSnapshot);
157
- const scope = sampleCountsView.scope(countsSnapshot);
158
- return [
159
- {
160
- id: 'total-samples',
161
- headline: `Total samples: ${formatMetric(total.value.current, totalSamples.definition.format)}`,
162
- context: `Demonstration values for ${scope}.`,
163
- visual: (
164
- <MetricWidget metric={totalSamples} source={totalSnapshot} />
165
- ),
166
- visualKind: 'metric',
167
- evidence: totalSamples,
168
- },
169
- {
170
- id: 'counts-by-group',
171
- headline: 'Counts by group',
172
- context: `Demonstration values for ${scope}.`,
173
- visual: (
174
- <TableWidget
175
- dataset={sampleCounts}
176
- source={countsSnapshot}
177
- pagination={false}
178
- />
179
- ),
180
- evidence: sampleCounts,
181
- },
182
- ];
183
- }}
602
+ <VisualizationWidget
603
+ dataset={serviceCosts}
604
+ source={result}
605
+ title={byService ? 'Cost by service' : 'Cost by customer'}
606
+ actions={serviceActions}
607
+ breakdown={servicesView.breakdown('breakdown')}
184
608
  >
609
+ {(rows, actions) => (
610
+ <BarChart
611
+ ariaLabel={byService ? 'Costs by service' : 'Costs by customer'}
612
+ unit="USD"
613
+ showYAxis
614
+ scrollToEnd={false}
615
+ valueFormat={costFormat}
616
+ items={rows.map(row => ({
617
+ ...row,
618
+ id: row.memberName,
619
+ label: row.memberName,
620
+ value: row.cost,
621
+ }))}
622
+ itemActions={actions}
623
+ />
624
+ )}
625
+ </VisualizationWidget>
626
+ );
627
+ });
628
+ const trendView = defineView({
629
+ dataContext: costDataContext,
630
+ operation: 'costTrend',
631
+ isEmpty: rows => rows.length === 0,
632
+ emptyFallback,
633
+ });
634
+ const weeklyCosts = trendView.dataset({
635
+ name: 'Weekly allocations',
636
+ select: rows => rows,
637
+ rowKey: row => row.periodStart,
638
+ columns: {
639
+ periodStart: { label: 'Allocation date', value: row => row.periodStart },
640
+ cost: { label: 'Cost', value: row => row.cost, format: costFormat },
641
+ },
642
+ evidence: { id: 'cost-trend', glossaryIds: ['allocatedCost'] },
643
+ });
644
+ const trendContent = trendView.content(result => (
645
+ <VisualizationWidget
646
+ dataset={weeklyCosts}
647
+ source={result}
648
+ title="Weekly allocated cost"
649
+ >
650
+ {rows => (
651
+ <ComposedChart
652
+ data={rows}
653
+ ariaLabel="Costs over time"
654
+ valueFormat={costFormat}
655
+ >
656
+ <ComposedChart.XAxis
657
+ dataKey="periodStart"
658
+ tickFormatter={value =>
659
+ formatDateRange({ start: String(value), end: String(value) })
660
+ }
661
+ />
662
+ <ComposedChart.YAxis domain={[0, 'auto']} />
663
+ <ComposedChart.Tooltip />
664
+ <ComposedChart.Line dataKey="cost" name="Allocated cost" />
665
+ </ComposedChart>
666
+ )}
667
+ </VisualizationWidget>
668
+ ));
669
+ const mixView = defineView({
670
+ dataContext: costDataContext,
671
+ operation: 'customerServiceMix',
672
+ isEmpty: rows => rows.length === 0,
673
+ emptyFallback,
674
+ });
675
+ const customerMix = mixView.dataset({
676
+ name: 'Customer service mix',
677
+ select: rows => rows,
678
+ rowKey: row => row.customerName,
679
+ columns: {
680
+ customerName: { label: 'Customer', value: row => row.customerName },
681
+ Compute: { value: row => row.Compute, format: costFormat },
682
+ Storage: { value: row => row.Storage, format: costFormat },
683
+ Networking: { value: row => row.Networking, format: costFormat },
684
+ Logging: { value: row => row.Logging, format: costFormat },
685
+ },
686
+ evidence: { id: 'customer-service-mix', glossaryIds: ['allocatedCost'] },
687
+ });
688
+ const mixContent = mixView.content(result => (
689
+ <VisualizationWidget
690
+ dataset={customerMix}
691
+ source={result}
692
+ title="Service mix by customer"
693
+ >
694
+ {rows => (
695
+ <ComposedChart
696
+ data={rows}
697
+ ariaLabel="Service mix by customer"
698
+ valueFormat={costFormat}
699
+ >
700
+ <ComposedChart.XAxis dataKey="customerName" />
701
+ <ComposedChart.YAxis domain={[0, 'auto']} />
702
+ <ComposedChart.Tooltip />
703
+ <ComposedChart.Legend />
704
+ {/* These service keys must match the columns returned by customerServiceMix. */}
705
+ <ComposedChart.Bar dataKey="Compute" name="Compute" stackId="cost" />
706
+ <ComposedChart.Bar
707
+ dataKey="Storage"
708
+ name="Storage"
709
+ stackId="cost"
710
+ fill="var(--atbl-chart-2)"
711
+ />
712
+ <ComposedChart.Bar
713
+ dataKey="Networking"
714
+ name="Networking"
715
+ stackId="cost"
716
+ fill="var(--atbl-chart-3)"
717
+ />
718
+ <ComposedChart.Bar
719
+ dataKey="Logging"
720
+ name="Logging"
721
+ stackId="cost"
722
+ fill="var(--atbl-chart-4)"
723
+ />
724
+ </ComposedChart>
725
+ )}
726
+ </VisualizationWidget>
727
+ ));
728
+ function App() {
729
+ return (
730
+ <DataApp view={servicesView} details={[customersDetail]}>
185
731
  <Stack>
186
- <TextContent>
187
- <h2>Sample counts</h2>
188
- </TextContent>
189
- <DataSection content={totalContent} label="Sample total" />
190
- <DataSection content={countsContent} label="Group counts" />
732
+ <DataSection content={totalContent} label="Cost summary" />
733
+ <Grid columns={2}>
734
+ <DataSection content={trendContent} label="Cost trend" />
735
+ <DataSection content={breakdownContent} label="Cost overview" />
736
+ </Grid>
737
+ <DataSection content={mixContent} label="Customer composition" />
191
738
  </Stack>
192
739
  </DataApp>
193
740
  );
194
741
  }
195
- injectDataAppStyles();
742
+
196
743
  mountDataApp({ app: dataApp, component: App });