@contractspec/example.analytics-dashboard 1.57.0 → 1.58.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/dist/browser/dashboard/dashboard.enum.js +34 -0
- package/dist/browser/dashboard/dashboard.operation.js +289 -0
- package/dist/browser/dashboard/dashboard.presentation.js +197 -0
- package/dist/browser/dashboard/dashboard.schema.js +126 -0
- package/dist/browser/dashboard/dashboard.test-spec.js +213 -0
- package/dist/browser/dashboard/index.js +299 -0
- package/dist/browser/dashboard.feature.js +84 -0
- package/dist/browser/datasource/posthog-datasource.js +289 -0
- package/dist/browser/docs/analytics-dashboard.docblock.js +103 -0
- package/dist/browser/docs/index.js +103 -0
- package/dist/browser/events.js +81 -0
- package/dist/browser/example.js +42 -0
- package/dist/browser/handlers/analytics.handlers.js +278 -0
- package/dist/browser/handlers/index.js +571 -0
- package/dist/browser/handlers/query.handlers.js +294 -0
- package/dist/browser/index.js +1677 -0
- package/dist/browser/query/index.js +159 -0
- package/dist/browser/query/query.enum.js +11 -0
- package/dist/browser/query/query.operation.js +154 -0
- package/dist/browser/query/query.presentation.js +119 -0
- package/dist/browser/query/query.schema.js +70 -0
- package/dist/browser/query/query.test-spec.js +113 -0
- package/dist/browser/query-engine/index.js +491 -0
- package/dist/browser/seeders/index.js +20 -0
- package/dist/browser/ui/AnalyticsDashboard.js +394 -0
- package/dist/browser/ui/hooks/index.js +69 -0
- package/dist/browser/ui/hooks/useAnalyticsData.js +66 -0
- package/dist/browser/ui/index.js +671 -0
- package/dist/browser/ui/renderers/analytics.markdown.js +275 -0
- package/dist/browser/ui/renderers/index.js +275 -0
- package/dist/dashboard/dashboard.enum.d.ts +3 -8
- package/dist/dashboard/dashboard.enum.d.ts.map +1 -1
- package/dist/dashboard/dashboard.enum.js +31 -39
- package/dist/dashboard/dashboard.operation.d.ts +444 -450
- package/dist/dashboard/dashboard.operation.d.ts.map +1 -1
- package/dist/dashboard/dashboard.operation.js +284 -207
- package/dist/dashboard/dashboard.presentation.d.ts +3 -8
- package/dist/dashboard/dashboard.presentation.d.ts.map +1 -1
- package/dist/dashboard/dashboard.presentation.js +193 -85
- package/dist/dashboard/dashboard.schema.d.ts +289 -294
- package/dist/dashboard/dashboard.schema.d.ts.map +1 -1
- package/dist/dashboard/dashboard.schema.js +119 -228
- package/dist/dashboard/dashboard.test-spec.d.ts +4 -9
- package/dist/dashboard/dashboard.test-spec.d.ts.map +1 -1
- package/dist/dashboard/dashboard.test-spec.js +209 -228
- package/dist/dashboard/index.d.ts +7 -4
- package/dist/dashboard/index.d.ts.map +1 -0
- package/dist/dashboard/index.js +299 -4
- package/dist/dashboard.feature.d.ts +1 -6
- package/dist/dashboard.feature.d.ts.map +1 -1
- package/dist/dashboard.feature.js +83 -175
- package/dist/datasource/posthog-datasource.d.ts +15 -19
- package/dist/datasource/posthog-datasource.d.ts.map +1 -1
- package/dist/datasource/posthog-datasource.js +274 -238
- package/dist/docs/analytics-dashboard.docblock.d.ts +2 -1
- package/dist/docs/analytics-dashboard.docblock.d.ts.map +1 -0
- package/dist/docs/analytics-dashboard.docblock.js +45 -56
- package/dist/docs/index.d.ts +2 -1
- package/dist/docs/index.d.ts.map +1 -0
- package/dist/docs/index.js +104 -1
- package/dist/events.d.ts +109 -115
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +74 -120
- package/dist/example.d.ts +2 -6
- package/dist/example.d.ts.map +1 -1
- package/dist/example.js +41 -55
- package/dist/handlers/analytics.handlers.d.ts +110 -109
- package/dist/handlers/analytics.handlers.d.ts.map +1 -1
- package/dist/handlers/analytics.handlers.js +267 -298
- package/dist/handlers/index.d.ts +3 -3
- package/dist/handlers/index.d.ts.map +1 -0
- package/dist/handlers/index.js +571 -3
- package/dist/handlers/query.handlers.d.ts +7 -11
- package/dist/handlers/query.handlers.d.ts.map +1 -1
- package/dist/handlers/query.handlers.js +292 -7
- package/dist/index.d.ts +12 -13
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1678 -14
- package/dist/node/dashboard/dashboard.enum.js +34 -0
- package/dist/node/dashboard/dashboard.operation.js +289 -0
- package/dist/node/dashboard/dashboard.presentation.js +197 -0
- package/dist/node/dashboard/dashboard.schema.js +126 -0
- package/dist/node/dashboard/dashboard.test-spec.js +213 -0
- package/dist/node/dashboard/index.js +299 -0
- package/dist/node/dashboard.feature.js +84 -0
- package/dist/node/datasource/posthog-datasource.js +289 -0
- package/dist/node/docs/analytics-dashboard.docblock.js +103 -0
- package/dist/node/docs/index.js +103 -0
- package/dist/node/events.js +81 -0
- package/dist/node/example.js +42 -0
- package/dist/node/handlers/analytics.handlers.js +278 -0
- package/dist/node/handlers/index.js +571 -0
- package/dist/node/handlers/query.handlers.js +294 -0
- package/dist/node/index.js +1677 -0
- package/dist/node/query/index.js +159 -0
- package/dist/node/query/query.enum.js +11 -0
- package/dist/node/query/query.operation.js +154 -0
- package/dist/node/query/query.presentation.js +119 -0
- package/dist/node/query/query.schema.js +70 -0
- package/dist/node/query/query.test-spec.js +113 -0
- package/dist/node/query-engine/index.js +491 -0
- package/dist/node/seeders/index.js +20 -0
- package/dist/node/ui/AnalyticsDashboard.js +394 -0
- package/dist/node/ui/hooks/index.js +69 -0
- package/dist/node/ui/hooks/useAnalyticsData.js +66 -0
- package/dist/node/ui/index.js +671 -0
- package/dist/node/ui/renderers/analytics.markdown.js +275 -0
- package/dist/node/ui/renderers/index.js +275 -0
- package/dist/query/index.d.ts +7 -4
- package/dist/query/index.d.ts.map +1 -0
- package/dist/query/index.js +159 -4
- package/dist/query/query.enum.d.ts +1 -6
- package/dist/query/query.enum.d.ts.map +1 -1
- package/dist/query/query.enum.js +10 -14
- package/dist/query/query.operation.d.ts +148 -154
- package/dist/query/query.operation.d.ts.map +1 -1
- package/dist/query/query.operation.js +151 -109
- package/dist/query/query.presentation.d.ts +2 -7
- package/dist/query/query.presentation.d.ts.map +1 -1
- package/dist/query/query.presentation.js +116 -56
- package/dist/query/query.schema.d.ts +121 -126
- package/dist/query/query.schema.d.ts.map +1 -1
- package/dist/query/query.schema.js +66 -152
- package/dist/query/query.test-spec.d.ts +2 -7
- package/dist/query/query.test-spec.d.ts.map +1 -1
- package/dist/query/query.test-spec.js +111 -121
- package/dist/query-engine/index.d.ts +84 -88
- package/dist/query-engine/index.d.ts.map +1 -1
- package/dist/query-engine/index.js +489 -188
- package/dist/seeders/index.d.ts +4 -8
- package/dist/seeders/index.d.ts.map +1 -1
- package/dist/seeders/index.js +18 -16
- package/dist/ui/AnalyticsDashboard.d.ts +1 -6
- package/dist/ui/AnalyticsDashboard.d.ts.map +1 -1
- package/dist/ui/AnalyticsDashboard.js +389 -259
- package/dist/ui/hooks/index.d.ts +2 -2
- package/dist/ui/hooks/index.d.ts.map +1 -0
- package/dist/ui/hooks/index.js +69 -4
- package/dist/ui/hooks/useAnalyticsData.d.ts +16 -20
- package/dist/ui/hooks/useAnalyticsData.d.ts.map +1 -1
- package/dist/ui/hooks/useAnalyticsData.js +63 -69
- package/dist/ui/index.d.ts +7 -6
- package/dist/ui/index.d.ts.map +1 -0
- package/dist/ui/index.js +671 -5
- package/dist/ui/renderers/analytics.markdown.d.ts +13 -14
- package/dist/ui/renderers/analytics.markdown.d.ts.map +1 -1
- package/dist/ui/renderers/analytics.markdown.js +266 -254
- package/dist/ui/renderers/index.d.ts +2 -2
- package/dist/ui/renderers/index.d.ts.map +1 -0
- package/dist/ui/renderers/index.js +275 -2
- package/package.json +328 -67
- package/dist/dashboard/dashboard.enum.js.map +0 -1
- package/dist/dashboard/dashboard.operation.js.map +0 -1
- package/dist/dashboard/dashboard.presentation.js.map +0 -1
- package/dist/dashboard/dashboard.schema.js.map +0 -1
- package/dist/dashboard/dashboard.test-spec.js.map +0 -1
- package/dist/dashboard.feature.js.map +0 -1
- package/dist/datasource/posthog-datasource.js.map +0 -1
- package/dist/docs/analytics-dashboard.docblock.js.map +0 -1
- package/dist/events.js.map +0 -1
- package/dist/example.js.map +0 -1
- package/dist/handlers/analytics.handlers.js.map +0 -1
- package/dist/handlers/query.handlers.js.map +0 -1
- package/dist/query/query.enum.js.map +0 -1
- package/dist/query/query.operation.js.map +0 -1
- package/dist/query/query.presentation.js.map +0 -1
- package/dist/query/query.schema.js.map +0 -1
- package/dist/query/query.test-spec.js.map +0 -1
- package/dist/query-engine/index.js.map +0 -1
- package/dist/seeders/index.js.map +0 -1
- package/dist/ui/AnalyticsDashboard.js.map +0 -1
- package/dist/ui/hooks/useAnalyticsData.js.map +0 -1
- package/dist/ui/renderers/analytics.markdown.js.map +0 -1
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
// src/datasource/posthog-datasource.ts
|
|
2
|
+
class PosthogQueryEngine {
|
|
3
|
+
reader;
|
|
4
|
+
defaultLimit;
|
|
5
|
+
constructor(reader, options = {}) {
|
|
6
|
+
this.reader = reader;
|
|
7
|
+
this.defaultLimit = options.defaultLimit ?? 100;
|
|
8
|
+
}
|
|
9
|
+
async execute(definition, params) {
|
|
10
|
+
const startTime = Date.now();
|
|
11
|
+
const validation = this.validateQuery(definition);
|
|
12
|
+
if (!validation.valid) {
|
|
13
|
+
return {
|
|
14
|
+
data: [],
|
|
15
|
+
columns: [],
|
|
16
|
+
rowCount: 0,
|
|
17
|
+
executionTimeMs: Date.now() - startTime,
|
|
18
|
+
cached: false,
|
|
19
|
+
error: validation.errors.join(", ")
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
if (!this.reader.queryHogQL) {
|
|
23
|
+
return {
|
|
24
|
+
data: [],
|
|
25
|
+
columns: [],
|
|
26
|
+
rowCount: 0,
|
|
27
|
+
executionTimeMs: Date.now() - startTime,
|
|
28
|
+
cached: false,
|
|
29
|
+
error: "Analytics reader does not support HogQL queries."
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const result = await this.executeHogQL(definition, params);
|
|
34
|
+
return {
|
|
35
|
+
data: result.data,
|
|
36
|
+
columns: result.columns,
|
|
37
|
+
rowCount: result.rowCount,
|
|
38
|
+
executionTimeMs: Date.now() - startTime,
|
|
39
|
+
cached: false
|
|
40
|
+
};
|
|
41
|
+
} catch (error) {
|
|
42
|
+
return {
|
|
43
|
+
data: [],
|
|
44
|
+
columns: [],
|
|
45
|
+
rowCount: 0,
|
|
46
|
+
executionTimeMs: Date.now() - startTime,
|
|
47
|
+
cached: false,
|
|
48
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
validateQuery(definition) {
|
|
53
|
+
const errors = [];
|
|
54
|
+
if (definition.type === "SQL" && !definition.sql) {
|
|
55
|
+
errors.push("SQL query is missing.");
|
|
56
|
+
}
|
|
57
|
+
if (definition.type === "AGGREGATION" && !definition.aggregation) {
|
|
58
|
+
errors.push("Aggregation definition is missing.");
|
|
59
|
+
}
|
|
60
|
+
if (definition.type === "METRIC" && !definition.metricIds?.length) {
|
|
61
|
+
errors.push("Metric IDs are missing.");
|
|
62
|
+
}
|
|
63
|
+
if (definition.type === "CUSTOM" && !definition.custom?.handler) {
|
|
64
|
+
errors.push("Custom handler is missing.");
|
|
65
|
+
}
|
|
66
|
+
return { valid: errors.length === 0, errors };
|
|
67
|
+
}
|
|
68
|
+
async executeHogQL(definition, params) {
|
|
69
|
+
if (!this.reader.queryHogQL) {
|
|
70
|
+
throw new Error("Analytics reader does not support HogQL queries.");
|
|
71
|
+
}
|
|
72
|
+
if (definition.type === "SQL") {
|
|
73
|
+
const result = await this.reader.queryHogQL({
|
|
74
|
+
query: definition.sql ?? "",
|
|
75
|
+
values: params.parameters ?? {}
|
|
76
|
+
});
|
|
77
|
+
return mapHogqlResult(result);
|
|
78
|
+
}
|
|
79
|
+
if (definition.type === "AGGREGATION" && definition.aggregation) {
|
|
80
|
+
const { query, values } = buildAggregationQuery(definition.aggregation, params);
|
|
81
|
+
const result = await this.reader.queryHogQL({ query, values });
|
|
82
|
+
return mapHogqlResult(result);
|
|
83
|
+
}
|
|
84
|
+
if (definition.type === "METRIC" && definition.metricIds) {
|
|
85
|
+
const { query, values } = buildMetricQuery(definition.metricIds, params);
|
|
86
|
+
const result = await this.reader.queryHogQL({ query, values });
|
|
87
|
+
return mapHogqlResult(result);
|
|
88
|
+
}
|
|
89
|
+
throw new Error("Unsupported query type for PostHog datasource.");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function createPosthogQueryEngine(reader, options) {
|
|
93
|
+
return new PosthogQueryEngine(reader, options);
|
|
94
|
+
}
|
|
95
|
+
function buildMetricQuery(metricIds, params) {
|
|
96
|
+
const range = resolveDateRange(params);
|
|
97
|
+
const values = {
|
|
98
|
+
dateFrom: range?.from.toISOString(),
|
|
99
|
+
dateTo: range?.to.toISOString()
|
|
100
|
+
};
|
|
101
|
+
const metricClauses = metricIds.map((metricId, index) => {
|
|
102
|
+
values[`metric${index}`] = metricId;
|
|
103
|
+
return `event = {metric${index}}`;
|
|
104
|
+
});
|
|
105
|
+
const whereClauses = [
|
|
106
|
+
metricClauses.length ? `(${metricClauses.join(" or ")})` : "",
|
|
107
|
+
range ? "timestamp >= {dateFrom} and timestamp < {dateTo}" : ""
|
|
108
|
+
].filter(Boolean).join(" and ");
|
|
109
|
+
return {
|
|
110
|
+
query: [
|
|
111
|
+
"select",
|
|
112
|
+
" event as metric,",
|
|
113
|
+
" count() as total",
|
|
114
|
+
"from events",
|
|
115
|
+
whereClauses ? `where ${whereClauses}` : "",
|
|
116
|
+
"group by metric"
|
|
117
|
+
].filter(Boolean).join(`
|
|
118
|
+
`),
|
|
119
|
+
values
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function buildAggregationQuery(aggregation, params) {
|
|
123
|
+
const measures = aggregation.measures.map((measure) => ({
|
|
124
|
+
...measure,
|
|
125
|
+
expression: buildMeasureExpression(measure.field, measure.aggregation)
|
|
126
|
+
}));
|
|
127
|
+
const dimensions = aggregation.dimensions.map((dimension) => ({
|
|
128
|
+
...dimension,
|
|
129
|
+
expression: buildDimensionExpression(dimension.field, dimension.type, dimension.granularity)
|
|
130
|
+
}));
|
|
131
|
+
const selections = [
|
|
132
|
+
...dimensions.map((dimension) => `${dimension.expression} as ${dimension.name}`),
|
|
133
|
+
...measures.map((measure) => `${measure.expression} as ${measure.name}`)
|
|
134
|
+
];
|
|
135
|
+
const values = {};
|
|
136
|
+
const filters = buildFilterClauses(aggregation, params, values);
|
|
137
|
+
const limit = aggregation.limit ?? 100;
|
|
138
|
+
const orderBy = aggregation.orderBy?.length ? `order by ${aggregation.orderBy.map((order) => `${order.field} ${order.direction}`).join(", ")}` : "";
|
|
139
|
+
return {
|
|
140
|
+
query: [
|
|
141
|
+
"select",
|
|
142
|
+
` ${selections.join(`,
|
|
143
|
+
`)}`,
|
|
144
|
+
`from ${aggregation.source}`,
|
|
145
|
+
filters.length ? `where ${filters.join(" and ")}` : "",
|
|
146
|
+
dimensions.length ? `group by ${dimensions.map((d) => d.name).join(", ")}` : "",
|
|
147
|
+
orderBy,
|
|
148
|
+
`limit ${limit}`
|
|
149
|
+
].filter(Boolean).join(`
|
|
150
|
+
`),
|
|
151
|
+
values
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function buildMeasureExpression(field, aggregation) {
|
|
155
|
+
switch (aggregation) {
|
|
156
|
+
case "COUNT":
|
|
157
|
+
return field === "*" ? "count()" : `count(${field})`;
|
|
158
|
+
case "COUNT_DISTINCT":
|
|
159
|
+
return `countDistinct(${field})`;
|
|
160
|
+
case "SUM":
|
|
161
|
+
return `sum(${field})`;
|
|
162
|
+
case "AVG":
|
|
163
|
+
return `avg(${field})`;
|
|
164
|
+
case "MIN":
|
|
165
|
+
return `min(${field})`;
|
|
166
|
+
case "MAX":
|
|
167
|
+
return `max(${field})`;
|
|
168
|
+
default:
|
|
169
|
+
return `count(${field})`;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function buildDimensionExpression(field, type, granularity) {
|
|
173
|
+
if (type === "TIME") {
|
|
174
|
+
switch (granularity) {
|
|
175
|
+
case "HOUR":
|
|
176
|
+
return `toStartOfHour(${field})`;
|
|
177
|
+
case "WEEK":
|
|
178
|
+
return `toStartOfWeek(${field})`;
|
|
179
|
+
case "MONTH":
|
|
180
|
+
return `toStartOfMonth(${field})`;
|
|
181
|
+
case "YEAR":
|
|
182
|
+
return `toStartOfYear(${field})`;
|
|
183
|
+
case "DAY":
|
|
184
|
+
default:
|
|
185
|
+
return `toStartOfDay(${field})`;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return field;
|
|
189
|
+
}
|
|
190
|
+
function buildFilterClauses(aggregation, params, values) {
|
|
191
|
+
const clauses = [];
|
|
192
|
+
const range = resolveDateRange(params);
|
|
193
|
+
if (range) {
|
|
194
|
+
values.dateFrom = range.from.toISOString();
|
|
195
|
+
values.dateTo = range.to.toISOString();
|
|
196
|
+
clauses.push(`timestamp >= {dateFrom} and timestamp < {dateTo}`);
|
|
197
|
+
}
|
|
198
|
+
aggregation.filters?.forEach((filter, index) => {
|
|
199
|
+
const key = `filter${index}`;
|
|
200
|
+
switch (filter.operator) {
|
|
201
|
+
case "eq":
|
|
202
|
+
values[key] = filter.value;
|
|
203
|
+
clauses.push(`${filter.field} = {${key}}`);
|
|
204
|
+
break;
|
|
205
|
+
case "neq":
|
|
206
|
+
values[key] = filter.value;
|
|
207
|
+
clauses.push(`${filter.field} != {${key}}`);
|
|
208
|
+
break;
|
|
209
|
+
case "gt":
|
|
210
|
+
values[key] = filter.value;
|
|
211
|
+
clauses.push(`${filter.field} > {${key}}`);
|
|
212
|
+
break;
|
|
213
|
+
case "gte":
|
|
214
|
+
values[key] = filter.value;
|
|
215
|
+
clauses.push(`${filter.field} >= {${key}}`);
|
|
216
|
+
break;
|
|
217
|
+
case "lt":
|
|
218
|
+
values[key] = filter.value;
|
|
219
|
+
clauses.push(`${filter.field} < {${key}}`);
|
|
220
|
+
break;
|
|
221
|
+
case "lte":
|
|
222
|
+
values[key] = filter.value;
|
|
223
|
+
clauses.push(`${filter.field} <= {${key}}`);
|
|
224
|
+
break;
|
|
225
|
+
case "contains":
|
|
226
|
+
values[key] = filter.value;
|
|
227
|
+
clauses.push(`${filter.field} LIKE '%' || {${key}} || '%'`);
|
|
228
|
+
break;
|
|
229
|
+
case "between": {
|
|
230
|
+
if (Array.isArray(filter.value) && filter.value.length >= 2) {
|
|
231
|
+
values[`${key}Start`] = filter.value[0];
|
|
232
|
+
values[`${key}End`] = filter.value[1];
|
|
233
|
+
clauses.push(`${filter.field} BETWEEN {${key}Start} AND {${key}End}`);
|
|
234
|
+
}
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
case "in":
|
|
238
|
+
case "nin":
|
|
239
|
+
if (Array.isArray(filter.value)) {
|
|
240
|
+
const placeholders = [];
|
|
241
|
+
filter.value.forEach((value, valueIndex) => {
|
|
242
|
+
const valueKey = `${key}_${valueIndex}`;
|
|
243
|
+
values[valueKey] = value;
|
|
244
|
+
placeholders.push(`{${valueKey}}`);
|
|
245
|
+
});
|
|
246
|
+
if (placeholders.length) {
|
|
247
|
+
clauses.push(`${filter.field} ${filter.operator === "in" ? "IN" : "NOT IN"} (${placeholders.join(", ")})`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
break;
|
|
251
|
+
default:
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
return clauses;
|
|
256
|
+
}
|
|
257
|
+
function resolveDateRange(params) {
|
|
258
|
+
const range = params.dateRange;
|
|
259
|
+
if (!range)
|
|
260
|
+
return null;
|
|
261
|
+
return { from: range.start, to: range.end };
|
|
262
|
+
}
|
|
263
|
+
function mapHogqlResult(result) {
|
|
264
|
+
if (!Array.isArray(result.results) || !Array.isArray(result.columns)) {
|
|
265
|
+
return { data: [], columns: [], rowCount: 0 };
|
|
266
|
+
}
|
|
267
|
+
const columns = result.columns.map((name) => ({
|
|
268
|
+
name,
|
|
269
|
+
type: "STRING"
|
|
270
|
+
}));
|
|
271
|
+
const data = result.results.map((row) => {
|
|
272
|
+
if (!Array.isArray(row))
|
|
273
|
+
return {};
|
|
274
|
+
const record = {};
|
|
275
|
+
result.columns?.forEach((column, index) => {
|
|
276
|
+
record[column] = row[index];
|
|
277
|
+
});
|
|
278
|
+
return record;
|
|
279
|
+
});
|
|
280
|
+
return {
|
|
281
|
+
data,
|
|
282
|
+
columns,
|
|
283
|
+
rowCount: data.length
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
export {
|
|
287
|
+
createPosthogQueryEngine,
|
|
288
|
+
PosthogQueryEngine
|
|
289
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// src/docs/analytics-dashboard.docblock.ts
|
|
2
|
+
import { registerDocBlocks } from "@contractspec/lib.contracts/docs";
|
|
3
|
+
var analyticsDashboardDocBlocks = [
|
|
4
|
+
{
|
|
5
|
+
id: "docs.examples.analytics-dashboard",
|
|
6
|
+
title: "Analytics Dashboard",
|
|
7
|
+
summary: "Multi-tenant analytics with dashboards, widgets, query builder, and scheduled reports built on the Event Bus.",
|
|
8
|
+
kind: "reference",
|
|
9
|
+
visibility: "public",
|
|
10
|
+
route: "/docs/examples/analytics-dashboard",
|
|
11
|
+
tags: ["analytics", "dashboards", "bi", "queries"],
|
|
12
|
+
body: `## Entities
|
|
13
|
+
|
|
14
|
+
- Dashboard, Widget, Query, Report.
|
|
15
|
+
- Widget/query configs stay declarative for regeneration.
|
|
16
|
+
|
|
17
|
+
## Contracts
|
|
18
|
+
|
|
19
|
+
- \`analytics.dashboard.create\`, \`analytics.widget.add\`, \`analytics.query.execute\`, \`analytics.dashboard.get\`.
|
|
20
|
+
- Metrics can source from Event Bus schemas and Usage/Metering module.
|
|
21
|
+
|
|
22
|
+
## Events
|
|
23
|
+
|
|
24
|
+
- dashboard.created/updated, widget.added, report.scheduled/sent.
|
|
25
|
+
- Emitted for audit + notification hooks.
|
|
26
|
+
|
|
27
|
+
## UI / Presentations
|
|
28
|
+
|
|
29
|
+
- Dashboard list, dashboard view, query builder, widget gallery.
|
|
30
|
+
- Registered under \`analytics-dashboard\` template in Template Registry.
|
|
31
|
+
|
|
32
|
+
## Notes
|
|
33
|
+
|
|
34
|
+
- Enforce org scoping for multi-tenant isolation.
|
|
35
|
+
- Use Feature Flags for beta widgets; Metering to track query volume.
|
|
36
|
+
- PostHog datasource can back query execution via HogQL for dashboard widgets.
|
|
37
|
+
`
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
id: "docs.examples.analytics-dashboard.goal",
|
|
41
|
+
title: "Analytics Dashboard — Goal",
|
|
42
|
+
summary: "Why this template matters and what success looks like.",
|
|
43
|
+
kind: "goal",
|
|
44
|
+
visibility: "public",
|
|
45
|
+
route: "/docs/examples/analytics-dashboard/goal",
|
|
46
|
+
tags: ["analytics", "goal"],
|
|
47
|
+
body: `## Why it matters
|
|
48
|
+
- Give teams a regenerable analytics workspace that stays in sync with Event Bus, Usage/Metering, and presentations.
|
|
49
|
+
- Avoid dashboard drift by keeping schema-first widgets/queries.
|
|
50
|
+
|
|
51
|
+
## Business/Product goal
|
|
52
|
+
- Deliver tenant-scoped dashboards, governed queries, and scheduled reports with clear ownership and auditability.
|
|
53
|
+
- Enable feature-flagged rollout of new widgets and sampling for cost control.
|
|
54
|
+
|
|
55
|
+
## Success criteria
|
|
56
|
+
- Dashboards can be regenerated safely from spec changes.
|
|
57
|
+
- Queries/widgets have enforced validation; PII is redacted per policy.`
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
id: "docs.examples.analytics-dashboard.usage",
|
|
61
|
+
title: "Analytics Dashboard — Usage",
|
|
62
|
+
summary: "How to seed, extend, and safely regenerate dashboards.",
|
|
63
|
+
kind: "usage",
|
|
64
|
+
visibility: "public",
|
|
65
|
+
route: "/docs/examples/analytics-dashboard/usage",
|
|
66
|
+
tags: ["analytics", "usage"],
|
|
67
|
+
body: `## Setup
|
|
68
|
+
1) Seed dashboards/widgets (via template registry) to preload sample queries.
|
|
69
|
+
2) Configure org/tenant scope and attach Usage/Metering for cost/sampling controls.
|
|
70
|
+
|
|
71
|
+
## Extend & regenerate
|
|
72
|
+
1) Add or modify widget/query schemas in the spec (inputs, validation, PII paths).
|
|
73
|
+
2) Regenerate to update UI + API + events; review presentations for redaction.
|
|
74
|
+
3) Use Feature Flags to roll out new widgets or sampling knobs gradually.
|
|
75
|
+
|
|
76
|
+
## Guardrails
|
|
77
|
+
- Keep all query inputs validated; mark PII paths in policy.
|
|
78
|
+
- Use Audit Trail for report deliveries; Notifications for scheduled sends.`
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
id: "docs.examples.analytics-dashboard.constraints",
|
|
82
|
+
title: "Analytics Dashboard — Constraints & Safety",
|
|
83
|
+
summary: "Internal guardrails for queries, widgets, and regeneration.",
|
|
84
|
+
kind: "reference",
|
|
85
|
+
visibility: "internal",
|
|
86
|
+
route: "/docs/examples/analytics-dashboard/constraints",
|
|
87
|
+
tags: ["analytics", "constraints", "internal"],
|
|
88
|
+
body: `## Constraints
|
|
89
|
+
- Queries and widgets must declare inputs/validation in spec; no ad-hoc query strings.
|
|
90
|
+
- Regeneration must preserve sampling/windowing semantics; document changes explicitly.
|
|
91
|
+
- Events/usage metrics should remain consistent with Metering/Audit wiring.
|
|
92
|
+
|
|
93
|
+
## PII & Data
|
|
94
|
+
- Mark PII paths (user ids, emails) in presentations for redaction.
|
|
95
|
+
- Avoid exposing raw query payloads in MCP/web without policy checks.
|
|
96
|
+
|
|
97
|
+
## Verification
|
|
98
|
+
- Add fixtures for widget/query schema changes and scheduled reports.
|
|
99
|
+
- Run regeneration diff when adjusting query builders; ensure UI/markdown targets updated.
|
|
100
|
+
- Confirm feature-flagged widgets default to safe/off for new tenants.`
|
|
101
|
+
}
|
|
102
|
+
];
|
|
103
|
+
registerDocBlocks(analyticsDashboardDocBlocks);
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// src/docs/analytics-dashboard.docblock.ts
|
|
2
|
+
import { registerDocBlocks } from "@contractspec/lib.contracts/docs";
|
|
3
|
+
var analyticsDashboardDocBlocks = [
|
|
4
|
+
{
|
|
5
|
+
id: "docs.examples.analytics-dashboard",
|
|
6
|
+
title: "Analytics Dashboard",
|
|
7
|
+
summary: "Multi-tenant analytics with dashboards, widgets, query builder, and scheduled reports built on the Event Bus.",
|
|
8
|
+
kind: "reference",
|
|
9
|
+
visibility: "public",
|
|
10
|
+
route: "/docs/examples/analytics-dashboard",
|
|
11
|
+
tags: ["analytics", "dashboards", "bi", "queries"],
|
|
12
|
+
body: `## Entities
|
|
13
|
+
|
|
14
|
+
- Dashboard, Widget, Query, Report.
|
|
15
|
+
- Widget/query configs stay declarative for regeneration.
|
|
16
|
+
|
|
17
|
+
## Contracts
|
|
18
|
+
|
|
19
|
+
- \`analytics.dashboard.create\`, \`analytics.widget.add\`, \`analytics.query.execute\`, \`analytics.dashboard.get\`.
|
|
20
|
+
- Metrics can source from Event Bus schemas and Usage/Metering module.
|
|
21
|
+
|
|
22
|
+
## Events
|
|
23
|
+
|
|
24
|
+
- dashboard.created/updated, widget.added, report.scheduled/sent.
|
|
25
|
+
- Emitted for audit + notification hooks.
|
|
26
|
+
|
|
27
|
+
## UI / Presentations
|
|
28
|
+
|
|
29
|
+
- Dashboard list, dashboard view, query builder, widget gallery.
|
|
30
|
+
- Registered under \`analytics-dashboard\` template in Template Registry.
|
|
31
|
+
|
|
32
|
+
## Notes
|
|
33
|
+
|
|
34
|
+
- Enforce org scoping for multi-tenant isolation.
|
|
35
|
+
- Use Feature Flags for beta widgets; Metering to track query volume.
|
|
36
|
+
- PostHog datasource can back query execution via HogQL for dashboard widgets.
|
|
37
|
+
`
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
id: "docs.examples.analytics-dashboard.goal",
|
|
41
|
+
title: "Analytics Dashboard — Goal",
|
|
42
|
+
summary: "Why this template matters and what success looks like.",
|
|
43
|
+
kind: "goal",
|
|
44
|
+
visibility: "public",
|
|
45
|
+
route: "/docs/examples/analytics-dashboard/goal",
|
|
46
|
+
tags: ["analytics", "goal"],
|
|
47
|
+
body: `## Why it matters
|
|
48
|
+
- Give teams a regenerable analytics workspace that stays in sync with Event Bus, Usage/Metering, and presentations.
|
|
49
|
+
- Avoid dashboard drift by keeping schema-first widgets/queries.
|
|
50
|
+
|
|
51
|
+
## Business/Product goal
|
|
52
|
+
- Deliver tenant-scoped dashboards, governed queries, and scheduled reports with clear ownership and auditability.
|
|
53
|
+
- Enable feature-flagged rollout of new widgets and sampling for cost control.
|
|
54
|
+
|
|
55
|
+
## Success criteria
|
|
56
|
+
- Dashboards can be regenerated safely from spec changes.
|
|
57
|
+
- Queries/widgets have enforced validation; PII is redacted per policy.`
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
id: "docs.examples.analytics-dashboard.usage",
|
|
61
|
+
title: "Analytics Dashboard — Usage",
|
|
62
|
+
summary: "How to seed, extend, and safely regenerate dashboards.",
|
|
63
|
+
kind: "usage",
|
|
64
|
+
visibility: "public",
|
|
65
|
+
route: "/docs/examples/analytics-dashboard/usage",
|
|
66
|
+
tags: ["analytics", "usage"],
|
|
67
|
+
body: `## Setup
|
|
68
|
+
1) Seed dashboards/widgets (via template registry) to preload sample queries.
|
|
69
|
+
2) Configure org/tenant scope and attach Usage/Metering for cost/sampling controls.
|
|
70
|
+
|
|
71
|
+
## Extend & regenerate
|
|
72
|
+
1) Add or modify widget/query schemas in the spec (inputs, validation, PII paths).
|
|
73
|
+
2) Regenerate to update UI + API + events; review presentations for redaction.
|
|
74
|
+
3) Use Feature Flags to roll out new widgets or sampling knobs gradually.
|
|
75
|
+
|
|
76
|
+
## Guardrails
|
|
77
|
+
- Keep all query inputs validated; mark PII paths in policy.
|
|
78
|
+
- Use Audit Trail for report deliveries; Notifications for scheduled sends.`
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
id: "docs.examples.analytics-dashboard.constraints",
|
|
82
|
+
title: "Analytics Dashboard — Constraints & Safety",
|
|
83
|
+
summary: "Internal guardrails for queries, widgets, and regeneration.",
|
|
84
|
+
kind: "reference",
|
|
85
|
+
visibility: "internal",
|
|
86
|
+
route: "/docs/examples/analytics-dashboard/constraints",
|
|
87
|
+
tags: ["analytics", "constraints", "internal"],
|
|
88
|
+
body: `## Constraints
|
|
89
|
+
- Queries and widgets must declare inputs/validation in spec; no ad-hoc query strings.
|
|
90
|
+
- Regeneration must preserve sampling/windowing semantics; document changes explicitly.
|
|
91
|
+
- Events/usage metrics should remain consistent with Metering/Audit wiring.
|
|
92
|
+
|
|
93
|
+
## PII & Data
|
|
94
|
+
- Mark PII paths (user ids, emails) in presentations for redaction.
|
|
95
|
+
- Avoid exposing raw query payloads in MCP/web without policy checks.
|
|
96
|
+
|
|
97
|
+
## Verification
|
|
98
|
+
- Add fixtures for widget/query schema changes and scheduled reports.
|
|
99
|
+
- Run regeneration diff when adjusting query builders; ensure UI/markdown targets updated.
|
|
100
|
+
- Confirm feature-flagged widgets default to safe/off for new tenants.`
|
|
101
|
+
}
|
|
102
|
+
];
|
|
103
|
+
registerDocBlocks(analyticsDashboardDocBlocks);
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// src/events.ts
|
|
2
|
+
import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
|
|
3
|
+
import { defineEvent } from "@contractspec/lib.contracts";
|
|
4
|
+
var DashboardEventPayload = defineSchemaModel({
|
|
5
|
+
name: "DashboardEventPayload",
|
|
6
|
+
description: "Payload for dashboard events",
|
|
7
|
+
fields: {
|
|
8
|
+
dashboardId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
9
|
+
title: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
10
|
+
orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
11
|
+
createdBy: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
12
|
+
timestamp: { type: ScalarTypeEnum.DateTime(), isOptional: false }
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
var WidgetEventPayload = defineSchemaModel({
|
|
16
|
+
name: "WidgetEventPayload",
|
|
17
|
+
description: "Payload for widget events",
|
|
18
|
+
fields: {
|
|
19
|
+
widgetId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
20
|
+
dashboardId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
21
|
+
widgetType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
22
|
+
orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
23
|
+
timestamp: { type: ScalarTypeEnum.DateTime(), isOptional: false }
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
var QueryEventPayload = defineSchemaModel({
|
|
27
|
+
name: "QueryEventPayload",
|
|
28
|
+
description: "Payload for query events",
|
|
29
|
+
fields: {
|
|
30
|
+
queryId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
31
|
+
name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
32
|
+
queryType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
33
|
+
orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
34
|
+
createdBy: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
35
|
+
timestamp: { type: ScalarTypeEnum.DateTime(), isOptional: false }
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
var DashboardCreatedEvent = defineEvent({
|
|
39
|
+
meta: {
|
|
40
|
+
key: "analytics.dashboard.created",
|
|
41
|
+
version: "1.0.0",
|
|
42
|
+
description: "A dashboard was created.",
|
|
43
|
+
stability: "stable",
|
|
44
|
+
owners: ["@analytics-team"],
|
|
45
|
+
tags: ["dashboard"]
|
|
46
|
+
},
|
|
47
|
+
payload: DashboardEventPayload
|
|
48
|
+
});
|
|
49
|
+
var WidgetAddedEvent = defineEvent({
|
|
50
|
+
meta: {
|
|
51
|
+
key: "analytics.widget.added",
|
|
52
|
+
version: "1.0.0",
|
|
53
|
+
description: "A widget was added to a dashboard.",
|
|
54
|
+
stability: "stable",
|
|
55
|
+
owners: ["@analytics-team"],
|
|
56
|
+
tags: ["dashboard"]
|
|
57
|
+
},
|
|
58
|
+
payload: WidgetEventPayload
|
|
59
|
+
});
|
|
60
|
+
var QueryCreatedEvent = defineEvent({
|
|
61
|
+
meta: {
|
|
62
|
+
key: "analytics.query.created",
|
|
63
|
+
version: "1.0.0",
|
|
64
|
+
description: "A query was created.",
|
|
65
|
+
stability: "stable",
|
|
66
|
+
owners: ["@analytics-team"],
|
|
67
|
+
tags: ["dashboard"]
|
|
68
|
+
},
|
|
69
|
+
payload: QueryEventPayload
|
|
70
|
+
});
|
|
71
|
+
var AnalyticsDashboardEvents = {
|
|
72
|
+
DashboardCreatedEvent,
|
|
73
|
+
WidgetAddedEvent,
|
|
74
|
+
QueryCreatedEvent
|
|
75
|
+
};
|
|
76
|
+
export {
|
|
77
|
+
WidgetAddedEvent,
|
|
78
|
+
QueryCreatedEvent,
|
|
79
|
+
DashboardCreatedEvent,
|
|
80
|
+
AnalyticsDashboardEvents
|
|
81
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// src/example.ts
|
|
2
|
+
import { defineExample } from "@contractspec/lib.contracts";
|
|
3
|
+
var example = defineExample({
|
|
4
|
+
meta: {
|
|
5
|
+
key: "analytics-dashboard",
|
|
6
|
+
version: "1.0.0",
|
|
7
|
+
title: "Analytics Dashboard",
|
|
8
|
+
description: "Tenant-scoped dashboards, widgets, query builder, and scheduled reports (spec-first widgets/queries).",
|
|
9
|
+
kind: "template",
|
|
10
|
+
visibility: "public",
|
|
11
|
+
stability: "experimental",
|
|
12
|
+
owners: ["@platform.core"],
|
|
13
|
+
tags: ["analytics", "dashboards", "bi", "queries"]
|
|
14
|
+
},
|
|
15
|
+
docs: {
|
|
16
|
+
rootDocId: "docs.examples.analytics-dashboard",
|
|
17
|
+
goalDocId: "docs.examples.analytics-dashboard.goal",
|
|
18
|
+
usageDocId: "docs.examples.analytics-dashboard.usage",
|
|
19
|
+
constraintsDocId: "docs.examples.analytics-dashboard.constraints"
|
|
20
|
+
},
|
|
21
|
+
entrypoints: {
|
|
22
|
+
packageName: "@contractspec/example.analytics-dashboard",
|
|
23
|
+
feature: "./feature",
|
|
24
|
+
contracts: "./contracts",
|
|
25
|
+
presentations: "./presentations",
|
|
26
|
+
handlers: "./handlers",
|
|
27
|
+
docs: "./docs"
|
|
28
|
+
},
|
|
29
|
+
surfaces: {
|
|
30
|
+
templates: true,
|
|
31
|
+
sandbox: {
|
|
32
|
+
enabled: true,
|
|
33
|
+
modes: ["playground", "specs", "builder", "markdown", "evolution"]
|
|
34
|
+
},
|
|
35
|
+
studio: { enabled: true, installable: true },
|
|
36
|
+
mcp: { enabled: true }
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
var example_default = example;
|
|
40
|
+
export {
|
|
41
|
+
example_default as default
|
|
42
|
+
};
|