measurr 0.3.0 → 0.5.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/README.md +175 -28
- package/dist/analytics.d.ts +47 -4
- package/dist/dataset.d.ts +50 -30
- package/dist/handler.d.ts +56 -0
- package/dist/handler.js +49 -0
- package/dist/index-g255jb1s.js +149 -0
- package/dist/index-g2mjqx4f.js +303 -0
- package/dist/{index-wrr67sxq.js → index-zgb1e89d.js} +160 -451
- package/dist/index.d.ts +1 -1
- package/dist/index.js +7 -5
- package/dist/period.d.ts +5 -0
- package/dist/shapes.d.ts +111 -0
- package/dist/shapes.js +134 -0
- package/dist/testing.d.ts +9 -4
- package/dist/testing.js +13 -3
- package/package.json +14 -2
- package/src/analytics.ts +337 -40
- package/src/dataset.ts +135 -60
- package/src/handler.ts +124 -0
- package/src/index.ts +3 -0
- package/src/period.ts +32 -0
- package/src/plan.ts +6 -0
- package/src/shapes.ts +295 -0
- package/src/testing.ts +31 -9
|
@@ -1,151 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
MAX_PERIOD_DAYS,
|
|
3
|
+
isValidTimezone,
|
|
4
|
+
PeriodError,
|
|
5
|
+
resolvePeriod,
|
|
6
|
+
previousPeriod
|
|
7
|
+
} from "./index-g255jb1s.js";
|
|
5
8
|
|
|
6
9
|
// src/query.ts
|
|
7
10
|
import { z } from "zod";
|
|
8
|
-
|
|
9
|
-
// src/period.ts
|
|
10
|
-
var MAX_PERIOD_DAYS = 731;
|
|
11
|
-
var DEFAULT_PERIOD = {
|
|
12
|
-
last: { days: 30 }
|
|
13
|
-
};
|
|
14
|
-
var DAY_MS = 86400000;
|
|
15
|
-
var formatterCache = new Map;
|
|
16
|
-
function formatter(timezone) {
|
|
17
|
-
let cached = formatterCache.get(timezone);
|
|
18
|
-
if (!cached) {
|
|
19
|
-
cached = new Intl.DateTimeFormat("en-US", {
|
|
20
|
-
timeZone: timezone,
|
|
21
|
-
hourCycle: "h23",
|
|
22
|
-
year: "numeric",
|
|
23
|
-
month: "2-digit",
|
|
24
|
-
day: "2-digit",
|
|
25
|
-
hour: "2-digit",
|
|
26
|
-
minute: "2-digit",
|
|
27
|
-
second: "2-digit"
|
|
28
|
-
});
|
|
29
|
-
formatterCache.set(timezone, cached);
|
|
30
|
-
}
|
|
31
|
-
return cached;
|
|
32
|
-
}
|
|
33
|
-
function isValidTimezone(timezone) {
|
|
34
|
-
if (!/^[A-Za-z][A-Za-z0-9_+\-/]*$/.test(timezone))
|
|
35
|
-
return false;
|
|
36
|
-
try {
|
|
37
|
-
formatter(timezone);
|
|
38
|
-
return true;
|
|
39
|
-
} catch {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
function localParts(instant, timezone) {
|
|
44
|
-
const parts = {};
|
|
45
|
-
for (const part of formatter(timezone).formatToParts(instant)) {
|
|
46
|
-
if (part.type !== "literal")
|
|
47
|
-
parts[part.type] = Number(part.value);
|
|
48
|
-
}
|
|
49
|
-
return {
|
|
50
|
-
year: parts.year ?? 0,
|
|
51
|
-
month: parts.month ?? 1,
|
|
52
|
-
day: parts.day ?? 1,
|
|
53
|
-
hour: parts.hour ?? 0,
|
|
54
|
-
minute: parts.minute ?? 0,
|
|
55
|
-
second: parts.second ?? 0
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
function offsetAt(instant, timezone) {
|
|
59
|
-
const p = localParts(instant, timezone);
|
|
60
|
-
const asUtc = Date.UTC(p.year, p.month - 1, p.day, p.hour, p.minute, p.second);
|
|
61
|
-
return asUtc - (instant.getTime() - instant.getUTCMilliseconds());
|
|
62
|
-
}
|
|
63
|
-
function localDate(instant, timezone) {
|
|
64
|
-
const p = localParts(instant, timezone);
|
|
65
|
-
return formatDate(Date.UTC(p.year, p.month - 1, p.day));
|
|
66
|
-
}
|
|
67
|
-
function startOfLocalDay(date, timezone) {
|
|
68
|
-
const wallClock = parseDate(date);
|
|
69
|
-
const firstGuess = wallClock - offsetAt(new Date(wallClock), timezone);
|
|
70
|
-
const corrected = wallClock - offsetAt(new Date(firstGuess), timezone);
|
|
71
|
-
return new Date(corrected);
|
|
72
|
-
}
|
|
73
|
-
function parseDate(date) {
|
|
74
|
-
const [year, month, day] = date.split("-").map(Number);
|
|
75
|
-
return Date.UTC(year ?? 0, (month ?? 1) - 1, day ?? 1);
|
|
76
|
-
}
|
|
77
|
-
function formatDate(utcMs) {
|
|
78
|
-
return new Date(utcMs).toISOString().slice(0, 10);
|
|
79
|
-
}
|
|
80
|
-
function addDays(date, days) {
|
|
81
|
-
return formatDate(parseDate(date) + days * DAY_MS);
|
|
82
|
-
}
|
|
83
|
-
function daysBetween(from, to) {
|
|
84
|
-
return Math.round((parseDate(to) - parseDate(from)) / DAY_MS);
|
|
85
|
-
}
|
|
86
|
-
function isoWeekday(date) {
|
|
87
|
-
const day = new Date(parseDate(date)).getUTCDay();
|
|
88
|
-
return day === 0 ? 7 : day;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
class PeriodError extends Error {
|
|
92
|
-
name = "PeriodError";
|
|
93
|
-
}
|
|
94
|
-
function resolvePeriod(input, timezone, now) {
|
|
95
|
-
const today = localDate(now, timezone);
|
|
96
|
-
const [from, to] = periodDays(input, today);
|
|
97
|
-
if (daysBetween(from, to) < 0) {
|
|
98
|
-
throw new PeriodError(`The period starts (${from}) after it ends (${to}).`);
|
|
99
|
-
}
|
|
100
|
-
return periodFromDays(from, to, timezone);
|
|
101
|
-
}
|
|
102
|
-
function periodDays(input, today) {
|
|
103
|
-
if ("last" in input)
|
|
104
|
-
return [addDays(today, -(input.last.days - 1)), today];
|
|
105
|
-
if ("from" in input)
|
|
106
|
-
return [input.from, input.to];
|
|
107
|
-
const monthStart = `${today.slice(0, 8)}01`;
|
|
108
|
-
const weekStart = addDays(today, -(isoWeekday(today) - 1));
|
|
109
|
-
switch (input.preset) {
|
|
110
|
-
case "today":
|
|
111
|
-
return [today, today];
|
|
112
|
-
case "yesterday":
|
|
113
|
-
return [addDays(today, -1), addDays(today, -1)];
|
|
114
|
-
case "thisWeek":
|
|
115
|
-
return [weekStart, today];
|
|
116
|
-
case "lastWeek":
|
|
117
|
-
return [addDays(weekStart, -7), addDays(weekStart, -1)];
|
|
118
|
-
case "thisMonth":
|
|
119
|
-
return [monthStart, today];
|
|
120
|
-
case "lastMonth": {
|
|
121
|
-
const lastMonthEnd = addDays(monthStart, -1);
|
|
122
|
-
return [`${lastMonthEnd.slice(0, 8)}01`, lastMonthEnd];
|
|
123
|
-
}
|
|
124
|
-
case "thisYear":
|
|
125
|
-
return [`${today.slice(0, 4)}-01-01`, today];
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
function periodFromDays(from, to, timezone) {
|
|
129
|
-
const days = daysBetween(from, to) + 1;
|
|
130
|
-
if (days > MAX_PERIOD_DAYS) {
|
|
131
|
-
throw new PeriodError(`A period may cover at most ${MAX_PERIOD_DAYS} days (two years); ${from} to ${to} is ${days} days.`);
|
|
132
|
-
}
|
|
133
|
-
return {
|
|
134
|
-
from,
|
|
135
|
-
to,
|
|
136
|
-
timezone,
|
|
137
|
-
start: startOfLocalDay(from, timezone),
|
|
138
|
-
end: startOfLocalDay(addDays(to, 1), timezone),
|
|
139
|
-
days
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
function previousPeriod(period) {
|
|
143
|
-
const to = addDays(period.from, -1);
|
|
144
|
-
const from = addDays(to, -(period.days - 1));
|
|
145
|
-
return periodFromDays(from, to, period.timezone);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// src/query.ts
|
|
149
11
|
var MAX_MEASURES = 4;
|
|
150
12
|
var MAX_GROUP_BY = 2;
|
|
151
13
|
var MAX_LIMIT = 100;
|
|
@@ -437,6 +299,9 @@ function dimensionNodes(dataset, key, timezone) {
|
|
|
437
299
|
function baseWhere(dataset, tenant, period) {
|
|
438
300
|
const where = [];
|
|
439
301
|
if (tenant !== allTenants2) {
|
|
302
|
+
if (!dataset.tenantColumn) {
|
|
303
|
+
throw new Error(`Dataset ${dataset.key} has no tenant column; only allTenants may query it`);
|
|
304
|
+
}
|
|
440
305
|
const ids = typeof tenant === "object" ? [...new Set(tenant)] : [tenant];
|
|
441
306
|
const [only] = ids;
|
|
442
307
|
where.push(ids.length === 1 && only !== undefined ? {
|
|
@@ -734,6 +599,7 @@ function createAnalytics2(options) {
|
|
|
734
599
|
const sourceNames = Object.keys(options.sources);
|
|
735
600
|
const byKey = new Map;
|
|
736
601
|
const sourceOf = new Map;
|
|
602
|
+
const restrictedOf = new Map;
|
|
737
603
|
for (const dataset of datasets) {
|
|
738
604
|
if (byKey.has(dataset.key))
|
|
739
605
|
throw new Error(`Two datasets use the key ${dataset.key}`);
|
|
@@ -741,6 +607,11 @@ function createAnalytics2(options) {
|
|
|
741
607
|
const source = resolveSource(dataset, options.sources, sourceNames);
|
|
742
608
|
sourceOf.set(dataset.key, source);
|
|
743
609
|
compileEverything(dataset, source.dialect);
|
|
610
|
+
const restricted = restrictedFields(dataset);
|
|
611
|
+
if (restricted.length > 0 && !options.authorizeField) {
|
|
612
|
+
throw new Error(`Dataset ${dataset.key} marks fields with access, but createAnalytics has no authorizeField`);
|
|
613
|
+
}
|
|
614
|
+
restrictedOf.set(dataset.key, restricted);
|
|
744
615
|
}
|
|
745
616
|
const now = options.now ?? (() => new Date);
|
|
746
617
|
const querySchemas = new Map;
|
|
@@ -757,9 +628,17 @@ function createAnalytics2(options) {
|
|
|
757
628
|
throw new Error(`No source for ${dataset.key}`);
|
|
758
629
|
return source;
|
|
759
630
|
};
|
|
760
|
-
const querySchemaFor = (
|
|
761
|
-
const
|
|
762
|
-
|
|
631
|
+
const querySchemaFor = (selected) => {
|
|
632
|
+
const keyOf = (dataset) => {
|
|
633
|
+
if (byKey.get(dataset.key) === dataset)
|
|
634
|
+
return dataset.key;
|
|
635
|
+
const fields = [
|
|
636
|
+
...Object.keys(dataset.measures),
|
|
637
|
+
...Object.keys(dataset.dimensions)
|
|
638
|
+
];
|
|
639
|
+
return `${dataset.key}(${fields.join(" ")})`;
|
|
640
|
+
};
|
|
641
|
+
const selectionKey = selected.map(keyOf).sort().join(",");
|
|
763
642
|
let schema = querySchemas.get(selectionKey);
|
|
764
643
|
if (!schema) {
|
|
765
644
|
schema = buildQuerySchema(selected);
|
|
@@ -775,38 +654,68 @@ function createAnalytics2(options) {
|
|
|
775
654
|
}
|
|
776
655
|
return schema;
|
|
777
656
|
};
|
|
657
|
+
const deniedFields = async (dataset, ctx) => {
|
|
658
|
+
const restricted = restrictedOf.get(dataset.key) ?? [];
|
|
659
|
+
const authorizeField = options.authorizeField;
|
|
660
|
+
if (restricted.length === 0 || !authorizeField)
|
|
661
|
+
return restricted;
|
|
662
|
+
const mayUse = async (field) => {
|
|
663
|
+
const answers = await Promise.all(field.access.map((access) => authorizeField(dataset, field.key, access, ctx)));
|
|
664
|
+
return answers.every(Boolean);
|
|
665
|
+
};
|
|
666
|
+
const allowed = await Promise.all(restricted.map(mayUse));
|
|
667
|
+
return restricted.filter((_, index) => !allowed[index]);
|
|
668
|
+
};
|
|
778
669
|
const allowedDatasets = async (ctx) => {
|
|
779
|
-
const
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
670
|
+
const everyTenant = options.tenant(ctx) === allTenants2;
|
|
671
|
+
const views = await Promise.all(datasets.map(async (dataset) => {
|
|
672
|
+
if (!dataset.tenantColumn && !everyTenant)
|
|
673
|
+
return null;
|
|
674
|
+
if (options.authorize && !await options.authorize(dataset, ctx))
|
|
675
|
+
return null;
|
|
676
|
+
const view = withoutFields(dataset, await deniedFields(dataset, ctx));
|
|
677
|
+
return Object.keys(view.measures).length > 0 ? view : null;
|
|
678
|
+
}));
|
|
679
|
+
return views.filter((view) => view !== null);
|
|
784
680
|
};
|
|
785
681
|
const querySchemaForCtx = async (ctx) => {
|
|
786
682
|
const allowed = await allowedDatasets(ctx);
|
|
787
683
|
if (allowed.length === 0) {
|
|
788
684
|
throw new AnalyticsError2("forbidden", "No dataset is allowed");
|
|
789
685
|
}
|
|
790
|
-
return querySchemaFor(allowed
|
|
686
|
+
return querySchemaFor(allowed);
|
|
791
687
|
};
|
|
792
688
|
const prepare = async (input, ctx) => {
|
|
793
|
-
const parsed = querySchemaFor(
|
|
689
|
+
const parsed = querySchemaFor(datasets).safeParse(input);
|
|
794
690
|
if (!parsed.success) {
|
|
795
691
|
throw new AnalyticsError2("invalid_query", `Invalid analytics query: ${z2.prettifyError(parsed.error)}`, parsed.error.issues);
|
|
796
692
|
}
|
|
797
|
-
const
|
|
693
|
+
const parsedQuery = parsed.data;
|
|
694
|
+
const dataset = datasetOf(parsedQuery.dataset);
|
|
798
695
|
if (options.authorize && !await options.authorize(dataset, ctx)) {
|
|
799
696
|
throw new AnalyticsError2("forbidden", `Not allowed to query ${dataset.key}`);
|
|
800
697
|
}
|
|
698
|
+
const dimensionsUsed = [
|
|
699
|
+
...parsedQuery.groupBy ?? [],
|
|
700
|
+
...(parsedQuery.filters ?? []).map((filter) => filter.dimension)
|
|
701
|
+
];
|
|
702
|
+
const refused = (await deniedFields(dataset, ctx)).filter((field) => field.kind === "measure" ? parsedQuery.measures.includes(field.key) : dimensionsUsed.includes(field.key));
|
|
703
|
+
if (refused.length > 0) {
|
|
704
|
+
const names = refused.map((field) => `${field.kind} ${field.key}`);
|
|
705
|
+
throw new AnalyticsError2("forbidden", `Not allowed to use ${names.join(", ")} of ${dataset.key}`);
|
|
706
|
+
}
|
|
801
707
|
const tenant = options.tenant(ctx);
|
|
802
708
|
if (!isTenant(tenant)) {
|
|
803
709
|
throw new AnalyticsError2("missing_tenant", "Refusing to query without a tenant");
|
|
804
710
|
}
|
|
711
|
+
if (!dataset.tenantColumn && tenant !== allTenants2) {
|
|
712
|
+
throw new AnalyticsError2("forbidden", `${dataset.key} has no tenant column; only a caller allowed to see every tenant may query it`);
|
|
713
|
+
}
|
|
805
714
|
const timezone = await options.timezone?.(ctx) || "UTC";
|
|
806
715
|
if (!isValidTimezone(timezone)) {
|
|
807
716
|
throw new AnalyticsError2("invalid_timezone", `Unknown timezone ${timezone}`);
|
|
808
717
|
}
|
|
809
|
-
const query = normalizeQuery(dataset,
|
|
718
|
+
const query = normalizeQuery(dataset, parsedQuery);
|
|
810
719
|
const at = now();
|
|
811
720
|
let period;
|
|
812
721
|
try {
|
|
@@ -820,8 +729,8 @@ function createAnalytics2(options) {
|
|
|
820
729
|
const previous = query.compareToPrevious && period ? previousPeriod(period) : null;
|
|
821
730
|
return { dataset, query, tenant, timezone, period, previous, now: at };
|
|
822
731
|
};
|
|
823
|
-
const runQuery = async (
|
|
824
|
-
const { dataset, query, tenant, timezone, period, previous, now } =
|
|
732
|
+
const runQuery = async (prepared) => {
|
|
733
|
+
const { dataset, query, tenant, timezone, period, previous, now } = prepared;
|
|
825
734
|
const source = sourceFor(dataset);
|
|
826
735
|
const { dialect } = source;
|
|
827
736
|
const run = (statement) => source.execute(dialect.compile(statement));
|
|
@@ -872,21 +781,56 @@ function createAnalytics2(options) {
|
|
|
872
781
|
Promise.resolve(options.onQuery?.(event, ctx)).catch(() => {});
|
|
873
782
|
} catch {}
|
|
874
783
|
};
|
|
784
|
+
const cachedResult = async (key, entry) => {
|
|
785
|
+
try {
|
|
786
|
+
const value = await options.cache?.get(key, entry);
|
|
787
|
+
if (value === null || value === undefined)
|
|
788
|
+
return null;
|
|
789
|
+
const parsed = resultSchemaFor(entry.dataset).safeParse(value);
|
|
790
|
+
return parsed.success ? parsed.data : null;
|
|
791
|
+
} catch {
|
|
792
|
+
return null;
|
|
793
|
+
}
|
|
794
|
+
};
|
|
795
|
+
const store = (key, result, entry) => {
|
|
796
|
+
try {
|
|
797
|
+
Promise.resolve(options.cache?.set(key, result, entry)).catch(() => {});
|
|
798
|
+
} catch {}
|
|
799
|
+
};
|
|
875
800
|
const query = async (input, ctx) => {
|
|
876
801
|
const startedAt = performance.now();
|
|
877
802
|
const dataset = datasetKeyOf(input);
|
|
878
803
|
const durationMs = () => Math.round(performance.now() - startedAt);
|
|
804
|
+
let cache;
|
|
879
805
|
try {
|
|
880
|
-
const
|
|
806
|
+
const prepared = await prepare(input, ctx);
|
|
807
|
+
const entry = { dataset: prepared.dataset.key, ctx };
|
|
808
|
+
const key = options.cache && await cacheKey(prepared);
|
|
809
|
+
let result = key ? await cachedResult(key, entry) : null;
|
|
810
|
+
if (key)
|
|
811
|
+
cache = result ? "hit" : "miss";
|
|
812
|
+
if (!result) {
|
|
813
|
+
result = await runQuery(prepared);
|
|
814
|
+
if (key)
|
|
815
|
+
store(key, result, entry);
|
|
816
|
+
}
|
|
881
817
|
report({
|
|
882
818
|
dataset,
|
|
883
819
|
durationMs: durationMs(),
|
|
884
820
|
rows: result.rows.length,
|
|
885
|
-
outcome: "success"
|
|
821
|
+
outcome: "success",
|
|
822
|
+
...cache && { cache }
|
|
886
823
|
}, ctx);
|
|
887
824
|
return result;
|
|
888
825
|
} catch (error) {
|
|
889
|
-
report({
|
|
826
|
+
report({
|
|
827
|
+
dataset,
|
|
828
|
+
durationMs: durationMs(),
|
|
829
|
+
rows: 0,
|
|
830
|
+
outcome: "error",
|
|
831
|
+
...cache && { cache },
|
|
832
|
+
error
|
|
833
|
+
}, ctx);
|
|
890
834
|
throw error;
|
|
891
835
|
}
|
|
892
836
|
};
|
|
@@ -912,11 +856,29 @@ function createAnalytics2(options) {
|
|
|
912
856
|
datasets: options.datasets,
|
|
913
857
|
query,
|
|
914
858
|
explain,
|
|
915
|
-
querySchema: (schemaOptions) => schemaOptions && ("ctx" in schemaOptions) ? querySchemaForCtx(schemaOptions.ctx) : querySchemaFor(schemaOptions?.datasets),
|
|
859
|
+
querySchema: (schemaOptions) => schemaOptions && ("ctx" in schemaOptions) ? querySchemaForCtx(schemaOptions.ctx) : querySchemaFor(schemaOptions?.datasets?.map(datasetOf) ?? datasets),
|
|
916
860
|
resultSchema: (key) => resultSchemaFor(key),
|
|
917
861
|
listDatasets: async (ctx) => (await allowedDatasets(ctx)).map((dataset) => catalogEntry(dataset))
|
|
918
862
|
};
|
|
919
863
|
}
|
|
864
|
+
async function cacheKey(prepared) {
|
|
865
|
+
const { dataset, query, tenant, timezone, period, previous } = prepared;
|
|
866
|
+
const days = (target) => target && [target.from, target.to];
|
|
867
|
+
const scope = tenant === allTenants2 ? { all: true } : Array.isArray(tenant) ? [...new Set(tenant.map((id) => JSON.stringify(id)))].sort() : tenant;
|
|
868
|
+
const text = stableJson({
|
|
869
|
+
query,
|
|
870
|
+
tenant: scope,
|
|
871
|
+
timezone,
|
|
872
|
+
period: days(period),
|
|
873
|
+
previous: days(previous)
|
|
874
|
+
});
|
|
875
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
|
|
876
|
+
const hex = Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
877
|
+
return `measurr:${dataset.key}:${hex}`;
|
|
878
|
+
}
|
|
879
|
+
function stableJson(value) {
|
|
880
|
+
return JSON.stringify(value, (_key, inner) => inner && typeof inner === "object" && !Array.isArray(inner) ? Object.fromEntries(Object.entries(inner).sort(([a], [b]) => a < b ? -1 : 1)) : inner);
|
|
881
|
+
}
|
|
920
882
|
function isTenant(value) {
|
|
921
883
|
return value === allTenants2 || isTenantId(value) || Array.isArray(value) && value.every(isTenantId);
|
|
922
884
|
}
|
|
@@ -943,7 +905,7 @@ function resolveSource(dataset, sources, sourceNames) {
|
|
|
943
905
|
function compileEverything(dataset, dialect) {
|
|
944
906
|
const now = new Date(0);
|
|
945
907
|
const context = {
|
|
946
|
-
tenant: "tenant",
|
|
908
|
+
tenant: dataset.tenantColumn ? "tenant" : allTenants2,
|
|
947
909
|
timezone: "UTC",
|
|
948
910
|
period: resolvePeriod({ last: { days: 1 } }, "UTC", now),
|
|
949
911
|
now,
|
|
@@ -1009,6 +971,38 @@ function decodeKey2(value, dialect) {
|
|
|
1009
971
|
return value;
|
|
1010
972
|
return dialect.decodeNumber(value) ?? String(value);
|
|
1011
973
|
}
|
|
974
|
+
function restrictedFields(dataset) {
|
|
975
|
+
const measures = dataset.measures;
|
|
976
|
+
const accessOf = (key) => {
|
|
977
|
+
const entry = measures[key];
|
|
978
|
+
if (!entry)
|
|
979
|
+
return [];
|
|
980
|
+
const own = entry.access === undefined ? [] : [entry.access];
|
|
981
|
+
return entry.kind === "ratio" ? [...own, ...accessOf(entry.numerator), ...accessOf(entry.denominator)] : own;
|
|
982
|
+
};
|
|
983
|
+
return [
|
|
984
|
+
...Object.keys(measures).map((key) => ({
|
|
985
|
+
kind: "measure",
|
|
986
|
+
key,
|
|
987
|
+
access: [...new Set(accessOf(key))]
|
|
988
|
+
})),
|
|
989
|
+
...Object.entries(dataset.dimensions).map(([key, entry]) => ({
|
|
990
|
+
kind: "dimension",
|
|
991
|
+
key,
|
|
992
|
+
access: entry.access === undefined ? [] : [entry.access]
|
|
993
|
+
}))
|
|
994
|
+
].filter((field) => field.access.length > 0);
|
|
995
|
+
}
|
|
996
|
+
function withoutFields(dataset, fields) {
|
|
997
|
+
if (fields.length === 0)
|
|
998
|
+
return dataset;
|
|
999
|
+
const keep = (kind, entries) => Object.fromEntries(Object.entries(entries).filter(([key]) => !fields.some((field) => field.kind === kind && field.key === key)));
|
|
1000
|
+
return {
|
|
1001
|
+
...dataset,
|
|
1002
|
+
measures: keep("measure", dataset.measures),
|
|
1003
|
+
dimensions: keep("dimension", dataset.dimensions)
|
|
1004
|
+
};
|
|
1005
|
+
}
|
|
1012
1006
|
function catalogEntry(dataset) {
|
|
1013
1007
|
return {
|
|
1014
1008
|
key: dataset.key,
|
|
@@ -1021,7 +1015,8 @@ function catalogEntry(dataset) {
|
|
|
1021
1015
|
measures: Object.entries(dataset.measures).map(([key, entry]) => ({
|
|
1022
1016
|
key,
|
|
1023
1017
|
label: entry.label,
|
|
1024
|
-
description: entry.description
|
|
1018
|
+
description: entry.description,
|
|
1019
|
+
valueType: entry.kind !== "aggregate" ? "ratio" : entry.node.fn === "count" || entry.node.fn === "countDistinct" ? "count" : "number"
|
|
1025
1020
|
})),
|
|
1026
1021
|
dimensions: Object.entries(dataset.dimensions).map(([key, entry]) => ({
|
|
1027
1022
|
key,
|
|
@@ -1039,290 +1034,4 @@ function catalogEntry(dataset) {
|
|
|
1039
1034
|
};
|
|
1040
1035
|
}
|
|
1041
1036
|
|
|
1042
|
-
|
|
1043
|
-
function aggregate(result, fn, arg, options) {
|
|
1044
|
-
return {
|
|
1045
|
-
kind: "aggregate",
|
|
1046
|
-
result,
|
|
1047
|
-
label: options.label,
|
|
1048
|
-
description: options.description,
|
|
1049
|
-
node: {
|
|
1050
|
-
kind: "aggregate",
|
|
1051
|
-
fn,
|
|
1052
|
-
arg: arg?.node ?? null,
|
|
1053
|
-
where: options.where?.node ?? null
|
|
1054
|
-
}
|
|
1055
|
-
};
|
|
1056
|
-
}
|
|
1057
|
-
var measure2 = {
|
|
1058
|
-
count(options) {
|
|
1059
|
-
return aggregate("count", "count", null, options);
|
|
1060
|
-
},
|
|
1061
|
-
countWhere(condition, options) {
|
|
1062
|
-
return aggregate("count", "count", null, { ...options, where: condition });
|
|
1063
|
-
},
|
|
1064
|
-
countDistinct(expr, options) {
|
|
1065
|
-
return aggregate("count", "countDistinct", expr, options);
|
|
1066
|
-
},
|
|
1067
|
-
sum(expr, options) {
|
|
1068
|
-
return aggregate("number", "sum", expr, options);
|
|
1069
|
-
},
|
|
1070
|
-
avg(expr, options) {
|
|
1071
|
-
return aggregate("number", "avg", expr, options);
|
|
1072
|
-
},
|
|
1073
|
-
median(expr, options) {
|
|
1074
|
-
return aggregate("number", "median", expr, options);
|
|
1075
|
-
},
|
|
1076
|
-
share(options) {
|
|
1077
|
-
return {
|
|
1078
|
-
kind: "share",
|
|
1079
|
-
result: "ratio",
|
|
1080
|
-
label: options.label,
|
|
1081
|
-
description: options.description,
|
|
1082
|
-
numerator: options.numerator.node,
|
|
1083
|
-
denominator: options.denominator?.node ?? null
|
|
1084
|
-
};
|
|
1085
|
-
},
|
|
1086
|
-
ratio(numerator, denominator, options) {
|
|
1087
|
-
return {
|
|
1088
|
-
kind: "ratio",
|
|
1089
|
-
result: "ratio",
|
|
1090
|
-
label: options.label,
|
|
1091
|
-
description: options.description,
|
|
1092
|
-
numerator,
|
|
1093
|
-
denominator
|
|
1094
|
-
};
|
|
1095
|
-
}
|
|
1096
|
-
};
|
|
1097
|
-
function base(options) {
|
|
1098
|
-
return {
|
|
1099
|
-
label: options.label,
|
|
1100
|
-
description: options.description,
|
|
1101
|
-
groupable: options.filterOnly !== true,
|
|
1102
|
-
filterable: options.groupOnly !== true
|
|
1103
|
-
};
|
|
1104
|
-
}
|
|
1105
|
-
function withAccess(_options, dimension) {
|
|
1106
|
-
return dimension;
|
|
1107
|
-
}
|
|
1108
|
-
var dimension2 = {
|
|
1109
|
-
enum(expr, values, options) {
|
|
1110
|
-
for (const value of Object.values(values)) {
|
|
1111
|
-
if (!(value in options.labels)) {
|
|
1112
|
-
throw new Error(`${options.label}: the value ${value} has no label`);
|
|
1113
|
-
}
|
|
1114
|
-
}
|
|
1115
|
-
return withAccess(options, {
|
|
1116
|
-
...base(options),
|
|
1117
|
-
category: "closed",
|
|
1118
|
-
kind: "enum",
|
|
1119
|
-
expr: expr.node,
|
|
1120
|
-
values: options.labels,
|
|
1121
|
-
empty: options.empty
|
|
1122
|
-
});
|
|
1123
|
-
},
|
|
1124
|
-
computed(options) {
|
|
1125
|
-
return withAccess(options, {
|
|
1126
|
-
...base(options),
|
|
1127
|
-
category: "closed",
|
|
1128
|
-
kind: "computed",
|
|
1129
|
-
expr: options.expr.node,
|
|
1130
|
-
values: options.values,
|
|
1131
|
-
empty: options.empty
|
|
1132
|
-
});
|
|
1133
|
-
},
|
|
1134
|
-
relation(options) {
|
|
1135
|
-
return withAccess(options, {
|
|
1136
|
-
...base(options),
|
|
1137
|
-
category: "open",
|
|
1138
|
-
kind: "relation",
|
|
1139
|
-
key: options.key.node,
|
|
1140
|
-
name: options.name.node,
|
|
1141
|
-
empty: options.empty
|
|
1142
|
-
});
|
|
1143
|
-
},
|
|
1144
|
-
manyToMany(options) {
|
|
1145
|
-
return withAccess(options, {
|
|
1146
|
-
...base(options),
|
|
1147
|
-
category: "open",
|
|
1148
|
-
kind: "manyToMany",
|
|
1149
|
-
table: options.through.table.ref,
|
|
1150
|
-
on: options.through.on.node,
|
|
1151
|
-
key: options.through.key.node,
|
|
1152
|
-
name: options.through.name.node,
|
|
1153
|
-
empty: options.empty,
|
|
1154
|
-
note: options.note
|
|
1155
|
-
});
|
|
1156
|
-
},
|
|
1157
|
-
number(expr, options) {
|
|
1158
|
-
return withAccess(options, {
|
|
1159
|
-
...base(options),
|
|
1160
|
-
category: "numeric",
|
|
1161
|
-
kind: "number",
|
|
1162
|
-
expr: expr.node,
|
|
1163
|
-
labelFor: options.labelFor
|
|
1164
|
-
});
|
|
1165
|
-
},
|
|
1166
|
-
timeBucket(time, unit, options) {
|
|
1167
|
-
return withAccess(options, {
|
|
1168
|
-
...base({
|
|
1169
|
-
...options,
|
|
1170
|
-
label: options?.label ?? unit[0]?.toUpperCase() + unit.slice(1)
|
|
1171
|
-
}),
|
|
1172
|
-
category: "time",
|
|
1173
|
-
kind: "timeBucket",
|
|
1174
|
-
time,
|
|
1175
|
-
unit,
|
|
1176
|
-
labelFor: options?.labelFor
|
|
1177
|
-
});
|
|
1178
|
-
},
|
|
1179
|
-
timePart(time, part, options) {
|
|
1180
|
-
return withAccess(options, {
|
|
1181
|
-
...base({
|
|
1182
|
-
...options,
|
|
1183
|
-
label: options?.label ?? (part === "isodow" ? "Weekday" : "Hour")
|
|
1184
|
-
}),
|
|
1185
|
-
category: "numeric",
|
|
1186
|
-
kind: "timePart",
|
|
1187
|
-
time,
|
|
1188
|
-
part,
|
|
1189
|
-
labelFor: options?.labelFor
|
|
1190
|
-
});
|
|
1191
|
-
}
|
|
1192
|
-
};
|
|
1193
|
-
function defineDataset2(definition) {
|
|
1194
|
-
const timeEntries = Object.entries(definition.time);
|
|
1195
|
-
const firstTime = timeEntries[0];
|
|
1196
|
-
if (!firstTime) {
|
|
1197
|
-
throw new Error(`Dataset ${definition.key}: declare at least one time field`);
|
|
1198
|
-
}
|
|
1199
|
-
const time = Object.fromEntries(timeEntries.map(([key, field]) => [
|
|
1200
|
-
key,
|
|
1201
|
-
{ column: field.column.node, label: field.label }
|
|
1202
|
-
]));
|
|
1203
|
-
const dataset = {
|
|
1204
|
-
key: definition.key,
|
|
1205
|
-
label: definition.label,
|
|
1206
|
-
description: definition.description,
|
|
1207
|
-
from: definition.from.ref,
|
|
1208
|
-
source: definition.source,
|
|
1209
|
-
tenantColumn: definition.tenantColumn.node,
|
|
1210
|
-
scope: definition.scope?.node ?? null,
|
|
1211
|
-
time,
|
|
1212
|
-
defaultTime: definition.defaultTime ?? firstTime[0],
|
|
1213
|
-
defaultPeriod: definition.defaultPeriod ?? DEFAULT_PERIOD,
|
|
1214
|
-
measures: definition.measures,
|
|
1215
|
-
dimensions: definition.dimensions
|
|
1216
|
-
};
|
|
1217
|
-
return validateDataset(dataset);
|
|
1218
|
-
}
|
|
1219
|
-
function validateDataset(dataset) {
|
|
1220
|
-
const fail = (message) => {
|
|
1221
|
-
throw new Error(`Dataset ${dataset.key}: ${message}`);
|
|
1222
|
-
};
|
|
1223
|
-
const check = (node, where, extraRoots = []) => {
|
|
1224
|
-
visitScoped(node, [dataset.from, ...extraRoots], (inner, roots) => {
|
|
1225
|
-
if (inner.kind === "column") {
|
|
1226
|
-
const root = rootOf(inner.table);
|
|
1227
|
-
if (!roots.includes(root)) {
|
|
1228
|
-
fail(`${where} reads ${inner.table.name}.${inner.column}, which is not joined to ${dataset.from.name}`);
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
});
|
|
1232
|
-
};
|
|
1233
|
-
check(dataset.tenantColumn, "tenantColumn");
|
|
1234
|
-
if (dataset.scope)
|
|
1235
|
-
check(dataset.scope, "scope");
|
|
1236
|
-
for (const [key, field] of Object.entries(dataset.time)) {
|
|
1237
|
-
check(field.column, `time field ${key}`);
|
|
1238
|
-
}
|
|
1239
|
-
const measures = dataset.measures;
|
|
1240
|
-
for (const [key, entry] of Object.entries(measures)) {
|
|
1241
|
-
if (entry.kind === "aggregate")
|
|
1242
|
-
check(entry.node, `measure ${key}`);
|
|
1243
|
-
if (entry.kind === "share") {
|
|
1244
|
-
check(entry.numerator, `measure ${key}`);
|
|
1245
|
-
if (entry.denominator)
|
|
1246
|
-
check(entry.denominator, `measure ${key}`);
|
|
1247
|
-
}
|
|
1248
|
-
if (entry.kind === "ratio")
|
|
1249
|
-
checkRatio(measures, key, [], fail);
|
|
1250
|
-
}
|
|
1251
|
-
const dimensions = dataset.dimensions;
|
|
1252
|
-
for (const [key, entry] of Object.entries(dimensions)) {
|
|
1253
|
-
const where = `dimension ${key}`;
|
|
1254
|
-
switch (entry.kind) {
|
|
1255
|
-
case "enum":
|
|
1256
|
-
case "number":
|
|
1257
|
-
check(entry.expr, where);
|
|
1258
|
-
break;
|
|
1259
|
-
case "computed":
|
|
1260
|
-
check(entry.expr, where);
|
|
1261
|
-
for (const value of plainCaseValues(entry.expr)) {
|
|
1262
|
-
if (!(value in entry.values)) {
|
|
1263
|
-
fail(`${where} can produce "${value}", which is missing from its values`);
|
|
1264
|
-
}
|
|
1265
|
-
}
|
|
1266
|
-
break;
|
|
1267
|
-
case "relation":
|
|
1268
|
-
check(entry.key, where);
|
|
1269
|
-
check(entry.name, where);
|
|
1270
|
-
break;
|
|
1271
|
-
case "manyToMany":
|
|
1272
|
-
if (entry.table.join) {
|
|
1273
|
-
fail(`${where}: through.table must be a standalone table(), not a join`);
|
|
1274
|
-
}
|
|
1275
|
-
check(entry.on, where, [entry.table]);
|
|
1276
|
-
check(entry.key, where, [entry.table]);
|
|
1277
|
-
check(entry.name, where, [entry.table]);
|
|
1278
|
-
break;
|
|
1279
|
-
case "timeBucket":
|
|
1280
|
-
case "timePart":
|
|
1281
|
-
if (!(entry.time in dataset.time))
|
|
1282
|
-
fail(`${where} names unknown time field ${entry.time}`);
|
|
1283
|
-
break;
|
|
1284
|
-
}
|
|
1285
|
-
}
|
|
1286
|
-
return dataset;
|
|
1287
|
-
}
|
|
1288
|
-
function checkRatio(measures, key, path, fail) {
|
|
1289
|
-
if (path.includes(key))
|
|
1290
|
-
fail(`ratio measures form a cycle: ${[...path, key].join(" -> ")}`);
|
|
1291
|
-
const entry = measures[key];
|
|
1292
|
-
if (!entry)
|
|
1293
|
-
fail(`ratio measure ${path.at(-1)} names unknown measure ${key}`);
|
|
1294
|
-
if (entry?.kind !== "ratio")
|
|
1295
|
-
return;
|
|
1296
|
-
checkRatio(measures, entry.numerator, [...path, key], fail);
|
|
1297
|
-
checkRatio(measures, entry.denominator, [...path, key], fail);
|
|
1298
|
-
}
|
|
1299
|
-
function visitScoped(node, roots, visit, seenJoins = new Set) {
|
|
1300
|
-
visit(node, roots);
|
|
1301
|
-
if (node.kind === "exists") {
|
|
1302
|
-
visitScoped(node.where, [...roots, node.table], visit, seenJoins);
|
|
1303
|
-
return;
|
|
1304
|
-
}
|
|
1305
|
-
if (node.kind === "column" && node.table.join && !seenJoins.has(node.table)) {
|
|
1306
|
-
seenJoins.add(node.table);
|
|
1307
|
-
visitScoped(node.table.join.on, roots, visit, seenJoins);
|
|
1308
|
-
return;
|
|
1309
|
-
}
|
|
1310
|
-
for (const child of directChildren(node)) {
|
|
1311
|
-
visitScoped(child, roots, visit, seenJoins);
|
|
1312
|
-
}
|
|
1313
|
-
}
|
|
1314
|
-
function plainCaseValues(node) {
|
|
1315
|
-
if (node.kind === "param" && typeof node.value === "string")
|
|
1316
|
-
return [node.value];
|
|
1317
|
-
if (node.kind === "case") {
|
|
1318
|
-
return [
|
|
1319
|
-
...node.branches.flatMap((branch) => plainCaseValues(branch.result)),
|
|
1320
|
-
...node.otherwise ? plainCaseValues(node.otherwise) : []
|
|
1321
|
-
];
|
|
1322
|
-
}
|
|
1323
|
-
if (node.kind === "coalesce")
|
|
1324
|
-
return node.items.flatMap(plainCaseValues);
|
|
1325
|
-
return [];
|
|
1326
|
-
}
|
|
1327
|
-
|
|
1328
|
-
export { MAX_MEASURES, AnalyticsResultSchema2, allTenants2, AnalyticsError2, isAnalyticsError2, createAnalytics2, measure2, dimension2, defineDataset2 };
|
|
1037
|
+
export { MAX_MEASURES, AnalyticsResultSchema2, allTenants2, AnalyticsError2, isAnalyticsError2, createAnalytics2 };
|