@10x-media/analytics 0.1.0-beta.0 → 0.1.0-beta.2
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/CHANGELOG.md +127 -0
- package/README.md +38 -357
- package/dist/adapters/ga4/ga4.d.ts +20 -0
- package/dist/adapters/ga4/ga4.js +187 -0
- package/dist/adapters/ga4/ga4.js.map +1 -0
- package/dist/{fetchJson-DZb2_Puj.js → adapters/http/fetchJson.js} +2 -2
- package/dist/adapters/http/fetchJson.js.map +1 -0
- package/dist/adapters/plausible/plausible.d.ts +15 -0
- package/dist/adapters/plausible/plausible.js +148 -0
- package/dist/adapters/plausible/plausible.js.map +1 -0
- package/dist/adapters/posthog/posthog.d.ts +23 -0
- package/dist/adapters/posthog/posthog.js +132 -0
- package/dist/adapters/posthog/posthog.js.map +1 -0
- package/dist/{series-zytbzPoc.js → adapters/series.js} +2 -2
- package/dist/adapters/series.js.map +1 -0
- package/dist/adapters/umami/umami.d.ts +18 -0
- package/dist/adapters/umami/umami.js +139 -0
- package/dist/{exports/adapters → adapters/umami}/umami.js.map +1 -1
- package/dist/binding/resolvePath.js +61 -0
- package/dist/binding/resolvePath.js.map +1 -0
- package/dist/binding/types.d.ts +55 -0
- package/dist/charts/BarList.d.ts +20 -0
- package/dist/charts/BarList.js +73 -0
- package/dist/charts/BarList.js.map +1 -0
- package/dist/charts/TrendChart.d.ts +25 -0
- package/dist/charts/TrendChart.js +195 -0
- package/dist/charts/TrendChart.js.map +1 -0
- package/dist/charts/bars.d.ts +9 -0
- package/dist/charts/bars.js +19 -0
- package/dist/charts/bars.js.map +1 -0
- package/dist/charts/bucket.js +86 -0
- package/dist/charts/bucket.js.map +1 -0
- package/dist/charts/spline.js +84 -0
- package/dist/charts/spline.js.map +1 -0
- package/dist/charts/styles.js +42 -0
- package/dist/charts/styles.js.map +1 -0
- package/dist/core/cacheKey.js +28 -0
- package/dist/core/cacheKey.js.map +1 -0
- package/dist/core/capabilities.d.ts +12 -0
- package/dist/core/capabilities.js +12 -0
- package/dist/core/capabilities.js.map +1 -0
- package/dist/{contract-CWZPKWuR.d.ts → core/contract.d.ts} +42 -5
- package/dist/core/contract.js +10 -0
- package/dist/core/contract.js.map +1 -0
- package/dist/core/granularity.js +17 -0
- package/dist/core/granularity.js.map +1 -0
- package/dist/core/options.d.ts +140 -0
- package/dist/core/options.js +114 -0
- package/dist/core/options.js.map +1 -0
- package/dist/core/registry.js +24 -0
- package/dist/core/registry.js.map +1 -0
- package/dist/core/scopedRead.js +57 -0
- package/dist/core/scopedRead.js.map +1 -0
- package/dist/exports/adapters/ga4.d.ts +2 -20
- package/dist/exports/adapters/ga4.js +1 -176
- package/dist/exports/adapters/native.d.ts +2 -22
- package/dist/exports/adapters/native.js +1 -848
- package/dist/exports/adapters/plausible.d.ts +2 -15
- package/dist/exports/adapters/plausible.js +1 -142
- package/dist/exports/adapters/posthog.d.ts +2 -17
- package/dist/exports/adapters/posthog.js +1 -118
- package/dist/exports/adapters/umami.d.ts +2 -18
- package/dist/exports/adapters/umami.js +1 -138
- package/dist/exports/client.d.ts +5 -77
- package/dist/exports/client.js +4 -386
- package/dist/exports/geo.d.ts +4 -19
- package/dist/exports/geo.js +3 -1
- package/dist/exports/i18n.d.ts +3 -63
- package/dist/exports/i18n.js +2 -2
- package/dist/exports/next.d.ts +2 -0
- package/dist/exports/next.js +2 -0
- package/dist/exports/rsc.d.ts +16 -120
- package/dist/exports/rsc.js +14 -598
- package/dist/exports/types.d.ts +6 -4
- package/dist/exports/types.js +2 -1
- package/dist/fields/AnalyticsPanelClient.js +193 -0
- package/dist/fields/AnalyticsPanelClient.js.map +1 -0
- package/dist/fields/AnalyticsPanelField.d.ts +28 -0
- package/dist/fields/AnalyticsPanelField.js +54 -0
- package/dist/fields/AnalyticsPanelField.js.map +1 -0
- package/dist/fields/AnalyticsStatField.d.ts +31 -0
- package/dist/fields/AnalyticsStatField.js +104 -0
- package/dist/fields/AnalyticsStatField.js.map +1 -0
- package/dist/fields/dateRange/field.js +55 -0
- package/dist/fields/dateRange/field.js.map +1 -0
- package/dist/fields/dateRange/styles.js +23 -0
- package/dist/fields/dateRange/styles.js.map +1 -0
- package/dist/fields/dateRange/types.d.ts +11 -0
- package/dist/fields/dateRange/ui.d.ts +9 -0
- package/dist/fields/dateRange/ui.js +373 -0
- package/dist/fields/dateRange/ui.js.map +1 -0
- package/dist/fields/emptyState.js +46 -0
- package/dist/fields/emptyState.js.map +1 -0
- package/dist/fields/factories.d.ts +60 -0
- package/dist/fields/factories.js +94 -0
- package/dist/fields/factories.js.map +1 -0
- package/dist/fields/format.d.ts +7 -0
- package/dist/fields/format.js +22 -0
- package/dist/fields/format.js.map +1 -0
- package/dist/fields/readForDocument.d.ts +57 -0
- package/dist/fields/readForDocument.js +147 -0
- package/dist/fields/readForDocument.js.map +1 -0
- package/dist/index.d.ts +18 -3
- package/dist/index.js +83 -1028
- package/dist/index.js.map +1 -1
- package/dist/native/collections/events.js +101 -0
- package/dist/native/collections/events.js.map +1 -0
- package/dist/native/collections/rollups.js +115 -0
- package/dist/native/collections/rollups.js.map +1 -0
- package/dist/native/collections/seen.js +47 -0
- package/dist/native/collections/seen.js.map +1 -0
- package/dist/native/geo/composeGeoResolvers.d.ts +7 -0
- package/dist/native/geo/composeGeoResolvers.js +14 -0
- package/dist/native/geo/composeGeoResolvers.js.map +1 -0
- package/dist/{geoResolver-CasIa4T9.d.ts → native/geo/geoResolver.d.ts} +2 -2
- package/dist/native/geo/geoResolver.js +11 -0
- package/dist/native/geo/geoResolver.js.map +1 -0
- package/dist/native/geo/maxmindResolver.d.ts +16 -0
- package/dist/native/geo/maxmindResolver.js +55 -0
- package/dist/native/geo/maxmindResolver.js.map +1 -0
- package/dist/native/ingest/device.js +15 -0
- package/dist/native/ingest/device.js.map +1 -0
- package/dist/native/ingest/endpoint.js +30 -0
- package/dist/native/ingest/endpoint.js.map +1 -0
- package/dist/native/ingest/flushBatch.js +69 -0
- package/dist/native/ingest/flushBatch.js.map +1 -0
- package/dist/native/ingest/normalizeEvent.js +39 -0
- package/dist/native/ingest/normalizeEvent.js.map +1 -0
- package/dist/native/ingest/salt.js +14 -0
- package/dist/native/ingest/salt.js.map +1 -0
- package/dist/native/ingest/source.js +22 -0
- package/dist/native/ingest/source.js.map +1 -0
- package/dist/native/ingest/visitorHash.js +12 -0
- package/dist/native/ingest/visitorHash.js.map +1 -0
- package/dist/native/ingest/writeBuffer.js +55 -0
- package/dist/native/ingest/writeBuffer.js.map +1 -0
- package/dist/native/ingest/writeEvent.js +14 -0
- package/dist/native/ingest/writeEvent.js.map +1 -0
- package/dist/native/nativeAdapter.d.ts +22 -0
- package/dist/native/nativeAdapter.js +216 -0
- package/dist/native/nativeAdapter.js.map +1 -0
- package/dist/native/realtime/buildRealtime.js +53 -0
- package/dist/native/realtime/buildRealtime.js.map +1 -0
- package/dist/native/retention/pruneTask.js +29 -0
- package/dist/native/retention/pruneTask.js.map +1 -0
- package/dist/native/rollupAcc.js +53 -0
- package/dist/native/rollupAcc.js.map +1 -0
- package/dist/native/rollups/applyDistinctDeltas.js +9 -0
- package/dist/native/rollups/applyDistinctDeltas.js.map +1 -0
- package/dist/native/rollups/bumpRollup.js +52 -0
- package/dist/native/rollups/bumpRollup.js.map +1 -0
- package/dist/native/rollups/deltas.js +31 -0
- package/dist/native/rollups/deltas.js.map +1 -0
- package/dist/native/rollups/insertIfNew.js +19 -0
- package/dist/native/rollups/insertIfNew.js.map +1 -0
- package/dist/next/posthogProxyRewrites.d.ts +36 -0
- package/dist/next/posthogProxyRewrites.js +50 -0
- package/dist/next/posthogProxyRewrites.js.map +1 -0
- package/dist/plugin/documentEndpoint.js +80 -0
- package/dist/plugin/documentEndpoint.js.map +1 -0
- package/dist/plugin/paths.js +11 -0
- package/dist/plugin/paths.js.map +1 -0
- package/dist/plugin/realtimeEndpoint.js +36 -0
- package/dist/plugin/realtimeEndpoint.js.map +1 -0
- package/dist/plugin/registerTranslations.js +19 -0
- package/dist/plugin/registerTranslations.js.map +1 -0
- package/dist/plugin/runtime.js +21 -0
- package/dist/plugin/runtime.js.map +1 -0
- package/dist/plugin/warmTask.js +165 -0
- package/dist/plugin/warmTask.js.map +1 -0
- package/dist/providers/collection.js +159 -0
- package/dist/providers/collection.js.map +1 -0
- package/dist/providers/factory.js +62 -0
- package/dist/providers/factory.js.map +1 -0
- package/dist/providers/resolver.js +67 -0
- package/dist/providers/resolver.js.map +1 -0
- package/dist/providers/secrets.js +27 -0
- package/dist/providers/secrets.js.map +1 -0
- package/dist/surfacing/cacheStore.js +26 -0
- package/dist/surfacing/cacheStore.js.map +1 -0
- package/dist/surfacing/coalesce.js +15 -0
- package/dist/surfacing/coalesce.js.map +1 -0
- package/dist/surfacing/engine.js +63 -0
- package/dist/surfacing/engine.js.map +1 -0
- package/dist/surfacing/queue.js +44 -0
- package/dist/surfacing/queue.js.map +1 -0
- package/dist/sync/collection.js +70 -0
- package/dist/sync/collection.js.map +1 -0
- package/dist/sync/syncTask.js +123 -0
- package/dist/sync/syncTask.js.map +1 -0
- package/dist/testing/memoryAdapter.d.ts +1 -1
- package/dist/timeframe/presets.d.ts +6 -0
- package/dist/timeframe/presets.js +59 -0
- package/dist/timeframe/presets.js.map +1 -0
- package/dist/timeframe/tz.js +110 -0
- package/dist/timeframe/tz.js.map +1 -0
- package/dist/translations/de.js +78 -0
- package/dist/translations/de.js.map +1 -0
- package/dist/{translations-keiKtSjF.js → translations/en.js} +30 -25
- package/dist/translations/en.js.map +1 -0
- package/dist/translations/index.d.ts +15 -0
- package/dist/translations/index.js +30 -0
- package/dist/translations/index.js.map +1 -0
- package/dist/translations/keys.d.ts +81 -0
- package/dist/{keys-DEqL4LOT.js → translations/keys.js} +29 -5
- package/dist/translations/keys.js.map +1 -0
- package/dist/translations/metricKeys.js +28 -0
- package/dist/translations/metricKeys.js.map +1 -0
- package/dist/translations/server.js +13 -0
- package/dist/translations/server.js.map +1 -0
- package/dist/translations/useTranslation.js +12 -0
- package/dist/translations/useTranslation.js.map +1 -0
- package/dist/widgets/AnalyticsBreakdownWidget.d.ts +7 -0
- package/dist/widgets/AnalyticsBreakdownWidget.js +85 -0
- package/dist/widgets/AnalyticsBreakdownWidget.js.map +1 -0
- package/dist/widgets/AnalyticsMetricWidget.d.ts +7 -0
- package/dist/widgets/AnalyticsMetricWidget.js +89 -0
- package/dist/widgets/AnalyticsMetricWidget.js.map +1 -0
- package/dist/widgets/AnalyticsRealtimeWidget.d.ts +7 -0
- package/dist/widgets/AnalyticsRealtimeWidget.js +64 -0
- package/dist/widgets/AnalyticsRealtimeWidget.js.map +1 -0
- package/dist/widgets/AnalyticsTrendWidget.d.ts +7 -0
- package/dist/widgets/AnalyticsTrendWidget.js +99 -0
- package/dist/widgets/AnalyticsTrendWidget.js.map +1 -0
- package/dist/widgets/ComparisonDelta.js +83 -0
- package/dist/widgets/ComparisonDelta.js.map +1 -0
- package/dist/widgets/RealtimeCounter.d.ts +19 -0
- package/dist/widgets/RealtimeCounter.js +76 -0
- package/dist/widgets/RealtimeCounter.js.map +1 -0
- package/dist/widgets/breakdownTypes.js +29 -0
- package/dist/widgets/breakdownTypes.js.map +1 -0
- package/dist/widgets/cardChrome.d.ts +8 -0
- package/dist/widgets/cardChrome.js +22 -0
- package/dist/widgets/cardChrome.js.map +1 -0
- package/dist/widgets/comparison.js +43 -0
- package/dist/widgets/comparison.js.map +1 -0
- package/dist/widgets/customWidget.d.ts +20 -0
- package/dist/widgets/customWidget.js +30 -0
- package/dist/widgets/customWidget.js.map +1 -0
- package/dist/widgets/defaults.d.ts +12 -0
- package/dist/widgets/defaults.js +72 -0
- package/dist/widgets/defaults.js.map +1 -0
- package/dist/widgets/range.js +28 -0
- package/dist/widgets/range.js.map +1 -0
- package/dist/widgets/readForWidget.d.ts +31 -0
- package/dist/widgets/readForWidget.js +72 -0
- package/dist/widgets/readForWidget.js.map +1 -0
- package/dist/widgets/readForWidgetBreakdown.d.ts +38 -0
- package/dist/widgets/readForWidgetBreakdown.js +80 -0
- package/dist/widgets/readForWidgetBreakdown.js.map +1 -0
- package/dist/{readForWidgetRealtime-D1BD37ST.d.ts → widgets/readForWidgetRealtime.d.ts} +5 -3
- package/dist/widgets/readForWidgetRealtime.js +63 -0
- package/dist/widgets/readForWidgetRealtime.js.map +1 -0
- package/dist/widgets/readForWidgetSeries.d.ts +44 -0
- package/dist/widgets/readForWidgetSeries.js +117 -0
- package/dist/widgets/readForWidgetSeries.js.map +1 -0
- package/dist/widgets/realtimePoll.js +26 -0
- package/dist/widgets/realtimePoll.js.map +1 -0
- package/dist/widgets/registerWidgets.js +228 -0
- package/dist/widgets/registerWidgets.js.map +1 -0
- package/dist/widgets/types.js +13 -0
- package/dist/widgets/types.js.map +1 -0
- package/package.json +19 -5
- package/dist/RealtimeCounter-BJrjMPSa.js +0 -473
- package/dist/RealtimeCounter-BJrjMPSa.js.map +0 -1
- package/dist/dist-B6HruP5v.js +0 -4731
- package/dist/dist-B6HruP5v.js.map +0 -1
- package/dist/exports/adapters/ga4.js.map +0 -1
- package/dist/exports/adapters/native.js.map +0 -1
- package/dist/exports/adapters/plausible.js.map +0 -1
- package/dist/exports/adapters/posthog.js.map +0 -1
- package/dist/exports/client.js.map +0 -1
- package/dist/exports/rsc.js.map +0 -1
- package/dist/fetchJson-DZb2_Puj.js.map +0 -1
- package/dist/index-CZdCT0WJ.d.ts +0 -98
- package/dist/keys-DEqL4LOT.js.map +0 -1
- package/dist/lib-CE5x1zLQ.js +0 -1058
- package/dist/lib-CE5x1zLQ.js.map +0 -1
- package/dist/maxmindResolver-CXYXXdul.js +0 -99
- package/dist/maxmindResolver-CXYXXdul.js.map +0 -1
- package/dist/presets-CBfq_uJX.d.ts +0 -38
- package/dist/series-zytbzPoc.js.map +0 -1
- package/dist/server-Cuvhgz-S.js +0 -492
- package/dist/server-Cuvhgz-S.js.map +0 -1
- package/dist/translations-keiKtSjF.js.map +0 -1
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
//#region src/widgets/defaults.ts
|
|
2
|
+
/**
|
|
3
|
+
* Default analytics widget instances to spread into an app's own
|
|
4
|
+
* `admin.dashboard.defaultLayout`. A plugin must never set `defaultLayout` itself
|
|
5
|
+
* (Payload applies it with `??=`, so any setter would clobber the app's layout).
|
|
6
|
+
*/
|
|
7
|
+
const analyticsDefaultWidgets = () => [
|
|
8
|
+
{
|
|
9
|
+
widgetSlug: "analytics-trend",
|
|
10
|
+
width: "large",
|
|
11
|
+
data: {
|
|
12
|
+
metric: "pageviews",
|
|
13
|
+
timeframe: "last30days"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
widgetSlug: "analytics-metric",
|
|
18
|
+
width: "small",
|
|
19
|
+
data: {
|
|
20
|
+
metric: "pageviews",
|
|
21
|
+
timeframe: "last30days"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
widgetSlug: "analytics-metric",
|
|
26
|
+
width: "small",
|
|
27
|
+
data: {
|
|
28
|
+
metric: "visitors",
|
|
29
|
+
timeframe: "last30days"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
widgetSlug: "analytics-breakdown-pages",
|
|
34
|
+
width: "medium",
|
|
35
|
+
data: {
|
|
36
|
+
metric: "pageviews",
|
|
37
|
+
timeframe: "last30days",
|
|
38
|
+
limit: 5
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
widgetSlug: "analytics-breakdown-sources",
|
|
43
|
+
width: "medium",
|
|
44
|
+
data: {
|
|
45
|
+
metric: "pageviews",
|
|
46
|
+
timeframe: "last30days",
|
|
47
|
+
limit: 5
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
widgetSlug: "analytics-breakdown-devices",
|
|
52
|
+
width: "medium",
|
|
53
|
+
data: {
|
|
54
|
+
metric: "pageviews",
|
|
55
|
+
timeframe: "last30days",
|
|
56
|
+
limit: 5
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
widgetSlug: "analytics-breakdown-countries",
|
|
61
|
+
width: "medium",
|
|
62
|
+
data: {
|
|
63
|
+
metric: "pageviews",
|
|
64
|
+
timeframe: "last30days",
|
|
65
|
+
limit: 5
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
];
|
|
69
|
+
//#endregion
|
|
70
|
+
export { analyticsDefaultWidgets };
|
|
71
|
+
|
|
72
|
+
//# sourceMappingURL=defaults.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaults.js","names":[],"sources":["../../src/widgets/defaults.ts"],"sourcesContent":["import type { WidgetInstance } from 'payload'\n\n/**\n * Default analytics widget instances to spread into an app's own\n * `admin.dashboard.defaultLayout`. A plugin must never set `defaultLayout` itself\n * (Payload applies it with `??=`, so any setter would clobber the app's layout).\n */\nexport const analyticsDefaultWidgets = (): WidgetInstance[] => [\n\t{\n\t\twidgetSlug: 'analytics-trend',\n\t\twidth: 'large',\n\t\tdata: { metric: 'pageviews', timeframe: 'last30days' },\n\t},\n\t{\n\t\twidgetSlug: 'analytics-metric',\n\t\twidth: 'small',\n\t\tdata: { metric: 'pageviews', timeframe: 'last30days' },\n\t},\n\t{\n\t\twidgetSlug: 'analytics-metric',\n\t\twidth: 'small',\n\t\tdata: { metric: 'visitors', timeframe: 'last30days' },\n\t},\n\t{\n\t\twidgetSlug: 'analytics-breakdown-pages',\n\t\twidth: 'medium',\n\t\tdata: { metric: 'pageviews', timeframe: 'last30days', limit: 5 },\n\t},\n\t{\n\t\twidgetSlug: 'analytics-breakdown-sources',\n\t\twidth: 'medium',\n\t\tdata: { metric: 'pageviews', timeframe: 'last30days', limit: 5 },\n\t},\n\t{\n\t\twidgetSlug: 'analytics-breakdown-devices',\n\t\twidth: 'medium',\n\t\tdata: { metric: 'pageviews', timeframe: 'last30days', limit: 5 },\n\t},\n\t{\n\t\twidgetSlug: 'analytics-breakdown-countries',\n\t\twidth: 'medium',\n\t\tdata: { metric: 'pageviews', timeframe: 'last30days', limit: 5 },\n\t},\n]\n"],"mappings":";;;;;;AAOA,MAAa,gCAAkD;CAC9D;EACC,YAAY;EACZ,OAAO;EACP,MAAM;GAAE,QAAQ;GAAa,WAAW;EAAa;CACtD;CACA;EACC,YAAY;EACZ,OAAO;EACP,MAAM;GAAE,QAAQ;GAAa,WAAW;EAAa;CACtD;CACA;EACC,YAAY;EACZ,OAAO;EACP,MAAM;GAAE,QAAQ;GAAY,WAAW;EAAa;CACrD;CACA;EACC,YAAY;EACZ,OAAO;EACP,MAAM;GAAE,QAAQ;GAAa,WAAW;GAAc,OAAO;EAAE;CAChE;CACA;EACC,YAAY;EACZ,OAAO;EACP,MAAM;GAAE,QAAQ;GAAa,WAAW;GAAc,OAAO;EAAE;CAChE;CACA;EACC,YAAY;EACZ,OAAO;EACP,MAAM;GAAE,QAAQ;GAAa,WAAW;GAAc,OAAO;EAAE;CAChE;CACA;EACC,YAAY;EACZ,OAAO;EACP,MAAM;GAAE,QAAQ;GAAa,WAAW;GAAc,OAAO;EAAE;CAChE;AACD"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//#region src/widgets/range.ts
|
|
2
|
+
/**
|
|
3
|
+
* Resolve a widget's explicit custom range to a concrete DateRange, or undefined when
|
|
4
|
+
* the widget is on a relative preset, the custom range is incomplete, or either bound
|
|
5
|
+
* is an unparseable date (a corrupt or hand-set widgetData would otherwise reach the
|
|
6
|
+
* cache key and throw on `toISOString`).
|
|
7
|
+
*/
|
|
8
|
+
const resolveCustomRange = (timeframe, range) => {
|
|
9
|
+
if (timeframe !== "custom" || !range?.from || !range?.to) return;
|
|
10
|
+
const start = new Date(range.from);
|
|
11
|
+
const end = new Date(range.to);
|
|
12
|
+
if (Number.isNaN(start.getTime()) || Number.isNaN(end.getTime())) return;
|
|
13
|
+
return {
|
|
14
|
+
start,
|
|
15
|
+
end
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
const formatDay = (d, locale) => d.toLocaleDateString(locale, {
|
|
19
|
+
month: "short",
|
|
20
|
+
day: "numeric",
|
|
21
|
+
year: "numeric"
|
|
22
|
+
});
|
|
23
|
+
/** Server-side caption for a custom range, e.g. "Jun 1, 2026 - Jun 23, 2026". */
|
|
24
|
+
const formatRangeCaption = (range, locale) => `${formatDay(range.start, locale)} - ${formatDay(range.end, locale)}`;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { formatRangeCaption, resolveCustomRange };
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=range.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"range.js","names":[],"sources":["../../src/widgets/range.ts"],"sourcesContent":["import type { DateRange } from '../core/contract'\nimport type { WidgetRange } from './types'\n\n/**\n * Resolve a widget's explicit custom range to a concrete DateRange, or undefined when\n * the widget is on a relative preset, the custom range is incomplete, or either bound\n * is an unparseable date (a corrupt or hand-set widgetData would otherwise reach the\n * cache key and throw on `toISOString`).\n */\nexport const resolveCustomRange = (\n\ttimeframe: string | undefined,\n\trange: WidgetRange | undefined\n): DateRange | undefined => {\n\tif (timeframe !== 'custom' || !range?.from || !range?.to) {\n\t\treturn undefined\n\t}\n\tconst start = new Date(range.from)\n\tconst end = new Date(range.to)\n\tif (Number.isNaN(start.getTime()) || Number.isNaN(end.getTime())) {\n\t\treturn undefined\n\t}\n\treturn { start, end }\n}\n\nconst formatDay = (d: Date, locale: string): string =>\n\td.toLocaleDateString(locale, { month: 'short', day: 'numeric', year: 'numeric' })\n\n/** Server-side caption for a custom range, e.g. \"Jun 1, 2026 - Jun 23, 2026\". */\nexport const formatRangeCaption = (range: DateRange, locale: string): string =>\n\t`${formatDay(range.start, locale)} - ${formatDay(range.end, locale)}`\n"],"mappings":";;;;;;;AASA,MAAa,sBACZ,WACA,UAC2B;CAC3B,IAAI,cAAc,YAAY,CAAC,OAAO,QAAQ,CAAC,OAAO,IACrD;CAED,MAAM,QAAQ,IAAI,KAAK,MAAM,IAAI;CACjC,MAAM,MAAM,IAAI,KAAK,MAAM,EAAE;CAC7B,IAAI,OAAO,MAAM,MAAM,QAAQ,CAAC,KAAK,OAAO,MAAM,IAAI,QAAQ,CAAC,GAC9D;CAED,OAAO;EAAE;EAAO;CAAI;AACrB;AAEA,MAAM,aAAa,GAAS,WAC3B,EAAE,mBAAmB,QAAQ;CAAE,OAAO;CAAS,KAAK;CAAW,MAAM;AAAU,CAAC;;AAGjF,MAAa,sBAAsB,OAAkB,WACpD,GAAG,UAAU,MAAM,OAAO,MAAM,EAAE,KAAK,UAAU,MAAM,KAAK,MAAM"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { DateRange, MetricKey } from "../core/contract.js";
|
|
2
|
+
import { TimeframePreset } from "../timeframe/presets.js";
|
|
3
|
+
import { PayloadRequest } from "payload";
|
|
4
|
+
|
|
5
|
+
//#region src/widgets/readForWidget.d.ts
|
|
6
|
+
type WidgetReadStatus = 'ok' | 'not-configured' | 'unavailable';
|
|
7
|
+
interface WidgetReadResult {
|
|
8
|
+
status: WidgetReadStatus;
|
|
9
|
+
adapterId: string;
|
|
10
|
+
dateRange: DateRange;
|
|
11
|
+
metrics: Partial<Record<MetricKey, number>>;
|
|
12
|
+
clamped?: boolean;
|
|
13
|
+
/** Previous-window totals, present only when the adapter supports comparison. */
|
|
14
|
+
previousMetrics?: Partial<Record<MetricKey, number>>;
|
|
15
|
+
/** The previous comparable window, present only when comparison ran. */
|
|
16
|
+
comparisonRange?: DateRange;
|
|
17
|
+
}
|
|
18
|
+
interface ReadForWidgetArgs {
|
|
19
|
+
req: PayloadRequest;
|
|
20
|
+
metrics: MetricKey[];
|
|
21
|
+
timeframe: TimeframePreset;
|
|
22
|
+
adapterId?: string;
|
|
23
|
+
now: Date;
|
|
24
|
+
range?: DateRange;
|
|
25
|
+
/** Explicit scope override; omitted resolves via the plugin's scopeResolver. */
|
|
26
|
+
scope?: string | null;
|
|
27
|
+
}
|
|
28
|
+
declare const readForWidget: (args: ReadForWidgetArgs) => Promise<WidgetReadResult>;
|
|
29
|
+
//#endregion
|
|
30
|
+
export { ReadForWidgetArgs, WidgetReadResult, WidgetReadStatus, readForWidget };
|
|
31
|
+
//# sourceMappingURL=readForWidget.d.ts.map
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { satisfiesCapabilities } from "../core/capabilities.js";
|
|
2
|
+
import { getRuntime, resolveTimezoneFor } from "../plugin/runtime.js";
|
|
3
|
+
import { resolveReadContext } from "../core/scopedRead.js";
|
|
4
|
+
import { resolveTimeframe } from "../timeframe/presets.js";
|
|
5
|
+
import { previousWindow } from "./comparison.js";
|
|
6
|
+
//#region src/widgets/readForWidget.ts
|
|
7
|
+
const readForWidget = async (args) => {
|
|
8
|
+
const { req, metrics, timeframe, adapterId, now, range } = args;
|
|
9
|
+
const emptyMetrics = {};
|
|
10
|
+
const runtime = getRuntime(req.payload);
|
|
11
|
+
if (!runtime) return {
|
|
12
|
+
status: "unavailable",
|
|
13
|
+
adapterId: adapterId ?? "",
|
|
14
|
+
dateRange: range ?? resolveTimeframe(timeframe, now),
|
|
15
|
+
metrics: emptyMetrics
|
|
16
|
+
};
|
|
17
|
+
const ctx = await resolveReadContext({
|
|
18
|
+
runtime,
|
|
19
|
+
req,
|
|
20
|
+
adapterId,
|
|
21
|
+
scope: args.scope
|
|
22
|
+
});
|
|
23
|
+
if (!ctx.ok) return {
|
|
24
|
+
status: "unavailable",
|
|
25
|
+
adapterId: adapterId ?? "",
|
|
26
|
+
dateRange: range ?? resolveTimeframe(timeframe, now),
|
|
27
|
+
metrics: emptyMetrics
|
|
28
|
+
};
|
|
29
|
+
const tz = await resolveTimezoneFor(runtime, req, ctx.scope);
|
|
30
|
+
const dateRange = range ?? resolveTimeframe(timeframe, now, tz);
|
|
31
|
+
const base = {
|
|
32
|
+
dateRange,
|
|
33
|
+
metrics: emptyMetrics
|
|
34
|
+
};
|
|
35
|
+
const adapter = ctx.adapter;
|
|
36
|
+
if (!adapter.isConfigured()) return {
|
|
37
|
+
status: "not-configured",
|
|
38
|
+
adapterId: adapter.id,
|
|
39
|
+
...base
|
|
40
|
+
};
|
|
41
|
+
if (!satisfiesCapabilities(adapter.capabilities, { metrics })) return {
|
|
42
|
+
status: "unavailable",
|
|
43
|
+
adapterId: adapter.id,
|
|
44
|
+
...base
|
|
45
|
+
};
|
|
46
|
+
const comparisonRange = runtime.comparison && adapter.capabilities.comparison ? previousWindow(dateRange, tz) ?? void 0 : void 0;
|
|
47
|
+
const [result, previous] = await Promise.all([runtime.engine.read(adapter, {
|
|
48
|
+
metrics,
|
|
49
|
+
dateRange,
|
|
50
|
+
timezone: tz,
|
|
51
|
+
scope: ctx.queryScope
|
|
52
|
+
}), comparisonRange ? runtime.engine.read(adapter, {
|
|
53
|
+
metrics,
|
|
54
|
+
dateRange: comparisonRange,
|
|
55
|
+
timezone: tz,
|
|
56
|
+
scope: ctx.queryScope
|
|
57
|
+
}) : void 0]);
|
|
58
|
+
const previousMetrics = previous ? previous.totals ?? {} : void 0;
|
|
59
|
+
return {
|
|
60
|
+
status: "ok",
|
|
61
|
+
adapterId: adapter.id,
|
|
62
|
+
dateRange,
|
|
63
|
+
metrics: result.totals ?? {},
|
|
64
|
+
clamped: result.meta.clamped ?? false,
|
|
65
|
+
previousMetrics,
|
|
66
|
+
comparisonRange
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
//#endregion
|
|
70
|
+
export { readForWidget };
|
|
71
|
+
|
|
72
|
+
//# sourceMappingURL=readForWidget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readForWidget.js","names":[],"sources":["../../src/widgets/readForWidget.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\nimport { satisfiesCapabilities } from '../core/capabilities'\nimport type { AnalyticsAdapter, DateRange, MetricKey } from '../core/contract'\nimport { resolveReadContext } from '../core/scopedRead'\nimport { getRuntime, resolveTimezoneFor } from '../plugin/runtime'\nimport { resolveTimeframe, type TimeframePreset } from '../timeframe/presets'\nimport { previousWindow } from './comparison'\n\nexport type WidgetReadStatus = 'ok' | 'not-configured' | 'unavailable'\n\nexport interface WidgetReadResult {\n\tstatus: WidgetReadStatus\n\tadapterId: string\n\tdateRange: DateRange\n\tmetrics: Partial<Record<MetricKey, number>>\n\tclamped?: boolean\n\t/** Previous-window totals, present only when the adapter supports comparison. */\n\tpreviousMetrics?: Partial<Record<MetricKey, number>>\n\t/** The previous comparable window, present only when comparison ran. */\n\tcomparisonRange?: DateRange\n}\n\nexport interface ReadForWidgetArgs {\n\treq: PayloadRequest\n\tmetrics: MetricKey[]\n\ttimeframe: TimeframePreset\n\tadapterId?: string\n\tnow: Date\n\trange?: DateRange\n\t/** Explicit scope override; omitted resolves via the plugin's scopeResolver. */\n\tscope?: string | null\n}\n\nexport const readForWidget = async (args: ReadForWidgetArgs): Promise<WidgetReadResult> => {\n\tconst { req, metrics, timeframe, adapterId, now, range } = args\n\tconst emptyMetrics = {} as Partial<Record<MetricKey, number>>\n\n\tconst runtime = getRuntime(req.payload)\n\tif (!runtime) {\n\t\treturn {\n\t\t\tstatus: 'unavailable',\n\t\t\tadapterId: adapterId ?? '',\n\t\t\tdateRange: range ?? resolveTimeframe(timeframe, now),\n\t\t\tmetrics: emptyMetrics,\n\t\t}\n\t}\n\tconst ctx = await resolveReadContext({ runtime, req, adapterId, scope: args.scope })\n\tif (!ctx.ok) {\n\t\treturn {\n\t\t\tstatus: 'unavailable',\n\t\t\tadapterId: adapterId ?? '',\n\t\t\tdateRange: range ?? resolveTimeframe(timeframe, now),\n\t\t\tmetrics: emptyMetrics,\n\t\t}\n\t}\n\tconst tz = await resolveTimezoneFor(runtime, req, ctx.scope)\n\tconst dateRange = range ?? resolveTimeframe(timeframe, now, tz)\n\tconst base = { dateRange, metrics: emptyMetrics }\n\tconst adapter: AnalyticsAdapter = ctx.adapter\n\tif (!adapter.isConfigured()) {\n\t\treturn { status: 'not-configured', adapterId: adapter.id, ...base }\n\t}\n\tif (!satisfiesCapabilities(adapter.capabilities, { metrics })) {\n\t\treturn { status: 'unavailable', adapterId: adapter.id, ...base }\n\t}\n\tconst comparisonRange =\n\t\truntime.comparison && adapter.capabilities.comparison\n\t\t\t? (previousWindow(dateRange, tz) ?? undefined)\n\t\t\t: undefined\n\tconst [result, previous] = await Promise.all([\n\t\truntime.engine.read(adapter, { metrics, dateRange, timezone: tz, scope: ctx.queryScope }),\n\t\tcomparisonRange\n\t\t\t? runtime.engine.read(adapter, {\n\t\t\t\t\tmetrics,\n\t\t\t\t\tdateRange: comparisonRange,\n\t\t\t\t\ttimezone: tz,\n\t\t\t\t\tscope: ctx.queryScope,\n\t\t\t\t})\n\t\t\t: undefined,\n\t])\n\tconst previousMetrics = previous ? (previous.totals ?? {}) : undefined\n\treturn {\n\t\tstatus: 'ok',\n\t\tadapterId: adapter.id,\n\t\tdateRange,\n\t\tmetrics: result.totals ?? {},\n\t\tclamped: result.meta.clamped ?? false,\n\t\tpreviousMetrics,\n\t\tcomparisonRange,\n\t}\n}\n"],"mappings":";;;;;;AAiCA,MAAa,gBAAgB,OAAO,SAAuD;CAC1F,MAAM,EAAE,KAAK,SAAS,WAAW,WAAW,KAAK,UAAU;CAC3D,MAAM,eAAe,CAAC;CAEtB,MAAM,UAAU,WAAW,IAAI,OAAO;CACtC,IAAI,CAAC,SACJ,OAAO;EACN,QAAQ;EACR,WAAW,aAAa;EACxB,WAAW,SAAS,iBAAiB,WAAW,GAAG;EACnD,SAAS;CACV;CAED,MAAM,MAAM,MAAM,mBAAmB;EAAE;EAAS;EAAK;EAAW,OAAO,KAAK;CAAM,CAAC;CACnF,IAAI,CAAC,IAAI,IACR,OAAO;EACN,QAAQ;EACR,WAAW,aAAa;EACxB,WAAW,SAAS,iBAAiB,WAAW,GAAG;EACnD,SAAS;CACV;CAED,MAAM,KAAK,MAAM,mBAAmB,SAAS,KAAK,IAAI,KAAK;CAC3D,MAAM,YAAY,SAAS,iBAAiB,WAAW,KAAK,EAAE;CAC9D,MAAM,OAAO;EAAE;EAAW,SAAS;CAAa;CAChD,MAAM,UAA4B,IAAI;CACtC,IAAI,CAAC,QAAQ,aAAa,GACzB,OAAO;EAAE,QAAQ;EAAkB,WAAW,QAAQ;EAAI,GAAG;CAAK;CAEnE,IAAI,CAAC,sBAAsB,QAAQ,cAAc,EAAE,QAAQ,CAAC,GAC3D,OAAO;EAAE,QAAQ;EAAe,WAAW,QAAQ;EAAI,GAAG;CAAK;CAEhE,MAAM,kBACL,QAAQ,cAAc,QAAQ,aAAa,aACvC,eAAe,WAAW,EAAE,KAAK,KAAA,IAClC,KAAA;CACJ,MAAM,CAAC,QAAQ,YAAY,MAAM,QAAQ,IAAI,CAC5C,QAAQ,OAAO,KAAK,SAAS;EAAE;EAAS;EAAW,UAAU;EAAI,OAAO,IAAI;CAAW,CAAC,GACxF,kBACG,QAAQ,OAAO,KAAK,SAAS;EAC7B;EACA,WAAW;EACX,UAAU;EACV,OAAO,IAAI;CACZ,CAAC,IACA,KAAA,CACJ,CAAC;CACD,MAAM,kBAAkB,WAAY,SAAS,UAAU,CAAC,IAAK,KAAA;CAC7D,OAAO;EACN,QAAQ;EACR,WAAW,QAAQ;EACnB;EACA,SAAS,OAAO,UAAU,CAAC;EAC3B,SAAS,OAAO,KAAK,WAAW;EAChC;EACA;CACD;AACD"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { DateRange, DimensionKey, MetricKey } from "../core/contract.js";
|
|
2
|
+
import { TimeframePreset } from "../timeframe/presets.js";
|
|
3
|
+
import { WidgetReadStatus } from "./readForWidget.js";
|
|
4
|
+
import { PayloadRequest } from "payload";
|
|
5
|
+
|
|
6
|
+
//#region src/widgets/readForWidgetBreakdown.d.ts
|
|
7
|
+
interface BreakdownRow {
|
|
8
|
+
label: string;
|
|
9
|
+
value: number;
|
|
10
|
+
}
|
|
11
|
+
interface WidgetBreakdownResult {
|
|
12
|
+
status: WidgetReadStatus;
|
|
13
|
+
adapterId: string;
|
|
14
|
+
dateRange: DateRange;
|
|
15
|
+
rows: BreakdownRow[];
|
|
16
|
+
clamped?: boolean;
|
|
17
|
+
}
|
|
18
|
+
interface ReadForWidgetBreakdownArgs {
|
|
19
|
+
req: PayloadRequest;
|
|
20
|
+
metric: MetricKey;
|
|
21
|
+
dimension: DimensionKey;
|
|
22
|
+
timeframe: TimeframePreset;
|
|
23
|
+
limit: number;
|
|
24
|
+
adapterId?: string;
|
|
25
|
+
now: Date;
|
|
26
|
+
range?: DateRange;
|
|
27
|
+
/** Explicit scope override; omitted resolves via the plugin's scopeResolver. */
|
|
28
|
+
scope?: string | null;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Site-wide ranked breakdown of one metric by one dimension, read through the engine.
|
|
32
|
+
* Mirrors `readForWidget` but gates on the dimension too and maps each result row to a
|
|
33
|
+
* `{ label, value }` pair the bar list renders. The adapter owns sorting and limiting.
|
|
34
|
+
*/
|
|
35
|
+
declare const readForWidgetBreakdown: (args: ReadForWidgetBreakdownArgs) => Promise<WidgetBreakdownResult>;
|
|
36
|
+
//#endregion
|
|
37
|
+
export { BreakdownRow, ReadForWidgetBreakdownArgs, WidgetBreakdownResult, readForWidgetBreakdown };
|
|
38
|
+
//# sourceMappingURL=readForWidgetBreakdown.d.ts.map
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { satisfiesCapabilities } from "../core/capabilities.js";
|
|
2
|
+
import { getRuntime, resolveTimezoneFor } from "../plugin/runtime.js";
|
|
3
|
+
import { resolveReadContext } from "../core/scopedRead.js";
|
|
4
|
+
import { resolveTimeframe } from "../timeframe/presets.js";
|
|
5
|
+
//#region src/widgets/readForWidgetBreakdown.ts
|
|
6
|
+
/**
|
|
7
|
+
* Site-wide ranked breakdown of one metric by one dimension, read through the engine.
|
|
8
|
+
* Mirrors `readForWidget` but gates on the dimension too and maps each result row to a
|
|
9
|
+
* `{ label, value }` pair the bar list renders. The adapter owns sorting and limiting.
|
|
10
|
+
*/
|
|
11
|
+
const readForWidgetBreakdown = async (args) => {
|
|
12
|
+
const { req, metric, dimension, timeframe, limit, adapterId, now, range } = args;
|
|
13
|
+
const emptyRows = [];
|
|
14
|
+
const runtime = getRuntime(req.payload);
|
|
15
|
+
if (!runtime) return {
|
|
16
|
+
status: "unavailable",
|
|
17
|
+
adapterId: adapterId ?? "",
|
|
18
|
+
dateRange: range ?? resolveTimeframe(timeframe, now),
|
|
19
|
+
rows: emptyRows
|
|
20
|
+
};
|
|
21
|
+
const ctx = await resolveReadContext({
|
|
22
|
+
runtime,
|
|
23
|
+
req,
|
|
24
|
+
adapterId,
|
|
25
|
+
scope: args.scope
|
|
26
|
+
});
|
|
27
|
+
if (!ctx.ok) return {
|
|
28
|
+
status: "unavailable",
|
|
29
|
+
adapterId: adapterId ?? "",
|
|
30
|
+
dateRange: range ?? resolveTimeframe(timeframe, now),
|
|
31
|
+
rows: emptyRows
|
|
32
|
+
};
|
|
33
|
+
const tz = await resolveTimezoneFor(runtime, req, ctx.scope);
|
|
34
|
+
const dateRange = range ?? resolveTimeframe(timeframe, now, tz);
|
|
35
|
+
const base = {
|
|
36
|
+
dateRange,
|
|
37
|
+
rows: emptyRows
|
|
38
|
+
};
|
|
39
|
+
const adapter = ctx.adapter;
|
|
40
|
+
if (!adapter.isConfigured()) return {
|
|
41
|
+
status: "not-configured",
|
|
42
|
+
adapterId: adapter.id,
|
|
43
|
+
...base
|
|
44
|
+
};
|
|
45
|
+
if (!satisfiesCapabilities(adapter.capabilities, {
|
|
46
|
+
metrics: [metric],
|
|
47
|
+
dimensions: [dimension]
|
|
48
|
+
})) return {
|
|
49
|
+
status: "unavailable",
|
|
50
|
+
adapterId: adapter.id,
|
|
51
|
+
...base
|
|
52
|
+
};
|
|
53
|
+
const result = await runtime.engine.read(adapter, {
|
|
54
|
+
metrics: [metric],
|
|
55
|
+
dimensions: [dimension],
|
|
56
|
+
dateRange,
|
|
57
|
+
limit,
|
|
58
|
+
order: {
|
|
59
|
+
metric,
|
|
60
|
+
direction: "desc"
|
|
61
|
+
},
|
|
62
|
+
timezone: tz,
|
|
63
|
+
scope: ctx.queryScope
|
|
64
|
+
});
|
|
65
|
+
const rows = result.rows.map((row) => ({
|
|
66
|
+
label: row.dimensions?.[dimension] ?? "(none)",
|
|
67
|
+
value: row.metrics[metric] ?? 0
|
|
68
|
+
}));
|
|
69
|
+
return {
|
|
70
|
+
status: "ok",
|
|
71
|
+
adapterId: adapter.id,
|
|
72
|
+
dateRange,
|
|
73
|
+
rows,
|
|
74
|
+
clamped: result.meta.clamped ?? false
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
//#endregion
|
|
78
|
+
export { readForWidgetBreakdown };
|
|
79
|
+
|
|
80
|
+
//# sourceMappingURL=readForWidgetBreakdown.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readForWidgetBreakdown.js","names":[],"sources":["../../src/widgets/readForWidgetBreakdown.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\nimport { satisfiesCapabilities } from '../core/capabilities'\nimport type { AnalyticsAdapter, DateRange, DimensionKey, MetricKey } from '../core/contract'\nimport { resolveReadContext } from '../core/scopedRead'\nimport { getRuntime, resolveTimezoneFor } from '../plugin/runtime'\nimport { resolveTimeframe, type TimeframePreset } from '../timeframe/presets'\nimport type { WidgetReadStatus } from './readForWidget'\n\nexport interface BreakdownRow {\n\tlabel: string\n\tvalue: number\n}\n\nexport interface WidgetBreakdownResult {\n\tstatus: WidgetReadStatus\n\tadapterId: string\n\tdateRange: DateRange\n\trows: BreakdownRow[]\n\tclamped?: boolean\n}\n\nexport interface ReadForWidgetBreakdownArgs {\n\treq: PayloadRequest\n\tmetric: MetricKey\n\tdimension: DimensionKey\n\ttimeframe: TimeframePreset\n\tlimit: number\n\tadapterId?: string\n\tnow: Date\n\trange?: DateRange\n\t/** Explicit scope override; omitted resolves via the plugin's scopeResolver. */\n\tscope?: string | null\n}\n\n/**\n * Site-wide ranked breakdown of one metric by one dimension, read through the engine.\n * Mirrors `readForWidget` but gates on the dimension too and maps each result row to a\n * `{ label, value }` pair the bar list renders. The adapter owns sorting and limiting.\n */\nexport const readForWidgetBreakdown = async (\n\targs: ReadForWidgetBreakdownArgs\n): Promise<WidgetBreakdownResult> => {\n\tconst { req, metric, dimension, timeframe, limit, adapterId, now, range } = args\n\tconst emptyRows = [] as BreakdownRow[]\n\n\tconst runtime = getRuntime(req.payload)\n\tif (!runtime) {\n\t\treturn {\n\t\t\tstatus: 'unavailable',\n\t\t\tadapterId: adapterId ?? '',\n\t\t\tdateRange: range ?? resolveTimeframe(timeframe, now),\n\t\t\trows: emptyRows,\n\t\t}\n\t}\n\tconst ctx = await resolveReadContext({ runtime, req, adapterId, scope: args.scope })\n\tif (!ctx.ok) {\n\t\treturn {\n\t\t\tstatus: 'unavailable',\n\t\t\tadapterId: adapterId ?? '',\n\t\t\tdateRange: range ?? resolveTimeframe(timeframe, now),\n\t\t\trows: emptyRows,\n\t\t}\n\t}\n\tconst tz = await resolveTimezoneFor(runtime, req, ctx.scope)\n\tconst dateRange = range ?? resolveTimeframe(timeframe, now, tz)\n\tconst base = { dateRange, rows: emptyRows }\n\tconst adapter: AnalyticsAdapter = ctx.adapter\n\tif (!adapter.isConfigured()) {\n\t\treturn { status: 'not-configured', adapterId: adapter.id, ...base }\n\t}\n\tif (\n\t\t!satisfiesCapabilities(adapter.capabilities, { metrics: [metric], dimensions: [dimension] })\n\t) {\n\t\treturn { status: 'unavailable', adapterId: adapter.id, ...base }\n\t}\n\tconst result = await runtime.engine.read(adapter, {\n\t\tmetrics: [metric],\n\t\tdimensions: [dimension],\n\t\tdateRange,\n\t\tlimit,\n\t\torder: { metric, direction: 'desc' },\n\t\ttimezone: tz,\n\t\tscope: ctx.queryScope,\n\t})\n\tconst rows = result.rows.map((row) => ({\n\t\tlabel: row.dimensions?.[dimension] ?? '(none)',\n\t\tvalue: row.metrics[metric] ?? 0,\n\t}))\n\treturn {\n\t\tstatus: 'ok',\n\t\tadapterId: adapter.id,\n\t\tdateRange,\n\t\trows,\n\t\tclamped: result.meta.clamped ?? false,\n\t}\n}\n"],"mappings":";;;;;;;;;;AAuCA,MAAa,yBAAyB,OACrC,SACoC;CACpC,MAAM,EAAE,KAAK,QAAQ,WAAW,WAAW,OAAO,WAAW,KAAK,UAAU;CAC5E,MAAM,YAAY,CAAC;CAEnB,MAAM,UAAU,WAAW,IAAI,OAAO;CACtC,IAAI,CAAC,SACJ,OAAO;EACN,QAAQ;EACR,WAAW,aAAa;EACxB,WAAW,SAAS,iBAAiB,WAAW,GAAG;EACnD,MAAM;CACP;CAED,MAAM,MAAM,MAAM,mBAAmB;EAAE;EAAS;EAAK;EAAW,OAAO,KAAK;CAAM,CAAC;CACnF,IAAI,CAAC,IAAI,IACR,OAAO;EACN,QAAQ;EACR,WAAW,aAAa;EACxB,WAAW,SAAS,iBAAiB,WAAW,GAAG;EACnD,MAAM;CACP;CAED,MAAM,KAAK,MAAM,mBAAmB,SAAS,KAAK,IAAI,KAAK;CAC3D,MAAM,YAAY,SAAS,iBAAiB,WAAW,KAAK,EAAE;CAC9D,MAAM,OAAO;EAAE;EAAW,MAAM;CAAU;CAC1C,MAAM,UAA4B,IAAI;CACtC,IAAI,CAAC,QAAQ,aAAa,GACzB,OAAO;EAAE,QAAQ;EAAkB,WAAW,QAAQ;EAAI,GAAG;CAAK;CAEnE,IACC,CAAC,sBAAsB,QAAQ,cAAc;EAAE,SAAS,CAAC,MAAM;EAAG,YAAY,CAAC,SAAS;CAAE,CAAC,GAE3F,OAAO;EAAE,QAAQ;EAAe,WAAW,QAAQ;EAAI,GAAG;CAAK;CAEhE,MAAM,SAAS,MAAM,QAAQ,OAAO,KAAK,SAAS;EACjD,SAAS,CAAC,MAAM;EAChB,YAAY,CAAC,SAAS;EACtB;EACA;EACA,OAAO;GAAE;GAAQ,WAAW;EAAO;EACnC,UAAU;EACV,OAAO,IAAI;CACZ,CAAC;CACD,MAAM,OAAO,OAAO,KAAK,KAAK,SAAS;EACtC,OAAO,IAAI,aAAa,cAAc;EACtC,OAAO,IAAI,QAAQ,WAAW;CAC/B,EAAE;CACF,OAAO;EACN,QAAQ;EACR,WAAW,QAAQ;EACnB;EACA;EACA,SAAS,OAAO,KAAK,WAAW;CACjC;AACD"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MetricKey } from "../core/contract.js";
|
|
2
2
|
import { PayloadRequest } from "payload";
|
|
3
3
|
|
|
4
4
|
//#region src/widgets/readForWidgetRealtime.d.ts
|
|
@@ -19,6 +19,8 @@ interface ReadForWidgetRealtimeArgs {
|
|
|
19
19
|
windowMinutes: number;
|
|
20
20
|
adapterId?: string;
|
|
21
21
|
now: Date;
|
|
22
|
+
/** Explicit scope override; omitted resolves via the plugin's scopeResolver. */
|
|
23
|
+
scope?: string | null;
|
|
22
24
|
}
|
|
23
25
|
/**
|
|
24
26
|
* Site-wide realtime read for the "active now" widget. The window arrives as a
|
|
@@ -28,5 +30,5 @@ interface ReadForWidgetRealtimeArgs {
|
|
|
28
30
|
*/
|
|
29
31
|
declare const readForWidgetRealtime: (args: ReadForWidgetRealtimeArgs) => Promise<WidgetRealtimeResult>;
|
|
30
32
|
//#endregion
|
|
31
|
-
export {
|
|
32
|
-
//# sourceMappingURL=readForWidgetRealtime
|
|
33
|
+
export { ReadForWidgetRealtimeArgs, RealtimePoint, WidgetRealtimeResult, WidgetRealtimeStatus, readForWidgetRealtime };
|
|
34
|
+
//# sourceMappingURL=readForWidgetRealtime.d.ts.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { satisfiesCapabilities } from "../core/capabilities.js";
|
|
2
|
+
import { getRuntime } from "../plugin/runtime.js";
|
|
3
|
+
import { resolveReadContext } from "../core/scopedRead.js";
|
|
4
|
+
import { kvCacheStore } from "../surfacing/cacheStore.js";
|
|
5
|
+
//#region src/widgets/readForWidgetRealtime.ts
|
|
6
|
+
const empty = (status, adapterId) => ({
|
|
7
|
+
status,
|
|
8
|
+
adapterId,
|
|
9
|
+
activeNow: 0,
|
|
10
|
+
series: []
|
|
11
|
+
});
|
|
12
|
+
/**
|
|
13
|
+
* Site-wide realtime read for the "active now" widget. The window arrives as a
|
|
14
|
+
* dateRange (now - windowMinutes -> now). Cached under a realtime-specific key bucketed
|
|
15
|
+
* to the realtime TTL: it is deliberately NOT routed through the engine, whose key
|
|
16
|
+
* day-snaps the range (so a "last 30 min" read would collide with a "today" aggregate).
|
|
17
|
+
*/
|
|
18
|
+
const readForWidgetRealtime = async (args) => {
|
|
19
|
+
const { req, metric, windowMinutes, adapterId, now } = args;
|
|
20
|
+
const runtime = getRuntime(req.payload);
|
|
21
|
+
if (!runtime) return empty("unavailable", adapterId ?? "");
|
|
22
|
+
const ctx = await resolveReadContext({
|
|
23
|
+
runtime,
|
|
24
|
+
req,
|
|
25
|
+
adapterId,
|
|
26
|
+
scope: args.scope
|
|
27
|
+
});
|
|
28
|
+
if (!ctx.ok) return empty("unavailable", adapterId ?? "");
|
|
29
|
+
const adapter = ctx.adapter;
|
|
30
|
+
if (!adapter.isConfigured()) return empty("not-configured", adapter.id);
|
|
31
|
+
if (!adapter.realtime || !satisfiesCapabilities(adapter.capabilities, { realtime: true })) return empty("unavailable", adapter.id);
|
|
32
|
+
const ttlSeconds = runtime.ttl.realtime ?? adapter.capabilities.recommendedTtl.realtime;
|
|
33
|
+
const bucket = Math.floor(now.getTime() / 1e3 / Math.max(1, ttlSeconds));
|
|
34
|
+
const scopeKey = ctx.queryScope === void 0 ? "" : `:${encodeURIComponent(ctx.queryScope)}`;
|
|
35
|
+
const key = `rt:${adapter.id}:${metric}:${windowMinutes}:${bucket}${scopeKey}`;
|
|
36
|
+
const store = kvCacheStore(req.payload.kv);
|
|
37
|
+
const cached = await store.get(key);
|
|
38
|
+
if (cached) return cached;
|
|
39
|
+
const dateRange = {
|
|
40
|
+
start: /* @__PURE__ */ new Date(now.getTime() - windowMinutes * 6e4),
|
|
41
|
+
end: now
|
|
42
|
+
};
|
|
43
|
+
const result = await adapter.realtime({
|
|
44
|
+
metrics: [metric],
|
|
45
|
+
dateRange,
|
|
46
|
+
scope: ctx.queryScope
|
|
47
|
+
}, {});
|
|
48
|
+
const out = {
|
|
49
|
+
status: "ok",
|
|
50
|
+
adapterId: adapter.id,
|
|
51
|
+
activeNow: result.totals?.[metric] ?? 0,
|
|
52
|
+
series: result.rows.map((r) => ({
|
|
53
|
+
date: r.timestamp ?? "",
|
|
54
|
+
value: r.metrics[metric] ?? 0
|
|
55
|
+
}))
|
|
56
|
+
};
|
|
57
|
+
await store.set(key, out, ttlSeconds);
|
|
58
|
+
return out;
|
|
59
|
+
};
|
|
60
|
+
//#endregion
|
|
61
|
+
export { readForWidgetRealtime };
|
|
62
|
+
|
|
63
|
+
//# sourceMappingURL=readForWidgetRealtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readForWidgetRealtime.js","names":[],"sources":["../../src/widgets/readForWidgetRealtime.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\nimport { satisfiesCapabilities } from '../core/capabilities'\nimport type { AnalyticsAdapter, MetricKey } from '../core/contract'\nimport { resolveReadContext } from '../core/scopedRead'\nimport { getRuntime } from '../plugin/runtime'\nimport { kvCacheStore } from '../surfacing/cacheStore'\n\nexport type WidgetRealtimeStatus = 'ok' | 'not-configured' | 'unavailable'\n\nexport interface RealtimePoint {\n\tdate: string\n\tvalue: number\n}\n\nexport interface WidgetRealtimeResult {\n\tstatus: WidgetRealtimeStatus\n\tadapterId: string\n\tactiveNow: number\n\tseries: RealtimePoint[]\n}\n\nexport interface ReadForWidgetRealtimeArgs {\n\treq: PayloadRequest\n\tmetric: MetricKey\n\twindowMinutes: number\n\tadapterId?: string\n\tnow: Date\n\t/** Explicit scope override; omitted resolves via the plugin's scopeResolver. */\n\tscope?: string | null\n}\n\nconst empty = (status: WidgetRealtimeStatus, adapterId: string): WidgetRealtimeResult => ({\n\tstatus,\n\tadapterId,\n\tactiveNow: 0,\n\tseries: [],\n})\n\n/**\n * Site-wide realtime read for the \"active now\" widget. The window arrives as a\n * dateRange (now - windowMinutes -> now). Cached under a realtime-specific key bucketed\n * to the realtime TTL: it is deliberately NOT routed through the engine, whose key\n * day-snaps the range (so a \"last 30 min\" read would collide with a \"today\" aggregate).\n */\nexport const readForWidgetRealtime = async (\n\targs: ReadForWidgetRealtimeArgs\n): Promise<WidgetRealtimeResult> => {\n\tconst { req, metric, windowMinutes, adapterId, now } = args\n\tconst runtime = getRuntime(req.payload)\n\tif (!runtime) {\n\t\treturn empty('unavailable', adapterId ?? '')\n\t}\n\tconst ctx = await resolveReadContext({ runtime, req, adapterId, scope: args.scope })\n\tif (!ctx.ok) {\n\t\treturn empty('unavailable', adapterId ?? '')\n\t}\n\tconst adapter: AnalyticsAdapter = ctx.adapter\n\tif (!adapter.isConfigured()) {\n\t\treturn empty('not-configured', adapter.id)\n\t}\n\tif (!adapter.realtime || !satisfiesCapabilities(adapter.capabilities, { realtime: true })) {\n\t\treturn empty('unavailable', adapter.id)\n\t}\n\n\tconst ttlSeconds = runtime.ttl.realtime ?? adapter.capabilities.recommendedTtl.realtime\n\tconst bucket = Math.floor(now.getTime() / 1000 / Math.max(1, ttlSeconds))\n\tconst scopeKey = ctx.queryScope === undefined ? '' : `:${encodeURIComponent(ctx.queryScope)}`\n\tconst key = `rt:${adapter.id}:${metric}:${windowMinutes}:${bucket}${scopeKey}`\n\tconst store = kvCacheStore(req.payload.kv)\n\tconst cached = await store.get<WidgetRealtimeResult>(key)\n\tif (cached) {\n\t\treturn cached\n\t}\n\n\tconst dateRange = { start: new Date(now.getTime() - windowMinutes * 60_000), end: now }\n\tconst result = await adapter.realtime({ metrics: [metric], dateRange, scope: ctx.queryScope }, {})\n\tconst out: WidgetRealtimeResult = {\n\t\tstatus: 'ok',\n\t\tadapterId: adapter.id,\n\t\tactiveNow: result.totals?.[metric] ?? 0,\n\t\tseries: result.rows.map((r) => ({ date: r.timestamp ?? '', value: r.metrics[metric] ?? 0 })),\n\t}\n\tawait store.set(key, out, ttlSeconds)\n\treturn out\n}\n"],"mappings":";;;;;AA+BA,MAAM,SAAS,QAA8B,eAA6C;CACzF;CACA;CACA,WAAW;CACX,QAAQ,CAAC;AACV;;;;;;;AAQA,MAAa,wBAAwB,OACpC,SACmC;CACnC,MAAM,EAAE,KAAK,QAAQ,eAAe,WAAW,QAAQ;CACvD,MAAM,UAAU,WAAW,IAAI,OAAO;CACtC,IAAI,CAAC,SACJ,OAAO,MAAM,eAAe,aAAa,EAAE;CAE5C,MAAM,MAAM,MAAM,mBAAmB;EAAE;EAAS;EAAK;EAAW,OAAO,KAAK;CAAM,CAAC;CACnF,IAAI,CAAC,IAAI,IACR,OAAO,MAAM,eAAe,aAAa,EAAE;CAE5C,MAAM,UAA4B,IAAI;CACtC,IAAI,CAAC,QAAQ,aAAa,GACzB,OAAO,MAAM,kBAAkB,QAAQ,EAAE;CAE1C,IAAI,CAAC,QAAQ,YAAY,CAAC,sBAAsB,QAAQ,cAAc,EAAE,UAAU,KAAK,CAAC,GACvF,OAAO,MAAM,eAAe,QAAQ,EAAE;CAGvC,MAAM,aAAa,QAAQ,IAAI,YAAY,QAAQ,aAAa,eAAe;CAC/E,MAAM,SAAS,KAAK,MAAM,IAAI,QAAQ,IAAI,MAAO,KAAK,IAAI,GAAG,UAAU,CAAC;CACxE,MAAM,WAAW,IAAI,eAAe,KAAA,IAAY,KAAK,IAAI,mBAAmB,IAAI,UAAU;CAC1F,MAAM,MAAM,MAAM,QAAQ,GAAG,GAAG,OAAO,GAAG,cAAc,GAAG,SAAS;CACpE,MAAM,QAAQ,aAAa,IAAI,QAAQ,EAAE;CACzC,MAAM,SAAS,MAAM,MAAM,IAA0B,GAAG;CACxD,IAAI,QACH,OAAO;CAGR,MAAM,YAAY;EAAE,uBAAO,IAAI,KAAK,IAAI,QAAQ,IAAI,gBAAgB,GAAM;EAAG,KAAK;CAAI;CACtF,MAAM,SAAS,MAAM,QAAQ,SAAS;EAAE,SAAS,CAAC,MAAM;EAAG;EAAW,OAAO,IAAI;CAAW,GAAG,CAAC,CAAC;CACjG,MAAM,MAA4B;EACjC,QAAQ;EACR,WAAW,QAAQ;EACnB,WAAW,OAAO,SAAS,WAAW;EACtC,QAAQ,OAAO,KAAK,KAAK,OAAO;GAAE,MAAM,EAAE,aAAa;GAAI,OAAO,EAAE,QAAQ,WAAW;EAAE,EAAE;CAC5F;CACA,MAAM,MAAM,IAAI,KAAK,KAAK,UAAU;CACpC,OAAO;AACR"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { DateRange, MetricKey } from "../core/contract.js";
|
|
2
|
+
import { TimeframePreset } from "../timeframe/presets.js";
|
|
3
|
+
import { WidgetReadStatus } from "./readForWidget.js";
|
|
4
|
+
import { PayloadRequest } from "payload";
|
|
5
|
+
|
|
6
|
+
//#region src/widgets/readForWidgetSeries.d.ts
|
|
7
|
+
interface SeriesPoint {
|
|
8
|
+
date: string;
|
|
9
|
+
value: number;
|
|
10
|
+
}
|
|
11
|
+
interface WidgetSeriesResult {
|
|
12
|
+
status: WidgetReadStatus;
|
|
13
|
+
adapterId: string;
|
|
14
|
+
dateRange: DateRange;
|
|
15
|
+
/** Reporting timezone the read resolved in; the trend axis buckets in it. */
|
|
16
|
+
timezone: string;
|
|
17
|
+
points: SeriesPoint[];
|
|
18
|
+
total: number;
|
|
19
|
+
clamped?: boolean;
|
|
20
|
+
/** Previous-window headline total, present only when the adapter supports comparison. */
|
|
21
|
+
previousTotal?: number;
|
|
22
|
+
/** The previous comparable window, present only when comparison ran. */
|
|
23
|
+
comparisonRange?: DateRange;
|
|
24
|
+
}
|
|
25
|
+
interface ReadForWidgetSeriesArgs {
|
|
26
|
+
req: PayloadRequest;
|
|
27
|
+
metric: MetricKey;
|
|
28
|
+
timeframe: TimeframePreset;
|
|
29
|
+
adapterId?: string;
|
|
30
|
+
now: Date;
|
|
31
|
+
range?: DateRange;
|
|
32
|
+
/** Explicit scope override; omitted resolves via the plugin's scopeResolver. */
|
|
33
|
+
scope?: string | null;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Site-wide time-series read for a trend widget: resolve the timeframe, pick the
|
|
37
|
+
* adapter, gate on the metric and day-granularity support, read through the engine,
|
|
38
|
+
* then return a zero-filled daily series plus the headline total. Mirrors
|
|
39
|
+
* `readForWidget` but yields points instead of a single value.
|
|
40
|
+
*/
|
|
41
|
+
declare const readForWidgetSeries: (args: ReadForWidgetSeriesArgs) => Promise<WidgetSeriesResult>;
|
|
42
|
+
//#endregion
|
|
43
|
+
export { ReadForWidgetSeriesArgs, SeriesPoint, WidgetSeriesResult, readForWidgetSeries };
|
|
44
|
+
//# sourceMappingURL=readForWidgetSeries.d.ts.map
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { satisfiesCapabilities } from "../core/capabilities.js";
|
|
2
|
+
import { supportsGranularity } from "../core/granularity.js";
|
|
3
|
+
import { addDaysInTz, startOfDayInTz, zonedDayIso } from "../timeframe/tz.js";
|
|
4
|
+
import { getRuntime, resolveTimezoneFor } from "../plugin/runtime.js";
|
|
5
|
+
import { resolveReadContext } from "../core/scopedRead.js";
|
|
6
|
+
import { resolveTimeframe } from "../timeframe/presets.js";
|
|
7
|
+
import { previousWindow } from "./comparison.js";
|
|
8
|
+
//#region src/widgets/readForWidgetSeries.ts
|
|
9
|
+
const MAX_SERIES_DAYS = 366;
|
|
10
|
+
/**
|
|
11
|
+
* Project time-series rows onto a contiguous daily axis, zero-filling gaps. Days are
|
|
12
|
+
* counted in `tz` (defaulting to UTC), so a reporting-timezone read aligns each bucket
|
|
13
|
+
* to that zone's midnight. The axis is capped at the most recent `MAX_SERIES_DAYS` so
|
|
14
|
+
* an unbounded range (all time) stays a bounded, drawable sparkline; the headline total
|
|
15
|
+
* still reflects the full range.
|
|
16
|
+
*/
|
|
17
|
+
const fillDailySeries = (args) => {
|
|
18
|
+
const { rows, dateRange, metric } = args;
|
|
19
|
+
const tz = args.tz ?? "UTC";
|
|
20
|
+
const lastDay = startOfDayInTz(dateRange.end, tz);
|
|
21
|
+
const requestedStart = startOfDayInTz(dateRange.start, tz);
|
|
22
|
+
const byDay = /* @__PURE__ */ new Map();
|
|
23
|
+
for (const row of rows) {
|
|
24
|
+
if (!row.timestamp) continue;
|
|
25
|
+
const day = zonedDayIso(new Date(row.timestamp), tz);
|
|
26
|
+
byDay.set(day, (byDay.get(day) ?? 0) + (row.metrics[metric] ?? 0));
|
|
27
|
+
}
|
|
28
|
+
const isoDays = [];
|
|
29
|
+
let cursor = lastDay;
|
|
30
|
+
for (let i = 0; i < MAX_SERIES_DAYS && cursor.getTime() >= requestedStart.getTime(); i += 1) {
|
|
31
|
+
isoDays.push(cursor.toISOString());
|
|
32
|
+
cursor = addDaysInTz(cursor, -1, tz);
|
|
33
|
+
}
|
|
34
|
+
return isoDays.reverse().map((date) => ({
|
|
35
|
+
date,
|
|
36
|
+
value: byDay.get(date) ?? 0
|
|
37
|
+
}));
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Site-wide time-series read for a trend widget: resolve the timeframe, pick the
|
|
41
|
+
* adapter, gate on the metric and day-granularity support, read through the engine,
|
|
42
|
+
* then return a zero-filled daily series plus the headline total. Mirrors
|
|
43
|
+
* `readForWidget` but yields points instead of a single value.
|
|
44
|
+
*/
|
|
45
|
+
const readForWidgetSeries = async (args) => {
|
|
46
|
+
const { req, metric, timeframe, adapterId, now, range } = args;
|
|
47
|
+
const fallback = (status, id) => ({
|
|
48
|
+
status,
|
|
49
|
+
adapterId: id,
|
|
50
|
+
dateRange: range ?? resolveTimeframe(timeframe, now),
|
|
51
|
+
timezone: "UTC",
|
|
52
|
+
points: [],
|
|
53
|
+
total: 0
|
|
54
|
+
});
|
|
55
|
+
const runtime = getRuntime(req.payload);
|
|
56
|
+
if (!runtime) return fallback("unavailable", adapterId ?? "");
|
|
57
|
+
const ctx = await resolveReadContext({
|
|
58
|
+
runtime,
|
|
59
|
+
req,
|
|
60
|
+
adapterId,
|
|
61
|
+
scope: args.scope
|
|
62
|
+
});
|
|
63
|
+
if (!ctx.ok) return fallback("unavailable", adapterId ?? "");
|
|
64
|
+
const tz = await resolveTimezoneFor(runtime, req, ctx.scope);
|
|
65
|
+
const dateRange = range ?? resolveTimeframe(timeframe, now, tz);
|
|
66
|
+
const base = {
|
|
67
|
+
dateRange,
|
|
68
|
+
timezone: tz,
|
|
69
|
+
points: [],
|
|
70
|
+
total: 0
|
|
71
|
+
};
|
|
72
|
+
const adapter = ctx.adapter;
|
|
73
|
+
if (!adapter.isConfigured()) return {
|
|
74
|
+
status: "not-configured",
|
|
75
|
+
adapterId: adapter.id,
|
|
76
|
+
...base
|
|
77
|
+
};
|
|
78
|
+
if (!satisfiesCapabilities(adapter.capabilities, { metrics: [metric] }) || !supportsGranularity(adapter.capabilities, "day")) return {
|
|
79
|
+
status: "unavailable",
|
|
80
|
+
adapterId: adapter.id,
|
|
81
|
+
...base
|
|
82
|
+
};
|
|
83
|
+
const comparisonRange = runtime.comparison && adapter.capabilities.comparison ? previousWindow(dateRange, tz) ?? void 0 : void 0;
|
|
84
|
+
const [result, previous] = await Promise.all([runtime.engine.read(adapter, {
|
|
85
|
+
metrics: [metric],
|
|
86
|
+
dateRange,
|
|
87
|
+
granularity: "day",
|
|
88
|
+
timezone: tz,
|
|
89
|
+
scope: ctx.queryScope
|
|
90
|
+
}), comparisonRange ? runtime.engine.read(adapter, {
|
|
91
|
+
metrics: [metric],
|
|
92
|
+
dateRange: comparisonRange,
|
|
93
|
+
timezone: tz,
|
|
94
|
+
scope: ctx.queryScope
|
|
95
|
+
}) : void 0]);
|
|
96
|
+
const previousTotal = previous ? previous.totals?.[metric] : void 0;
|
|
97
|
+
return {
|
|
98
|
+
status: "ok",
|
|
99
|
+
adapterId: adapter.id,
|
|
100
|
+
dateRange,
|
|
101
|
+
timezone: tz,
|
|
102
|
+
points: fillDailySeries({
|
|
103
|
+
rows: result.rows,
|
|
104
|
+
dateRange,
|
|
105
|
+
metric,
|
|
106
|
+
tz
|
|
107
|
+
}),
|
|
108
|
+
total: result.totals?.[metric] ?? 0,
|
|
109
|
+
clamped: result.meta.clamped ?? false,
|
|
110
|
+
previousTotal,
|
|
111
|
+
comparisonRange
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
//#endregion
|
|
115
|
+
export { fillDailySeries, readForWidgetSeries };
|
|
116
|
+
|
|
117
|
+
//# sourceMappingURL=readForWidgetSeries.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readForWidgetSeries.js","names":[],"sources":["../../src/widgets/readForWidgetSeries.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\nimport { satisfiesCapabilities } from '../core/capabilities'\nimport type { AnalyticsAdapter, AnalyticsRow, DateRange, MetricKey } from '../core/contract'\nimport { supportsGranularity } from '../core/granularity'\nimport { resolveReadContext } from '../core/scopedRead'\nimport { getRuntime, resolveTimezoneFor } from '../plugin/runtime'\nimport { resolveTimeframe, type TimeframePreset } from '../timeframe/presets'\nimport { addDaysInTz, DEFAULT_TIMEZONE, startOfDayInTz, zonedDayIso } from '../timeframe/tz'\nimport { previousWindow } from './comparison'\nimport type { WidgetReadStatus } from './readForWidget'\n\nexport interface SeriesPoint {\n\tdate: string\n\tvalue: number\n}\n\nexport interface WidgetSeriesResult {\n\tstatus: WidgetReadStatus\n\tadapterId: string\n\tdateRange: DateRange\n\t/** Reporting timezone the read resolved in; the trend axis buckets in it. */\n\ttimezone: string\n\tpoints: SeriesPoint[]\n\ttotal: number\n\tclamped?: boolean\n\t/** Previous-window headline total, present only when the adapter supports comparison. */\n\tpreviousTotal?: number\n\t/** The previous comparable window, present only when comparison ran. */\n\tcomparisonRange?: DateRange\n}\n\nexport interface ReadForWidgetSeriesArgs {\n\treq: PayloadRequest\n\tmetric: MetricKey\n\ttimeframe: TimeframePreset\n\tadapterId?: string\n\tnow: Date\n\trange?: DateRange\n\t/** Explicit scope override; omitted resolves via the plugin's scopeResolver. */\n\tscope?: string | null\n}\n\nconst MAX_SERIES_DAYS = 366\n\n/**\n * Project time-series rows onto a contiguous daily axis, zero-filling gaps. Days are\n * counted in `tz` (defaulting to UTC), so a reporting-timezone read aligns each bucket\n * to that zone's midnight. The axis is capped at the most recent `MAX_SERIES_DAYS` so\n * an unbounded range (all time) stays a bounded, drawable sparkline; the headline total\n * still reflects the full range.\n */\nexport const fillDailySeries = (args: {\n\trows: AnalyticsRow[]\n\tdateRange: DateRange\n\tmetric: MetricKey\n\ttz?: string\n}): SeriesPoint[] => {\n\tconst { rows, dateRange, metric } = args\n\tconst tz = args.tz ?? DEFAULT_TIMEZONE\n\tconst lastDay = startOfDayInTz(dateRange.end, tz)\n\tconst requestedStart = startOfDayInTz(dateRange.start, tz)\n\tconst byDay = new Map<string, number>()\n\tfor (const row of rows) {\n\t\tif (!row.timestamp) {\n\t\t\tcontinue\n\t\t}\n\t\tconst day = zonedDayIso(new Date(row.timestamp), tz)\n\t\tbyDay.set(day, (byDay.get(day) ?? 0) + (row.metrics[metric] ?? 0))\n\t}\n\t// Walk calendar days back from the last day so DST-length days never mis-step, capping\n\t// the axis at MAX_SERIES_DAYS or the requested start, whichever is more recent.\n\tconst isoDays: string[] = []\n\tlet cursor = lastDay\n\tfor (let i = 0; i < MAX_SERIES_DAYS && cursor.getTime() >= requestedStart.getTime(); i += 1) {\n\t\tisoDays.push(cursor.toISOString())\n\t\tcursor = addDaysInTz(cursor, -1, tz)\n\t}\n\treturn isoDays.reverse().map((date) => ({ date, value: byDay.get(date) ?? 0 }))\n}\n\n/**\n * Site-wide time-series read for a trend widget: resolve the timeframe, pick the\n * adapter, gate on the metric and day-granularity support, read through the engine,\n * then return a zero-filled daily series plus the headline total. Mirrors\n * `readForWidget` but yields points instead of a single value.\n */\nexport const readForWidgetSeries = async (\n\targs: ReadForWidgetSeriesArgs\n): Promise<WidgetSeriesResult> => {\n\tconst { req, metric, timeframe, adapterId, now, range } = args\n\tconst fallback = (status: WidgetReadStatus, id: string): WidgetSeriesResult => ({\n\t\tstatus,\n\t\tadapterId: id,\n\t\tdateRange: range ?? resolveTimeframe(timeframe, now),\n\t\ttimezone: DEFAULT_TIMEZONE,\n\t\tpoints: [],\n\t\ttotal: 0,\n\t})\n\n\tconst runtime = getRuntime(req.payload)\n\tif (!runtime) {\n\t\treturn fallback('unavailable', adapterId ?? '')\n\t}\n\tconst ctx = await resolveReadContext({ runtime, req, adapterId, scope: args.scope })\n\tif (!ctx.ok) {\n\t\treturn fallback('unavailable', adapterId ?? '')\n\t}\n\tconst tz = await resolveTimezoneFor(runtime, req, ctx.scope)\n\tconst dateRange = range ?? resolveTimeframe(timeframe, now, tz)\n\tconst base = { dateRange, timezone: tz, points: [] as SeriesPoint[], total: 0 }\n\tconst adapter: AnalyticsAdapter = ctx.adapter\n\tif (!adapter.isConfigured()) {\n\t\treturn { status: 'not-configured', adapterId: adapter.id, ...base }\n\t}\n\tif (\n\t\t!satisfiesCapabilities(adapter.capabilities, { metrics: [metric] }) ||\n\t\t!supportsGranularity(adapter.capabilities, 'day')\n\t) {\n\t\treturn { status: 'unavailable', adapterId: adapter.id, ...base }\n\t}\n\tconst comparisonRange =\n\t\truntime.comparison && adapter.capabilities.comparison\n\t\t\t? (previousWindow(dateRange, tz) ?? undefined)\n\t\t\t: undefined\n\tconst [result, previous] = await Promise.all([\n\t\truntime.engine.read(adapter, {\n\t\t\tmetrics: [metric],\n\t\t\tdateRange,\n\t\t\tgranularity: 'day',\n\t\t\ttimezone: tz,\n\t\t\tscope: ctx.queryScope,\n\t\t}),\n\t\tcomparisonRange\n\t\t\t? runtime.engine.read(adapter, {\n\t\t\t\t\tmetrics: [metric],\n\t\t\t\t\tdateRange: comparisonRange,\n\t\t\t\t\ttimezone: tz,\n\t\t\t\t\tscope: ctx.queryScope,\n\t\t\t\t})\n\t\t\t: undefined,\n\t])\n\tconst previousTotal = previous ? previous.totals?.[metric] : undefined\n\treturn {\n\t\tstatus: 'ok',\n\t\tadapterId: adapter.id,\n\t\tdateRange,\n\t\ttimezone: tz,\n\t\tpoints: fillDailySeries({ rows: result.rows, dateRange, metric, tz }),\n\t\ttotal: result.totals?.[metric] ?? 0,\n\t\tclamped: result.meta.clamped ?? false,\n\t\tpreviousTotal,\n\t\tcomparisonRange,\n\t}\n}\n"],"mappings":";;;;;;;;AA0CA,MAAM,kBAAkB;;;;;;;;AASxB,MAAa,mBAAmB,SAKX;CACpB,MAAM,EAAE,MAAM,WAAW,WAAW;CACpC,MAAM,KAAK,KAAK,MAAA;CAChB,MAAM,UAAU,eAAe,UAAU,KAAK,EAAE;CAChD,MAAM,iBAAiB,eAAe,UAAU,OAAO,EAAE;CACzD,MAAM,wBAAQ,IAAI,IAAoB;CACtC,KAAK,MAAM,OAAO,MAAM;EACvB,IAAI,CAAC,IAAI,WACR;EAED,MAAM,MAAM,YAAY,IAAI,KAAK,IAAI,SAAS,GAAG,EAAE;EACnD,MAAM,IAAI,MAAM,MAAM,IAAI,GAAG,KAAK,MAAM,IAAI,QAAQ,WAAW,EAAE;CAClE;CAGA,MAAM,UAAoB,CAAC;CAC3B,IAAI,SAAS;CACb,KAAK,IAAI,IAAI,GAAG,IAAI,mBAAmB,OAAO,QAAQ,KAAK,eAAe,QAAQ,GAAG,KAAK,GAAG;EAC5F,QAAQ,KAAK,OAAO,YAAY,CAAC;EACjC,SAAS,YAAY,QAAQ,IAAI,EAAE;CACpC;CACA,OAAO,QAAQ,QAAQ,EAAE,KAAK,UAAU;EAAE;EAAM,OAAO,MAAM,IAAI,IAAI,KAAK;CAAE,EAAE;AAC/E;;;;;;;AAQA,MAAa,sBAAsB,OAClC,SACiC;CACjC,MAAM,EAAE,KAAK,QAAQ,WAAW,WAAW,KAAK,UAAU;CAC1D,MAAM,YAAY,QAA0B,QAAoC;EAC/E;EACA,WAAW;EACX,WAAW,SAAS,iBAAiB,WAAW,GAAG;EACnD,UAAA;EACA,QAAQ,CAAC;EACT,OAAO;CACR;CAEA,MAAM,UAAU,WAAW,IAAI,OAAO;CACtC,IAAI,CAAC,SACJ,OAAO,SAAS,eAAe,aAAa,EAAE;CAE/C,MAAM,MAAM,MAAM,mBAAmB;EAAE;EAAS;EAAK;EAAW,OAAO,KAAK;CAAM,CAAC;CACnF,IAAI,CAAC,IAAI,IACR,OAAO,SAAS,eAAe,aAAa,EAAE;CAE/C,MAAM,KAAK,MAAM,mBAAmB,SAAS,KAAK,IAAI,KAAK;CAC3D,MAAM,YAAY,SAAS,iBAAiB,WAAW,KAAK,EAAE;CAC9D,MAAM,OAAO;EAAE;EAAW,UAAU;EAAI,QAAQ,CAAC;EAAoB,OAAO;CAAE;CAC9E,MAAM,UAA4B,IAAI;CACtC,IAAI,CAAC,QAAQ,aAAa,GACzB,OAAO;EAAE,QAAQ;EAAkB,WAAW,QAAQ;EAAI,GAAG;CAAK;CAEnE,IACC,CAAC,sBAAsB,QAAQ,cAAc,EAAE,SAAS,CAAC,MAAM,EAAE,CAAC,KAClE,CAAC,oBAAoB,QAAQ,cAAc,KAAK,GAEhD,OAAO;EAAE,QAAQ;EAAe,WAAW,QAAQ;EAAI,GAAG;CAAK;CAEhE,MAAM,kBACL,QAAQ,cAAc,QAAQ,aAAa,aACvC,eAAe,WAAW,EAAE,KAAK,KAAA,IAClC,KAAA;CACJ,MAAM,CAAC,QAAQ,YAAY,MAAM,QAAQ,IAAI,CAC5C,QAAQ,OAAO,KAAK,SAAS;EAC5B,SAAS,CAAC,MAAM;EAChB;EACA,aAAa;EACb,UAAU;EACV,OAAO,IAAI;CACZ,CAAC,GACD,kBACG,QAAQ,OAAO,KAAK,SAAS;EAC7B,SAAS,CAAC,MAAM;EAChB,WAAW;EACX,UAAU;EACV,OAAO,IAAI;CACZ,CAAC,IACA,KAAA,CACJ,CAAC;CACD,MAAM,gBAAgB,WAAW,SAAS,SAAS,UAAU,KAAA;CAC7D,OAAO;EACN,QAAQ;EACR,WAAW,QAAQ;EACnB;EACA,UAAU;EACV,QAAQ,gBAAgB;GAAE,MAAM,OAAO;GAAM;GAAW;GAAQ;EAAG,CAAC;EACpE,OAAO,OAAO,SAAS,WAAW;EAClC,SAAS,OAAO,KAAK,WAAW;EAChC;EACA;CACD;AACD"}
|