derive-dashboards 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (410) hide show
  1. package/README.md +8 -5
  2. package/dist/adapter/sql-adapter.mjs +4 -4
  3. package/dist/body.mjs +11 -1
  4. package/dist/cache.d.mts +1 -1
  5. package/dist/cli/add.mjs +130 -0
  6. package/dist/cli/check.mjs +14 -1
  7. package/dist/cli/cli.mjs +14 -12
  8. package/dist/cli/config.mjs +12 -1
  9. package/dist/cli/doctor.mjs +147 -0
  10. package/dist/cli/import/dbt-schema.mjs +2 -2
  11. package/dist/cli/import/index.mjs +2 -2
  12. package/dist/cli/init.mjs +3 -2
  13. package/dist/cli/json.mjs +21 -0
  14. package/dist/cli/layers.mjs +33 -0
  15. package/dist/core/conformance/cases.d.mts +3 -0
  16. package/dist/core/conformance/cases.mjs +20 -4
  17. package/dist/core/conformance/check.mjs +6 -3
  18. package/dist/core/conformance/drill-cases.mjs +120 -0
  19. package/dist/core/document/layout.mjs +1 -1
  20. package/dist/core/document/view-layout.d.mts +14 -0
  21. package/dist/core/document/view-layout.mjs +157 -0
  22. package/dist/core/document/view.d.mts +26 -7
  23. package/dist/core/document/view.mjs +42 -11
  24. package/dist/core/engine/filter.mjs +2 -2
  25. package/dist/core/engine/index.d.mts +14 -2
  26. package/dist/core/engine/index.mjs +12 -1
  27. package/dist/core/engine/leaf.mjs +9 -4
  28. package/dist/core/engine/members.mjs +2 -2
  29. package/dist/core/engine/postgres.mjs +1 -1
  30. package/dist/core/engine/record-layout.d.mts +11 -0
  31. package/dist/core/engine/record-layout.mjs +7 -7
  32. package/dist/core/engine/records.d.mts +3 -2
  33. package/dist/core/engine/records.mjs +2 -1
  34. package/dist/core/engine/segmented.mjs +5 -2
  35. package/dist/core/engine/sql-records.mjs +2 -1
  36. package/dist/core/engine/statement.mjs +37 -3
  37. package/dist/core/engine/types.d.mts +3 -0
  38. package/dist/core/engine/version.d.mts +1 -1
  39. package/dist/core/engine/version.mjs +1 -1
  40. package/dist/core/error/codes.d.mts +33 -1
  41. package/dist/core/error/codes.mjs +30 -2
  42. package/dist/core/index.d.mts +13 -7
  43. package/dist/core/index.mjs +9 -5
  44. package/dist/core/model/catalog/check.mjs +1 -1
  45. package/dist/core/model/hints.d.mts +36 -0
  46. package/dist/core/model/member.d.mts +4 -2
  47. package/dist/core/model/member.mjs +19 -3
  48. package/dist/core/model/types.d.mts +21 -19
  49. package/dist/core/model/validate/drill.mjs +70 -0
  50. package/dist/core/model/validate/index.mjs +3 -0
  51. package/dist/core/model/validate/members.mjs +7 -4
  52. package/dist/core/model/validate/shape.mjs +150 -0
  53. package/dist/core/model/validate/source.mjs +5 -3
  54. package/dist/core/policy/bind-query.d.mts +12 -0
  55. package/dist/core/policy/bind-query.mjs +53 -0
  56. package/dist/core/query/alias.mjs +7 -1
  57. package/dist/core/query/drill/compose.d.mts +22 -0
  58. package/dist/core/query/drill/compose.mjs +85 -0
  59. package/dist/core/query/drill/fields.d.mts +21 -0
  60. package/dist/core/query/drill/fields.mjs +91 -0
  61. package/dist/core/query/drill/internal.d.mts +11 -0
  62. package/dist/core/query/drill/items.d.mts +29 -0
  63. package/dist/core/query/drill/items.mjs +98 -0
  64. package/dist/core/query/drill/line.d.mts +31 -0
  65. package/dist/core/query/drill/line.mjs +183 -0
  66. package/dist/core/query/drill/links.d.mts +33 -0
  67. package/dist/core/query/drill/links.mjs +31 -0
  68. package/dist/core/query/drill/operand.mjs +7 -0
  69. package/dist/core/query/drill/scopes.d.mts +22 -0
  70. package/dist/core/query/drill/scopes.mjs +58 -0
  71. package/dist/core/query/drill/selection.d.mts +37 -0
  72. package/dist/core/query/drill/selection.mjs +144 -0
  73. package/dist/core/query/drill/steps.d.mts +14 -0
  74. package/dist/core/query/drill/steps.mjs +35 -0
  75. package/dist/core/query/drill/synthetic-expr.mjs +85 -0
  76. package/dist/core/query/drill/synthetic.d.mts +22 -0
  77. package/dist/core/query/drill/synthetic.mjs +165 -0
  78. package/dist/core/query/drill/time.d.mts +11 -0
  79. package/dist/core/query/drill/time.mjs +71 -0
  80. package/dist/core/query/drill/types.d.mts +155 -0
  81. package/dist/core/query/drill/types.mjs +4 -0
  82. package/dist/core/query/drill/views.d.mts +62 -0
  83. package/dist/core/query/drill/views.mjs +257 -0
  84. package/dist/core/query/filter.d.mts +6 -0
  85. package/dist/core/query/picks.d.mts +8 -2
  86. package/dist/core/query/picks.mjs +11 -4
  87. package/dist/core/query/segments.d.mts +12 -2
  88. package/dist/core/query/segments.mjs +5 -2
  89. package/dist/core/result/columns.mjs +20 -4
  90. package/dist/core/result/types.d.mts +5 -1
  91. package/dist/core/schema/aliases.d.mts +7 -0
  92. package/dist/core/schema/aliases.mjs +77 -0
  93. package/dist/core/schema/answer-parts.mjs +117 -0
  94. package/dist/core/schema/answers.d.mts +2109 -128
  95. package/dist/core/schema/answers.mjs +11 -113
  96. package/dist/core/schema/commands.d.mts +2 -2
  97. package/dist/core/schema/document.d.mts +1 -1
  98. package/dist/core/schema/drill-answers.mjs +179 -0
  99. package/dist/core/schema/drill.d.mts +260 -0
  100. package/dist/core/schema/drill.mjs +107 -0
  101. package/dist/core/schema/index.d.mts +6 -3
  102. package/dist/core/schema/index.mjs +6 -3
  103. package/dist/core/schema/picks.d.mts +35 -0
  104. package/dist/core/schema/picks.mjs +23 -0
  105. package/dist/core/schema/query.d.mts +82 -1
  106. package/dist/core/schema/query.mjs +9 -5
  107. package/dist/core/schema/tools.mjs +1 -1
  108. package/dist/core/schema/view.d.mts +0 -35
  109. package/dist/core/schema/view.mjs +19 -24
  110. package/dist/core/store/schema.d.mts +5 -0
  111. package/dist/core/store/schema.mjs +10 -0
  112. package/dist/core/types/api.d.mts +21 -1
  113. package/dist/core/types/api.mjs +1 -1
  114. package/dist/core/types/drill-api.d.mts +210 -0
  115. package/dist/core/types/options.d.mts +21 -0
  116. package/dist/core/types/viewer.d.mts +5 -0
  117. package/dist/cursor-seal.d.mts +8 -0
  118. package/dist/cursor-seal.mjs +98 -0
  119. package/dist/dashboard-id.mjs +1 -1
  120. package/dist/dashboards.mjs +4 -0
  121. package/dist/drill-access.mjs +93 -0
  122. package/dist/drill-change.mjs +76 -0
  123. package/dist/drill-crumbs.mjs +113 -0
  124. package/dist/drill-distinct.mjs +130 -0
  125. package/dist/drill-explain.mjs +64 -0
  126. package/dist/drill-identity.mjs +35 -0
  127. package/dist/drill-item-id.mjs +35 -0
  128. package/dist/drill-items.mjs +132 -0
  129. package/dist/drill-open.mjs +169 -0
  130. package/dist/drill-seal.mjs +170 -0
  131. package/dist/drill-secret.mjs +14 -0
  132. package/dist/drill-share.mjs +53 -0
  133. package/dist/drill-summary.mjs +158 -0
  134. package/dist/drill-targets.mjs +53 -0
  135. package/dist/drill-totals.mjs +8 -0
  136. package/dist/drill-views.mjs +175 -0
  137. package/dist/drill.mjs +157 -0
  138. package/dist/editing.mjs +3 -3
  139. package/dist/endpoints/dashboards.mjs +28 -5
  140. package/dist/endpoints/drill.mjs +55 -0
  141. package/dist/endpoints/index.d.mts +245 -4
  142. package/dist/endpoints/index.mjs +5 -1
  143. package/dist/endpoints/meta.mjs +10 -1
  144. package/dist/endpoints/query.mjs +26 -4
  145. package/dist/execute.d.mts +19 -1
  146. package/dist/execute.mjs +34 -27
  147. package/dist/handler.mjs +5 -1
  148. package/dist/index.d.mts +3 -1
  149. package/dist/index.mjs +29 -2
  150. package/dist/llms.txt +22 -0
  151. package/dist/mcp/drill.mjs +61 -0
  152. package/dist/mcp/format.mjs +69 -11
  153. package/dist/mcp/index.d.mts +15 -4
  154. package/dist/mcp/index.mjs +13 -7
  155. package/dist/mcp/query.d.mts +7 -2
  156. package/dist/mcp/query.mjs +40 -6
  157. package/dist/node-postgres.d.mts +8 -4
  158. package/dist/node-postgres.mjs +29 -6
  159. package/dist/owner.mjs +3 -3
  160. package/dist/postgres-connection.d.mts +8 -2
  161. package/dist/postgres-connection.mjs +2 -1
  162. package/dist/postgres-store.mjs +1 -0
  163. package/dist/react/Canvas.mjs +28 -5
  164. package/dist/react/Cell.mjs +2 -1
  165. package/dist/react/Dashboard.d.mts +10 -5
  166. package/dist/react/Dashboard.mjs +62 -22
  167. package/dist/react/DashboardList.mjs +52 -24
  168. package/dist/react/DashboardListRow.mjs +49 -0
  169. package/dist/react/Discard.mjs +12 -4
  170. package/dist/react/Editing.mjs +19 -5
  171. package/dist/react/FilterBar.d.mts +7 -9
  172. package/dist/react/Grid.mjs +19 -24
  173. package/dist/react/Header.mjs +10 -1
  174. package/dist/react/History.mjs +5 -0
  175. package/dist/react/Parts.mjs +6 -2
  176. package/dist/react/ReadFailed.mjs +23 -0
  177. package/dist/react/ReviewPanel.mjs +13 -2
  178. package/dist/react/RowBody.d.mts +19 -0
  179. package/dist/react/RowBody.mjs +55 -0
  180. package/dist/react/Spaces.mjs +15 -1
  181. package/dist/react/Tile.d.mts +3 -1
  182. package/dist/react/Tile.mjs +84 -32
  183. package/dist/react/_virtual/_rolldown/runtime.mjs +12 -0
  184. package/dist/react/arrange-steps.mjs +16 -13
  185. package/dist/react/arrange.mjs +10 -3
  186. package/dist/react/builder/Calcs.mjs +35 -11
  187. package/dist/react/builder/FilterEditor.mjs +196 -0
  188. package/dist/react/builder/Filters.mjs +172 -116
  189. package/dist/react/builder/Formula.mjs +82 -0
  190. package/dist/react/builder/Groups.mjs +1 -0
  191. package/dist/react/builder/Preview.mjs +1 -1
  192. package/dist/react/builder/RangeEditor.mjs +184 -0
  193. package/dist/react/builder/Sections.mjs +41 -6
  194. package/dist/react/builder/Time.mjs +138 -60
  195. package/dist/react/builder/Values.mjs +85 -0
  196. package/dist/react/builder/WidgetBuilder.mjs +50 -49
  197. package/dist/react/builder/base.d.mts +5 -0
  198. package/dist/react/builder/base.mjs +33 -8
  199. package/dist/react/builder/calc.mjs +15 -1
  200. package/dist/react/builder/escape.d.mts +1 -0
  201. package/dist/react/builder/escape.mjs +62 -0
  202. package/dist/react/builder/filter-tree.d.mts +9 -0
  203. package/dist/react/builder/filter-tree.mjs +186 -0
  204. package/dist/react/builder/formula-parse.d.mts +18 -0
  205. package/dist/react/builder/formula-parse.mjs +136 -0
  206. package/dist/react/builder/labels.d.mts +1 -1
  207. package/dist/react/builder/state.d.mts +2 -1
  208. package/dist/react/builder/state.mjs +23 -37
  209. package/dist/react/builder/useBuilder.d.mts +12 -1
  210. package/dist/react/builder/useBuilder.mjs +6 -3
  211. package/dist/react/builder/words.d.mts +59 -0
  212. package/dist/react/builder/words.mjs +73 -0
  213. package/dist/react/client.d.mts +2 -0
  214. package/dist/react/client.mjs +1 -0
  215. package/dist/react/context.d.mts +28 -1
  216. package/dist/react/context.mjs +25 -4
  217. package/dist/react/drill/Bars.mjs +179 -0
  218. package/dist/react/drill/Controls.mjs +173 -0
  219. package/dist/react/drill/DashboardTile.d.mts +18 -0
  220. package/dist/react/drill/DashboardTile.mjs +107 -0
  221. package/dist/react/drill/DrillPanel.d.mts +31 -0
  222. package/dist/react/drill/DrillPanel.mjs +104 -0
  223. package/dist/react/drill/DrillTabs.mjs +52 -0
  224. package/dist/react/drill/Header.mjs +131 -0
  225. package/dist/react/drill/Items.d.mts +22 -0
  226. package/dist/react/drill/Items.mjs +164 -0
  227. package/dist/react/drill/ItemsFoot.mjs +60 -0
  228. package/dist/react/drill/Notes.mjs +58 -0
  229. package/dist/react/drill/PanelBody.mjs +74 -0
  230. package/dist/react/drill/PanelStates.mjs +37 -0
  231. package/dist/react/drill/board.d.mts +18 -0
  232. package/dist/react/drill/board.mjs +91 -0
  233. package/dist/react/drill/counts.mjs +14 -0
  234. package/dist/react/drill/host.mjs +26 -0
  235. package/dist/react/drill/item-parts.d.mts +5 -0
  236. package/dist/react/drill/item-parts.mjs +76 -0
  237. package/dist/react/drill/item-type.d.mts +26 -0
  238. package/dist/react/drill/item-type.mjs +7 -0
  239. package/dist/react/drill/lazy.d.mts +6 -0
  240. package/dist/react/drill/lazy.mjs +17 -0
  241. package/dist/react/drill/pages.mjs +48 -0
  242. package/dist/react/drill/panel-focus.mjs +51 -0
  243. package/dist/react/drill/props.d.mts +25 -0
  244. package/dist/react/drill/props.mjs +72 -0
  245. package/dist/react/drill/state.d.mts +18 -0
  246. package/dist/react/drill/state.mjs +68 -0
  247. package/dist/react/drill/useDrill.d.mts +46 -0
  248. package/dist/react/drill/useDrill.mjs +194 -0
  249. package/dist/react/drill/words.d.mts +101 -0
  250. package/dist/react/drill/words.mjs +79 -0
  251. package/dist/react/english.mjs +29 -6
  252. package/dist/react/filters/bar.d.mts +12 -0
  253. package/dist/react/filters/bar.mjs +135 -0
  254. package/dist/react/filters/calendar.mjs +193 -0
  255. package/dist/react/filters/chip.mjs +174 -0
  256. package/dist/react/filters/context.d.mts +22 -0
  257. package/dist/react/filters/context.mjs +78 -0
  258. package/dist/react/filters/dates.mjs +89 -0
  259. package/dist/react/filters/days.d.mts +36 -0
  260. package/dist/react/filters/days.mjs +87 -0
  261. package/dist/react/filters/field-step.mjs +81 -0
  262. package/dist/react/filters/hooks.d.mts +34 -0
  263. package/dist/react/filters/hooks.mjs +94 -0
  264. package/dist/react/filters/listbox.mjs +131 -0
  265. package/dist/react/filters/logic.d.mts +59 -0
  266. package/dist/react/filters/logic.mjs +109 -0
  267. package/dist/react/filters/menu-search.mjs +211 -0
  268. package/dist/react/filters/menu.mjs +85 -0
  269. package/dist/react/filters/number.mjs +127 -0
  270. package/dist/react/filters/numbers.d.mts +47 -0
  271. package/dist/react/filters/numbers.mjs +151 -0
  272. package/dist/react/filters/operator.mjs +104 -0
  273. package/dist/react/filters/phone.mjs +104 -0
  274. package/dist/react/filters/popover.d.mts +19 -0
  275. package/dist/react/filters/popover.mjs +153 -0
  276. package/dist/react/filters/rules.d.mts +8 -0
  277. package/dist/react/filters/rules.mjs +55 -0
  278. package/dist/react/filters/sheet.mjs +196 -0
  279. package/dist/react/filters/state.mjs +132 -0
  280. package/dist/react/filters/suggest.d.mts +47 -0
  281. package/dist/react/filters/suggest.mjs +97 -0
  282. package/dist/react/filters/values.mjs +166 -0
  283. package/dist/react/filters/words.d.mts +81 -0
  284. package/dist/react/filters/words.mjs +73 -0
  285. package/dist/react/format.d.mts +3 -3
  286. package/dist/react/format.mjs +45 -16
  287. package/dist/react/index.d.mts +34 -5
  288. package/dist/react/index.mjs +15 -4
  289. package/dist/react/messages.d.mts +47 -2
  290. package/dist/react/place.mjs +7 -3
  291. package/dist/react/renderers/index.d.mts +10 -2
  292. package/dist/react/renderers/index.mjs +116 -14
  293. package/dist/react/renderers/list.d.mts +22 -0
  294. package/dist/react/renderers/list.mjs +118 -0
  295. package/dist/react/review-words.mjs +1 -1
  296. package/dist/react/review.mjs +2 -2
  297. package/dist/react/scope.mjs +2 -7
  298. package/dist/react/series.d.mts +54 -0
  299. package/dist/react/series.mjs +191 -0
  300. package/dist/react/theme.css +2734 -1296
  301. package/dist/react/theme.css.d.ts +1 -0
  302. package/dist/react/tile-store.mjs +15 -3
  303. package/dist/react/useBoard.d.mts +7 -0
  304. package/dist/react/useBoard.mjs +4 -1
  305. package/dist/react/useDashboard.d.mts +4 -0
  306. package/dist/react/useDashboard.mjs +8 -1
  307. package/dist/react/useTiles.mjs +50 -15
  308. package/dist/react/view-grid.mjs +16 -0
  309. package/dist/react/view.d.mts +9 -0
  310. package/dist/react/view.mjs +22 -10
  311. package/dist/reading.d.mts +2 -0
  312. package/dist/reading.mjs +9 -1
  313. package/dist/recharts/axes.mjs +28 -0
  314. package/dist/recharts/axis.mjs +84 -0
  315. package/dist/recharts/bar.d.mts +7 -0
  316. package/dist/recharts/bar.mjs +140 -0
  317. package/dist/recharts/charts.d.mts +4 -6
  318. package/dist/recharts/charts.mjs +2 -114
  319. package/dist/recharts/index.d.mts +5 -3
  320. package/dist/recharts/index.mjs +6 -3
  321. package/dist/recharts/line.d.mts +2 -0
  322. package/dist/recharts/line.mjs +111 -0
  323. package/dist/recharts/marks.mjs +112 -0
  324. package/dist/recharts/parts.mjs +88 -0
  325. package/dist/recharts/series.d.mts +2 -22
  326. package/dist/recharts/series.mjs +1 -64
  327. package/dist/recharts/table.mjs +71 -0
  328. package/dist/records-read.mjs +44 -0
  329. package/dist/registry/filter-bar.json +26 -0
  330. package/dist/skills/derive-dashboards/SKILL.md +69 -0
  331. package/dist/store/apply.mjs +5 -5
  332. package/dist/store/commands.mjs +4 -4
  333. package/dist/store/discard.mjs +7 -7
  334. package/dist/store/drafts.mjs +5 -5
  335. package/dist/store/editor.mjs +1 -1
  336. package/dist/store/publish.mjs +12 -12
  337. package/dist/store/reader.mjs +41 -22
  338. package/dist/store/review.mjs +16 -16
  339. package/dist/store/rows.mjs +20 -20
  340. package/dist/store/undo.mjs +3 -3
  341. package/dist/store/unpublished.mjs +4 -4
  342. package/dist/store/versions.mjs +14 -14
  343. package/dist/testing/adapter-suite.mjs +4 -4
  344. package/dist/tiles.mjs +37 -3
  345. package/package.json +12 -10
  346. package/dist/react/FilterBar.mjs +0 -119
  347. package/dist/react/Records.d.mts +0 -12
  348. package/dist/react/Records.mjs +0 -97
  349. package/src/adapter/sql-adapter.ts +0 -95
  350. package/src/adapter/statement.ts +0 -185
  351. package/src/authorize.ts +0 -73
  352. package/src/body.ts +0 -169
  353. package/src/bounded.ts +0 -43
  354. package/src/cache.ts +0 -192
  355. package/src/checks.ts +0 -170
  356. package/src/custom-source.ts +0 -59
  357. package/src/dashboard-id.ts +0 -36
  358. package/src/dashboards.ts +0 -132
  359. package/src/editing.ts +0 -148
  360. package/src/endpoints/access.ts +0 -41
  361. package/src/endpoints/dashboards.ts +0 -92
  362. package/src/endpoints/drafts.ts +0 -138
  363. package/src/endpoints/index.ts +0 -21
  364. package/src/endpoints/meta.ts +0 -137
  365. package/src/endpoints/query.ts +0 -86
  366. package/src/endpoints/read.ts +0 -92
  367. package/src/endpoints/review.ts +0 -89
  368. package/src/endpoints/values.ts +0 -62
  369. package/src/endpoints/versions.ts +0 -104
  370. package/src/execute.ts +0 -207
  371. package/src/handler.ts +0 -232
  372. package/src/http.ts +0 -66
  373. package/src/index.ts +0 -163
  374. package/src/introspect.ts +0 -205
  375. package/src/mcp/api.ts +0 -59
  376. package/src/mcp/format.ts +0 -67
  377. package/src/mcp/index.ts +0 -151
  378. package/src/mcp/query.ts +0 -153
  379. package/src/mcp/resolve.ts +0 -114
  380. package/src/node-postgres.ts +0 -81
  381. package/src/owner.ts +0 -36
  382. package/src/postgres-connection.ts +0 -143
  383. package/src/postgres-store.ts +0 -82
  384. package/src/principal.ts +0 -39
  385. package/src/reading.ts +0 -56
  386. package/src/request-rows.ts +0 -23
  387. package/src/request.ts +0 -22
  388. package/src/storage.ts +0 -63
  389. package/src/store/apply.ts +0 -129
  390. package/src/store/commands.ts +0 -75
  391. package/src/store/conflicts.ts +0 -98
  392. package/src/store/create-tables.ts +0 -104
  393. package/src/store/discard.ts +0 -42
  394. package/src/store/drafts.ts +0 -133
  395. package/src/store/editor.ts +0 -28
  396. package/src/store/log.ts +0 -28
  397. package/src/store/publish.ts +0 -199
  398. package/src/store/publisher.ts +0 -21
  399. package/src/store/reader.ts +0 -139
  400. package/src/store/review.ts +0 -239
  401. package/src/store/rows.ts +0 -250
  402. package/src/store/tenant.ts +0 -28
  403. package/src/store/undo.ts +0 -56
  404. package/src/store/unpublished.ts +0 -33
  405. package/src/store/versions.ts +0 -106
  406. package/src/store/writer.ts +0 -19
  407. package/src/testing/adapter-suite.ts +0 -239
  408. package/src/testing/index.ts +0 -2
  409. package/src/tile-doc.ts +0 -46
  410. package/src/tiles.ts +0 -120
@@ -0,0 +1,144 @@
1
+ import { isMark } from "../../result/marks.mjs";
2
+ import { nextBucketStart } from "../../time/bucket.mjs";
3
+ import { formatTextTime, parseDate, parseTextTime } from "../../time/instant.mjs";
4
+ import { startOfDate } from "../../time/zone.mjs";
5
+ // src/query/drill/selection.ts
6
+ /** The instant a bucket's label starts at: a local date in the zone, or an hour's instant. */
7
+ function labelStart(label, grain, calendar) {
8
+ if (grain === "hour") return parseTextTime(label);
9
+ const date = parseDate(label);
10
+ return date === void 0 ? void 0 : startOfDate(date, calendar.timezone);
11
+ }
12
+ /** A time point's step: its bucket's exact window from the engine's calendar, cut to the range. */
13
+ function bucketStep(drawn, column, cell) {
14
+ if (column.member === void 0 || column.grain === void 0 || typeof cell !== "string") return void 0;
15
+ const start = labelStart(cell, column.grain, drawn.calendar);
16
+ if (start === void 0) return void 0;
17
+ const end = nextBucketStart(start, column.grain, drawn.calendar);
18
+ const from = Math.max(start, drawn.span?.from ?? start);
19
+ const to = Math.min(end, drawn.span?.to ?? end);
20
+ return { bucket: {
21
+ member: column.member,
22
+ from: formatTextTime(from),
23
+ to: formatTextTime(to),
24
+ grain: column.grain
25
+ } };
26
+ }
27
+ /** The group values and Other marks of a row's dimension cells; undefined when one cannot drill. */
28
+ function groupParts(columns, row) {
29
+ const parts = {
30
+ group: [],
31
+ others: []
32
+ };
33
+ for (const [i, column] of columns.entries()) {
34
+ const cell = row[i] ?? null;
35
+ if (!column.id.startsWith("d")) continue;
36
+ if (column.member === void 0) return void 0;
37
+ if (isMark(cell)) {
38
+ if (cell.mark === "other") parts.others.push(i);
39
+ } else if (typeof cell === "object" && cell !== null) return void 0;
40
+ else parts.group.push({
41
+ member: column.member,
42
+ value: cell
43
+ });
44
+ }
45
+ return parts;
46
+ }
47
+ /** A row's plain values at some columns; undefined when one is a mark or an exact number. */
48
+ function plainAt(row, at) {
49
+ const cells = at.map((i) => row[i] ?? null);
50
+ return cells.every((c) => typeof c !== "object" || c === null) ? cells : void 0;
51
+ }
52
+ /** The longest a kept set may be as text, so a sealed selection stays inside the path's cap. */
53
+ const MAX_KEPT_TEXT = 4e3;
54
+ /** An Other step from its kept tuples; refused past `MAX_KEPT_GROUPS` or the text cap. */
55
+ function keptStep(members, kept) {
56
+ if (kept.length > 50) return { refused: "OTHER_TOO_LARGE" };
57
+ if (JSON.stringify(kept).length > MAX_KEPT_TEXT) return { refused: "OTHER_TOO_LARGE" };
58
+ return { steps: [{ other: {
59
+ members,
60
+ kept
61
+ } }] };
62
+ }
63
+ /** A dimension's own limit's Other: the rows outside every value the engine kept for it. */
64
+ function segmentStep(drawn, at) {
65
+ const column = drawn.columns[at];
66
+ const index = drawn.columns.slice(0, at).filter((c) => c.id.startsWith("d")).length;
67
+ const kept = drawn.cuts?.segments.find(([i]) => i === index)?.[1];
68
+ if (column?.member === void 0 || kept === void 0) return { refused: "OTHER_CUT" };
69
+ const values = kept.flatMap((v) => typeof v === "object" && v !== null ? [] : [[v]]);
70
+ if (values.length !== kept.length) return { refused: "OTHER_CUT" };
71
+ return keptStep([column.member], values);
72
+ }
73
+ /**
74
+ * The query's own limit's Other: the rows in none of the groups it kept. Its rows are every
75
+ * group it kept, so the kept set is read from them, but only when that is the whole set: not
76
+ * past the row cap, and not when a kept group is itself a dimension's Other (two cuts).
77
+ */
78
+ function queryOtherStep(drawn, at) {
79
+ if (drawn.truncated === true) return { refused: "OTHER_CUT" };
80
+ const kept = /* @__PURE__ */ new Map();
81
+ for (const row of drawn.rows) {
82
+ if (at.every((i) => isMark(row[i] ?? null))) continue;
83
+ const values = plainAt(row, at);
84
+ if (values === void 0) return { refused: "OTHER_CUT" };
85
+ kept.set(JSON.stringify(values), values);
86
+ }
87
+ const members = at.map((i) => drawn.columns[i]?.member);
88
+ if (members.some((m) => m === void 0)) return { refused: "OTHER_CUT" };
89
+ return keptStep(members.filter((m) => m !== void 0), [...kept.values()]);
90
+ }
91
+ /**
92
+ * An Other row's steps: the query limit's Other when every dimension is Other and the query has
93
+ * a limit of its own, else each dimension's own Other; under a search, the search too, so the
94
+ * Other of a searched Breakdown stays the searched rows. Refused when a kept set is not whole.
95
+ */
96
+ function otherSteps(drawn, others, dims) {
97
+ if (drawn.truncated === true) return { refused: "OTHER_CUT" };
98
+ const search = drawn.search === void 0 ? [] : [{ search: drawn.search }];
99
+ const parts = drawn.limited === true && others.length === dims ? [queryOtherStep(drawn, others)] : others.map((i) => segmentStep(drawn, i));
100
+ const refused = parts.find((p) => "refused" in p);
101
+ if (refused !== void 0) return refused;
102
+ return { steps: [...search, ...parts.flatMap((p) => "steps" in p ? p.steps : [])] };
103
+ }
104
+ /** The row's own value of each line, by column id: what was clicked. */
105
+ function clickedOf(columns, row) {
106
+ const clicked = {};
107
+ for (const [i, column] of columns.entries()) {
108
+ const cell = row[i] ?? null;
109
+ if ((column.id.startsWith("m") || column.id.startsWith("c")) && !isMark(cell)) clicked[column.id] = cell;
110
+ }
111
+ return clicked;
112
+ }
113
+ /**
114
+ * What a click on one row of an answer selects: its time bucket's exact window, the values of
115
+ * the fields it groups by (a total's folded fields left out, so a total selects its row, its
116
+ * column or the whole tile), and Other with the exact groups its limit kept. Refused, with the
117
+ * reason, for a row that cannot drill: a field the answer does not name, or an Other whose
118
+ * kept set is too large or not whole.
119
+ */
120
+ function rowSelection(drawn, row) {
121
+ const time = timeSteps(drawn, row);
122
+ const parts = groupParts(drawn.columns, row);
123
+ if (time === void 0 || parts === void 0) return { refused: "OTHER_CUT" };
124
+ const dims = drawn.columns.filter((c) => c.id.startsWith("d")).length;
125
+ const other = parts.others.length === 0 ? { steps: [] } : otherSteps(drawn, parts.others, dims);
126
+ if ("refused" in other) return other;
127
+ return { selection: {
128
+ steps: [
129
+ ...time,
130
+ ...parts.group.length === 0 ? [] : [{ group: parts.group }],
131
+ ...other.steps
132
+ ],
133
+ clicked: clickedOf(drawn.columns, row)
134
+ } };
135
+ }
136
+ /** A row's time step, if the answer has a bucket column; undefined when its label does not read. */
137
+ function timeSteps(drawn, row) {
138
+ const at = drawn.columns.findIndex((c) => c.id === "t");
139
+ const bucket = drawn.columns[at];
140
+ if (bucket === void 0) return [];
141
+ const step = bucketStep(drawn, bucket, row[at] ?? null);
142
+ return step === void 0 ? void 0 : [step];
143
+ }
144
+ export { rowSelection };
@@ -0,0 +1,14 @@
1
+ import { DrillStep, Selection } from "./types.mjs";
2
+ /** The fields a step names. */
3
+ export declare function stepMembers(step: DrillStep): readonly string[];
4
+ /** The fields a path fixes to one value, and those it cuts an Other on (Breakdown offers those again). */
5
+ export declare function pathFields(steps: readonly DrillStep[]): {
6
+ fixed: string[];
7
+ other: string[];
8
+ };
9
+ /**
10
+ * Which selections of a path stay, by index: a click on a field the path already fixes replaces
11
+ * that step and drops the ones after it, so a path never fixes a field twice. Under Other the
12
+ * same field may be clicked again: it narrows to one of the values Other left out.
13
+ */
14
+ export declare function keptPath(path: readonly Selection[]): number[];
@@ -0,0 +1,35 @@
1
+ // src/query/drill/steps.ts
2
+ /** The fields a step names. */
3
+ function stepMembers(step) {
4
+ if ("group" in step) return step.group.map((g) => g.member);
5
+ if ("search" in step) return [step.search.member];
6
+ return "other" in step ? step.other.members : [step.bucket.member];
7
+ }
8
+ /** The fields a selection fixes: its group values and time window, not its Other or search. */
9
+ const fixed = (selection) => new Set(selection.steps.filter((s) => !("other" in s) && !("search" in s)).flatMap((s) => stepMembers(s)));
10
+ /** The fields a path fixes to one value, and those it cuts an Other on (Breakdown offers those again). */
11
+ function pathFields(steps) {
12
+ return {
13
+ fixed: steps.flatMap((s) => "group" in s || "bucket" in s ? stepMembers(s) : []),
14
+ other: steps.flatMap((s) => "other" in s ? s.other.members : [])
15
+ };
16
+ }
17
+ /**
18
+ * Which selections of a path stay, by index: a click on a field the path already fixes replaces
19
+ * that step and drops the ones after it, so a path never fixes a field twice. Under Other the
20
+ * same field may be clicked again: it narrows to one of the values Other left out.
21
+ */
22
+ function keptPath(path) {
23
+ const kept = [];
24
+ for (const [i, selection] of path.entries()) {
25
+ const fields = fixed(selection);
26
+ const clash = kept.findIndex((k) => {
27
+ const earlier = path[k];
28
+ return earlier !== void 0 && [...fixed(earlier)].some((m) => fields.has(m));
29
+ });
30
+ if (clash >= 0) kept.splice(clash);
31
+ kept.push(i);
32
+ }
33
+ return kept;
34
+ }
35
+ export { keptPath, pathFields, stepMembers };
@@ -0,0 +1,85 @@
1
+ import { parseMemberId } from "../../model/member.mjs";
2
+ import { fold } from "../../value/casefold.mjs";
3
+ import { operandColumn } from "./operand.mjs";
4
+ // src/query/drill/synthetic-expr.ts
5
+ /**
6
+ * An own dimension's leaf as a condition on its column. A field that compares without case
7
+ * compares folded, its column and its values both, as a filter on it does.
8
+ */
9
+ function leafCondition(def, source, f) {
10
+ if (!("member" in f) || !("values" in f) || !("op" in f)) return void 0;
11
+ const dim = ownColumn(def, source, f.member);
12
+ if (dim === void 0) return void 0;
13
+ if (dim.kind !== "string" || dim.collation !== "ci") return {
14
+ left: { col: dim.column },
15
+ op: f.op,
16
+ values: f.values
17
+ };
18
+ const values = f.values.map((v) => typeof v === "string" ? fold(v) : v);
19
+ return {
20
+ left: {
21
+ fn: "lower_fold",
22
+ args: [{ col: dim.column }]
23
+ },
24
+ op: f.op,
25
+ values
26
+ };
27
+ }
28
+ /** A member's own dimension when it reads a column of the source, not through a join or a clock. */
29
+ function ownColumn(def, source, member) {
30
+ const ref = parseMemberId(member);
31
+ const dim = ref?.source === source && ref.join === void 0 ? def.dimensions[ref.name] : void 0;
32
+ return dim !== void 0 && "column" in dim && dim.kind !== "time" ? dim : void 0;
33
+ }
34
+ const TRUE = { lit: true };
35
+ const FALSE = { lit: false };
36
+ /** `case when <test> then <yes> else <no> end`. */
37
+ const branch = (when, yes, no) => ({
38
+ case: [{
39
+ when,
40
+ then: yes
41
+ }],
42
+ else: no
43
+ });
44
+ /**
45
+ * A filter as a true-or-false expression, when it is leaves on own fields joined by all, any and
46
+ * not; undefined for anything else (a join, `some`, a viewer value).
47
+ */
48
+ function filterExpr(def, source, f) {
49
+ if ("not" in f) {
50
+ const inner = filterExpr(def, source, f.not);
51
+ return inner === void 0 ? void 0 : branch(isTrue(inner), FALSE, TRUE);
52
+ }
53
+ if ("all" in f) return joined(def, source, f.all, true);
54
+ if ("any" in f) return joined(def, source, f.any, false);
55
+ const condition = leafCondition(def, source, f);
56
+ return condition === void 0 ? void 0 : branch(condition, TRUE, FALSE);
57
+ }
58
+ const isTrue = (left) => ({
59
+ left,
60
+ op: "equals",
61
+ values: [true]
62
+ });
63
+ /** Filters joined by all (`all`) or any, as one expression; undefined when one cannot be. */
64
+ function joined(def, source, list, all) {
65
+ const parts = list.flatMap((p) => filterExpr(def, source, p) ?? []);
66
+ if (parts.length !== list.length) return;
67
+ return parts.reduceRight((rest, part) => branch(isTrue(part), all ? rest : TRUE, all ? FALSE : rest), all ? TRUE : FALSE);
68
+ }
69
+ /** A ratio's numerator's `where`, as a true-or-false expression, when it has one that can be. */
70
+ function numeratorExpr(def, source, numerator) {
71
+ const top = def.measures[numerator];
72
+ if (top === void 0) return void 0;
73
+ const where = "where" in top ? top.where : void 0;
74
+ const holds = where === void 0 ? TRUE : filterExpr(def, source, where);
75
+ if (holds === void 0) return void 0;
76
+ const operand = operandColumn(top);
77
+ if (operand === void 0) return holds;
78
+ const set = branch({
79
+ left: { col: operand },
80
+ op: "set",
81
+ values: []
82
+ }, TRUE, FALSE);
83
+ return branch(isTrue(holds), set, FALSE);
84
+ }
85
+ export { FALSE, TRUE, branch, numeratorExpr };
@@ -0,0 +1,22 @@
1
+ import { Model, Numeric } from "../../model/types.mjs";
2
+ import { Cell } from "../types.mjs";
3
+ /** The row count every source has in a drill. */
4
+ export declare const ROW_COUNT = "dd_rows";
5
+ /** A one-column key as a field, hidden: a drill looks a linked row up by it. */
6
+ export declare const KEY_FIELD = "dd_key";
7
+ /** The model a drill reads: each source with its drill's own fields (`dd_...`). */
8
+ export declare function drillModel(model: Model): Model;
9
+ /**
10
+ * A measure's own column, read as text by a drill's field, as the measure's values read: an
11
+ * exact number at its scale (minor units moved to it, decimal text padded to it), a float as a
12
+ * number. Text that is not an exact value of the column stays as it came.
13
+ */
14
+ export declare function storedCell(numeric: Numeric | undefined, value: Cell): Cell;
15
+ /** A true-or-false field as read back: a boolean, whatever text the store wrote it as. */
16
+ export declare function flagCell(cell: Cell): Cell;
17
+ /**
18
+ * The member of the host's model a drill's own field stands for, so an access check asks the
19
+ * host about a member it knows: a measure's field is that measure, a bucket its number field,
20
+ * the row count none (the source's own check covers it). Any other member is itself.
21
+ */
22
+ export declare function drillOwner(id: string): string | undefined;
@@ -0,0 +1,165 @@
1
+ import { formatExact, storedExact } from "../../exact/decimal.mjs";
2
+ import { parseMemberId } from "../../model/member.mjs";
3
+ import { memberLabel } from "../../result/columns.mjs";
4
+ import { operandColumn } from "./operand.mjs";
5
+ import { FALSE, TRUE, branch, numeratorExpr } from "./synthetic-expr.mjs";
6
+ // src/query/drill/synthetic.ts
7
+ /** The row count every source has in a drill. */
8
+ const ROW_COUNT = "dd_rows";
9
+ const valueName = (measure) => `dd_value_${measure}`;
10
+ const distinctName = (measure) => `dd_distinct_${measure}`;
11
+ /** A one-column key as a field, hidden: a drill looks a linked row up by it. */
12
+ const KEY_FIELD = "dd_key";
13
+ const flagName = (measure) => `dd_flag_${measure}`;
14
+ const setName = (measure) => `dd_set_${measure}`;
15
+ /** A sum's least row value: a share of it is only a share when no row is negative. */
16
+ const minName = (measure) => `dd_min_${measure}`;
17
+ const bucketName = (dimension) => `dd_bucket_${dimension}`;
18
+ /** A column no sensitive or hidden field reads, so showing it row by row shows nothing kept back. */
19
+ function openColumn(def, column) {
20
+ return !Object.values(def.dimensions).some((d) => "column" in d && d.column === column && (d.sensitive === true || d.hidden === true));
21
+ }
22
+ /** A column's values as text, exactly as stored: a drill's own field, never offered. */
23
+ const textField = (column) => ({
24
+ kind: "string",
25
+ column,
26
+ collation: "binary",
27
+ hidden: true
28
+ });
29
+ /** A source's one-column key as a hidden text field, so a linked row can be looked up by its key. */
30
+ const keyField = (def) => {
31
+ const [column, ...more] = def.key;
32
+ return column === void 0 || more.length > 0 ? [] : [[KEY_FIELD, textField(column)]];
33
+ };
34
+ /** The labels of a number's buckets from its edges: "< 10", "10 to 100", "100 or more". */
35
+ const bucketLabels = (edges) => [
36
+ `< ${String(edges[0])}`,
37
+ ...edges.slice(1).map((e, i) => `${String(edges[i])} to ${String(e)}`),
38
+ `${String(edges.at(-1))} or more`
39
+ ];
40
+ /** "This column is set", true or false: a predicate, never a value, so it may read any column. */
41
+ const setField = (column) => ({
42
+ kind: "expr",
43
+ expr: branch({
44
+ left: { col: column },
45
+ op: "set",
46
+ values: []
47
+ }, TRUE, FALSE),
48
+ type: "boolean",
49
+ hidden: true
50
+ });
51
+ /**
52
+ * The fields one measure adds: whether its column is set (for its population, whoever asks),
53
+ * its column per row and its distinct values (only when no kept-back field reads the column),
54
+ * and its numerator flag.
55
+ */
56
+ function measureFields(def, source, name, m) {
57
+ const operand = operandColumn(m);
58
+ const of = "of" in m && typeof m.of === "string" ? m.of : void 0;
59
+ const distinct = m.kind === "count" ? m.distinct : void 0;
60
+ const expr = m.kind === "ratio" ? numeratorExpr(def, source, m.numerator) : void 0;
61
+ const field = (on, entry) => on ? [entry] : [];
62
+ return [
63
+ ...operand === void 0 ? [] : [[setName(name), setField(operand)]],
64
+ ...of === void 0 ? [] : field(openColumn(def, of), [valueName(name), textField(of)]),
65
+ ...distinct === void 0 ? [] : field(openColumn(def, distinct), [distinctName(name), textField(distinct)]),
66
+ ...expr === void 0 ? [] : [[flagName(name), {
67
+ kind: "expr",
68
+ expr,
69
+ type: "boolean",
70
+ hidden: true
71
+ }]]
72
+ ];
73
+ }
74
+ /** A number's declared buckets as a bucket field, offered in Breakdown in its place. */
75
+ function bucketFields(def) {
76
+ return Object.entries(def.dimensions).flatMap(([name, d]) => d.kind === "number" && d.buckets !== void 0 && d.buckets.length > 0 && d.sensitive !== true && d.hidden !== true && d.drill !== false ? [[bucketName(name), {
77
+ kind: "bucket",
78
+ of: d.column,
79
+ edges: d.buckets,
80
+ labels: bucketLabels(d.buckets),
81
+ label: memberLabel(name, d.label)
82
+ }]] : []);
83
+ }
84
+ /** One source with a drill's own fields; a name the source already has is left as it is. */
85
+ function drillSource(source, def) {
86
+ const fields = [
87
+ ...Object.entries(def.measures).flatMap(([n, m]) => measureFields(def, source, n, m)),
88
+ ...bucketFields(def),
89
+ ...keyField(def)
90
+ ].filter(([n]) => def.dimensions[n] === void 0);
91
+ const rows = def.measures["dd_rows"] === void 0 ? { [ROW_COUNT]: {
92
+ kind: "count",
93
+ merge: "sum"
94
+ } } : {};
95
+ const least = Object.entries(def.measures).flatMap(([n, m]) => m.kind === "sum" && typeof m.of === "string" && def.measures[minName(n)] === void 0 ? [[minName(n), {
96
+ kind: "min",
97
+ of: m.of,
98
+ numeric: m.numeric,
99
+ merge: "min",
100
+ hidden: true
101
+ }]] : []);
102
+ return {
103
+ ...def,
104
+ measures: {
105
+ ...def.measures,
106
+ ...rows,
107
+ ...Object.fromEntries(least)
108
+ },
109
+ dimensions: {
110
+ ...def.dimensions,
111
+ ...Object.fromEntries(fields)
112
+ }
113
+ };
114
+ }
115
+ /** The model a drill reads: each source with its drill's own fields (`dd_...`). */
116
+ function drillModel(model) {
117
+ return {
118
+ ...model,
119
+ sources: Object.fromEntries(Object.entries(model.sources).map(([n, s]) => [n, drillSource(n, s)]))
120
+ };
121
+ }
122
+ /**
123
+ * A measure's own column, read as text by a drill's field, as the measure's values read: an
124
+ * exact number at its scale (minor units moved to it, decimal text padded to it), a float as a
125
+ * number. Text that is not an exact value of the column stays as it came.
126
+ */
127
+ function storedCell(numeric, value) {
128
+ if (typeof value === "object") return value;
129
+ const cell = typeof value === "number" && Number.isSafeInteger(value) ? String(value) : value;
130
+ if (numeric === void 0 || numeric === "float") return typeof cell === "string" && cell !== "" && Number.isFinite(Number(cell)) ? Number(cell) : cell;
131
+ const exact = storedExact(cell, numeric.stored, numeric.scale);
132
+ return exact === null ? cell : {
133
+ t: "decimal",
134
+ v: formatExact(exact, numeric.scale)
135
+ };
136
+ }
137
+ /** A true-or-false field as read back: a boolean, whatever text the store wrote it as. */
138
+ function flagCell(cell) {
139
+ if (cell === "true" || cell === "t") return true;
140
+ return cell === "false" || cell === "f" ? false : cell;
141
+ }
142
+ /** A drill's own field's prefix, and whether the rest of its name is a measure's or a dimension's. */
143
+ const OWN = Object.freeze([
144
+ ["dd_value_", "measure"],
145
+ ["dd_distinct_", "measure"],
146
+ ["dd_flag_", "measure"],
147
+ ["dd_set_", "measure"],
148
+ ["dd_min_", "measure"],
149
+ ["dd_bucket_", "dimension"]
150
+ ]);
151
+ /**
152
+ * The member of the host's model a drill's own field stands for, so an access check asks the
153
+ * host about a member it knows: a measure's field is that measure, a bucket its number field,
154
+ * the row count none (the source's own check covers it). Any other member is itself.
155
+ */
156
+ function drillOwner(id) {
157
+ const ref = parseMemberId(id);
158
+ if (ref === void 0 || !ref.name.startsWith("dd_")) return id;
159
+ if (ref.name === "dd_rows") return void 0;
160
+ const own = OWN.find(([prefix]) => ref.name.startsWith(prefix));
161
+ if (own === void 0) return id;
162
+ const name = ref.name.slice(own[0].length);
163
+ return ref.join === void 0 ? `${ref.source}.${name}` : `${ref.source}.${ref.join}.${name}`;
164
+ }
165
+ export { KEY_FIELD, ROW_COUNT, drillModel, drillOwner, flagCell, flagName, minName, setName, storedCell, valueName };
@@ -0,0 +1,11 @@
1
+ import { Model } from "../../model/types.mjs";
2
+ import { Moment } from "../../time/temporal.mjs";
3
+ import { Calendar, Grain, WireQuery } from "../types.mjs";
4
+ /** When a request runs, as the engine resolves ranges: its `now`, zone and quantum. */
5
+ export type DrillClock = {
6
+ now: Moment;
7
+ calendar: Calendar;
8
+ quantumSeconds: number;
9
+ };
10
+ /** The rows behind a drill over time: its range (the last 12 months when the tile has none), by a grain. */
11
+ export declare function trendQuery(model: Model, set: WireQuery, clock: DrillClock, grain?: Grain): WireQuery;
@@ -0,0 +1,71 @@
1
+ import { EngineError } from "../../error/refusal.mjs";
2
+ import { resolveDimension } from "../../model/member.mjs";
3
+ import { MICROS_PER_DAY, formatTextTime } from "../../time/instant.mjs";
4
+ import { resolveRange } from "../../time/range.mjs";
5
+ import { momentMicros } from "../../time/temporal.mjs";
6
+ import { trendDimension } from "./fields.mjs";
7
+ // src/query/drill/time.ts
8
+ const refuse = (message) => {
9
+ throw new EngineError("SELECTION_INVALID", message);
10
+ };
11
+ /** A window's instants; refused unless both ends read and `from` is before `to`. */
12
+ function windowSpan(step, calendar) {
13
+ const span = resolveRange(step, 0, calendar);
14
+ if (span === void 0 || span.from >= span.to) return refuse("A time step has no span.");
15
+ return span;
16
+ }
17
+ /**
18
+ * The tile's range, narrowed to every time step's window: never wider than either. Without a
19
+ * time step it is the tile's own range, with no grain and no comparison.
20
+ */
21
+ function drilledTime(model, query, steps, clock) {
22
+ const buckets = steps.flatMap((s) => "bucket" in s ? [s.bucket] : []);
23
+ const own = query.time;
24
+ if (buckets.length === 0) return own === void 0 ? void 0 : {
25
+ dimension: own.dimension,
26
+ range: own.range
27
+ };
28
+ const dimension = trendDimension(model, query) ?? refuse("This source has no time field.");
29
+ if (buckets.some((b) => b.member !== dimension)) refuse(`A time step is on "${dimension}".`);
30
+ const quantum = clock.quantumSeconds * 1e6 || 1;
31
+ const spans = [...own === void 0 ? [] : [resolveRange(own.range, momentMicros(clock.now), clock.calendar, quantum)], ...buckets.map((b) => windowSpan(b, clock.calendar))];
32
+ const from = Math.max(...spans.map((s) => s?.from ?? -Infinity));
33
+ const to = Math.max(from, Math.min(...spans.map((s) => s?.to ?? Infinity)));
34
+ return {
35
+ dimension,
36
+ range: {
37
+ from: formatTextTime(from),
38
+ to: formatTextTime(to)
39
+ }
40
+ };
41
+ }
42
+ /** A grain that draws a range in a readable number of points. */
43
+ function fitGrain(days, dateOnly) {
44
+ if (days <= 2 && !dateOnly) return "hour";
45
+ if (days <= 62) return "day";
46
+ if (days <= 210) return "week";
47
+ return days <= 1100 ? "month" : "quarter";
48
+ }
49
+ /** The rows behind a drill over time: its range (the last 12 months when the tile has none), by a grain. */
50
+ function trendQuery(model, set, clock, grain) {
51
+ const dimension = trendDimension(model, set) ?? refuse("This source has no time field to draw over time.");
52
+ const range = set.time?.range ?? {
53
+ last: 12,
54
+ unit: "month",
55
+ includeCurrent: true
56
+ };
57
+ const span = resolveRange(range, momentMicros(clock.now), clock.calendar);
58
+ const days = span === void 0 ? 0 : (span.to - span.from) / MICROS_PER_DAY;
59
+ const def = resolveDimension(model, set.source, dimension)?.dimension;
60
+ const dateOnly = def?.kind === "time" && def.storage === "date";
61
+ return {
62
+ ...set,
63
+ time: {
64
+ dimension,
65
+ range,
66
+ grain: grain ?? fitGrain(days, dateOnly)
67
+ },
68
+ fill: "zero"
69
+ };
70
+ }
71
+ export { drilledTime, trendQuery };