@10x-media/analytics 1.0.0-beta.14 → 1.0.0-beta.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/README.md +1 -1
  3. package/dist/adapters/ga4/ga4.d.ts +7 -0
  4. package/dist/adapters/ga4/ga4.js +17 -3
  5. package/dist/adapters/ga4/ga4.js.map +1 -1
  6. package/dist/adapters/plausible/plausible.d.ts +6 -0
  7. package/dist/adapters/plausible/plausible.js +6 -3
  8. package/dist/adapters/plausible/plausible.js.map +1 -1
  9. package/dist/adapters/posthog/cloud.js +20 -0
  10. package/dist/adapters/posthog/cloud.js.map +1 -0
  11. package/dist/adapters/posthog/credentials.js +20 -0
  12. package/dist/adapters/posthog/credentials.js.map +1 -0
  13. package/dist/adapters/posthog/posthog.d.ts +10 -3
  14. package/dist/adapters/posthog/posthog.js +100 -27
  15. package/dist/adapters/posthog/posthog.js.map +1 -1
  16. package/dist/adapters/posthog/projectCheck.js +56 -0
  17. package/dist/adapters/posthog/projectCheck.js.map +1 -0
  18. package/dist/adapters/umami/umami.d.ts +6 -0
  19. package/dist/adapters/umami/umami.js +3 -1
  20. package/dist/adapters/umami/umami.js.map +1 -1
  21. package/dist/capture/trackerConfig.js +29 -6
  22. package/dist/capture/trackerConfig.js.map +1 -1
  23. package/dist/capture/withoutConsent.js +91 -0
  24. package/dist/capture/withoutConsent.js.map +1 -0
  25. package/dist/core/capture.d.ts +41 -11
  26. package/dist/core/captureOptions.js +97 -8
  27. package/dist/core/captureOptions.js.map +1 -1
  28. package/dist/core/contract.d.ts +25 -0
  29. package/dist/core/contract.js.map +1 -1
  30. package/dist/core/options.d.ts +24 -2
  31. package/dist/core/options.js.map +1 -1
  32. package/dist/core/sourceOptions.js +2 -1
  33. package/dist/core/sourceOptions.js.map +1 -1
  34. package/dist/exports/adapters/contract.d.ts +3 -3
  35. package/dist/exports/adapters/ga4.d.ts +4 -4
  36. package/dist/exports/adapters/native.d.ts +5 -5
  37. package/dist/exports/adapters/plausible.d.ts +4 -4
  38. package/dist/exports/adapters/posthog.d.ts +4 -4
  39. package/dist/exports/adapters/umami.d.ts +4 -4
  40. package/dist/exports/next.d.ts +4 -4
  41. package/dist/exports/react.d.ts +3 -2
  42. package/dist/exports/site.d.ts +2 -2
  43. package/dist/exports/testing.d.ts +4 -4
  44. package/dist/exports/tracker.d.ts +3 -2
  45. package/dist/exports/types.d.ts +5 -5
  46. package/dist/index.d.ts +5 -5
  47. package/dist/index.js +2 -0
  48. package/dist/index.js.map +1 -1
  49. package/dist/native/collections/hits.js +1 -0
  50. package/dist/native/collections/hits.js.map +1 -1
  51. package/dist/native/engine/engine.js +5 -1
  52. package/dist/native/engine/engine.js.map +1 -1
  53. package/dist/native/engine/planner.js +2 -1
  54. package/dist/native/engine/planner.js.map +1 -1
  55. package/dist/native/engine/toHit.js +4 -2
  56. package/dist/native/engine/toHit.js.map +1 -1
  57. package/dist/native/ingest/endpoint.js +5 -2
  58. package/dist/native/ingest/endpoint.js.map +1 -1
  59. package/dist/native/ingest/normalizeEvent.d.ts +6 -1
  60. package/dist/native/ingest/normalizeEvent.js +17 -7
  61. package/dist/native/ingest/normalizeEvent.js.map +1 -1
  62. package/dist/native/ingest/serverTrack.js +2 -1
  63. package/dist/native/ingest/serverTrack.js.map +1 -1
  64. package/dist/native/ingest/validate.js +3 -0
  65. package/dist/native/ingest/validate.js.map +1 -1
  66. package/dist/native/nativeAdapter.d.ts +10 -1
  67. package/dist/native/nativeAdapter.js +11 -1
  68. package/dist/native/nativeAdapter.js.map +1 -1
  69. package/dist/native/store/aggregate.js +1 -1
  70. package/dist/native/store/aggregate.js.map +1 -1
  71. package/dist/native/store/hitRow.js +3 -0
  72. package/dist/native/store/hitRow.js.map +1 -1
  73. package/dist/native/store/hits.js +3 -1
  74. package/dist/native/store/hits.js.map +1 -1
  75. package/dist/native/store/mongo/aggregatePipelines.js +10 -2
  76. package/dist/native/store/mongo/aggregatePipelines.js.map +1 -1
  77. package/dist/native/store/mongo/pipelines.js +15 -5
  78. package/dist/native/store/mongo/pipelines.js.map +1 -1
  79. package/dist/native/store/parts.js +9 -3
  80. package/dist/native/store/parts.js.map +1 -1
  81. package/dist/native/store/rawRead.js +6 -3
  82. package/dist/native/store/rawRead.js.map +1 -1
  83. package/dist/native/store/sql/aggregateSql.js +6 -2
  84. package/dist/native/store/sql/aggregateSql.js.map +1 -1
  85. package/dist/native/store/sql/columns.js +1 -0
  86. package/dist/native/store/sql/columns.js.map +1 -1
  87. package/dist/native/store/sql/compile.js +19 -7
  88. package/dist/native/store/sql/compile.js.map +1 -1
  89. package/dist/native/store/types.d.ts +7 -1
  90. package/dist/native/store/types.js +10 -0
  91. package/dist/native/store/types.js.map +1 -0
  92. package/dist/plugin/createRuntime.js +10 -1
  93. package/dist/plugin/createRuntime.js.map +1 -1
  94. package/dist/plugin/runtime.js.map +1 -1
  95. package/dist/providers/collection.d.ts +2 -0
  96. package/dist/providers/collection.js +36 -9
  97. package/dist/providers/collection.js.map +1 -1
  98. package/dist/providers/factory.d.ts +9 -1
  99. package/dist/providers/factory.js +14 -5
  100. package/dist/providers/factory.js.map +1 -1
  101. package/dist/providers/hosts.js +14 -6
  102. package/dist/providers/hosts.js.map +1 -1
  103. package/dist/providers/registry.js +2 -1
  104. package/dist/providers/registry.js.map +1 -1
  105. package/dist/react/AnalyticsProvider.d.ts +6 -0
  106. package/dist/react/AnalyticsProvider.js.map +1 -1
  107. package/dist/react/useAnalytics.js +9 -5
  108. package/dist/react/useAnalytics.js.map +1 -1
  109. package/dist/site/AnalyticsScripts.d.ts +4 -3
  110. package/dist/site/AnalyticsScripts.js +18 -4
  111. package/dist/site/AnalyticsScripts.js.map +1 -1
  112. package/dist/surfacing/todaySplit.js +7 -1
  113. package/dist/surfacing/todaySplit.js.map +1 -1
  114. package/dist/tracker/config.d.ts +26 -1
  115. package/dist/tracker/config.js.map +1 -1
  116. package/dist/tracker/createTracker.d.ts +2 -1
  117. package/dist/tracker/createTracker.js +91 -59
  118. package/dist/tracker/createTracker.js.map +1 -1
  119. package/dist/tracker/gate.d.ts +8 -0
  120. package/dist/tracker/gate.js +61 -11
  121. package/dist/tracker/gate.js.map +1 -1
  122. package/dist/tracker/loadScript.js +2 -1
  123. package/dist/tracker/loadScript.js.map +1 -1
  124. package/dist/tracker/registry.js +14 -3
  125. package/dist/tracker/registry.js.map +1 -1
  126. package/dist/tracker/sinks/createSink.js +27 -8
  127. package/dist/tracker/sinks/createSink.js.map +1 -1
  128. package/dist/tracker/sinks/ga4.js +38 -6
  129. package/dist/tracker/sinks/ga4.js.map +1 -1
  130. package/dist/tracker/sinks/native.js +17 -3
  131. package/dist/tracker/sinks/native.js.map +1 -1
  132. package/dist/tracker/sinks/plausible.js +8 -2
  133. package/dist/tracker/sinks/plausible.js.map +1 -1
  134. package/dist/tracker/sinks/posthog.js +108 -16
  135. package/dist/tracker/sinks/posthog.js.map +1 -1
  136. package/dist/tracker/sinks/umami.js +2 -2
  137. package/dist/tracker/sinks/umami.js.map +1 -1
  138. package/dist/tracker/sinks/vendor.js +25 -11
  139. package/dist/tracker/sinks/vendor.js.map +1 -1
  140. package/dist/tracker/types.d.ts +10 -3
  141. package/dist/translations/ar.js +10 -0
  142. package/dist/translations/ar.js.map +1 -1
  143. package/dist/translations/de.js +10 -0
  144. package/dist/translations/de.js.map +1 -1
  145. package/dist/translations/en.js +10 -0
  146. package/dist/translations/en.js.map +1 -1
  147. package/dist/translations/es.js +10 -0
  148. package/dist/translations/es.js.map +1 -1
  149. package/dist/translations/fr.js +10 -0
  150. package/dist/translations/fr.js.map +1 -1
  151. package/dist/translations/id.js +10 -0
  152. package/dist/translations/id.js.map +1 -1
  153. package/dist/translations/keys.d.ts +10 -0
  154. package/dist/translations/keys.js +11 -1
  155. package/dist/translations/keys.js.map +1 -1
  156. package/dist/translations/ko.js +10 -0
  157. package/dist/translations/ko.js.map +1 -1
  158. package/dist/translations/pt.js +10 -0
  159. package/dist/translations/pt.js.map +1 -1
  160. package/dist/translations/ru.js +10 -0
  161. package/dist/translations/ru.js.map +1 -1
  162. package/dist/translations/uk.js +10 -0
  163. package/dist/translations/uk.js.map +1 -1
  164. package/dist/translations/zh.js +10 -0
  165. package/dist/translations/zh.js.map +1 -1
  166. package/dist/view/consentShare.js +15 -0
  167. package/dist/view/consentShare.js.map +1 -0
  168. package/dist/view/sections/OverviewCards.js +38 -28
  169. package/dist/view/sections/OverviewCards.js.map +1 -1
  170. package/dist/widgets/notices.js +6 -1
  171. package/dist/widgets/notices.js.map +1 -1
  172. package/dist/widgets/readMeta.d.ts +4 -0
  173. package/dist/widgets/readMeta.js +5 -0
  174. package/dist/widgets/readMeta.js.map +1 -1
  175. package/package.json +3 -3
@@ -1 +1 @@
1
- {"version":3,"file":"parts.js","names":[],"sources":["../../../src/native/store/parts.ts"],"sourcesContent":["import type { MetricKey, MetricValue, MetricValues } from '../../core/contract'\n\n/**\n * Additive counters behind every metric: averages are stored as numerator and denominator, so a\n * week is exact. `null`: the row's kind does not count that part at all, unlike counting none.\n */\nexport interface MetricParts {\n\tpageviews: number | null\n\t/** Events and goal completions. */\n\tevents: number | null\n\t/** Distinct visitors of the row's own day: summed over days, a person counts once per day. */\n\tvisitors: number | null\n\tsessions: number | null\n\t/** Sessions that started on the row: the bounce rate's denominator. */\n\tentries: number | null\n\t/** Entries whose session had one pageview and no event or goal. */\n\tbounces: number | null\n\t/** Engaged milliseconds of the sessions in `sessions`. */\n\tsessionMs: number | null\n\t/** Engaged milliseconds of the pageviews in `pageviews`. */\n\tdurationMs: number | null\n\t/** Scroll depth summed over the pageviews that measured it. */\n\tscrollSum: number | null\n\t/** Pageviews that measured their scroll depth. */\n\tscrollSamples: number | null\n\tconversions: number | null\n\trevenue: number | null\n\timpressions: number | null\n\tinteractions: number | null\n}\n\nexport const PART_KEYS: ReadonlyArray<keyof MetricParts> = [\n\t'pageviews',\n\t'events',\n\t'visitors',\n\t'sessions',\n\t'entries',\n\t'bounces',\n\t'sessionMs',\n\t'durationMs',\n\t'scrollSum',\n\t'scrollSamples',\n\t'conversions',\n\t'revenue',\n\t'impressions',\n\t'interactions',\n]\n\n/** Nothing counted, the identity of {@link addParts}. */\nexport const emptyParts = (): MetricParts => ({\n\tpageviews: null,\n\tevents: null,\n\tvisitors: null,\n\tsessions: null,\n\tentries: null,\n\tbounces: null,\n\tsessionMs: null,\n\tdurationMs: null,\n\tscrollSum: null,\n\tscrollSamples: null,\n\tconversions: null,\n\trevenue: null,\n\timpressions: null,\n\tinteractions: null,\n})\n\n/** One part of two rows together: a count with nothing beside it stays that count. */\nexport const addPart = (a: number | null, b: number | null): number | null => {\n\tif (a === null) {\n\t\treturn b\n\t}\n\treturn b === null ? a : a + b\n}\n\nexport const addParts = (a: MetricParts, b: MetricParts): MetricParts => {\n\tconst sum = emptyParts()\n\tfor (const key of PART_KEYS) {\n\t\tsum[key] = addPart(a[key], b[key])\n\t}\n\treturn sum\n}\n\nexport const sumParts = (all: Iterable<MetricParts>): MetricParts => {\n\tconst sum = emptyParts()\n\tfor (const parts of all) {\n\t\tfor (const key of PART_KEYS) {\n\t\t\tsum[key] = addPart(sum[key], parts[key])\n\t\t}\n\t}\n\treturn sum\n}\n\n/**\n * One part over another: absent when the row's kind does not count the denominator, `null` when\n * it counted none of it (nothing to average over), else rounded.\n */\nconst ratio = (\n\tnumerator: number | null,\n\tdenominator: number | null,\n\tscale = 1\n): MetricValue | undefined => {\n\tif (denominator === null) {\n\t\treturn undefined\n\t}\n\tif (denominator <= 0 || numerator === null) {\n\t\treturn null\n\t}\n\treturn Math.round((numerator / denominator) * scale)\n}\n\nconst count = (part: number | null): MetricValue | undefined => part ?? undefined\n\nconst metricOf = (parts: MetricParts, metric: MetricKey): MetricValue | undefined => {\n\tswitch (metric) {\n\t\tcase 'pageviews':\n\t\t\treturn count(parts.pageviews)\n\t\tcase 'events':\n\t\t\treturn count(parts.events)\n\t\tcase 'visitors':\n\t\t\treturn count(parts.visitors)\n\t\tcase 'visits':\n\t\tcase 'sessions':\n\t\t\treturn count(parts.sessions)\n\t\tcase 'conversions':\n\t\t\treturn count(parts.conversions)\n\t\tcase 'revenue':\n\t\t\treturn count(parts.revenue)\n\t\tcase 'impressions':\n\t\t\treturn count(parts.impressions)\n\t\tcase 'interactions':\n\t\t\treturn count(parts.interactions)\n\t\tcase 'avgDuration':\n\t\t\treturn ratio(parts.durationMs, parts.pageviews)\n\t\tcase 'visitDuration':\n\t\t\treturn ratio(parts.sessionMs, parts.sessions)\n\t\tcase 'bounceRate':\n\t\t\treturn ratio(parts.bounces, parts.entries, 100)\n\t\tcase 'scrollDepth': {\n\t\t\t// One engagement is capped on the wire, but nothing caps how many arrive for a pageview.\n\t\t\tconst depth = ratio(parts.scrollSum, parts.scrollSamples)\n\t\t\treturn typeof depth === 'number' ? Math.min(100, depth) : depth\n\t\t}\n\t}\n}\n\n/**\n * The metrics a read asked for, each average its own numerator over its own denominator. A metric\n * the row's kind does not count is left out rather than read as zero.\n */\nexport const partsToMetrics = (parts: MetricParts, metrics: readonly MetricKey[]): MetricValues => {\n\tconst out: MetricValues = {}\n\tfor (const metric of metrics) {\n\t\tconst value = metricOf(parts, metric)\n\t\tif (value !== undefined) {\n\t\t\tout[metric] = value\n\t\t}\n\t}\n\treturn out\n}\n"],"mappings":";AA+BA,MAAa,YAA8C;CAC1D;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACD;;AAGA,MAAa,oBAAiC;CAC7C,WAAW;CACX,QAAQ;CACR,UAAU;CACV,UAAU;CACV,SAAS;CACT,SAAS;CACT,WAAW;CACX,YAAY;CACZ,WAAW;CACX,eAAe;CACf,aAAa;CACb,SAAS;CACT,aAAa;CACb,cAAc;AACf;;AAGA,MAAa,WAAW,GAAkB,MAAoC;CAC7E,IAAI,MAAM,MACT,OAAO;CAER,OAAO,MAAM,OAAO,IAAI,IAAI;AAC7B;AAEA,MAAa,YAAY,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,MAAa,kBAAkB,OAAoB,YAAgD;CAClG,MAAM,MAAoB,CAAC;CAC3B,KAAK,MAAM,UAAU,SAAS;EAC7B,MAAM,QAAQ,SAAS,OAAO,MAAM;EACpC,IAAI,UAAU,KAAA,GACb,IAAI,UAAU;CAEhB;CACA,OAAO;AACR"}
1
+ {"version":3,"file":"parts.js","names":[],"sources":["../../../src/native/store/parts.ts"],"sourcesContent":["import type { MetricKey, MetricValue, MetricValues } from '../../core/contract'\n\n/**\n * Additive counters behind every metric: averages are stored as numerator and denominator, so a\n * week is exact. `null`: the row's kind does not count that part at all, unlike counting none.\n */\nexport interface MetricParts {\n\tpageviews: number | null\n\t/** Events and goal completions. */\n\tevents: number | null\n\t/** Distinct visitors of the row's own day: summed over days, a person counts once per day. */\n\tvisitors: number | null\n\tsessions: number | null\n\t/** Sessions that started on the row: the bounce rate's denominator. */\n\tentries: number | null\n\t/** Entries whose session had one pageview and no event or goal. */\n\tbounces: number | null\n\t/** Engaged milliseconds of the sessions in `sessions`. */\n\tsessionMs: number | null\n\t/** Engaged milliseconds of the pageviews in `pageviews`. */\n\tdurationMs: number | null\n\t/** Scroll depth summed over the pageviews that measured it. */\n\tscrollSum: number | null\n\t/** Pageviews that measured their scroll depth. */\n\tscrollSamples: number | null\n\tconversions: number | null\n\trevenue: number | null\n\timpressions: number | null\n\tinteractions: number | null\n\t/**\n\t * Pageviews with no visitor (anonymous, or server code's of no one's): they measure no time, so\n\t * time on page leaves them out. Null on a day counted before it existed, read as none.\n\t */\n\tpageviewsWithoutVisitor: number | null\n\t/** Pageviews the page measured cookielessly or anonymously, before or without consent. */\n\tpageviewsWithoutConsent: number | null\n}\n\nexport const PART_KEYS: ReadonlyArray<keyof MetricParts> = [\n\t'pageviews',\n\t'events',\n\t'visitors',\n\t'sessions',\n\t'entries',\n\t'bounces',\n\t'sessionMs',\n\t'durationMs',\n\t'scrollSum',\n\t'scrollSamples',\n\t'conversions',\n\t'revenue',\n\t'impressions',\n\t'interactions',\n\t'pageviewsWithoutVisitor',\n\t'pageviewsWithoutConsent',\n]\n\n/** Nothing counted, the identity of {@link addParts}. */\nexport const emptyParts = (): MetricParts => ({\n\tpageviews: null,\n\tevents: null,\n\tvisitors: null,\n\tsessions: null,\n\tentries: null,\n\tbounces: null,\n\tsessionMs: null,\n\tdurationMs: null,\n\tscrollSum: null,\n\tscrollSamples: null,\n\tconversions: null,\n\trevenue: null,\n\timpressions: null,\n\tinteractions: null,\n\tpageviewsWithoutVisitor: null,\n\tpageviewsWithoutConsent: null,\n})\n\n/** One part of two rows together: a count with nothing beside it stays that count. */\nexport const addPart = (a: number | null, b: number | null): number | null => {\n\tif (a === null) {\n\t\treturn b\n\t}\n\treturn b === null ? a : a + b\n}\n\nexport const addParts = (a: MetricParts, b: MetricParts): MetricParts => {\n\tconst sum = emptyParts()\n\tfor (const key of PART_KEYS) {\n\t\tsum[key] = addPart(a[key], b[key])\n\t}\n\treturn sum\n}\n\nexport const sumParts = (all: Iterable<MetricParts>): MetricParts => {\n\tconst sum = emptyParts()\n\tfor (const parts of all) {\n\t\tfor (const key of PART_KEYS) {\n\t\t\tsum[key] = addPart(sum[key], parts[key])\n\t\t}\n\t}\n\treturn sum\n}\n\n/**\n * One part over another: absent when the row's kind does not count the denominator, `null` when\n * it counted none of it (nothing to average over), else rounded.\n */\nconst ratio = (\n\tnumerator: number | null,\n\tdenominator: number | null,\n\tscale = 1\n): MetricValue | undefined => {\n\tif (denominator === null) {\n\t\treturn undefined\n\t}\n\tif (denominator <= 0 || numerator === null) {\n\t\treturn null\n\t}\n\treturn Math.round((numerator / denominator) * scale)\n}\n\nconst count = (part: number | null): MetricValue | undefined => part ?? undefined\n\n/** The pageviews time on page averages over: those with a visitor. */\nconst timedPageviews = (parts: MetricParts): number | null =>\n\tparts.pageviews === null ? null : parts.pageviews - (parts.pageviewsWithoutVisitor ?? 0)\n\nconst metricOf = (parts: MetricParts, metric: MetricKey): MetricValue | undefined => {\n\tswitch (metric) {\n\t\tcase 'pageviews':\n\t\t\treturn count(parts.pageviews)\n\t\tcase 'events':\n\t\t\treturn count(parts.events)\n\t\tcase 'visitors':\n\t\t\treturn count(parts.visitors)\n\t\tcase 'visits':\n\t\tcase 'sessions':\n\t\t\treturn count(parts.sessions)\n\t\tcase 'conversions':\n\t\t\treturn count(parts.conversions)\n\t\tcase 'revenue':\n\t\t\treturn count(parts.revenue)\n\t\tcase 'impressions':\n\t\t\treturn count(parts.impressions)\n\t\tcase 'interactions':\n\t\t\treturn count(parts.interactions)\n\t\tcase 'avgDuration':\n\t\t\treturn ratio(parts.durationMs, timedPageviews(parts))\n\t\tcase 'visitDuration':\n\t\t\treturn ratio(parts.sessionMs, parts.sessions)\n\t\tcase 'bounceRate':\n\t\t\treturn ratio(parts.bounces, parts.entries, 100)\n\t\tcase 'scrollDepth': {\n\t\t\t// One engagement is capped on the wire, but nothing caps how many arrive for a pageview.\n\t\t\tconst depth = ratio(parts.scrollSum, parts.scrollSamples)\n\t\t\treturn typeof depth === 'number' ? Math.min(100, depth) : depth\n\t\t}\n\t}\n}\n\n/**\n * The metrics a read asked for, each average its own numerator over its own denominator. A metric\n * the row's kind does not count is left out rather than read as zero.\n */\nexport const partsToMetrics = (parts: MetricParts, metrics: readonly MetricKey[]): MetricValues => {\n\tconst out: MetricValues = {}\n\tfor (const metric of metrics) {\n\t\tconst value = metricOf(parts, metric)\n\t\tif (value !== undefined) {\n\t\t\tout[metric] = value\n\t\t}\n\t}\n\treturn out\n}\n"],"mappings":";AAsCA,MAAa,YAA8C;CAC1D;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACD;;AAGA,MAAa,oBAAiC;CAC7C,WAAW;CACX,QAAQ;CACR,UAAU;CACV,UAAU;CACV,SAAS;CACT,SAAS;CACT,WAAW;CACX,YAAY;CACZ,WAAW;CACX,eAAe;CACf,aAAa;CACb,SAAS;CACT,aAAa;CACb,cAAc;CACd,yBAAyB;CACzB,yBAAyB;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AC7C,KAAK,cACJ,OAAO,MAAM,MAAM,SAAS,MAAM,SAAS,GAAG;EAC/C,KAAK,eAAe;GAEnB,MAAM,QAAQ,MAAM,MAAM,WAAW,MAAM,aAAa;GACxD,OAAO,OAAO,UAAU,WAAW,KAAK,IAAI,KAAK,KAAK,IAAI;EAC3D;CACD;AACD;;;;;AAMA,MAAa,kBAAkB,OAAoB,YAAgD;CAClG,MAAM,MAAoB,CAAC;CAC3B,KAAK,MAAM,UAAU,SAAS;EAC7B,MAAM,QAAQ,SAAS,OAAO,MAAM;EACpC,IAAI,UAAU,KAAA,GACb,IAAI,UAAU;CAEhB;CACA,OAAO;AACR"}
@@ -119,7 +119,7 @@ const checkRowLimit = (limit) => {
119
119
  /** Refuses an order by anything but the parts, or in no direction. */
120
120
  const checkOrder = (order) => {
121
121
  if (order === void 0) return;
122
- if (!isPart(order.part) || order.per !== void 0 && !isPart(order.per)) refuse("a raw read orders by its parts");
122
+ if (!isPart(order.part) || order.per !== void 0 && !isPart(order.per) || order.less !== void 0 && (order.per === void 0 || !isPart(order.less))) refuse("a raw read orders by its parts");
123
123
  if (order.dir !== "asc" && order.dir !== "desc") refuse(`"${String(order.dir)}" is no direction`);
124
124
  };
125
125
  const ALWAYS = [
@@ -133,7 +133,9 @@ const HIT_PARTS = [
133
133
  "pageviews",
134
134
  "durationMs",
135
135
  "scrollSum",
136
- "scrollSamples"
136
+ "scrollSamples",
137
+ "pageviewsWithoutVisitor",
138
+ "pageviewsWithoutConsent"
137
139
  ];
138
140
  const VISIT_PARTS = [
139
141
  "sessions",
@@ -268,7 +270,8 @@ const compareCodePoints = (a, b) => {
268
270
  const rankOf = (parts, order) => {
269
271
  const part = parts[order.part];
270
272
  if (order.per === void 0) return part;
271
- const per = parts[order.per];
273
+ const whole = parts[order.per];
274
+ const per = whole === null || order.less === void 0 ? whole : whole - (parts[order.less] ?? 0);
272
275
  return part === null || per === null || per === 0 ? null : part / per;
273
276
  };
274
277
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"rawRead.js","names":[],"sources":["../../../src/native/store/rawRead.ts"],"sourcesContent":["import { unsupportedFilter } from '../../core/capabilities'\nimport type {\n\tAnalyticsFilter,\n\tDimensionKey,\n\tFilterOperator,\n\tFilterOperatorMap,\n} from '../../core/contract'\nimport { UnsupportedFilterError } from '../../core/sourceErrors'\nimport { isOneOf } from '../../core/values'\nimport { MINUTE_MS } from '../../timeframe/tz'\nimport { emptyParts, type MetricParts, PART_KEYS } from './parts'\nimport { checkMatchesPattern } from './pattern'\nimport type {\n\tHitRow,\n\tHitWindow,\n\tRawOrder,\n\tRawRead,\n\tRawRow,\n\tRealtimePart,\n\tRealtimeRead,\n\tRealtimeResult,\n\tRealtimeTop,\n} from './types'\n\n/** The hits a raw read takes from `start` on: its scopes, its end and its hostname. */\nexport const readWindow = (read: RawRead, start: Date): HitWindow => ({\n\tscope: read.scope,\n\tscopes: read.scopes,\n\tstart,\n\tend: read.end,\n\thostname: read.hostname,\n})\n\n/**\n * A hit more than this long after the latest activity before it (an engagement's lasts until its\n * `lastAt`) opens a new visit of its visitor on that hostname.\n */\nexport const SESSION_GAP_MINUTES = 30\n\nexport const SESSION_GAP_MS = SESSION_GAP_MINUTES * MINUTE_MS\n\n/**\n * What a visit takes from the landing of the visitor's latest landing visit that day, so a visit\n * reopened by an internal link keeps its campaign; before any landing, from its own first hit.\n */\nexport const ACQUISITION_ATTRIBUTES = [\n\t'ref',\n\t'source',\n\t'channel',\n\t'clickId',\n\t'utmSource',\n\t'utmMedium',\n\t'utmCampaign',\n\t'utmContent',\n\t'utmTerm',\n] as const\n\n/** What a visit takes from its own first hit. */\nexport const VISITOR_ATTRIBUTES = [\n\t'country',\n\t'region',\n\t'city',\n\t'language',\n\t'device',\n\t'browser',\n\t'os',\n] as const\n\ntype AcquisitionAttribute = (typeof ACQUISITION_ATTRIBUTES)[number]\ntype VisitorAttribute = (typeof VISITOR_ATTRIBUTES)[number]\nexport type SessionAttribute = AcquisitionAttribute | VisitorAttribute\n\nexport const SESSION_ATTRIBUTES: readonly SessionAttribute[] = [\n\t...ACQUISITION_ATTRIBUTES,\n\t...VISITOR_ATTRIBUTES,\n]\n\n/** Whether a visit takes an attribute from its latest landing visit, not its own first hit. */\nexport const isAcquisition = (attribute: SessionAttribute): boolean =>\n\tisOneOf(ACQUISITION_ATTRIBUTES, attribute)\n\n/** The dimensions that read a hit's visit rather than the hit; a hit with no visitor has none. */\nexport const SESSION_DIMENSIONS = {\n\treferrer: 'ref',\n\tsource: 'source',\n\tchannel: 'channel',\n\tutmSource: 'utmSource',\n\tutmMedium: 'utmMedium',\n\tutmCampaign: 'utmCampaign',\n\tutmContent: 'utmContent',\n\tutmTerm: 'utmTerm',\n\tcountry: 'country',\n\tregion: 'region',\n\tcity: 'city',\n\tlanguage: 'language',\n\tdevice: 'device',\n\tbrowser: 'browser',\n\tos: 'os',\n} as const satisfies Partial<Record<DimensionKey, SessionAttribute>>\n\nexport type SessionDimension = keyof typeof SESSION_DIMENSIONS\n\n/** The dimensions that read the hit, or one of its goals or items. */\nconst HIT_DIMENSIONS = ['page', 'event', 'goal', 'currency', 'item'] as const\n\ntype HitDimension = (typeof HIT_DIMENSIONS)[number]\nexport type RawDimension = SessionDimension | HitDimension\n\nexport const isSessionDimension = (dimension: string): dimension is SessionDimension =>\n\tObject.hasOwn(SESSION_DIMENSIONS, dimension)\n\nexport const isRawDimension = (dimension: string): dimension is RawDimension =>\n\tisSessionDimension(dimension) || isOneOf(HIT_DIMENSIONS, dimension)\n\nconst RAW_DIMENSIONS: readonly RawDimension[] = [\n\t...Object.keys(SESSION_DIMENSIONS).filter(isSessionDimension),\n\t...HIT_DIMENSIONS,\n]\n\n/** A store's `features.filters` when it applies these operators to every dimension it reads. */\nexport const rawFilterMap = (operators: readonly FilterOperator[]): FilterOperatorMap => {\n\tconst map: FilterOperatorMap = {}\n\tfor (const dimension of RAW_DIMENSIONS) {\n\t\tmap[dimension] = operators\n\t}\n\treturn map\n}\n\n/** A read that ran out of its time, or on SQLite one refused because it would have. */\nexport class ReadTimeoutError extends Error {\n\treadonly timeoutMs: number\n\t/** Refused before it ran: the database answered the estimate at once, so it is not an outage. */\n\treadonly refused: boolean\n\n\tconstructor(\n\t\tstore: string,\n\t\ttimeoutMs: number,\n\t\t{ cause, refused = false }: { cause?: unknown; refused?: boolean } = {}\n\t) {\n\t\tsuper(\n\t\t\t`analytics: a raw read on ${store} cannot finish within ${timeoutMs} ms`,\n\t\t\tcause === undefined ? undefined : { cause }\n\t\t)\n\t\tthis.name = 'ReadTimeoutError'\n\t\tthis.timeoutMs = timeoutMs\n\t\tthis.refused = refused\n\t}\n}\n\n/**\n * What a read is counted from: every hit, one row per goal completion, or one per item a hit\n * carried (a click) or an engagement saw (a view).\n */\nexport type RawAxis = 'hit' | 'goal' | 'item'\n\n/** A filter as a hit's own value is tested against it. */\nexport interface HitFilter {\n\tdimension: RawDimension\n\toperator: FilterOperator\n\tvalue: string\n}\n\n/**\n * Visitors a read keeps before it cuts visits: those with a hit from `start` to the read's end\n * whose own value passes `filter` (any hit, without one). Leaving out the rest changes no number.\n */\nexport interface VisitorCondition {\n\tstart: Date\n\tfilter?: HitFilter\n}\n\nexport interface RawPlan {\n\tread: RawRead\n\taxis: RawAxis\n\t/** Visits are cut, because the read counts them or reads a visit dimension. */\n\tsessionize: boolean\n\tgroup: RawDimension | undefined\n\t/** Where the hits are read from: `read.from` when visits are cut, else `read.start`. */\n\tfrom: Date\n\t/** The parts the read counts; every other part is null. */\n\tcounted: ReadonlySet<keyof MetricParts>\n\t/**\n\t * The read groups or filters by currency, so its revenue is counted as each amount was sent: in\n\t * the currency it was sent in, as `currency` is keyed, never converted or zeroed.\n\t */\n\tsent: boolean\n\t/** Who it keeps before cutting visits; every condition holds. None when it cuts no visits. */\n\tvisitors: VisitorCondition[]\n}\n\nconst refuse = (message: string): never => {\n\tthrow new Error(`analytics: ${message}`)\n}\n\nconst isInstant = (value: unknown): value is Date =>\n\tvalue instanceof Date && !Number.isNaN(value.getTime())\n\nconst isPart = (value: unknown): value is keyof MetricParts => isOneOf(PART_KEYS, value)\n\nconst checkBuckets = (read: RawRead): void => {\n\tconst buckets = read.buckets\n\tif (buckets === undefined) {\n\t\treturn\n\t}\n\tconst first = buckets[0]\n\tif (first === undefined || !buckets.every(isInstant)) {\n\t\trefuse('buckets need at least one valid start')\n\t}\n\tif (isInstant(first) && first > read.start) {\n\t\trefuse('the first bucket has to start at or before the read')\n\t}\n\tfor (let i = 1; i < buckets.length; i++) {\n\t\tconst previous = buckets[i - 1]\n\t\tconst current = buckets[i]\n\t\tif (previous && current && current <= previous) {\n\t\t\trefuse('bucket starts have to ascend')\n\t\t}\n\t}\n}\n\n/** Refuses a read naming one scope and several, or an empty list of them. */\nexport const checkScopes = (read: { scope?: string; scopes?: readonly string[] }): void => {\n\tif (read.scopes === undefined) {\n\t\treturn\n\t}\n\tif (read.scope !== undefined) {\n\t\trefuse('a read names one scope or several, never both')\n\t}\n\tif (read.scopes.length === 0 || !read.scopes.every((scope) => typeof scope === 'string')) {\n\t\trefuse('a read of several scopes names at least one')\n\t}\n}\n\n/** Whether a scope is one a read takes: its scope, one of its scopes, or any without either. */\nexport const readsScope = (\n\tread: { scope?: string; scopes?: readonly string[] },\n\tscope: string\n): boolean =>\n\tread.scopes !== undefined\n\t\t? read.scopes.includes(scope)\n\t\t: read.scope === undefined || read.scope === scope\n\n/** Refuses a row limit that is no whole number of at least 1. */\nexport const checkRowLimit = (limit: number | undefined): void => {\n\tif (limit !== undefined && (!Number.isInteger(limit) || limit < 1)) {\n\t\trefuse(`a limit is a whole number of at least 1, got ${limit}`)\n\t}\n}\n\n/** Refuses an order by anything but the parts, or in no direction. */\nexport const checkOrder = (order: RawOrder | undefined): void => {\n\tif (order === undefined) {\n\t\treturn\n\t}\n\tif (!isPart(order.part) || (order.per !== undefined && !isPart(order.per))) {\n\t\trefuse('a raw read orders by its parts')\n\t}\n\tif (order.dir !== 'asc' && order.dir !== 'desc') {\n\t\trefuse(`\"${String(order.dir)}\" is no direction`)\n\t}\n}\n\nconst ALWAYS: ReadonlyArray<keyof MetricParts> = [\n\t'events',\n\t'visitors',\n\t'conversions',\n\t'revenue',\n\t'interactions',\n]\nconst HIT_PARTS: ReadonlyArray<keyof MetricParts> = [\n\t'pageviews',\n\t'durationMs',\n\t'scrollSum',\n\t'scrollSamples',\n]\nconst VISIT_PARTS: ReadonlyArray<keyof MetricParts> = [\n\t'sessions',\n\t'entries',\n\t'bounces',\n\t'sessionMs',\n]\n\n/**\n * An item is never viewed as a page, so its rows count views, clicks, the people behind them and\n * what they were worth; a visit is counted only where one is cut.\n */\nexport const countedParts = (axis: RawAxis, sessionize: boolean): ReadonlySet<keyof MetricParts> =>\n\tnew Set(\n\t\taxis === 'item'\n\t\t\t? [...ALWAYS, 'impressions']\n\t\t\t: [...ALWAYS, ...HIT_PARTS, ...(sessionize ? VISIT_PARTS : [])]\n\t)\n\n/**\n * Who a read cutting visits keeps (see {@link VisitorCondition}): visitors with a hit passing\n * each filter, from `from` for a visit dimension, else from the read's start, and any hit in range.\n */\nconst visitorConditions = (read: RawRead, from: Date): VisitorCondition[] => {\n\tconst filters: HitFilter[] = [\n\t\t...read.filters.flatMap((filter): HitFilter[] =>\n\t\t\tisRawDimension(filter.dimension) ? [{ ...filter, dimension: filter.dimension }] : []\n\t\t),\n\t\t...(read.path === undefined\n\t\t\t? []\n\t\t\t: [{ dimension: 'page' as const, operator: 'eq' as const, value: read.path }]),\n\t\t...(read.item === undefined\n\t\t\t? []\n\t\t\t: [{ dimension: 'item' as const, operator: 'eq' as const, value: read.item }]),\n\t]\n\tconst conditions = filters.map(\n\t\t(filter): VisitorCondition => ({\n\t\t\tstart: isSessionDimension(filter.dimension) ? from : read.start,\n\t\t\tfilter,\n\t\t})\n\t)\n\treturn from < read.start && !conditions.some((condition) => condition.start >= read.start)\n\t\t? [...conditions, { start: read.start }]\n\t\t: conditions\n}\n\n/**\n * A read checked against what the store applies, and what it takes to answer it. A filter the\n * store does not apply is refused with an `UnsupportedFilterError`; anything malformed throws.\n */\nexport const planRawRead = (\n\tread: RawRead,\n\tstore: { id: string; filters: FilterOperatorMap }\n): RawPlan => {\n\tif (!isInstant(read.start) || !isInstant(read.end)) {\n\t\trefuse('a raw read needs a valid start and end')\n\t}\n\tconst from = read.from ?? read.start\n\tif (!isInstant(from) || from > read.start) {\n\t\trefuse('a raw read cuts visits from its start or earlier')\n\t}\n\tcheckScopes(read)\n\tconst refused = unsupportedFilter(store, read.filters)\n\tif (refused) {\n\t\tthrow new UnsupportedFilterError(store.id, refused)\n\t}\n\tfor (const filter of read.filters) {\n\t\tif (filter.operator === 'matches') {\n\t\t\tcheckMatchesPattern(filter.value)\n\t\t}\n\t}\n\tconst group = read.groupBy\n\tif (group !== undefined && !isRawDimension(group)) {\n\t\trefuse(`raw reads cannot group by \"${group}\"`)\n\t}\n\tconst dimensions = [...read.filters.map((filter) => filter.dimension), ...(group ? [group] : [])]\n\tconst goal = dimensions.includes('goal')\n\tconst item = dimensions.includes('item') || read.item !== undefined\n\tif (goal && item) {\n\t\trefuse('a raw read counts goal completions or items, never both')\n\t}\n\tcheckBuckets(read)\n\tcheckOrder(read.order)\n\tif (read.values !== undefined && group === undefined) {\n\t\trefuse('values fill in a breakdown, so they need groupBy')\n\t}\n\tcheckRowLimit(read.limit)\n\tif (!Number.isInteger(read.timeoutMs) || read.timeoutMs < 1) {\n\t\trefuse(`a timeout is a whole number of milliseconds, got ${read.timeoutMs}`)\n\t}\n\tconst axis: RawAxis = goal ? 'goal' : item ? 'item' : 'hit'\n\tconst sessionize = read.sessions || dimensions.some(isSessionDimension)\n\treturn {\n\t\tread,\n\t\taxis,\n\t\tsessionize,\n\t\tgroup: group !== undefined && isRawDimension(group) ? group : undefined,\n\t\tfrom: sessionize ? from : read.start,\n\t\tcounted: countedParts(axis, sessionize),\n\t\tsent: dimensions.includes('currency'),\n\t\tvisitors: sessionize ? visitorConditions(read, from) : [],\n\t}\n}\n\n/**\n * The visit attributes a planned read takes, its group's and its filters' where they read a visit,\n * so a store cutting visits carries those alone.\n */\nexport const visitAttributesOf = (plan: RawPlan): SessionAttribute[] => {\n\tconst dimensions: string[] = [\n\t\t...plan.read.filters.map((filter) => filter.dimension),\n\t\t...(plan.group === undefined ? [] : [plan.group]),\n\t]\n\tconst taken = new Set<SessionAttribute>(\n\t\tdimensions.filter(isSessionDimension).map((dimension) => SESSION_DIMENSIONS[dimension])\n\t)\n\treturn SESSION_ATTRIBUTES.filter((attribute) => taken.has(attribute))\n}\n\n/** Refuses a bound on a count that is no whole number of at least 0. */\nexport const checkCountBound = (atMost: number): number => {\n\tif (!Number.isInteger(atMost) || atMost < 0) {\n\t\trefuse(`a count is bounded by a whole number of at least 0, got ${atMost}`)\n\t}\n\treturn atMost\n}\n\n/**\n * A hit's own value of a dimension, as a visitor condition tests it: a goal's or an item's is any\n * of the hit's goals or items, a visit dimension's the hit's own attribute, not its visit's.\n */\nconst hitValuesOf = (hit: HitRow, dimension: RawDimension): readonly string[] => {\n\tconst one = (value: string | undefined): readonly string[] => (value === undefined ? [] : [value])\n\tswitch (dimension) {\n\t\tcase 'page':\n\t\t\treturn [hit.path]\n\t\tcase 'event':\n\t\t\treturn hit.type === 'event' ? one(hit.name) : []\n\t\tcase 'currency':\n\t\t\treturn one(hit.originalCurrency ?? hit.currency)\n\t\tcase 'goal':\n\t\t\treturn (hit.goals ?? []).map((goal) => goal.slug)\n\t\tcase 'item':\n\t\t\treturn [...one(hit.item), ...(hit.impressions ?? [])]\n\t\tdefault:\n\t\t\treturn one(hit[SESSION_DIMENSIONS[dimension]])\n\t}\n}\n\n/** The key one visitor's hits share: a visitor id is per scope and hostname. */\nexport const visitorKeyOf = (hit: Pick<HitRow, 'scope' | 'hostname' | 'v'>): string =>\n\tJSON.stringify([hit.scope, hit.hostname, hit.v])\n\n/** Whether a hit meets a visitor condition: in its window, its own value passing its filter. */\nexport const meetsCondition = (hit: HitRow, condition: VisitorCondition, end: Date): boolean => {\n\tif (hit.ts < condition.start || hit.ts >= end) {\n\t\treturn false\n\t}\n\tconst { filter } = condition\n\treturn (\n\t\tfilter === undefined ||\n\t\thitValuesOf(hit, filter.dimension).some((value) => filterHolds(value, filter))\n\t)\n}\n\n/** Whether a planned raw or aggregate read can have no rows: an exact fill with no values to fill. */\nexport const fillsNothing = (plan: { read: Pick<RawRead, 'values'> }): boolean =>\n\tplan.read.values?.length === 0\n\n/** Whether one value passes a filter as the memory store applies it, case folded but for `eq`. */\nexport const filterHolds = (value: string, filter: AnalyticsFilter): boolean => {\n\tif (filter.operator === 'eq') {\n\t\treturn value === filter.value\n\t}\n\tif (filter.operator === 'contains') {\n\t\treturn value.toLowerCase().includes(filter.value.toLowerCase())\n\t}\n\treturn new RegExp(filter.value, 'i').test(value)\n}\n\n/** Parts out of what a store answered, the counted ones as numbers and the rest null. */\nexport const partsFrom = (\n\tcounted: ReadonlySet<keyof MetricParts>,\n\tpart: (key: keyof MetricParts) => number | null\n): MetricParts => {\n\tconst parts = emptyParts()\n\tfor (const key of PART_KEYS) {\n\t\tparts[key] = counted.has(key) ? (part(key) ?? 0) : null\n\t}\n\treturn parts\n}\n\n/** The parts a grouped row must count some of to be kept: a pageview, an event or a view. */\nexport const KEPT_PARTS = ['pageviews', 'events', 'impressions'] as const satisfies ReadonlyArray<\n\tkeyof MetricParts\n>\n\n/** Whether a grouped row, raw or aggregate, is kept: it counted some of {@link KEPT_PARTS}. */\nexport const countsSomething = (parts: MetricParts): boolean =>\n\tKEPT_PARTS.reduce((sum, part) => sum + (parts[part] ?? 0), 0) > 0\n\n/** The single row a read without groups answers when nothing matched. */\nexport const emptyRow = (plan: RawPlan): RawRow => ({\n\tvalue: null,\n\tparts: partsFrom(plan.counted, () => 0),\n})\n\n/** Strings in code point order, as SQLite, MongoDB and Postgres' C collation compare them. */\nexport const compareCodePoints = (a: string, b: string): number => {\n\tconst left = [...a]\n\tconst right = [...b]\n\tfor (let i = 0; i < Math.min(left.length, right.length); i++) {\n\t\tconst x = left[i]?.codePointAt(0) ?? 0\n\t\tconst y = right[i]?.codePointAt(0) ?? 0\n\t\tif (x !== y) {\n\t\t\treturn x - y\n\t\t}\n\t}\n\treturn left.length - right.length\n}\n\n/** What a row ranks by: a part, or one part over another, null where there is nothing to divide. */\nexport const rankOf = (parts: MetricParts, order: RawOrder): number | null => {\n\tconst part = parts[order.part]\n\tif (order.per === undefined) {\n\t\treturn part\n\t}\n\tconst per = parts[order.per]\n\treturn part === null || per === null || per === 0 ? null : part / per\n}\n\n/**\n * The order every store answers in: by rank (nothing to rank last), then value (a total's null\n * first), then `position`, a raw row's bucket or an aggregate row's day.\n */\nexport const compareRanked =\n\t<R extends { value: string | null; parts: MetricParts }>(\n\t\torder: RawOrder | undefined,\n\t\tposition: (row: R) => number\n\t) =>\n\t(a: R, b: R): number => {\n\t\tif (order) {\n\t\t\tconst x = rankOf(a.parts, order)\n\t\t\tconst y = rankOf(b.parts, order)\n\t\t\tif (x !== y) {\n\t\t\t\tif (x === null) {\n\t\t\t\t\treturn 1\n\t\t\t\t}\n\t\t\t\tif (y === null) {\n\t\t\t\t\treturn -1\n\t\t\t\t}\n\t\t\t\treturn order.dir === 'asc' ? x - y : y - x\n\t\t\t}\n\t\t}\n\t\tif (a.value !== b.value) {\n\t\t\tif (a.value === null) {\n\t\t\t\treturn -1\n\t\t\t}\n\t\t\tif (b.value === null) {\n\t\t\t\treturn 1\n\t\t\t}\n\t\t\treturn compareCodePoints(a.value, b.value)\n\t\t}\n\t\treturn position(a) - position(b)\n\t}\n\n/** {@link compareRanked} for raw rows, ties going by bucket. */\nexport const compareRows = (order: RawOrder | undefined): ((a: RawRow, b: RawRow) => number) =>\n\tcompareRanked<RawRow>(order, (row) => row.bucket ?? 0)\n\n/** The start of every minute from the one holding `start` up to `end`. */\nexport const minuteStarts = (start: Date, end: Date): Date[] => {\n\tconst starts: Date[] = []\n\tfor (\n\t\tlet at = start.getTime() - (((start.getTime() % MINUTE_MS) + MINUTE_MS) % MINUTE_MS);\n\t\tat < end.getTime();\n\t\tat += MINUTE_MS\n\t) {\n\t\tstarts.push(new Date(at))\n\t}\n\treturn starts.length > 0 ? starts : [new Date(start.getTime())]\n}\n\n/** Rows each ranked part of the live view lists when the read names no `top`. */\nexport const DEFAULT_TOP = 10\n\nconst countOf = (row: RawRow | undefined): { visitors: number; pageviews: number } => ({\n\tvisitors: row?.parts.visitors ?? 0,\n\tpageviews: row?.parts.pageviews ?? 0,\n})\n\nconst tops = (rows: readonly RawRow[]): RealtimeTop[] =>\n\trows.flatMap((row) => (row.value === null ? [] : [{ value: row.value, ...countOf(row) }]))\n\nconst REALTIME_PARTS: readonly RealtimePart[] = ['minutes', 'pages', 'sources']\n\n/**\n * The live view out of raw reads on every store; a part that fails fails the whole, never read as\n * no activity. Sources cut visits from `from` for the window's visitors alone (`VisitorCondition`).\n */\nexport const readRealtime = async (\n\treadRaw: (read: RawRead) => Promise<RawRow[]>,\n\tread: RealtimeRead\n): Promise<RealtimeResult> => {\n\tconst top = read.top ?? DEFAULT_TOP\n\tif (!Number.isInteger(top) || top < 1) {\n\t\trefuse(`top is a whole number of at least 1, got ${top}`)\n\t}\n\tconst wanted = new Set(read.parts ?? REALTIME_PARTS)\n\tfor (const part of wanted) {\n\t\tif (!REALTIME_PARTS.includes(part)) {\n\t\t\trefuse(`a realtime read answers ${REALTIME_PARTS.join(', ')}, not \"${String(part)}\"`)\n\t\t}\n\t}\n\tconst base: RawRead = {\n\t\t...(read.scopes === undefined ? { scope: read.scope } : { scopes: read.scopes }),\n\t\tstart: read.start,\n\t\tend: read.end,\n\t\thostname: read.hostname,\n\t\tpath: read.path,\n\t\tfilters: [],\n\t\tsessions: false,\n\t\ttimeoutMs: read.timeoutMs,\n\t\t...(read.signal ? { signal: read.signal } : {}),\n\t}\n\tconst ranked = { order: { part: 'visitors', dir: 'desc' }, limit: top } as const\n\tconst buckets = minuteStarts(read.start, read.end)\n\tconst when = (part: RealtimePart, raw: RawRead): Promise<RawRow[]> =>\n\t\twanted.has(part) ? readRaw(raw) : Promise.resolve([])\n\tconst [totals, ...parts] = await Promise.allSettled([\n\t\treadRaw(base),\n\t\twhen('minutes', { ...base, buckets }),\n\t\twhen('pages', { ...base, groupBy: 'page', ...ranked }),\n\t\twhen('sources', { ...base, from: read.from, groupBy: 'source', ...ranked }),\n\t])\n\tif (totals.status === 'rejected') {\n\t\tthrow totals.reason\n\t}\n\tconst [minutes, pages, sources] = parts.map((outcome): RawRow[] => {\n\t\tif (outcome.status === 'rejected') {\n\t\t\tthrow outcome.reason\n\t\t}\n\t\treturn outcome.value\n\t})\n\treturn {\n\t\t...countOf(totals.value[0]),\n\t\tminutes: (minutes ?? []).flatMap((row) => {\n\t\t\tconst minute = row.bucket === undefined ? undefined : buckets[row.bucket]\n\t\t\treturn minute ? [{ minute, ...countOf(row) }] : []\n\t\t}),\n\t\tpages: tops(pages ?? []),\n\t\tsources: tops(sources ?? []),\n\t}\n}\n"],"mappings":";;;;;;;;AAyBA,MAAa,cAAc,MAAe,WAA4B;CACrE,OAAO,KAAK;CACZ,QAAQ,KAAK;CACb;CACA,KAAK,KAAK;CACV,UAAU,KAAK;AAChB;AAQA,MAAa,iBAAA,KAAuC;;;;;AAMpD,MAAa,yBAAyB;CACrC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACD;;AAGA,MAAa,qBAAqB;CACjC;CACA;CACA;CACA;CACA;CACA;CACA;AACD;AAMA,MAAa,qBAAkD,CAC9D,GAAG,wBACH,GAAG,kBACJ;;AAGA,MAAa,iBAAiB,cAC7B,QAAQ,wBAAwB,SAAS;;AAG1C,MAAa,qBAAqB;CACjC,UAAU;CACV,QAAQ;CACR,SAAS;CACT,WAAW;CACX,WAAW;CACX,aAAa;CACb,YAAY;CACZ,SAAS;CACT,SAAS;CACT,QAAQ;CACR,MAAM;CACN,UAAU;CACV,QAAQ;CACR,SAAS;CACT,IAAI;AACL;;AAKA,MAAM,iBAAiB;CAAC;CAAQ;CAAS;CAAQ;CAAY;AAA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CAC5B;AACD"}
1
+ {"version":3,"file":"rawRead.js","names":[],"sources":["../../../src/native/store/rawRead.ts"],"sourcesContent":["import { unsupportedFilter } from '../../core/capabilities'\nimport type {\n\tAnalyticsFilter,\n\tDimensionKey,\n\tFilterOperator,\n\tFilterOperatorMap,\n} from '../../core/contract'\nimport { UnsupportedFilterError } from '../../core/sourceErrors'\nimport { isOneOf } from '../../core/values'\nimport { MINUTE_MS } from '../../timeframe/tz'\nimport { emptyParts, type MetricParts, PART_KEYS } from './parts'\nimport { checkMatchesPattern } from './pattern'\nimport type {\n\tHitRow,\n\tHitWindow,\n\tRawOrder,\n\tRawRead,\n\tRawRow,\n\tRealtimePart,\n\tRealtimeRead,\n\tRealtimeResult,\n\tRealtimeTop,\n} from './types'\n\n/** The hits a raw read takes from `start` on: its scopes, its end and its hostname. */\nexport const readWindow = (read: RawRead, start: Date): HitWindow => ({\n\tscope: read.scope,\n\tscopes: read.scopes,\n\tstart,\n\tend: read.end,\n\thostname: read.hostname,\n})\n\n/**\n * A hit more than this long after the latest activity before it (an engagement's lasts until its\n * `lastAt`) opens a new visit of its visitor on that hostname.\n */\nexport const SESSION_GAP_MINUTES = 30\n\nexport const SESSION_GAP_MS = SESSION_GAP_MINUTES * MINUTE_MS\n\n/**\n * What a visit takes from the landing of the visitor's latest landing visit that day, so a visit\n * reopened by an internal link keeps its campaign; before any landing, from its own first hit.\n */\nexport const ACQUISITION_ATTRIBUTES = [\n\t'ref',\n\t'source',\n\t'channel',\n\t'clickId',\n\t'utmSource',\n\t'utmMedium',\n\t'utmCampaign',\n\t'utmContent',\n\t'utmTerm',\n] as const\n\n/** What a visit takes from its own first hit. */\nexport const VISITOR_ATTRIBUTES = [\n\t'country',\n\t'region',\n\t'city',\n\t'language',\n\t'device',\n\t'browser',\n\t'os',\n] as const\n\ntype AcquisitionAttribute = (typeof ACQUISITION_ATTRIBUTES)[number]\ntype VisitorAttribute = (typeof VISITOR_ATTRIBUTES)[number]\nexport type SessionAttribute = AcquisitionAttribute | VisitorAttribute\n\nexport const SESSION_ATTRIBUTES: readonly SessionAttribute[] = [\n\t...ACQUISITION_ATTRIBUTES,\n\t...VISITOR_ATTRIBUTES,\n]\n\n/** Whether a visit takes an attribute from its latest landing visit, not its own first hit. */\nexport const isAcquisition = (attribute: SessionAttribute): boolean =>\n\tisOneOf(ACQUISITION_ATTRIBUTES, attribute)\n\n/** The dimensions that read a hit's visit rather than the hit; a hit with no visitor has none. */\nexport const SESSION_DIMENSIONS = {\n\treferrer: 'ref',\n\tsource: 'source',\n\tchannel: 'channel',\n\tutmSource: 'utmSource',\n\tutmMedium: 'utmMedium',\n\tutmCampaign: 'utmCampaign',\n\tutmContent: 'utmContent',\n\tutmTerm: 'utmTerm',\n\tcountry: 'country',\n\tregion: 'region',\n\tcity: 'city',\n\tlanguage: 'language',\n\tdevice: 'device',\n\tbrowser: 'browser',\n\tos: 'os',\n} as const satisfies Partial<Record<DimensionKey, SessionAttribute>>\n\nexport type SessionDimension = keyof typeof SESSION_DIMENSIONS\n\n/** The dimensions that read the hit, or one of its goals or items. */\nconst HIT_DIMENSIONS = ['page', 'event', 'goal', 'currency', 'item'] as const\n\ntype HitDimension = (typeof HIT_DIMENSIONS)[number]\nexport type RawDimension = SessionDimension | HitDimension\n\nexport const isSessionDimension = (dimension: string): dimension is SessionDimension =>\n\tObject.hasOwn(SESSION_DIMENSIONS, dimension)\n\nexport const isRawDimension = (dimension: string): dimension is RawDimension =>\n\tisSessionDimension(dimension) || isOneOf(HIT_DIMENSIONS, dimension)\n\nconst RAW_DIMENSIONS: readonly RawDimension[] = [\n\t...Object.keys(SESSION_DIMENSIONS).filter(isSessionDimension),\n\t...HIT_DIMENSIONS,\n]\n\n/** A store's `features.filters` when it applies these operators to every dimension it reads. */\nexport const rawFilterMap = (operators: readonly FilterOperator[]): FilterOperatorMap => {\n\tconst map: FilterOperatorMap = {}\n\tfor (const dimension of RAW_DIMENSIONS) {\n\t\tmap[dimension] = operators\n\t}\n\treturn map\n}\n\n/** A read that ran out of its time, or on SQLite one refused because it would have. */\nexport class ReadTimeoutError extends Error {\n\treadonly timeoutMs: number\n\t/** Refused before it ran: the database answered the estimate at once, so it is not an outage. */\n\treadonly refused: boolean\n\n\tconstructor(\n\t\tstore: string,\n\t\ttimeoutMs: number,\n\t\t{ cause, refused = false }: { cause?: unknown; refused?: boolean } = {}\n\t) {\n\t\tsuper(\n\t\t\t`analytics: a raw read on ${store} cannot finish within ${timeoutMs} ms`,\n\t\t\tcause === undefined ? undefined : { cause }\n\t\t)\n\t\tthis.name = 'ReadTimeoutError'\n\t\tthis.timeoutMs = timeoutMs\n\t\tthis.refused = refused\n\t}\n}\n\n/**\n * What a read is counted from: every hit, one row per goal completion, or one per item a hit\n * carried (a click) or an engagement saw (a view).\n */\nexport type RawAxis = 'hit' | 'goal' | 'item'\n\n/** A filter as a hit's own value is tested against it. */\nexport interface HitFilter {\n\tdimension: RawDimension\n\toperator: FilterOperator\n\tvalue: string\n}\n\n/**\n * Visitors a read keeps before it cuts visits: those with a hit from `start` to the read's end\n * whose own value passes `filter` (any hit, without one). Leaving out the rest changes no number.\n */\nexport interface VisitorCondition {\n\tstart: Date\n\tfilter?: HitFilter\n}\n\nexport interface RawPlan {\n\tread: RawRead\n\taxis: RawAxis\n\t/** Visits are cut, because the read counts them or reads a visit dimension. */\n\tsessionize: boolean\n\tgroup: RawDimension | undefined\n\t/** Where the hits are read from: `read.from` when visits are cut, else `read.start`. */\n\tfrom: Date\n\t/** The parts the read counts; every other part is null. */\n\tcounted: ReadonlySet<keyof MetricParts>\n\t/**\n\t * The read groups or filters by currency, so its revenue is counted as each amount was sent: in\n\t * the currency it was sent in, as `currency` is keyed, never converted or zeroed.\n\t */\n\tsent: boolean\n\t/** Who it keeps before cutting visits; every condition holds. None when it cuts no visits. */\n\tvisitors: VisitorCondition[]\n}\n\nconst refuse = (message: string): never => {\n\tthrow new Error(`analytics: ${message}`)\n}\n\nconst isInstant = (value: unknown): value is Date =>\n\tvalue instanceof Date && !Number.isNaN(value.getTime())\n\nconst isPart = (value: unknown): value is keyof MetricParts => isOneOf(PART_KEYS, value)\n\nconst checkBuckets = (read: RawRead): void => {\n\tconst buckets = read.buckets\n\tif (buckets === undefined) {\n\t\treturn\n\t}\n\tconst first = buckets[0]\n\tif (first === undefined || !buckets.every(isInstant)) {\n\t\trefuse('buckets need at least one valid start')\n\t}\n\tif (isInstant(first) && first > read.start) {\n\t\trefuse('the first bucket has to start at or before the read')\n\t}\n\tfor (let i = 1; i < buckets.length; i++) {\n\t\tconst previous = buckets[i - 1]\n\t\tconst current = buckets[i]\n\t\tif (previous && current && current <= previous) {\n\t\t\trefuse('bucket starts have to ascend')\n\t\t}\n\t}\n}\n\n/** Refuses a read naming one scope and several, or an empty list of them. */\nexport const checkScopes = (read: { scope?: string; scopes?: readonly string[] }): void => {\n\tif (read.scopes === undefined) {\n\t\treturn\n\t}\n\tif (read.scope !== undefined) {\n\t\trefuse('a read names one scope or several, never both')\n\t}\n\tif (read.scopes.length === 0 || !read.scopes.every((scope) => typeof scope === 'string')) {\n\t\trefuse('a read of several scopes names at least one')\n\t}\n}\n\n/** Whether a scope is one a read takes: its scope, one of its scopes, or any without either. */\nexport const readsScope = (\n\tread: { scope?: string; scopes?: readonly string[] },\n\tscope: string\n): boolean =>\n\tread.scopes !== undefined\n\t\t? read.scopes.includes(scope)\n\t\t: read.scope === undefined || read.scope === scope\n\n/** Refuses a row limit that is no whole number of at least 1. */\nexport const checkRowLimit = (limit: number | undefined): void => {\n\tif (limit !== undefined && (!Number.isInteger(limit) || limit < 1)) {\n\t\trefuse(`a limit is a whole number of at least 1, got ${limit}`)\n\t}\n}\n\n/** Refuses an order by anything but the parts, or in no direction. */\nexport const checkOrder = (order: RawOrder | undefined): void => {\n\tif (order === undefined) {\n\t\treturn\n\t}\n\tif (\n\t\t!isPart(order.part) ||\n\t\t(order.per !== undefined && !isPart(order.per)) ||\n\t\t(order.less !== undefined && (order.per === undefined || !isPart(order.less)))\n\t) {\n\t\trefuse('a raw read orders by its parts')\n\t}\n\tif (order.dir !== 'asc' && order.dir !== 'desc') {\n\t\trefuse(`\"${String(order.dir)}\" is no direction`)\n\t}\n}\n\nconst ALWAYS: ReadonlyArray<keyof MetricParts> = [\n\t'events',\n\t'visitors',\n\t'conversions',\n\t'revenue',\n\t'interactions',\n]\nconst HIT_PARTS: ReadonlyArray<keyof MetricParts> = [\n\t'pageviews',\n\t'durationMs',\n\t'scrollSum',\n\t'scrollSamples',\n\t'pageviewsWithoutVisitor',\n\t'pageviewsWithoutConsent',\n]\nconst VISIT_PARTS: ReadonlyArray<keyof MetricParts> = [\n\t'sessions',\n\t'entries',\n\t'bounces',\n\t'sessionMs',\n]\n\n/**\n * An item is never viewed as a page, so its rows count views, clicks, the people behind them and\n * what they were worth; a visit is counted only where one is cut.\n */\nexport const countedParts = (axis: RawAxis, sessionize: boolean): ReadonlySet<keyof MetricParts> =>\n\tnew Set(\n\t\taxis === 'item'\n\t\t\t? [...ALWAYS, 'impressions']\n\t\t\t: [...ALWAYS, ...HIT_PARTS, ...(sessionize ? VISIT_PARTS : [])]\n\t)\n\n/**\n * Who a read cutting visits keeps (see {@link VisitorCondition}): visitors with a hit passing\n * each filter, from `from` for a visit dimension, else from the read's start, and any hit in range.\n */\nconst visitorConditions = (read: RawRead, from: Date): VisitorCondition[] => {\n\tconst filters: HitFilter[] = [\n\t\t...read.filters.flatMap((filter): HitFilter[] =>\n\t\t\tisRawDimension(filter.dimension) ? [{ ...filter, dimension: filter.dimension }] : []\n\t\t),\n\t\t...(read.path === undefined\n\t\t\t? []\n\t\t\t: [{ dimension: 'page' as const, operator: 'eq' as const, value: read.path }]),\n\t\t...(read.item === undefined\n\t\t\t? []\n\t\t\t: [{ dimension: 'item' as const, operator: 'eq' as const, value: read.item }]),\n\t]\n\tconst conditions = filters.map(\n\t\t(filter): VisitorCondition => ({\n\t\t\tstart: isSessionDimension(filter.dimension) ? from : read.start,\n\t\t\tfilter,\n\t\t})\n\t)\n\treturn from < read.start && !conditions.some((condition) => condition.start >= read.start)\n\t\t? [...conditions, { start: read.start }]\n\t\t: conditions\n}\n\n/**\n * A read checked against what the store applies, and what it takes to answer it. A filter the\n * store does not apply is refused with an `UnsupportedFilterError`; anything malformed throws.\n */\nexport const planRawRead = (\n\tread: RawRead,\n\tstore: { id: string; filters: FilterOperatorMap }\n): RawPlan => {\n\tif (!isInstant(read.start) || !isInstant(read.end)) {\n\t\trefuse('a raw read needs a valid start and end')\n\t}\n\tconst from = read.from ?? read.start\n\tif (!isInstant(from) || from > read.start) {\n\t\trefuse('a raw read cuts visits from its start or earlier')\n\t}\n\tcheckScopes(read)\n\tconst refused = unsupportedFilter(store, read.filters)\n\tif (refused) {\n\t\tthrow new UnsupportedFilterError(store.id, refused)\n\t}\n\tfor (const filter of read.filters) {\n\t\tif (filter.operator === 'matches') {\n\t\t\tcheckMatchesPattern(filter.value)\n\t\t}\n\t}\n\tconst group = read.groupBy\n\tif (group !== undefined && !isRawDimension(group)) {\n\t\trefuse(`raw reads cannot group by \"${group}\"`)\n\t}\n\tconst dimensions = [...read.filters.map((filter) => filter.dimension), ...(group ? [group] : [])]\n\tconst goal = dimensions.includes('goal')\n\tconst item = dimensions.includes('item') || read.item !== undefined\n\tif (goal && item) {\n\t\trefuse('a raw read counts goal completions or items, never both')\n\t}\n\tcheckBuckets(read)\n\tcheckOrder(read.order)\n\tif (read.values !== undefined && group === undefined) {\n\t\trefuse('values fill in a breakdown, so they need groupBy')\n\t}\n\tcheckRowLimit(read.limit)\n\tif (!Number.isInteger(read.timeoutMs) || read.timeoutMs < 1) {\n\t\trefuse(`a timeout is a whole number of milliseconds, got ${read.timeoutMs}`)\n\t}\n\tconst axis: RawAxis = goal ? 'goal' : item ? 'item' : 'hit'\n\tconst sessionize = read.sessions || dimensions.some(isSessionDimension)\n\treturn {\n\t\tread,\n\t\taxis,\n\t\tsessionize,\n\t\tgroup: group !== undefined && isRawDimension(group) ? group : undefined,\n\t\tfrom: sessionize ? from : read.start,\n\t\tcounted: countedParts(axis, sessionize),\n\t\tsent: dimensions.includes('currency'),\n\t\tvisitors: sessionize ? visitorConditions(read, from) : [],\n\t}\n}\n\n/**\n * The visit attributes a planned read takes, its group's and its filters' where they read a visit,\n * so a store cutting visits carries those alone.\n */\nexport const visitAttributesOf = (plan: RawPlan): SessionAttribute[] => {\n\tconst dimensions: string[] = [\n\t\t...plan.read.filters.map((filter) => filter.dimension),\n\t\t...(plan.group === undefined ? [] : [plan.group]),\n\t]\n\tconst taken = new Set<SessionAttribute>(\n\t\tdimensions.filter(isSessionDimension).map((dimension) => SESSION_DIMENSIONS[dimension])\n\t)\n\treturn SESSION_ATTRIBUTES.filter((attribute) => taken.has(attribute))\n}\n\n/** Refuses a bound on a count that is no whole number of at least 0. */\nexport const checkCountBound = (atMost: number): number => {\n\tif (!Number.isInteger(atMost) || atMost < 0) {\n\t\trefuse(`a count is bounded by a whole number of at least 0, got ${atMost}`)\n\t}\n\treturn atMost\n}\n\n/**\n * A hit's own value of a dimension, as a visitor condition tests it: a goal's or an item's is any\n * of the hit's goals or items, a visit dimension's the hit's own attribute, not its visit's.\n */\nconst hitValuesOf = (hit: HitRow, dimension: RawDimension): readonly string[] => {\n\tconst one = (value: string | undefined): readonly string[] => (value === undefined ? [] : [value])\n\tswitch (dimension) {\n\t\tcase 'page':\n\t\t\treturn [hit.path]\n\t\tcase 'event':\n\t\t\treturn hit.type === 'event' ? one(hit.name) : []\n\t\tcase 'currency':\n\t\t\treturn one(hit.originalCurrency ?? hit.currency)\n\t\tcase 'goal':\n\t\t\treturn (hit.goals ?? []).map((goal) => goal.slug)\n\t\tcase 'item':\n\t\t\treturn [...one(hit.item), ...(hit.impressions ?? [])]\n\t\tdefault:\n\t\t\treturn one(hit[SESSION_DIMENSIONS[dimension]])\n\t}\n}\n\n/** The key one visitor's hits share: a visitor id is per scope and hostname. */\nexport const visitorKeyOf = (hit: Pick<HitRow, 'scope' | 'hostname' | 'v'>): string =>\n\tJSON.stringify([hit.scope, hit.hostname, hit.v])\n\n/** Whether a hit meets a visitor condition: in its window, its own value passing its filter. */\nexport const meetsCondition = (hit: HitRow, condition: VisitorCondition, end: Date): boolean => {\n\tif (hit.ts < condition.start || hit.ts >= end) {\n\t\treturn false\n\t}\n\tconst { filter } = condition\n\treturn (\n\t\tfilter === undefined ||\n\t\thitValuesOf(hit, filter.dimension).some((value) => filterHolds(value, filter))\n\t)\n}\n\n/** Whether a planned raw or aggregate read can have no rows: an exact fill with no values to fill. */\nexport const fillsNothing = (plan: { read: Pick<RawRead, 'values'> }): boolean =>\n\tplan.read.values?.length === 0\n\n/** Whether one value passes a filter as the memory store applies it, case folded but for `eq`. */\nexport const filterHolds = (value: string, filter: AnalyticsFilter): boolean => {\n\tif (filter.operator === 'eq') {\n\t\treturn value === filter.value\n\t}\n\tif (filter.operator === 'contains') {\n\t\treturn value.toLowerCase().includes(filter.value.toLowerCase())\n\t}\n\treturn new RegExp(filter.value, 'i').test(value)\n}\n\n/** Parts out of what a store answered, the counted ones as numbers and the rest null. */\nexport const partsFrom = (\n\tcounted: ReadonlySet<keyof MetricParts>,\n\tpart: (key: keyof MetricParts) => number | null\n): MetricParts => {\n\tconst parts = emptyParts()\n\tfor (const key of PART_KEYS) {\n\t\tparts[key] = counted.has(key) ? (part(key) ?? 0) : null\n\t}\n\treturn parts\n}\n\n/** The parts a grouped row must count some of to be kept: a pageview, an event or a view. */\nexport const KEPT_PARTS = ['pageviews', 'events', 'impressions'] as const satisfies ReadonlyArray<\n\tkeyof MetricParts\n>\n\n/** Whether a grouped row, raw or aggregate, is kept: it counted some of {@link KEPT_PARTS}. */\nexport const countsSomething = (parts: MetricParts): boolean =>\n\tKEPT_PARTS.reduce((sum, part) => sum + (parts[part] ?? 0), 0) > 0\n\n/** The single row a read without groups answers when nothing matched. */\nexport const emptyRow = (plan: RawPlan): RawRow => ({\n\tvalue: null,\n\tparts: partsFrom(plan.counted, () => 0),\n})\n\n/** Strings in code point order, as SQLite, MongoDB and Postgres' C collation compare them. */\nexport const compareCodePoints = (a: string, b: string): number => {\n\tconst left = [...a]\n\tconst right = [...b]\n\tfor (let i = 0; i < Math.min(left.length, right.length); i++) {\n\t\tconst x = left[i]?.codePointAt(0) ?? 0\n\t\tconst y = right[i]?.codePointAt(0) ?? 0\n\t\tif (x !== y) {\n\t\t\treturn x - y\n\t\t}\n\t}\n\treturn left.length - right.length\n}\n\n/** What a row ranks by: a part, or one part over another, null where there is nothing to divide. */\nexport const rankOf = (parts: MetricParts, order: RawOrder): number | null => {\n\tconst part = parts[order.part]\n\tif (order.per === undefined) {\n\t\treturn part\n\t}\n\tconst whole = parts[order.per]\n\tconst per = whole === null || order.less === undefined ? whole : whole - (parts[order.less] ?? 0)\n\treturn part === null || per === null || per === 0 ? null : part / per\n}\n\n/**\n * The order every store answers in: by rank (nothing to rank last), then value (a total's null\n * first), then `position`, a raw row's bucket or an aggregate row's day.\n */\nexport const compareRanked =\n\t<R extends { value: string | null; parts: MetricParts }>(\n\t\torder: RawOrder | undefined,\n\t\tposition: (row: R) => number\n\t) =>\n\t(a: R, b: R): number => {\n\t\tif (order) {\n\t\t\tconst x = rankOf(a.parts, order)\n\t\t\tconst y = rankOf(b.parts, order)\n\t\t\tif (x !== y) {\n\t\t\t\tif (x === null) {\n\t\t\t\t\treturn 1\n\t\t\t\t}\n\t\t\t\tif (y === null) {\n\t\t\t\t\treturn -1\n\t\t\t\t}\n\t\t\t\treturn order.dir === 'asc' ? x - y : y - x\n\t\t\t}\n\t\t}\n\t\tif (a.value !== b.value) {\n\t\t\tif (a.value === null) {\n\t\t\t\treturn -1\n\t\t\t}\n\t\t\tif (b.value === null) {\n\t\t\t\treturn 1\n\t\t\t}\n\t\t\treturn compareCodePoints(a.value, b.value)\n\t\t}\n\t\treturn position(a) - position(b)\n\t}\n\n/** {@link compareRanked} for raw rows, ties going by bucket. */\nexport const compareRows = (order: RawOrder | undefined): ((a: RawRow, b: RawRow) => number) =>\n\tcompareRanked<RawRow>(order, (row) => row.bucket ?? 0)\n\n/** The start of every minute from the one holding `start` up to `end`. */\nexport const minuteStarts = (start: Date, end: Date): Date[] => {\n\tconst starts: Date[] = []\n\tfor (\n\t\tlet at = start.getTime() - (((start.getTime() % MINUTE_MS) + MINUTE_MS) % MINUTE_MS);\n\t\tat < end.getTime();\n\t\tat += MINUTE_MS\n\t) {\n\t\tstarts.push(new Date(at))\n\t}\n\treturn starts.length > 0 ? starts : [new Date(start.getTime())]\n}\n\n/** Rows each ranked part of the live view lists when the read names no `top`. */\nexport const DEFAULT_TOP = 10\n\nconst countOf = (row: RawRow | undefined): { visitors: number; pageviews: number } => ({\n\tvisitors: row?.parts.visitors ?? 0,\n\tpageviews: row?.parts.pageviews ?? 0,\n})\n\nconst tops = (rows: readonly RawRow[]): RealtimeTop[] =>\n\trows.flatMap((row) => (row.value === null ? [] : [{ value: row.value, ...countOf(row) }]))\n\nconst REALTIME_PARTS: readonly RealtimePart[] = ['minutes', 'pages', 'sources']\n\n/**\n * The live view out of raw reads on every store; a part that fails fails the whole, never read as\n * no activity. Sources cut visits from `from` for the window's visitors alone (`VisitorCondition`).\n */\nexport const readRealtime = async (\n\treadRaw: (read: RawRead) => Promise<RawRow[]>,\n\tread: RealtimeRead\n): Promise<RealtimeResult> => {\n\tconst top = read.top ?? DEFAULT_TOP\n\tif (!Number.isInteger(top) || top < 1) {\n\t\trefuse(`top is a whole number of at least 1, got ${top}`)\n\t}\n\tconst wanted = new Set(read.parts ?? REALTIME_PARTS)\n\tfor (const part of wanted) {\n\t\tif (!REALTIME_PARTS.includes(part)) {\n\t\t\trefuse(`a realtime read answers ${REALTIME_PARTS.join(', ')}, not \"${String(part)}\"`)\n\t\t}\n\t}\n\tconst base: RawRead = {\n\t\t...(read.scopes === undefined ? { scope: read.scope } : { scopes: read.scopes }),\n\t\tstart: read.start,\n\t\tend: read.end,\n\t\thostname: read.hostname,\n\t\tpath: read.path,\n\t\tfilters: [],\n\t\tsessions: false,\n\t\ttimeoutMs: read.timeoutMs,\n\t\t...(read.signal ? { signal: read.signal } : {}),\n\t}\n\tconst ranked = { order: { part: 'visitors', dir: 'desc' }, limit: top } as const\n\tconst buckets = minuteStarts(read.start, read.end)\n\tconst when = (part: RealtimePart, raw: RawRead): Promise<RawRow[]> =>\n\t\twanted.has(part) ? readRaw(raw) : Promise.resolve([])\n\tconst [totals, ...parts] = await Promise.allSettled([\n\t\treadRaw(base),\n\t\twhen('minutes', { ...base, buckets }),\n\t\twhen('pages', { ...base, groupBy: 'page', ...ranked }),\n\t\twhen('sources', { ...base, from: read.from, groupBy: 'source', ...ranked }),\n\t])\n\tif (totals.status === 'rejected') {\n\t\tthrow totals.reason\n\t}\n\tconst [minutes, pages, sources] = parts.map((outcome): RawRow[] => {\n\t\tif (outcome.status === 'rejected') {\n\t\t\tthrow outcome.reason\n\t\t}\n\t\treturn outcome.value\n\t})\n\treturn {\n\t\t...countOf(totals.value[0]),\n\t\tminutes: (minutes ?? []).flatMap((row) => {\n\t\t\tconst minute = row.bucket === undefined ? undefined : buckets[row.bucket]\n\t\t\treturn minute ? [{ minute, ...countOf(row) }] : []\n\t\t}),\n\t\tpages: tops(pages ?? []),\n\t\tsources: tops(sources ?? []),\n\t}\n}\n"],"mappings":";;;;;;;;AAyBA,MAAa,cAAc,MAAe,WAA4B;CACrE,OAAO,KAAK;CACZ,QAAQ,KAAK;CACb;CACA,KAAK,KAAK;CACV,UAAU,KAAK;AAChB;AAQA,MAAa,iBAAA,KAAuC;;;;;AAMpD,MAAa,yBAAyB;CACrC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACD;;AAGA,MAAa,qBAAqB;CACjC;CACA;CACA;CACA;CACA;CACA;CACA;AACD;AAMA,MAAa,qBAAkD,CAC9D,GAAG,wBACH,GAAG,kBACJ;;AAGA,MAAa,iBAAiB,cAC7B,QAAQ,wBAAwB,SAAS;;AAG1C,MAAa,qBAAqB;CACjC,UAAU;CACV,QAAQ;CACR,SAAS;CACT,WAAW;CACX,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QAAQ;EAEf,OAAO,QAAQ;CAChB,CAAC;CACD,OAAO;EACN,GAAG,QAAQ,OAAO,MAAM,EAAE;EAC1B,UAAU,WAAW,CAAC,GAAG,SAAS,QAAQ;GACzC,MAAM,SAAS,IAAI,WAAW,KAAA,IAAY,KAAA,IAAY,QAAQ,IAAI;GAClE,OAAO,SAAS,CAAC;IAAE;IAAQ,GAAG,QAAQ,GAAG;GAAE,CAAC,IAAI,CAAC;EAClD,CAAC;EACD,OAAO,KAAK,SAAS,CAAC,CAAC;EACvB,SAAS,KAAK,WAAW,CAAC,CAAC;CAC5B;AACD"}
@@ -20,7 +20,9 @@ const AGGREGATE_COLUMNS = {
20
20
  conversions: "conversions",
21
21
  revenue: "revenue",
22
22
  impressions: "impressions",
23
- interactions: "interactions"
23
+ interactions: "interactions",
24
+ pageviewsWithoutVisitor: "pageviews_without_visitor",
25
+ pageviewsWithoutConsent: "pageviews_without_consent"
24
26
  };
25
27
  const EMPTY = raw("''");
26
28
  /** What a row of a day is keyed by: its kind, its two values and its hostname. */
@@ -60,7 +62,9 @@ const VISIT_TOTALS = new Map([
60
62
  ["scrollSamples", t("v_sc")],
61
63
  ["conversions", t("v_conv")],
62
64
  ["revenue", t("v_rev")],
63
- ["interactions", t("v_int")]
65
+ ["interactions", t("v_int")],
66
+ ["pageviewsWithoutVisitor", sql`CASE WHEN ${tField("v")} = '' THEN ${t("s_pv")} ELSE 0 END`],
67
+ ["pageviewsWithoutConsent", t("v_npc")]
64
68
  ]);
65
69
  /**
66
70
  * Site and visit kinds, one fact per visit off its row of `va` (the hits with no visitor make one
@@ -1 +1 @@
1
- {"version":3,"file":"aggregateSql.js","names":[],"sources":["../../../../src/native/store/sql/aggregateSql.ts"],"sourcesContent":["import type { CalendarDay } from '../../../timeframe/calendar'\nimport {\n\ttype AggregateKind,\n\ttype AggregatePlan,\n\ttype AggregateRecord,\n\tHIT_COUNTED,\n\tITEM_COUNTED,\n\tKIND_DIMS,\n\tKIND_DIMS2,\n\tkindsWritten,\n} from '../aggregate'\nimport { type CardinalityCaps, UNKNOWN_WHEN_FOLDED } from '../caps'\nimport type { SqlDialect } from '../dialect'\nimport { generationRowId } from '../ids'\nimport { type MetricParts, PART_KEYS } from '../parts'\nimport type { DayWindow, FoldRead, TimeWindow } from '../types'\nimport {\n\tcontributionsOf,\n\tdayVisitChain,\n\ttype FactKind,\n\tfold,\n\tGOAL,\n\tgoalField,\n\thitsCte,\n\titemFacts,\n\tlines,\n\trankSql,\n\ttype SqlTarget,\n\tscopeWhere,\n\tT,\n\tt,\n\ttField,\n\tvisitAttribute,\n} from './compile'\nimport { ident, join, NULL, raw, type SqlFragment, sql, valueList } from './fragment'\n\n/** Where aggregation runs: the hits, aggregates and state tables, as the executor names them. */\nexport interface AggregateTarget extends SqlTarget {\n\taggregates: SqlFragment\n\tstate: SqlFragment\n}\n\n/** Each part's aggregates column, as Payload's drizzle adapters name it. */\nconst AGGREGATE_COLUMNS: { readonly [K in keyof MetricParts]: string } = {\n\tpageviews: 'pageviews',\n\tevents: 'events',\n\tvisitors: 'visitors',\n\tsessions: 'sessions',\n\tentries: 'entries',\n\tbounces: 'bounces',\n\tsessionMs: 'session_ms',\n\tdurationMs: 'duration_ms',\n\tscrollSum: 'scroll_sum',\n\tscrollSamples: 'scroll_samples',\n\tconversions: 'conversions',\n\trevenue: 'revenue',\n\timpressions: 'impressions',\n\tinteractions: 'interactions',\n}\n\n/** The kind a day's read reports its hit count under, in `pageviews`. */\nexport const HITS_KIND = -1\n\nconst EMPTY = raw(\"''\")\n\n/** What a row of a day is keyed by: its kind, its two values and its hostname. */\nconst ROW_KEYS = ['k', 'v1', 'v2', 'hostname']\n\n/** A row's keys and the visitor, which facts fold by before they fold into the row. */\nconst VISITOR_KEYS = [...ROW_KEYS, 'v']\n\nconst integer = (value: number): SqlFragment => raw(String(Math.trunc(value)))\n\n/** `name(\"k\") AS (VALUES (0), (1))`: the kinds one fact source fans out into. */\nconst kindsCte = (name: string, kinds: readonly AggregateKind[]): SqlFragment =>\n\tsql`${ident(name)}(\"k\") AS (VALUES ${join(kinds.map((kind) => sql`(${integer(kind.index)})`))})`\n\nconst caseOf = (key: SqlFragment, branches: ReadonlyArray<[number, SqlFragment]>): SqlFragment =>\n\tsql`CASE ${key} ${join(\n\t\tbranches.map(([kind, value]) => sql`WHEN ${integer(kind)} THEN ${value}`),\n\t\t' '\n\t)} END`\n\n/** Only kinds whose second value is `''` take a fact without one; a pair needs both. */\nconst kept = (single: readonly AggregateKind[]): SqlFragment =>\n\tsql`\"v1\" IS NOT NULL AND \"v1\" <> '' AND \"v2\" IS NOT NULL AND (\"v2\" <> ''${\n\t\tsingle.length > 0\n\t\t\t? sql` OR \"k\" IN (${join(single.map((kind) => integer(kind.index)))})`\n\t\t\t: raw('')\n\t})`\n\nconst contributed = (\n\tfact: FactKind,\n\tcounted: ReadonlySet<keyof MetricParts>,\n\tdialect: SqlDialect\n): SqlFragment[] =>\n\t[...contributionsOf(fact, counted, dialect)].map(\n\t\t([part, value]) => sql`${value} AS ${ident(part)}`\n\t)\n\nconst folded = (\n\tcounted: ReadonlySet<keyof MetricParts>,\n\tlevel: 'visit' | 'visitor' | 'row'\n): SqlFragment[] => PART_KEYS.filter((part) => counted.has(part)).map((part) => fold(part, level))\n\n/**\n * Every part in its place, a part the source does not count written as a typed NULL: Postgres\n * types a union pair by pair, and two untyped NULLs make text, which no number joins afterwards.\n */\nconst allParts = (counted: ReadonlySet<keyof MetricParts>, dialect: SqlDialect): SqlFragment[] =>\n\tPART_KEYS.map((part) =>\n\t\tcounted.has(part) ? ident(part) : sql`${dialect.cast(NULL, 'real')} AS ${ident(part)}`\n\t)\n\nconst columns = (names: readonly string[]): SqlFragment => join(names.map((name) => ident(name)))\n\n/** What one visit adds to a visit kind: the chain's totals of the visit, on its row of `va`. */\nconst VISIT_TOTALS = new Map<keyof MetricParts, SqlFragment>([\n\t['pageviews', t('s_pv')],\n\t['events', t('s_int')],\n\t['visitors', sql`CASE WHEN ${t('v_act')} = 1 AND ${tField('v')} <> '' THEN 1 ELSE 0 END`],\n\t['sessions', sql`CASE WHEN ${t('v_act')} = 1 AND ${t('sn')} IS NOT NULL THEN 1 ELSE 0 END`],\n\t['entries', t('entry')],\n\t['bounces', t('bounce')],\n\t['sessionMs', t('s_ms')],\n\t['durationMs', t('s_ms')],\n\t['scrollSum', t('v_ss')],\n\t['scrollSamples', t('v_sc')],\n\t['conversions', t('v_conv')],\n\t['revenue', t('v_rev')],\n\t['interactions', t('v_int')],\n])\n\n/**\n * Site and visit kinds, one fact per visit off its row of `va` (the hits with no visitor make one\n * per hostname), fanned out per kind and folded per visitor, then per row.\n */\nconst visitKinds = (kinds: readonly AggregateKind[]): SqlFragment[] => {\n\tconst values = kinds.map((kind): [number, SqlFragment] => [\n\t\tkind.index,\n\t\tkind.source.from === 'visit' ? t(visitAttribute(kind.source.attribute)) : EMPTY,\n\t])\n\tconst parts = PART_KEYS.filter((part) => HIT_COUNTED.has(part))\n\treturn [\n\t\tkindsCte('ks', kinds),\n\t\tsql`sf AS (SELECT * FROM (SELECT \"ks\".\"k\", ${caseOf(ident('ks', 'k'), values)} AS \"v1\",\n\t\t\t${lines([...['hostname', 'v'].map((name) => t(name)), ...parts.map((part) => sql`${VISIT_TOTALS.get(part) ?? raw('0')} AS ${ident(part)}`)])}\n\t\t\tFROM va AS ${ident(T)} CROSS JOIN ks WHERE ${t('w')} = 0) AS \"f\"\n\t\tWHERE \"v1\" IS NOT NULL AND (\"v1\" <> '' OR \"k\" = ${integer(kinds[0]?.index ?? 0)}))`,\n\t\tsql`sp AS (SELECT ${lines([columns(['k', 'v1', 'hostname', 'v']), ...folded(HIT_COUNTED, 'visitor')])}\n\t\tFROM sf GROUP BY \"k\", \"v1\", \"hostname\", \"v\")`,\n\t\tsql`sr AS (SELECT ${lines([columns(['k', 'v1']), sql`'' AS \"v2\"`, ident('hostname'), ...folded(HIT_COUNTED, 'row')])}\n\t\tFROM sp GROUP BY \"k\", \"v1\", \"hostname\")`,\n\t]\n}\n\n/**\n * A hit kind's facts: every hit for pages, events only for events, money hits for currencies, each\n * by the currency it was sent in and counting its revenue as sent.\n */\nconst hitBranch = (kind: AggregateKind, dialect: SqlDialect): SqlFragment[] => {\n\tif (kind.source.from !== 'hit') {\n\t\treturn []\n\t}\n\tconst field = kind.source.field\n\tconst value =\n\t\tfield === 'path' ? tField('path') : field === 'name' ? tField('name') : t('sent_currency')\n\tconst where =\n\t\tfield === 'path'\n\t\t\t? raw('')\n\t\t\t: field === 'name'\n\t\t\t\t? sql` WHERE ${tField('type')} = 'event'`\n\t\t\t\t: sql` WHERE ${t('sent_currency')} IS NOT NULL`\n\treturn [\n\t\tsql`SELECT ${integer(kind.index)} AS \"k\", ${value} AS \"v1\", '' AS \"v2\",\n\t\t\t${lines([...['hostname', 'v', 'sn'].map((name) => t(name)), ...contributed({ axis: 'hit', sent: field === 'currency' }, HIT_COUNTED, dialect)])}\n\t\t\tFROM a AS ${ident(T)}${where}`,\n\t]\n}\n\n/**\n * Hit, goal and goal pair kinds: one fact per hit or goal completed, folded per visit first. A\n * completion takes its visit's attributes off its own row of `va`.\n */\nconst hitKinds = (\n\ttarget: SqlTarget,\n\thits: readonly AggregateKind[],\n\tgoals: readonly AggregateKind[]\n): SqlFragment[] => {\n\tconst { dialect } = target\n\tconst goalValues = goals.map((kind): [number, SqlFragment] => [\n\t\tkind.index,\n\t\tkind.source.from === 'goal' && kind.source.attribute !== null\n\t\t\t? t(visitAttribute(kind.source.attribute))\n\t\t\t: EMPTY,\n\t])\n\tconst completions = sql`SELECT \"kg\".\"k\", ${goalField(dialect, 'slug')} AS \"v1\", ${caseOf(ident('kg', 'k'), goalValues)} AS \"v2\",\n\t\t\t${lines([...['hostname', 'v', 'sn'].map((name) => t(name)), ...contributed({ axis: 'goal' }, HIT_COUNTED, dialect)])}\n\t\t\tFROM va AS ${ident(T)} ${dialect.crossJoinJsonArray(tField('goals'), GOAL, 'json')} CROSS JOIN kg\n\t\t\tWHERE ${t('w')} = 1`\n\treturn [\n\t\tkindsCte('kg', goals),\n\t\tsql`hf AS (SELECT * FROM (${join(\n\t\t\t[...hits.flatMap((kind) => hitBranch(kind, dialect)), completions],\n\t\t\t'\\n\\t\\tUNION ALL\\n\\t\\t'\n\t\t)}) AS \"f\"\n\t\tWHERE ${kept([...hits, ...goals].filter((kind) => kind.dim2 === ''))})`,\n\t\tsql`hk AS (SELECT ${lines([columns([...VISITOR_KEYS, 'sn']), ...folded(HIT_COUNTED, 'visit')])}\n\t\tFROM hf GROUP BY ${columns([...VISITOR_KEYS, 'sn'])})`,\n\t\tsql`hp AS (SELECT ${lines([columns(VISITOR_KEYS), ...folded(HIT_COUNTED, 'visitor')])}\n\t\tFROM hk GROUP BY ${columns(VISITOR_KEYS)})`,\n\t\tsql`hr AS (SELECT ${lines([columns(ROW_KEYS), ...folded(HIT_COUNTED, 'row')])}\n\t\tFROM hp GROUP BY ${columns(ROW_KEYS)})`,\n\t]\n}\n\n/** Item kinds: one fact per item clicked or seen, alone and by page (a server hit has no page). */\nconst itemKinds = (target: SqlTarget, items: readonly AggregateKind[]): SqlFragment[] => {\n\tconst { dialect } = target\n\tconst values = items.map((kind): [number, SqlFragment] => [\n\t\tkind.index,\n\t\tkind.source.from === 'item' && kind.source.page\n\t\t\t? sql`CASE WHEN ${dialect.isTrue(tField('server'))} THEN NULL ELSE ${tField('path')} END`\n\t\t\t: EMPTY,\n\t])\n\treturn [\n\t\titemFacts('xi', sql`a AS ${ident(T)}`, dialect),\n\t\tkindsCte('kx', items),\n\t\tsql`xf AS (SELECT * FROM (SELECT \"kx\".\"k\", ${t('x')} AS \"v1\", ${caseOf(ident('kx', 'k'), values)} AS \"v2\",\n\t\t\t${lines([...['hostname', 'v'].map((name) => t(name)), ...contributed({ axis: 'item' }, ITEM_COUNTED, dialect)])}\n\t\t\tFROM xi AS ${ident(T)} CROSS JOIN kx) AS \"f\"\n\t\tWHERE ${kept(items.filter((kind) => kind.dim2 === ''))})`,\n\t\tsql`xp AS (SELECT ${lines([columns(VISITOR_KEYS), ...folded(ITEM_COUNTED, 'visitor')])}\n\t\tFROM xf GROUP BY ${columns(VISITOR_KEYS)})`,\n\t\tsql`xr AS (SELECT ${lines([columns(ROW_KEYS), ...folded(ITEM_COUNTED, 'row')])}\n\t\tFROM xp GROUP BY ${columns(ROW_KEYS)})`,\n\t]\n}\n\n/**\n * `capDay` in the database: rows ranked within their cap's group, those past the cap folded into\n * its `(other)` row (`o` = 1), so a day answers at most the caps however many values it holds.\n */\nconst rankAndFold = (\n\tkinds: readonly AggregateKind[],\n\tcaps: CardinalityCaps,\n\tdialect: SqlDialect\n): { ranked: SqlFragment; select: SqlFragment } => {\n\tconst pairs = kinds.filter((kind) => kind.dim2 !== '').map((kind) => integer(kind.index))\n\tconst group =\n\t\tpairs.length === 0 ? EMPTY : sql`CASE WHEN \"k\" IN (${join(pairs)}) THEN \"v1\" ELSE '' END`\n\tconst capped = kinds.flatMap((kind): Array<[number, SqlFragment]> => {\n\t\tconst cap = kind.cap === null ? null : caps[kind.cap]\n\t\treturn cap === null ? [] : [[kind.index, integer(cap)]]\n\t})\n\tconst capOf = capped.length === 0 ? dialect.cast(NULL, 'integer') : caseOf(ident('k'), capped)\n\tconst order = join([\n\t\traw('COALESCE(\"pageviews\", \"impressions\", 0) DESC'),\n\t\traw('COALESCE(\"events\", 0) DESC'),\n\t\tsql`${dialect.byCodePoint(ident('v1'))} ASC`,\n\t\tsql`${dialect.byCodePoint(ident('v2'))} ASC`,\n\t])\n\tconst head = columns(ROW_KEYS)\n\tconst parts = columns(PART_KEYS)\n\tconst otherParts = PART_KEYS.map((part) =>\n\t\tUNKNOWN_WHEN_FOLDED.includes(part)\n\t\t\t? sql`${dialect.cast(NULL, 'real')} AS ${ident(part)}`\n\t\t\t: sql`SUM(${ident(part)}) AS ${ident(part)}`\n\t)\n\treturn {\n\t\tranked: sql`rk AS (SELECT ${head}, ${parts},\n\t\t\tROW_NUMBER() OVER (PARTITION BY \"k\", \"hostname\", ${group} ORDER BY ${order}) AS \"rn\",\n\t\t\t${capOf} AS \"cap\"\n\t\tFROM u)`,\n\t\tselect: sql`SELECT ${head}, ${parts}, 0 AS \"o\" FROM rk WHERE \"cap\" IS NULL OR \"rn\" <= \"cap\"\n\tUNION ALL SELECT \"k\", ${group} AS \"v1\", '' AS \"v2\", \"hostname\", ${join(otherParts)}, 1 AS \"o\"\n\t\tFROM rk WHERE \"rn\" > \"cap\" GROUP BY \"k\", \"hostname\", ${group}`,\n\t}\n}\n\n/**\n * A day's count in one statement: every kind's rows per hostname, capped (see {@link rankAndFold}),\n * uncounted parts NULL, and a {@link HITS_KIND} row holding the hits read.\n */\nexport const compileDayAggregation = (\n\twindow: TimeWindow,\n\ttarget: SqlTarget,\n\twritten: { itemPages: boolean; caps: CardinalityCaps }\n): SqlFragment => {\n\tconst { itemPages, caps } = written\n\tconst kinds = kindsWritten(itemPages)\n\tconst of = (from: string) => kinds.filter((kind) => kind.source.from === from)\n\tconst visit = [...of('site'), ...of('visit')]\n\tconst ctes = [\n\t\thitsCte(\n\t\t\twindow,\n\t\t\t{ sessionize: true, attributes: [], goals: true, impressions: true, sent: true },\n\t\t\ttarget\n\t\t),\n\t\t...dayVisitChain(target, window),\n\t\t...visitKinds(visit),\n\t\t...hitKinds(target, of('hit'), of('goal')),\n\t\t...itemKinds(target, of('item')),\n\t]\n\tconst head = columns(ROW_KEYS)\n\tconst { dialect } = target\n\tconst { ranked, select } = rankAndFold(kinds, caps, dialect)\n\tconst union = sql`u AS (SELECT ${head}, ${join(allParts(HIT_COUNTED, dialect))} FROM sr\n\t\tUNION ALL SELECT ${head}, ${join(allParts(HIT_COUNTED, dialect))} FROM hr\n\t\tUNION ALL SELECT ${head}, ${join(allParts(ITEM_COUNTED, dialect))} FROM xr)`\n\treturn sql`WITH ${join([...ctes, union, ranked], ',\\n\\t')}\n\t${select}\n\tUNION ALL SELECT ${integer(HITS_KIND)}, '', '', '', COUNT(*), ${join(\n\t\tPART_KEYS.slice(1).map(() => NULL)\n\t)}, 0 FROM a`\n}\n\nconst AGGREGATE_FIELDS: ReadonlyArray<Exclude<keyof AggregateRecord, 'parts'>> = [\n\t'id',\n\t'scope',\n\t'day',\n\t'hostname',\n\t'dim',\n\t'value',\n\t'dim2',\n\t'value2',\n\t'gen',\n]\n\nconst recordValues = (record: AggregateRecord): SqlFragment =>\n\tsql`(${join([\n\t\t...AGGREGATE_FIELDS.map((field) =>\n\t\t\tfield === 'id' ? sql`${generationRowId(record)}` : sql`${record[field]}`\n\t\t),\n\t\t...PART_KEYS.map((part) => {\n\t\t\tconst value = record.parts[part]\n\t\t\treturn value === null ? NULL : sql`${value}`\n\t\t}),\n\t])})`\n\n/**\n * Writes records as rows of their own generation beside the day's readable ones, which no read\n * counts until the day's marker names it. A row already written by the same run is left as it is.\n */\nexport const compileAggregateInsert = (\n\trecords: readonly AggregateRecord[],\n\ttarget: AggregateTarget\n): SqlFragment => {\n\tconst parts = PART_KEYS.map((part) => AGGREGATE_COLUMNS[part])\n\treturn sql`INSERT INTO ${target.aggregates} (${columns([...AGGREGATE_FIELDS, ...parts])})\n\t\tVALUES ${join(records.map(recordValues))}\n\t\tON CONFLICT (${ident('id')}) DO NOTHING`\n}\n\n/** The day's rows of every kind, as the indexes on scope, kind and day find them. */\nconst dayRows = (window: Pick<DayWindow, 'scope' | 'day'>): SqlFragment =>\n\tsql`\"scope\" = ${window.scope} AND \"day\" = ${window.day}\n\t\t\tAND \"dim\" IN (${valueList(KIND_DIMS)})\n\t\t\tAND \"dim2\" IN (${valueList(KIND_DIMS2)})`\n\n/** The generations the day's rows carry. */\nexport const compileDayGens = (\n\twindow: Pick<DayWindow, 'scope' | 'day'>,\n\ttarget: AggregateTarget\n): SqlFragment =>\n\tsql`SELECT DISTINCT \"gen\" FROM ${target.aggregates}\n\t\tWHERE ${dayRows(window)}`\n\n/** Which of a day's rows a delete takes: some generations, or all but one; at most `limit`. */\nexport type DayRowsDelete = ({ gens: readonly string[] } | { keep: string }) & { limit: number }\n\n/**\n * Deletes up to `limit` of the day's rows: those of the generations `gens` names, or of every\n * generation but `keep`. A statement of its own per share, so no writer waits out the whole day.\n */\nexport const compileDayRowsDelete = (\n\twindow: Pick<DayWindow, 'scope' | 'day'>,\n\twhich: DayRowsDelete,\n\ttarget: AggregateTarget\n): SqlFragment => {\n\tconst gens =\n\t\t'gens' in which ? sql`\"gen\" IN (${valueList(which.gens)})` : sql`\"gen\" <> ${which.keep}`\n\treturn sql`DELETE FROM ${target.aggregates}\n\t\tWHERE \"id\" IN (SELECT \"id\" FROM ${target.aggregates}\n\t\t\tWHERE ${dayRows(window)}\n\t\t\t\tAND ${gens}\n\t\t\tLIMIT ${integer(which.limit)})`\n}\n\ntype DayRanges = ReadonlyArray<readonly [CalendarDay, CalendarDay]>\n\n/** Ranges of days, with the span they cover first where there are several, as an index reads it. */\nconst dayRanges = (column: SqlFragment, days: DayRanges): SqlFragment => {\n\tconst ranges = join(\n\t\tdays.map(([from, to]) => sql`(${column} >= ${from} AND ${column} <= ${to})`),\n\t\t' OR '\n\t)\n\tif (days.length < 2) {\n\t\treturn ranges\n\t}\n\tconst from = Math.min(...days.map(([start]) => start))\n\tconst to = Math.max(...days.map(([, end]) => end))\n\treturn sql`${column} >= ${from} AND ${column} <= ${to} AND (${ranges})`\n}\n\n/** The completed days' generations of the scopes' ranges, as the `m` a read joins. */\nconst markersCte = (\n\tscopes: { scope?: string; scopes?: readonly string[] },\n\tdays: DayRanges,\n\ttarget: AggregateTarget\n): SqlFragment =>\n\tsql`m AS (SELECT \"scope\", \"day\", \"gen\" FROM ${target.state}\n\t\tWHERE \"kind\" = 'day' AND \"gen\" IS NOT NULL${join(\n\t\t\tscopeWhere(scopes, ident('scope')).map((condition) => sql` AND ${condition}`),\n\t\t\t''\n\t\t)} AND (${dayRanges(ident('day'), days)}))`\n\n/** A column of the aggregate rows a read takes, aliased `g`. */\nconst g = (name: string): SqlFragment => ident('g', name)\n\n/** The aggregate rows of the generations the markers `m` name, the only ones a read counts. */\nconst markedRows = (target: AggregateTarget): SqlFragment =>\n\tsql`${target.aggregates} AS \"g\"\n\t\t\tJOIN m ON \"m\".\"scope\" = \"g\".\"scope\" AND \"m\".\"day\" = \"g\".\"day\" AND \"m\".\"gen\" = \"g\".\"gen\"`\n\n/**\n * The rows a fold read decides on: of completed days, the value's and its `(other)` row for a\n * dimension, a pair's first value's `(other)` rows alone.\n */\nexport const compileFoldRead = (\n\tread: FoldRead,\n\tkind: AggregateKind,\n\ttarget: AggregateTarget\n): SqlFragment => {\n\tconst values =\n\t\tkind.dim2 === ''\n\t\t\t? sql`${g('value')} IN (${read.value}, '')`\n\t\t\t: sql`${g('value')} = ${read.value} AND ${g('value2')} = ''`\n\treturn sql`WITH ${markersCte({ scope: read.scope }, read.days, target)}\n\tSELECT ${g('day')} AS \"day\", ${g('hostname')} AS \"hostname\", ${g('value')} AS \"value\", ${g('value2')} AS \"value2\"\n\t\tFROM ${markedRows(target)}\n\t\tWHERE ${g('scope')} = ${read.scope} AND ${g('dim')} = ${kind.dim} AND ${g('dim2')} = ${kind.dim2}\n\t\t\tAND (${dayRanges(g('day'), read.days)})${\n\t\t\t\tread.hostname === undefined ? raw('') : sql` AND ${g('hostname')} = ${read.hostname}`\n\t\t\t}\n\t\t\tAND ${values}`\n}\n\n/** The hostnames a scope's site rows hold from `since` on. */\nexport const compileHostnames = (\n\tscope: string,\n\tsince: CalendarDay,\n\ttarget: AggregateTarget\n): SqlFragment =>\n\tsql`SELECT DISTINCT \"hostname\" FROM ${target.aggregates}\n\t\tWHERE \"scope\" = ${scope} AND \"dim\" = '' AND \"dim2\" = '' AND \"day\" >= ${since}`\n\n/**\n * One statement summing the rows of completed days: the markers' generations joined in, so a row\n * written by a run that never completed, or replaced since, is never read, all in one snapshot.\n */\nexport const compileAggregateRead = (plan: AggregatePlan, target: AggregateTarget): SqlFragment => {\n\tconst { read, kind } = plan\n\tconst { dialect } = target\n\tconst grouped = plan.group === 'value' ? g('value') : plan.group === 'value2' ? g('value2') : null\n\tconst markers = markersCte(read, read.days, target)\n\tconst conditions = [\n\t\t...scopeWhere(read, g('scope')),\n\t\tsql`${g('dim')} = ${kind.dim}`,\n\t\tsql`${g('dim2')} = ${kind.dim2}`,\n\t\tsql`(${dayRanges(g('day'), read.days)})`,\n\t\t...(read.hostname === undefined ? [] : [sql`${g('hostname')} = ${read.hostname}`]),\n\t\t...(read.value === undefined ? [] : [sql`${g('value')} = ${read.value}`]),\n\t\t...(read.values === undefined || grouped === null\n\t\t\t? []\n\t\t\t: [sql`${grouped} IN (${valueList(read.values)})`]),\n\t\t// An `(other)` row folds many values together: it is part of a total, never a value.\n\t\t...(grouped === null ? [] : [sql`${grouped} <> ''`]),\n\t]\n\tconst key = plan.group === 'day' ? g('day') : grouped\n\tconst keys = key === null ? [] : [sql`${key} AS ${ident(plan.group === 'day' ? 'day' : 'value')}`]\n\tconst sums = PART_KEYS.map((part) => sql`SUM(${g(AGGREGATE_COLUMNS[part])}) AS ${ident(part)}`)\n\tconst summed = sql`r AS (SELECT ${lines([...keys, ...sums, raw('COUNT(*) AS \"n\"')])}\n\t\tFROM ${markedRows(target)}\n\t\tWHERE ${join(conditions, '\\n\\t\\t\\tAND ')}${\n\t\t\tkey === null\n\t\t\t\t? raw('')\n\t\t\t\t: sql`\n\t\tGROUP BY ${key}`\n\t\t})`\n\tconst visible =\n\t\tplan.group === null\n\t\t\t? raw('')\n\t\t\t: sql`\n\tWHERE COALESCE(\"pageviews\", 0) + COALESCE(\"events\", 0) + COALESCE(\"impressions\", 0) > 0`\n\tconst ordering = [\n\t\t...[rankSql(read.order, kind.counted, dialect)].filter((rank) => rank !== null),\n\t\t...(plan.group === 'value' || plan.group === 'value2'\n\t\t\t? [sql`${dialect.byCodePoint(ident('value'))} ASC`]\n\t\t\t: []),\n\t\t...(plan.group === 'day' ? [sql`\"day\" ASC`] : []),\n\t]\n\tconst orderBy =\n\t\tordering.length > 0\n\t\t\t? sql`\n\tORDER BY ${join(ordering)}`\n\t\t\t: raw('')\n\tconst limit =\n\t\tread.limit === undefined\n\t\t\t? raw('')\n\t\t\t: sql`\n\tLIMIT ${read.limit}`\n\treturn sql`WITH ${markers},\n\t${summed}\n\tSELECT * FROM r${visible}${orderBy}${limit}`\n}\n"],"mappings":";;;;;;;;AA2CA,MAAM,oBAAmE;CACxE,WAAW;CACX,QAAQ;CACR,UAAU;CACV,UAAU;CACV,SAAS;CACT,SAAS;CACT,WAAW;CACX,YAAY;CACZ,WAAW;CACX,eAAe;CACf,aAAa;CACb,SAAS;CACT,aAAa;CACb,cAAc;AACf;AAKA,MAAM,QAAQ,IAAI,IAAI;;AAGtB,MAAM,WAAW;CAAC;CAAK;CAAM;CAAM;AAAU;;AAG7C,MAAM,eAAe,CAAC,GAAG,UAAU,GAAG;AAEtC,MAAM,WAAW,UAA+B,IAAI,OAAO,KAAK,MAAM,KAAK,CAAC,CAAC;;AAG7E,MAAM,YAAY,MAAc,UAC/B,GAAG,GAAG,MAAM,IAAI,EAAE,mBAAmB,KAAK,MAAM,KAAK,SAAS,GAAG,IAAI,QAAQ,KAAK,KAAK,EAAE,EAAE,CAAC,EAAE;AAE/F,MAAM,UAAU,KAAkB,aACjC,GAAG,QAAQ,IAAI,GAAG,KACjB,SAAS,KAAK,CAAC,MAAM,WAAW,GAAG,QAAQ,QAAQ,IAAI,EAAE,QAAQ,OAAO,GACxE,GACD,EAAE;;AAGH,MAAM,QAAQ,WACb,GAAG,uEACF,OAAO,SAAS,IACb,GAAG,eAAe,KAAK,OAAO,KAAK,SAAS,QAAQ,KAAK,KAAK,CAAC,CAAC,EAAE,KAClE,IAAI,EAAE,EACT;AAEF,MAAM,eACL,MACA,SACA,YAEA,CAAC,GAAG,gBAAgB,MAAM,SAAS,OAAO,CAAC,EAAE,KAC3C,CAAC,MAAM,WAAW,GAAG,GAAG,MAAM,MAAM,MAAM,IAAI,GAChD;AAED,MAAM,UACL,SACA,UACmB,UAAU,QAAQ,SAAS,QAAQ,IAAI,IAAI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AET,KAAK,UAAU,KAAA,IACZ,IAAI,EAAE,IACN,GAAG;SACC,KAAK;AAId"}
1
+ {"version":3,"file":"aggregateSql.js","names":[],"sources":["../../../../src/native/store/sql/aggregateSql.ts"],"sourcesContent":["import type { CalendarDay } from '../../../timeframe/calendar'\nimport {\n\ttype AggregateKind,\n\ttype AggregatePlan,\n\ttype AggregateRecord,\n\tHIT_COUNTED,\n\tITEM_COUNTED,\n\tKIND_DIMS,\n\tKIND_DIMS2,\n\tkindsWritten,\n} from '../aggregate'\nimport { type CardinalityCaps, UNKNOWN_WHEN_FOLDED } from '../caps'\nimport type { SqlDialect } from '../dialect'\nimport { generationRowId } from '../ids'\nimport { type MetricParts, PART_KEYS } from '../parts'\nimport type { DayWindow, FoldRead, TimeWindow } from '../types'\nimport {\n\tcontributionsOf,\n\tdayVisitChain,\n\ttype FactKind,\n\tfold,\n\tGOAL,\n\tgoalField,\n\thitsCte,\n\titemFacts,\n\tlines,\n\trankSql,\n\ttype SqlTarget,\n\tscopeWhere,\n\tT,\n\tt,\n\ttField,\n\tvisitAttribute,\n} from './compile'\nimport { ident, join, NULL, raw, type SqlFragment, sql, valueList } from './fragment'\n\n/** Where aggregation runs: the hits, aggregates and state tables, as the executor names them. */\nexport interface AggregateTarget extends SqlTarget {\n\taggregates: SqlFragment\n\tstate: SqlFragment\n}\n\n/** Each part's aggregates column, as Payload's drizzle adapters name it. */\nconst AGGREGATE_COLUMNS: { readonly [K in keyof MetricParts]: string } = {\n\tpageviews: 'pageviews',\n\tevents: 'events',\n\tvisitors: 'visitors',\n\tsessions: 'sessions',\n\tentries: 'entries',\n\tbounces: 'bounces',\n\tsessionMs: 'session_ms',\n\tdurationMs: 'duration_ms',\n\tscrollSum: 'scroll_sum',\n\tscrollSamples: 'scroll_samples',\n\tconversions: 'conversions',\n\trevenue: 'revenue',\n\timpressions: 'impressions',\n\tinteractions: 'interactions',\n\tpageviewsWithoutVisitor: 'pageviews_without_visitor',\n\tpageviewsWithoutConsent: 'pageviews_without_consent',\n}\n\n/** The kind a day's read reports its hit count under, in `pageviews`. */\nexport const HITS_KIND = -1\n\nconst EMPTY = raw(\"''\")\n\n/** What a row of a day is keyed by: its kind, its two values and its hostname. */\nconst ROW_KEYS = ['k', 'v1', 'v2', 'hostname']\n\n/** A row's keys and the visitor, which facts fold by before they fold into the row. */\nconst VISITOR_KEYS = [...ROW_KEYS, 'v']\n\nconst integer = (value: number): SqlFragment => raw(String(Math.trunc(value)))\n\n/** `name(\"k\") AS (VALUES (0), (1))`: the kinds one fact source fans out into. */\nconst kindsCte = (name: string, kinds: readonly AggregateKind[]): SqlFragment =>\n\tsql`${ident(name)}(\"k\") AS (VALUES ${join(kinds.map((kind) => sql`(${integer(kind.index)})`))})`\n\nconst caseOf = (key: SqlFragment, branches: ReadonlyArray<[number, SqlFragment]>): SqlFragment =>\n\tsql`CASE ${key} ${join(\n\t\tbranches.map(([kind, value]) => sql`WHEN ${integer(kind)} THEN ${value}`),\n\t\t' '\n\t)} END`\n\n/** Only kinds whose second value is `''` take a fact without one; a pair needs both. */\nconst kept = (single: readonly AggregateKind[]): SqlFragment =>\n\tsql`\"v1\" IS NOT NULL AND \"v1\" <> '' AND \"v2\" IS NOT NULL AND (\"v2\" <> ''${\n\t\tsingle.length > 0\n\t\t\t? sql` OR \"k\" IN (${join(single.map((kind) => integer(kind.index)))})`\n\t\t\t: raw('')\n\t})`\n\nconst contributed = (\n\tfact: FactKind,\n\tcounted: ReadonlySet<keyof MetricParts>,\n\tdialect: SqlDialect\n): SqlFragment[] =>\n\t[...contributionsOf(fact, counted, dialect)].map(\n\t\t([part, value]) => sql`${value} AS ${ident(part)}`\n\t)\n\nconst folded = (\n\tcounted: ReadonlySet<keyof MetricParts>,\n\tlevel: 'visit' | 'visitor' | 'row'\n): SqlFragment[] => PART_KEYS.filter((part) => counted.has(part)).map((part) => fold(part, level))\n\n/**\n * Every part in its place, a part the source does not count written as a typed NULL: Postgres\n * types a union pair by pair, and two untyped NULLs make text, which no number joins afterwards.\n */\nconst allParts = (counted: ReadonlySet<keyof MetricParts>, dialect: SqlDialect): SqlFragment[] =>\n\tPART_KEYS.map((part) =>\n\t\tcounted.has(part) ? ident(part) : sql`${dialect.cast(NULL, 'real')} AS ${ident(part)}`\n\t)\n\nconst columns = (names: readonly string[]): SqlFragment => join(names.map((name) => ident(name)))\n\n/** What one visit adds to a visit kind: the chain's totals of the visit, on its row of `va`. */\nconst VISIT_TOTALS = new Map<keyof MetricParts, SqlFragment>([\n\t['pageviews', t('s_pv')],\n\t['events', t('s_int')],\n\t['visitors', sql`CASE WHEN ${t('v_act')} = 1 AND ${tField('v')} <> '' THEN 1 ELSE 0 END`],\n\t['sessions', sql`CASE WHEN ${t('v_act')} = 1 AND ${t('sn')} IS NOT NULL THEN 1 ELSE 0 END`],\n\t['entries', t('entry')],\n\t['bounces', t('bounce')],\n\t['sessionMs', t('s_ms')],\n\t['durationMs', t('s_ms')],\n\t['scrollSum', t('v_ss')],\n\t['scrollSamples', t('v_sc')],\n\t['conversions', t('v_conv')],\n\t['revenue', t('v_rev')],\n\t['interactions', t('v_int')],\n\t['pageviewsWithoutVisitor', sql`CASE WHEN ${tField('v')} = '' THEN ${t('s_pv')} ELSE 0 END`],\n\t['pageviewsWithoutConsent', t('v_npc')],\n])\n\n/**\n * Site and visit kinds, one fact per visit off its row of `va` (the hits with no visitor make one\n * per hostname), fanned out per kind and folded per visitor, then per row.\n */\nconst visitKinds = (kinds: readonly AggregateKind[]): SqlFragment[] => {\n\tconst values = kinds.map((kind): [number, SqlFragment] => [\n\t\tkind.index,\n\t\tkind.source.from === 'visit' ? t(visitAttribute(kind.source.attribute)) : EMPTY,\n\t])\n\tconst parts = PART_KEYS.filter((part) => HIT_COUNTED.has(part))\n\treturn [\n\t\tkindsCte('ks', kinds),\n\t\tsql`sf AS (SELECT * FROM (SELECT \"ks\".\"k\", ${caseOf(ident('ks', 'k'), values)} AS \"v1\",\n\t\t\t${lines([...['hostname', 'v'].map((name) => t(name)), ...parts.map((part) => sql`${VISIT_TOTALS.get(part) ?? raw('0')} AS ${ident(part)}`)])}\n\t\t\tFROM va AS ${ident(T)} CROSS JOIN ks WHERE ${t('w')} = 0) AS \"f\"\n\t\tWHERE \"v1\" IS NOT NULL AND (\"v1\" <> '' OR \"k\" = ${integer(kinds[0]?.index ?? 0)}))`,\n\t\tsql`sp AS (SELECT ${lines([columns(['k', 'v1', 'hostname', 'v']), ...folded(HIT_COUNTED, 'visitor')])}\n\t\tFROM sf GROUP BY \"k\", \"v1\", \"hostname\", \"v\")`,\n\t\tsql`sr AS (SELECT ${lines([columns(['k', 'v1']), sql`'' AS \"v2\"`, ident('hostname'), ...folded(HIT_COUNTED, 'row')])}\n\t\tFROM sp GROUP BY \"k\", \"v1\", \"hostname\")`,\n\t]\n}\n\n/**\n * A hit kind's facts: every hit for pages, events only for events, money hits for currencies, each\n * by the currency it was sent in and counting its revenue as sent.\n */\nconst hitBranch = (kind: AggregateKind, dialect: SqlDialect): SqlFragment[] => {\n\tif (kind.source.from !== 'hit') {\n\t\treturn []\n\t}\n\tconst field = kind.source.field\n\tconst value =\n\t\tfield === 'path' ? tField('path') : field === 'name' ? tField('name') : t('sent_currency')\n\tconst where =\n\t\tfield === 'path'\n\t\t\t? raw('')\n\t\t\t: field === 'name'\n\t\t\t\t? sql` WHERE ${tField('type')} = 'event'`\n\t\t\t\t: sql` WHERE ${t('sent_currency')} IS NOT NULL`\n\treturn [\n\t\tsql`SELECT ${integer(kind.index)} AS \"k\", ${value} AS \"v1\", '' AS \"v2\",\n\t\t\t${lines([...['hostname', 'v', 'sn'].map((name) => t(name)), ...contributed({ axis: 'hit', sent: field === 'currency' }, HIT_COUNTED, dialect)])}\n\t\t\tFROM a AS ${ident(T)}${where}`,\n\t]\n}\n\n/**\n * Hit, goal and goal pair kinds: one fact per hit or goal completed, folded per visit first. A\n * completion takes its visit's attributes off its own row of `va`.\n */\nconst hitKinds = (\n\ttarget: SqlTarget,\n\thits: readonly AggregateKind[],\n\tgoals: readonly AggregateKind[]\n): SqlFragment[] => {\n\tconst { dialect } = target\n\tconst goalValues = goals.map((kind): [number, SqlFragment] => [\n\t\tkind.index,\n\t\tkind.source.from === 'goal' && kind.source.attribute !== null\n\t\t\t? t(visitAttribute(kind.source.attribute))\n\t\t\t: EMPTY,\n\t])\n\tconst completions = sql`SELECT \"kg\".\"k\", ${goalField(dialect, 'slug')} AS \"v1\", ${caseOf(ident('kg', 'k'), goalValues)} AS \"v2\",\n\t\t\t${lines([...['hostname', 'v', 'sn'].map((name) => t(name)), ...contributed({ axis: 'goal' }, HIT_COUNTED, dialect)])}\n\t\t\tFROM va AS ${ident(T)} ${dialect.crossJoinJsonArray(tField('goals'), GOAL, 'json')} CROSS JOIN kg\n\t\t\tWHERE ${t('w')} = 1`\n\treturn [\n\t\tkindsCte('kg', goals),\n\t\tsql`hf AS (SELECT * FROM (${join(\n\t\t\t[...hits.flatMap((kind) => hitBranch(kind, dialect)), completions],\n\t\t\t'\\n\\t\\tUNION ALL\\n\\t\\t'\n\t\t)}) AS \"f\"\n\t\tWHERE ${kept([...hits, ...goals].filter((kind) => kind.dim2 === ''))})`,\n\t\tsql`hk AS (SELECT ${lines([columns([...VISITOR_KEYS, 'sn']), ...folded(HIT_COUNTED, 'visit')])}\n\t\tFROM hf GROUP BY ${columns([...VISITOR_KEYS, 'sn'])})`,\n\t\tsql`hp AS (SELECT ${lines([columns(VISITOR_KEYS), ...folded(HIT_COUNTED, 'visitor')])}\n\t\tFROM hk GROUP BY ${columns(VISITOR_KEYS)})`,\n\t\tsql`hr AS (SELECT ${lines([columns(ROW_KEYS), ...folded(HIT_COUNTED, 'row')])}\n\t\tFROM hp GROUP BY ${columns(ROW_KEYS)})`,\n\t]\n}\n\n/** Item kinds: one fact per item clicked or seen, alone and by page (a server hit has no page). */\nconst itemKinds = (target: SqlTarget, items: readonly AggregateKind[]): SqlFragment[] => {\n\tconst { dialect } = target\n\tconst values = items.map((kind): [number, SqlFragment] => [\n\t\tkind.index,\n\t\tkind.source.from === 'item' && kind.source.page\n\t\t\t? sql`CASE WHEN ${dialect.isTrue(tField('server'))} THEN NULL ELSE ${tField('path')} END`\n\t\t\t: EMPTY,\n\t])\n\treturn [\n\t\titemFacts('xi', sql`a AS ${ident(T)}`, dialect),\n\t\tkindsCte('kx', items),\n\t\tsql`xf AS (SELECT * FROM (SELECT \"kx\".\"k\", ${t('x')} AS \"v1\", ${caseOf(ident('kx', 'k'), values)} AS \"v2\",\n\t\t\t${lines([...['hostname', 'v'].map((name) => t(name)), ...contributed({ axis: 'item' }, ITEM_COUNTED, dialect)])}\n\t\t\tFROM xi AS ${ident(T)} CROSS JOIN kx) AS \"f\"\n\t\tWHERE ${kept(items.filter((kind) => kind.dim2 === ''))})`,\n\t\tsql`xp AS (SELECT ${lines([columns(VISITOR_KEYS), ...folded(ITEM_COUNTED, 'visitor')])}\n\t\tFROM xf GROUP BY ${columns(VISITOR_KEYS)})`,\n\t\tsql`xr AS (SELECT ${lines([columns(ROW_KEYS), ...folded(ITEM_COUNTED, 'row')])}\n\t\tFROM xp GROUP BY ${columns(ROW_KEYS)})`,\n\t]\n}\n\n/**\n * `capDay` in the database: rows ranked within their cap's group, those past the cap folded into\n * its `(other)` row (`o` = 1), so a day answers at most the caps however many values it holds.\n */\nconst rankAndFold = (\n\tkinds: readonly AggregateKind[],\n\tcaps: CardinalityCaps,\n\tdialect: SqlDialect\n): { ranked: SqlFragment; select: SqlFragment } => {\n\tconst pairs = kinds.filter((kind) => kind.dim2 !== '').map((kind) => integer(kind.index))\n\tconst group =\n\t\tpairs.length === 0 ? EMPTY : sql`CASE WHEN \"k\" IN (${join(pairs)}) THEN \"v1\" ELSE '' END`\n\tconst capped = kinds.flatMap((kind): Array<[number, SqlFragment]> => {\n\t\tconst cap = kind.cap === null ? null : caps[kind.cap]\n\t\treturn cap === null ? [] : [[kind.index, integer(cap)]]\n\t})\n\tconst capOf = capped.length === 0 ? dialect.cast(NULL, 'integer') : caseOf(ident('k'), capped)\n\tconst order = join([\n\t\traw('COALESCE(\"pageviews\", \"impressions\", 0) DESC'),\n\t\traw('COALESCE(\"events\", 0) DESC'),\n\t\tsql`${dialect.byCodePoint(ident('v1'))} ASC`,\n\t\tsql`${dialect.byCodePoint(ident('v2'))} ASC`,\n\t])\n\tconst head = columns(ROW_KEYS)\n\tconst parts = columns(PART_KEYS)\n\tconst otherParts = PART_KEYS.map((part) =>\n\t\tUNKNOWN_WHEN_FOLDED.includes(part)\n\t\t\t? sql`${dialect.cast(NULL, 'real')} AS ${ident(part)}`\n\t\t\t: sql`SUM(${ident(part)}) AS ${ident(part)}`\n\t)\n\treturn {\n\t\tranked: sql`rk AS (SELECT ${head}, ${parts},\n\t\t\tROW_NUMBER() OVER (PARTITION BY \"k\", \"hostname\", ${group} ORDER BY ${order}) AS \"rn\",\n\t\t\t${capOf} AS \"cap\"\n\t\tFROM u)`,\n\t\tselect: sql`SELECT ${head}, ${parts}, 0 AS \"o\" FROM rk WHERE \"cap\" IS NULL OR \"rn\" <= \"cap\"\n\tUNION ALL SELECT \"k\", ${group} AS \"v1\", '' AS \"v2\", \"hostname\", ${join(otherParts)}, 1 AS \"o\"\n\t\tFROM rk WHERE \"rn\" > \"cap\" GROUP BY \"k\", \"hostname\", ${group}`,\n\t}\n}\n\n/**\n * A day's count in one statement: every kind's rows per hostname, capped (see {@link rankAndFold}),\n * uncounted parts NULL, and a {@link HITS_KIND} row holding the hits read.\n */\nexport const compileDayAggregation = (\n\twindow: TimeWindow,\n\ttarget: SqlTarget,\n\twritten: { itemPages: boolean; caps: CardinalityCaps }\n): SqlFragment => {\n\tconst { itemPages, caps } = written\n\tconst kinds = kindsWritten(itemPages)\n\tconst of = (from: string) => kinds.filter((kind) => kind.source.from === from)\n\tconst visit = [...of('site'), ...of('visit')]\n\tconst ctes = [\n\t\thitsCte(\n\t\t\twindow,\n\t\t\t{ sessionize: true, attributes: [], goals: true, impressions: true, sent: true },\n\t\t\ttarget\n\t\t),\n\t\t...dayVisitChain(target, window),\n\t\t...visitKinds(visit),\n\t\t...hitKinds(target, of('hit'), of('goal')),\n\t\t...itemKinds(target, of('item')),\n\t]\n\tconst head = columns(ROW_KEYS)\n\tconst { dialect } = target\n\tconst { ranked, select } = rankAndFold(kinds, caps, dialect)\n\tconst union = sql`u AS (SELECT ${head}, ${join(allParts(HIT_COUNTED, dialect))} FROM sr\n\t\tUNION ALL SELECT ${head}, ${join(allParts(HIT_COUNTED, dialect))} FROM hr\n\t\tUNION ALL SELECT ${head}, ${join(allParts(ITEM_COUNTED, dialect))} FROM xr)`\n\treturn sql`WITH ${join([...ctes, union, ranked], ',\\n\\t')}\n\t${select}\n\tUNION ALL SELECT ${integer(HITS_KIND)}, '', '', '', COUNT(*), ${join(\n\t\tPART_KEYS.slice(1).map(() => NULL)\n\t)}, 0 FROM a`\n}\n\nconst AGGREGATE_FIELDS: ReadonlyArray<Exclude<keyof AggregateRecord, 'parts'>> = [\n\t'id',\n\t'scope',\n\t'day',\n\t'hostname',\n\t'dim',\n\t'value',\n\t'dim2',\n\t'value2',\n\t'gen',\n]\n\nconst recordValues = (record: AggregateRecord): SqlFragment =>\n\tsql`(${join([\n\t\t...AGGREGATE_FIELDS.map((field) =>\n\t\t\tfield === 'id' ? sql`${generationRowId(record)}` : sql`${record[field]}`\n\t\t),\n\t\t...PART_KEYS.map((part) => {\n\t\t\tconst value = record.parts[part]\n\t\t\treturn value === null ? NULL : sql`${value}`\n\t\t}),\n\t])})`\n\n/**\n * Writes records as rows of their own generation beside the day's readable ones, which no read\n * counts until the day's marker names it. A row already written by the same run is left as it is.\n */\nexport const compileAggregateInsert = (\n\trecords: readonly AggregateRecord[],\n\ttarget: AggregateTarget\n): SqlFragment => {\n\tconst parts = PART_KEYS.map((part) => AGGREGATE_COLUMNS[part])\n\treturn sql`INSERT INTO ${target.aggregates} (${columns([...AGGREGATE_FIELDS, ...parts])})\n\t\tVALUES ${join(records.map(recordValues))}\n\t\tON CONFLICT (${ident('id')}) DO NOTHING`\n}\n\n/** The day's rows of every kind, as the indexes on scope, kind and day find them. */\nconst dayRows = (window: Pick<DayWindow, 'scope' | 'day'>): SqlFragment =>\n\tsql`\"scope\" = ${window.scope} AND \"day\" = ${window.day}\n\t\t\tAND \"dim\" IN (${valueList(KIND_DIMS)})\n\t\t\tAND \"dim2\" IN (${valueList(KIND_DIMS2)})`\n\n/** The generations the day's rows carry. */\nexport const compileDayGens = (\n\twindow: Pick<DayWindow, 'scope' | 'day'>,\n\ttarget: AggregateTarget\n): SqlFragment =>\n\tsql`SELECT DISTINCT \"gen\" FROM ${target.aggregates}\n\t\tWHERE ${dayRows(window)}`\n\n/** Which of a day's rows a delete takes: some generations, or all but one; at most `limit`. */\nexport type DayRowsDelete = ({ gens: readonly string[] } | { keep: string }) & { limit: number }\n\n/**\n * Deletes up to `limit` of the day's rows: those of the generations `gens` names, or of every\n * generation but `keep`. A statement of its own per share, so no writer waits out the whole day.\n */\nexport const compileDayRowsDelete = (\n\twindow: Pick<DayWindow, 'scope' | 'day'>,\n\twhich: DayRowsDelete,\n\ttarget: AggregateTarget\n): SqlFragment => {\n\tconst gens =\n\t\t'gens' in which ? sql`\"gen\" IN (${valueList(which.gens)})` : sql`\"gen\" <> ${which.keep}`\n\treturn sql`DELETE FROM ${target.aggregates}\n\t\tWHERE \"id\" IN (SELECT \"id\" FROM ${target.aggregates}\n\t\t\tWHERE ${dayRows(window)}\n\t\t\t\tAND ${gens}\n\t\t\tLIMIT ${integer(which.limit)})`\n}\n\ntype DayRanges = ReadonlyArray<readonly [CalendarDay, CalendarDay]>\n\n/** Ranges of days, with the span they cover first where there are several, as an index reads it. */\nconst dayRanges = (column: SqlFragment, days: DayRanges): SqlFragment => {\n\tconst ranges = join(\n\t\tdays.map(([from, to]) => sql`(${column} >= ${from} AND ${column} <= ${to})`),\n\t\t' OR '\n\t)\n\tif (days.length < 2) {\n\t\treturn ranges\n\t}\n\tconst from = Math.min(...days.map(([start]) => start))\n\tconst to = Math.max(...days.map(([, end]) => end))\n\treturn sql`${column} >= ${from} AND ${column} <= ${to} AND (${ranges})`\n}\n\n/** The completed days' generations of the scopes' ranges, as the `m` a read joins. */\nconst markersCte = (\n\tscopes: { scope?: string; scopes?: readonly string[] },\n\tdays: DayRanges,\n\ttarget: AggregateTarget\n): SqlFragment =>\n\tsql`m AS (SELECT \"scope\", \"day\", \"gen\" FROM ${target.state}\n\t\tWHERE \"kind\" = 'day' AND \"gen\" IS NOT NULL${join(\n\t\t\tscopeWhere(scopes, ident('scope')).map((condition) => sql` AND ${condition}`),\n\t\t\t''\n\t\t)} AND (${dayRanges(ident('day'), days)}))`\n\n/** A column of the aggregate rows a read takes, aliased `g`. */\nconst g = (name: string): SqlFragment => ident('g', name)\n\n/** The aggregate rows of the generations the markers `m` name, the only ones a read counts. */\nconst markedRows = (target: AggregateTarget): SqlFragment =>\n\tsql`${target.aggregates} AS \"g\"\n\t\t\tJOIN m ON \"m\".\"scope\" = \"g\".\"scope\" AND \"m\".\"day\" = \"g\".\"day\" AND \"m\".\"gen\" = \"g\".\"gen\"`\n\n/**\n * The rows a fold read decides on: of completed days, the value's and its `(other)` row for a\n * dimension, a pair's first value's `(other)` rows alone.\n */\nexport const compileFoldRead = (\n\tread: FoldRead,\n\tkind: AggregateKind,\n\ttarget: AggregateTarget\n): SqlFragment => {\n\tconst values =\n\t\tkind.dim2 === ''\n\t\t\t? sql`${g('value')} IN (${read.value}, '')`\n\t\t\t: sql`${g('value')} = ${read.value} AND ${g('value2')} = ''`\n\treturn sql`WITH ${markersCte({ scope: read.scope }, read.days, target)}\n\tSELECT ${g('day')} AS \"day\", ${g('hostname')} AS \"hostname\", ${g('value')} AS \"value\", ${g('value2')} AS \"value2\"\n\t\tFROM ${markedRows(target)}\n\t\tWHERE ${g('scope')} = ${read.scope} AND ${g('dim')} = ${kind.dim} AND ${g('dim2')} = ${kind.dim2}\n\t\t\tAND (${dayRanges(g('day'), read.days)})${\n\t\t\t\tread.hostname === undefined ? raw('') : sql` AND ${g('hostname')} = ${read.hostname}`\n\t\t\t}\n\t\t\tAND ${values}`\n}\n\n/** The hostnames a scope's site rows hold from `since` on. */\nexport const compileHostnames = (\n\tscope: string,\n\tsince: CalendarDay,\n\ttarget: AggregateTarget\n): SqlFragment =>\n\tsql`SELECT DISTINCT \"hostname\" FROM ${target.aggregates}\n\t\tWHERE \"scope\" = ${scope} AND \"dim\" = '' AND \"dim2\" = '' AND \"day\" >= ${since}`\n\n/**\n * One statement summing the rows of completed days: the markers' generations joined in, so a row\n * written by a run that never completed, or replaced since, is never read, all in one snapshot.\n */\nexport const compileAggregateRead = (plan: AggregatePlan, target: AggregateTarget): SqlFragment => {\n\tconst { read, kind } = plan\n\tconst { dialect } = target\n\tconst grouped = plan.group === 'value' ? g('value') : plan.group === 'value2' ? g('value2') : null\n\tconst markers = markersCte(read, read.days, target)\n\tconst conditions = [\n\t\t...scopeWhere(read, g('scope')),\n\t\tsql`${g('dim')} = ${kind.dim}`,\n\t\tsql`${g('dim2')} = ${kind.dim2}`,\n\t\tsql`(${dayRanges(g('day'), read.days)})`,\n\t\t...(read.hostname === undefined ? [] : [sql`${g('hostname')} = ${read.hostname}`]),\n\t\t...(read.value === undefined ? [] : [sql`${g('value')} = ${read.value}`]),\n\t\t...(read.values === undefined || grouped === null\n\t\t\t? []\n\t\t\t: [sql`${grouped} IN (${valueList(read.values)})`]),\n\t\t// An `(other)` row folds many values together: it is part of a total, never a value.\n\t\t...(grouped === null ? [] : [sql`${grouped} <> ''`]),\n\t]\n\tconst key = plan.group === 'day' ? g('day') : grouped\n\tconst keys = key === null ? [] : [sql`${key} AS ${ident(plan.group === 'day' ? 'day' : 'value')}`]\n\tconst sums = PART_KEYS.map((part) => sql`SUM(${g(AGGREGATE_COLUMNS[part])}) AS ${ident(part)}`)\n\tconst summed = sql`r AS (SELECT ${lines([...keys, ...sums, raw('COUNT(*) AS \"n\"')])}\n\t\tFROM ${markedRows(target)}\n\t\tWHERE ${join(conditions, '\\n\\t\\t\\tAND ')}${\n\t\t\tkey === null\n\t\t\t\t? raw('')\n\t\t\t\t: sql`\n\t\tGROUP BY ${key}`\n\t\t})`\n\tconst visible =\n\t\tplan.group === null\n\t\t\t? raw('')\n\t\t\t: sql`\n\tWHERE COALESCE(\"pageviews\", 0) + COALESCE(\"events\", 0) + COALESCE(\"impressions\", 0) > 0`\n\tconst ordering = [\n\t\t...[rankSql(read.order, kind.counted, dialect)].filter((rank) => rank !== null),\n\t\t...(plan.group === 'value' || plan.group === 'value2'\n\t\t\t? [sql`${dialect.byCodePoint(ident('value'))} ASC`]\n\t\t\t: []),\n\t\t...(plan.group === 'day' ? [sql`\"day\" ASC`] : []),\n\t]\n\tconst orderBy =\n\t\tordering.length > 0\n\t\t\t? sql`\n\tORDER BY ${join(ordering)}`\n\t\t\t: raw('')\n\tconst limit =\n\t\tread.limit === undefined\n\t\t\t? raw('')\n\t\t\t: sql`\n\tLIMIT ${read.limit}`\n\treturn sql`WITH ${markers},\n\t${summed}\n\tSELECT * FROM r${visible}${orderBy}${limit}`\n}\n"],"mappings":";;;;;;;;AA2CA,MAAM,oBAAmE;CACxE,WAAW;CACX,QAAQ;CACR,UAAU;CACV,UAAU;CACV,SAAS;CACT,SAAS;CACT,WAAW;CACX,YAAY;CACZ,WAAW;CACX,eAAe;CACf,aAAa;CACb,SAAS;CACT,aAAa;CACb,cAAc;CACd,yBAAyB;CACzB,yBAAyB;AAC1B;AAKA,MAAM,QAAQ,IAAI,IAAI;;AAGtB,MAAM,WAAW;CAAC;CAAK;CAAM;CAAM;AAAU;;AAG7C,MAAM,eAAe,CAAC,GAAG,UAAU,GAAG;AAEtC,MAAM,WAAW,UAA+B,IAAI,OAAO,KAAK,MAAM,KAAK,CAAC,CAAC;;AAG7E,MAAM,YAAY,MAAc,UAC/B,GAAG,GAAG,MAAM,IAAI,EAAE,mBAAmB,KAAK,MAAM,KAAK,SAAS,GAAG,IAAI,QAAQ,KAAK,KAAK,EAAE,EAAE,CAAC,EAAE;AAE/F,M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AG;EAAM,GAAG;EAAM,IAAI,mBAAiB;CAAC,CAAC,EAAE;SAC5E,WAAW,MAAM,EAAE;UAClB,KAAK,YAAY,WAAc,IACtC,QAAQ,OACL,IAAI,EAAE,IACN,GAAG;aACI,MACV;CACF,MAAM,UACL,KAAK,UAAU,OACZ,IAAI,EAAE,IACN,GAAG;;CAEP,MAAM,WAAW;EAChB,GAAG,CAAC,QAAQ,KAAK,OAAO,KAAK,SAAS,OAAO,CAAC,EAAE,QAAQ,SAAS,SAAS,IAAI;EAC9E,GAAI,KAAK,UAAU,WAAW,KAAK,UAAU,WAC1C,CAAC,GAAG,GAAG,QAAQ,YAAY,MAAM,OAAO,CAAC,EAAE,KAAK,IAChD,CAAC;EACJ,GAAI,KAAK,UAAU,QAAQ,CAAC,GAAG,WAAW,IAAI,CAAC;CAChD;CAWA,OAAO,GAAG,QAAQ,QAAQ;GACxB,OAAO;kBACQ,UAXhB,SAAS,SAAS,IACf,GAAG;YACI,KAAK,QAAQ,MACpB,IAAI,EAAE,IAET,KAAK,UAAU,KAAA,IACZ,IAAI,EAAE,IACN,GAAG;SACC,KAAK;AAId"}
@@ -11,6 +11,7 @@ const HIT_COLUMNS = {
11
11
  v: "v",
12
12
  landing: "landing",
13
13
  server: "server",
14
+ mode: "mode",
14
15
  name: "name",
15
16
  ref: "ref",
16
17
  source: "source",
@@ -1 +1 @@
1
- {"version":3,"file":"columns.js","names":[],"sources":["../../../../src/native/store/sql/columns.ts"],"sourcesContent":["import type { HitRow } from '../types'\n\n/** Each field's column as Payload's drizzle adapters name it; the type holds every field to one. */\nexport const HIT_COLUMNS: { readonly [K in keyof HitRow]-?: string } = {\n\tid: 'id',\n\tscope: 'scope',\n\tts: 'ts',\n\tlastAt: 'last_at',\n\ttype: 'type',\n\thostname: 'hostname',\n\tpath: 'path',\n\tv: 'v',\n\tlanding: 'landing',\n\tserver: 'server',\n\tname: 'name',\n\tref: 'ref',\n\tsource: 'source',\n\tchannel: 'channel',\n\tchannelVersion: 'channel_version',\n\tclickId: 'click_id',\n\tutmSource: 'utm_source',\n\tutmMedium: 'utm_medium',\n\tutmCampaign: 'utm_campaign',\n\tutmContent: 'utm_content',\n\tutmTerm: 'utm_term',\n\tcountry: 'country',\n\tregion: 'region',\n\tcity: 'city',\n\tlanguage: 'language',\n\tdevice: 'device',\n\tbrowser: 'browser',\n\tos: 'os',\n\tdurationMs: 'duration_ms',\n\tscrollDepth: 'scroll_depth',\n\tvalue: 'value',\n\toriginalValue: 'original_value',\n\tconversions: 'conversions',\n\trevenue: 'revenue',\n\tcurrency: 'currency',\n\toriginalCurrency: 'original_currency',\n\titem: 'item',\n\tprops: 'props',\n\tgoals: 'goals',\n\timpressions: 'impressions',\n}\n"],"mappings":";;AAGA,MAAa,cAA0D;CACtE,IAAI;CACJ,OAAO;CACP,IAAI;CACJ,QAAQ;CACR,MAAM;CACN,UAAU;CACV,MAAM;CACN,GAAG;CACH,SAAS;CACT,QAAQ;CACR,MAAM;CACN,KAAK;CACL,QAAQ;CACR,SAAS;CACT,gBAAgB;CAChB,SAAS;CACT,WAAW;CACX,WAAW;CACX,aAAa;CACb,YAAY;CACZ,SAAS;CACT,SAAS;CACT,QAAQ;CACR,MAAM;CACN,UAAU;CACV,QAAQ;CACR,SAAS;CACT,IAAI;CACJ,YAAY;CACZ,aAAa;CACb,OAAO;CACP,eAAe;CACf,aAAa;CACb,SAAS;CACT,UAAU;CACV,kBAAkB;CAClB,MAAM;CACN,OAAO;CACP,OAAO;CACP,aAAa;AACd"}
1
+ {"version":3,"file":"columns.js","names":[],"sources":["../../../../src/native/store/sql/columns.ts"],"sourcesContent":["import type { HitRow } from '../types'\n\n/** Each field's column as Payload's drizzle adapters name it; the type holds every field to one. */\nexport const HIT_COLUMNS: { readonly [K in keyof HitRow]-?: string } = {\n\tid: 'id',\n\tscope: 'scope',\n\tts: 'ts',\n\tlastAt: 'last_at',\n\ttype: 'type',\n\thostname: 'hostname',\n\tpath: 'path',\n\tv: 'v',\n\tlanding: 'landing',\n\tserver: 'server',\n\tmode: 'mode',\n\tname: 'name',\n\tref: 'ref',\n\tsource: 'source',\n\tchannel: 'channel',\n\tchannelVersion: 'channel_version',\n\tclickId: 'click_id',\n\tutmSource: 'utm_source',\n\tutmMedium: 'utm_medium',\n\tutmCampaign: 'utm_campaign',\n\tutmContent: 'utm_content',\n\tutmTerm: 'utm_term',\n\tcountry: 'country',\n\tregion: 'region',\n\tcity: 'city',\n\tlanguage: 'language',\n\tdevice: 'device',\n\tbrowser: 'browser',\n\tos: 'os',\n\tdurationMs: 'duration_ms',\n\tscrollDepth: 'scroll_depth',\n\tvalue: 'value',\n\toriginalValue: 'original_value',\n\tconversions: 'conversions',\n\trevenue: 'revenue',\n\tcurrency: 'currency',\n\toriginalCurrency: 'original_currency',\n\titem: 'item',\n\tprops: 'props',\n\tgoals: 'goals',\n\timpressions: 'impressions',\n}\n"],"mappings":";;AAGA,MAAa,cAA0D;CACtE,IAAI;CACJ,OAAO;CACP,IAAI;CACJ,QAAQ;CACR,MAAM;CACN,UAAU;CACV,MAAM;CACN,GAAG;CACH,SAAS;CACT,QAAQ;CACR,MAAM;CACN,MAAM;CACN,KAAK;CACL,QAAQ;CACR,SAAS;CACT,gBAAgB;CAChB,SAAS;CACT,WAAW;CACX,WAAW;CACX,aAAa;CACb,YAAY;CACZ,SAAS;CACT,SAAS;CACT,QAAQ;CACR,MAAM;CACN,UAAU;CACV,QAAQ;CACR,SAAS;CACT,IAAI;CACJ,YAAY;CACZ,aAAa;CACb,OAAO;CACP,eAAe;CACf,aAAa;CACb,SAAS;CACT,UAAU;CACV,kBAAkB;CAClB,MAAM;CACN,OAAO;CACP,OAAO;CACP,aAAa;AACd"}
@@ -2,7 +2,7 @@ import { ITEM_CLICK_EVENT } from "../../../goals/constants.js";
2
2
  import { ident, join, raw, sql, valueList } from "./fragment.js";
3
3
  import { KEPT_PARTS, SESSION_ATTRIBUTES, SESSION_DIMENSIONS, isAcquisition, isRawDimension, readWindow, visitAttributesOf } from "../rawRead.js";
4
4
  import { ORDER_PREFIX, SERVER_ORDER_PREFIX, WIRE_ORDER_PREFIX } from "../ids.js";
5
- import { TWIN_WINDOW_MS } from "../hits.js";
5
+ import { TWIN_WINDOW_MS, WITHOUT_CONSENT_MODES } from "../hits.js";
6
6
  import { HIT_COLUMNS } from "./columns.js";
7
7
  //#region src/native/store/sql/compile.ts
8
8
  /** A hits column by its field, unqualified. */
@@ -34,8 +34,13 @@ const HIT_FIELDS = [
34
34
  "path",
35
35
  "name",
36
36
  "durationMs",
37
- "scrollDepth"
37
+ "scrollDepth",
38
+ "mode"
38
39
  ];
40
+ /** The modes measured before or without consent, as a list of literals. */
41
+ const WITHOUT_CONSENT = raw(WITHOUT_CONSENT_MODES.map((mode) => `'${mode}'`).join(", "));
42
+ /** Whether a row of `field`'s hits is a pageview the page measured before or without consent. */
43
+ const pageviewWithoutConsent = (field) => sql`${field("type")} = 'pageview' AND ${field("mode")} IN (${WITHOUT_CONSENT})`;
39
44
  /** A goal completion's amount as sent: before it was converted, else as counted. */
40
45
  const SENT_KEYS = ["sent", "value"];
41
46
  /** The sale a hit records, which the server's record of an order hands to the browser's. */
@@ -217,7 +222,8 @@ const VISIT_SUMS = [
217
222
  raw(`SUM(CASE WHEN "type" = 'pageview' AND "scroll_depth" IS NOT NULL THEN 1 ELSE 0 END) OVER whole AS "v_sc"`),
218
223
  raw(`SUM(COALESCE("conversions", 0)) OVER whole AS "v_conv"`),
219
224
  raw(`SUM(COALESCE("revenue", 0)) OVER whole AS "v_rev"`),
220
- raw(`SUM(CASE WHEN "type" = 'event' AND "name" = '${ITEM_CLICK_EVENT}' AND "item" IS NOT NULL AND "item" <> '' THEN 1 ELSE 0 END) OVER whole AS "v_int"`)
225
+ raw(`SUM(CASE WHEN "type" = 'event' AND "name" = '${ITEM_CLICK_EVENT}' AND "item" IS NOT NULL AND "item" <> '' THEN 1 ELSE 0 END) OVER whole AS "v_int"`),
226
+ sql`SUM(CASE WHEN ${pageviewWithoutConsent(column)} THEN 1 ELSE 0 END) OVER whole AS "v_npc"`
221
227
  ];
222
228
  /** Visit starts and numbers, each visitor's hits framed by time, then id by code point. */
223
229
  const numberVisits = ({ dialect, visitor }) => {
@@ -361,6 +367,7 @@ const VISIT_ROW = [
361
367
  "v",
362
368
  "sn",
363
369
  "type",
370
+ "mode",
364
371
  "eng",
365
372
  "name",
366
373
  "item",
@@ -378,7 +385,8 @@ const VISIT_ROW = [
378
385
  "v_sc",
379
386
  "v_conv",
380
387
  "v_rev",
381
- "v_int"
388
+ "v_int",
389
+ "v_npc"
382
390
  ];
383
391
  /**
384
392
  * A scope's day cut once for every kind: `va` has a row per visit (`w` 0) and per goal hit (`w` 1).
@@ -475,7 +483,9 @@ const contributionsOf = ({ axis, sent = false }, counted, dialect) => {
475
483
  ["conversions", goal ? sql`CASE WHEN COALESCE(${tField("conversions")}, 0) > 0 THEN 1 ELSE 0 END` : sql`COALESCE(${tField("conversions")}, 0)`],
476
484
  ["revenue", goal ? sent ? sql`COALESCE(${goalField(dialect, "sent")}, ${goalField(dialect, "value")}, 0)` : completion : sql`COALESCE(${sent ? t("sent_revenue") : tField("revenue")}, 0)`],
477
485
  ["impressions", t("imp")],
478
- ["interactions", sql`CASE WHEN ${tField("type")} = 'event' AND ${tField("name")} = ${raw(`'${ITEM_CLICK_EVENT}'`)} AND ${tField("item")} IS NOT NULL AND ${tField("item")} <> '' THEN 1 ELSE 0 END`]
486
+ ["interactions", sql`CASE WHEN ${tField("type")} = 'event' AND ${tField("name")} = ${raw(`'${ITEM_CLICK_EVENT}'`)} AND ${tField("item")} IS NOT NULL AND ${tField("item")} <> '' THEN 1 ELSE 0 END`],
487
+ ["pageviewsWithoutVisitor", sql`CASE WHEN ${tField("type")} = 'pageview' AND ${tField("v")} = '' THEN 1 ELSE 0 END`],
488
+ ["pageviewsWithoutConsent", sql`CASE WHEN ${pageviewWithoutConsent(tField)} THEN 1 ELSE 0 END`]
479
489
  ]);
480
490
  return new Map([...all].filter(([part]) => counted.has(part)));
481
491
  };
@@ -529,8 +539,10 @@ const rankSql = (order, counted, dialect) => {
529
539
  if (!order || !counted.has(order.part)) return null;
530
540
  const direction = raw(order.dir === "asc" ? "ASC" : "DESC");
531
541
  if (order.per === void 0) return sql`${quoted(order.part)} ${direction} NULLS LAST`;
532
- if (!counted.has(order.per)) return null;
533
- return sql`${dialect.cast(quoted(order.part), "real")} / NULLIF(${dialect.cast(quoted(order.per), "real")}, 0) ${direction} NULLS LAST`;
542
+ if (!counted.has(order.per) || order.less !== void 0 && !counted.has(order.less)) return null;
543
+ const whole = dialect.cast(quoted(order.per), "real");
544
+ const per = order.less === void 0 ? whole : sql`(${whole} - COALESCE(${dialect.cast(quoted(order.less), "real")}, 0))`;
545
+ return sql`${dialect.cast(quoted(order.part), "real")} / NULLIF(${per}, 0) ${direction} NULLS LAST`;
534
546
  };
535
547
  /**
536
548
  * One statement: the window's hits, the visit chain, the facts filtered after visits are cut, then