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,170 @@
1
+ import { base64url, seal, unseal } from "./cursor-seal.mjs";
2
+ import { fingerprint, pathDigest, revisionOf } from "./drill-identity.mjs";
3
+ import { warnSecret } from "./drill-secret.mjs";
4
+ import { DRILL_REASONS, parseTextTime } from "derive-dashboards/core";
5
+ import { EngineError, rowSelection } from "derive-dashboards/core/engine";
6
+ import { pinnedSchema, selectionSchema } from "derive-dashboards/core/schema";
7
+ // src/drill-seal.ts
8
+ /** An answer drills only when it is small enough to read row by row. */
9
+ const MAX_SELECTIONS = 1e3;
10
+ const bindingOf = (scope, purpose) => ({
11
+ purpose,
12
+ principal: scope.principal,
13
+ on: scope.origin
14
+ });
15
+ /** The instants of a resolved range, to cut a time point's window to it. */
16
+ function spanOf(range) {
17
+ const from = range === void 0 ? void 0 : parseTextTime(range.from);
18
+ const to = range === void 0 ? void 0 : parseTextTime(range.to);
19
+ return from === void 0 || to === void 0 ? {} : { span: {
20
+ from,
21
+ to
22
+ } };
23
+ }
24
+ /** A column as a selection reads it: its id, member and grain only. */
25
+ const slim = (c) => ({
26
+ id: c.id,
27
+ label: "",
28
+ type: c.type,
29
+ ...c.member === void 0 ? {} : { member: c.member },
30
+ ...c.grain === void 0 ? {} : { grain: c.grain }
31
+ });
32
+ /** What a drawn answer's rows open to, and why the others do not. */
33
+ function closedRows(drawn) {
34
+ return drawn.rows.flatMap((row, i) => {
35
+ if (i >= MAX_SELECTIONS) return [];
36
+ const picked = rowSelection(drawn, row);
37
+ return "refused" in picked ? [{
38
+ row: i,
39
+ code: picked.refused,
40
+ reason: DRILL_REASONS[picked.refused]
41
+ }] : [];
42
+ });
43
+ }
44
+ /** One answer sealed once, under an execution and a path, with the rows that do not open. */
45
+ async function sealAnswer(scope, drawn, at) {
46
+ const body = {
47
+ drawn: {
48
+ ...drawn,
49
+ columns: drawn.columns.map((c) => slim(c)),
50
+ rows: drawn.rows.slice(0, MAX_SELECTIONS)
51
+ },
52
+ ...at
53
+ };
54
+ const answer = await seal(await scope.rt.cursorKey, JSON.stringify(body), bindingOf(scope, "answer"));
55
+ const closed = closedRows(drawn);
56
+ return {
57
+ answer,
58
+ ...closed.length === 0 ? {} : { closed }
59
+ };
60
+ }
61
+ /** An answer as drawn, for a selection per row. */
62
+ function drawnOf(answer, rows, range, extra) {
63
+ return {
64
+ columns: answer.columns,
65
+ rows,
66
+ calendar: answer.receipt.calendar,
67
+ ...spanOf(range),
68
+ ...answer.cuts === void 0 ? {} : { cuts: answer.cuts },
69
+ ...answer.status === "truncated" ? { truncated: true } : {},
70
+ ...extra
71
+ };
72
+ }
73
+ /** Warns once that drill links, executions and cursors break across instances and restarts without a `secret`. */
74
+ const randomId = () => base64url(crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(12)));
75
+ /** Whether an answer drills: drilling is on, a person asks, and its source is not kept out. */
76
+ const drillsHere = (scope, query) => scope.rt.options.drill !== false && scope.principal.channel === "user" && scope.rt.options.model.sources[query.source]?.drill !== false;
77
+ /** What the drill pins when the answer is drawn: its range, instant, calendar, version and who drew it. */
78
+ async function pinnedFor(scope, answer, at) {
79
+ const { compare } = answer;
80
+ return {
81
+ id: randomId(),
82
+ by: await fingerprint(scope.rt, scope.principal),
83
+ revision: await revisionOf(at.query),
84
+ ...answer.receipt.range === void 0 ? {} : { range: answer.receipt.range },
85
+ ...compare === void 0 ? {} : { compare: compare.range },
86
+ calendar: answer.receipt.calendar,
87
+ ...at.version === void 0 ? {} : { version: at.version },
88
+ ...at.now === void 0 ? {} : { now: at.now.toISOString() }
89
+ };
90
+ }
91
+ /**
92
+ * What a client drills an answer with: its execution (range, comparison range, calendar,
93
+ * version, who drew it) and the answer sealed once (and the comparison
94
+ * period's). None for a viewer who is not signed in: a public link never drills.
95
+ */
96
+ async function issueDrill(scope, answer, at) {
97
+ if (!drillsHere(scope, at.query)) return void 0;
98
+ warnSecret(scope.rt);
99
+ const { compare } = answer;
100
+ const pinned = await pinnedFor(scope, answer, at);
101
+ const root = await pathDigest([]);
102
+ const own = { limited: at.query.limit !== void 0 && at.query.other === true };
103
+ const [execution, sealed, previous] = await Promise.all([
104
+ seal(await scope.rt.cursorKey, JSON.stringify(pinned), bindingOf(scope, "execution")),
105
+ sealAnswer(scope, drawnOf(answer, answer.rows, answer.receipt.range, own), {
106
+ exec: pinned.id,
107
+ parent: root
108
+ }),
109
+ compare === void 0 ? void 0 : sealAnswer(scope, drawnOf(answer, compare.rows, compare.range, own), {
110
+ exec: pinned.id,
111
+ parent: root,
112
+ previous: true
113
+ })
114
+ ]);
115
+ const cut = answer.rows.length > MAX_SELECTIONS;
116
+ return {
117
+ execution,
118
+ ...sealed,
119
+ ...previous === void 0 ? {} : { previous },
120
+ ...cut ? {
121
+ code: "ROW_CAP",
122
+ reason: DRILL_REASONS.ROW_CAP
123
+ } : {}
124
+ };
125
+ }
126
+ const invalid = () => new EngineError("SELECTION_INVALID", "This link's drill is out of date: open it again from the chart.");
127
+ /** A sealed text opened; refused when it does not open. */
128
+ async function opened(scope, sealed, purpose) {
129
+ const text = await unseal(await scope.rt.cursorKey, sealed, bindingOf(scope, purpose));
130
+ if (text === void 0) throw invalid();
131
+ return JSON.parse(text);
132
+ }
133
+ async function openSelection(scope, sealed) {
134
+ const parsed = selectionSchema.safeParse(await opened(scope, sealed, "selection"));
135
+ if (!parsed.success) throw invalid();
136
+ return parsed.data;
137
+ }
138
+ async function openExecution(scope, sealed) {
139
+ const parsed = pinnedSchema.safeParse(await opened(scope, sealed, "execution"));
140
+ if (!parsed.success) throw invalid();
141
+ return parsed.data;
142
+ }
143
+ /** A selection sealed on its own, as a normalized path carries it. */
144
+ async function sealSelection(scope, selection) {
145
+ return seal(await scope.rt.cursorKey, JSON.stringify(selection), bindingOf(scope, "selection"));
146
+ }
147
+ /** Whether a path entry names a row of a sealed answer (`<answer>#<row>`) rather than a selection. */
148
+ const isRowRef = (entry) => entry.includes("#");
149
+ /**
150
+ * The selection of one row of a sealed answer: read from the answer as it was drawn, bound to
151
+ * its execution and the steps it was drawn under. Refused when the row does not open.
152
+ */
153
+ async function openRow(scope, entry) {
154
+ const [token = "", place = ""] = entry.split("#");
155
+ const previous = place.startsWith("p");
156
+ const row = Number(previous ? place.slice(1) : place);
157
+ const body = await opened(scope, token, "answer");
158
+ if (previous !== (body.previous === true)) throw invalid();
159
+ const cells = body.drawn.rows[row];
160
+ const picked = cells === void 0 ? void 0 : rowSelection(body.drawn, cells);
161
+ if (picked === void 0) throw invalid();
162
+ if ("refused" in picked) throw new EngineError("DRILL_UNSUPPORTED", DRILL_REASONS[picked.refused]);
163
+ return {
164
+ ...picked.selection,
165
+ exec: body.exec,
166
+ parent: body.parent,
167
+ ...previous ? { previous: true } : {}
168
+ };
169
+ }
170
+ export { drawnOf, isRowRef, issueDrill, openExecution, openRow, openSelection, sealAnswer, sealSelection };
@@ -0,0 +1,14 @@
1
+ // src/drill-secret.ts
2
+ /**
3
+ * In production, the missing `secret` is said when the server starts, not at the first drill:
4
+ * there, several instances or a restart would break every drill link and cursor.
5
+ */
6
+ function warnSecretAtStart(rt) {
7
+ if (globalThis.process?.env?.["NODE_ENV"] === "production" && rt.options.drill !== false) warnSecret(rt);
8
+ }
9
+ function warnSecret(rt) {
10
+ if (rt.options.secret !== void 0 || rt.warned.secret) return;
11
+ rt.warned.secret = true;
12
+ console.warn("derive-dashboards: no `secret` is set, so drill links and records cursors open only on this process until it restarts. Set one shared secret when more than one instance serves the app.");
13
+ }
14
+ export { warnSecret, warnSecretAtStart };
@@ -0,0 +1,53 @@
1
+ import { drillRead } from "./drill-access.mjs";
2
+ import { composedOn, lineValue } from "./drill-summary.mjs";
3
+ import { numberOf } from "./drill-totals.mjs";
4
+ import { resultColumns } from "derive-dashboards/core";
5
+ import { drillMeasure, parentQuery } from "derive-dashboards/core/engine";
6
+ // src/drill-share.ts
7
+ /**
8
+ * The step before's value, read now beside the drill's own (Rules, section 2): only for a line
9
+ * that adds up, below the root. Read live, as the drill's own value is, so a share never divides
10
+ * a count of now by a count of when the chart was drawn.
11
+ */
12
+ /** The share of the step before: none for a part below zero or a parent that is not above it. */
13
+ function shareFrom(value, parent) {
14
+ const part = numberOf(value);
15
+ if (part === void 0 || part < 0 || parent === void 0 || parent.value <= 0) return void 0;
16
+ return {
17
+ value: part / parent.value,
18
+ of: parent.of
19
+ };
20
+ }
21
+ /** Whether the drill is a step below the root: the root has no step before it. */
22
+ const belowRoot = (o) => (o.selections.at(-1)?.steps.length ?? 0) > 0;
23
+ /**
24
+ * The step before's value, read now beside the drill's own, for a line that adds up below the
25
+ * root: with a sum's least row value, none when a row may be negative.
26
+ */
27
+ async function parentOf(o, set, line) {
28
+ if (!line.additive || !belowRoot(o)) return;
29
+ const before = o.selections.slice(0, -1).flatMap((s) => s.steps);
30
+ const { query, least } = parentQuery(o.x.rt.options.model, composedOn(o, before), o.drill);
31
+ const answer = await drillRead(o, query);
32
+ const entry = drillMeasure(set, o.drill) ?? "m1";
33
+ if (least !== void 0 && (numberOf(lineValue(answer, least)) ?? -1) < 0) return;
34
+ const value = numberOf(lineValue(answer, entry));
35
+ const of = before.length === 0 ? "tile" : "parent";
36
+ return value === void 0 ? void 0 : {
37
+ value,
38
+ of
39
+ };
40
+ }
41
+ /** The label of the field a line counts only where set ("Hours"). */
42
+ function operandLabel(o, line) {
43
+ if (line.value === void 0 || line.semantics !== "non-null") return void 0;
44
+ const [column] = resultColumns(o.x.rt.options.model, {
45
+ ...o.x.query,
46
+ dimensions: [{
47
+ id: "d1",
48
+ member: line.value
49
+ }]
50
+ });
51
+ return column?.label;
52
+ }
53
+ export { operandLabel, parentOf, shareFrom };
@@ -0,0 +1,158 @@
1
+ import { clockOf, drillRead, mayList, mayRead, readableFor } from "./drill-access.mjs";
2
+ import { operandLabel, parentOf, shareFrom } from "./drill-share.mjs";
3
+ import { numberOf } from "./drill-totals.mjs";
4
+ import { servesRecords } from "./reading.mjs";
5
+ import { DRILL_REASONS, isMark, resultColumns } from "derive-dashboards/core";
6
+ import { EngineError, composeDrill, drillMeasure, lineScopes, measuresOnly, scopeSteps, scopedLines, summaryQuery, trendDimension } from "derive-dashboards/core/engine";
7
+ // src/drill-summary.ts
8
+ /** A value cell as a plain cell: a mark (Other, Total) has no value of its own here. */
9
+ const plainCell = (cell) => cell === void 0 || isMark(cell) ? null : cell;
10
+ /** A line's value in an answer of one row, at its column; refused when the answer has no such column. */
11
+ function lineValue(answer, line) {
12
+ const at = answer.columns.findIndex((c) => c.id === line);
13
+ if (at < 0) throw new EngineError("SELECTION_INVALID", `The drill's answer has no line "${line}".`);
14
+ return plainCell(answer.rows[0]?.[at]);
15
+ }
16
+ const ON = { ok: true };
17
+ const off = (code) => ({
18
+ ok: false,
19
+ code,
20
+ reason: DRILL_REASONS[code]
21
+ });
22
+ /** Whether Items can list: the line lists rows, the data layer serves them, and the viewer may see them. */
23
+ async function itemsCan(o, line) {
24
+ if (!line.items.ok) return line.items;
25
+ const { source } = o.x.query;
26
+ if (!servesRecords({
27
+ options: o.x.rt.options,
28
+ request: o.x.request,
29
+ source
30
+ })) return off("RECORDS_UNAVAILABLE");
31
+ return await mayList(o, source) ? ON : off("RECORDS_DENIED");
32
+ }
33
+ /** Whether Over time can draw: a time field the viewer may read. */
34
+ async function trendCan(o) {
35
+ const time = trendDimension(o.x.rt.options.model, o.x.query);
36
+ return time !== void 0 && await mayRead(o, time) ? ON : off("NO_TIME");
37
+ }
38
+ /** Which views a drill can show, and why not; no statement runs. */
39
+ /** Whether the drilled line is a formula that reads the whole chart, its bucket or the period before. */
40
+ const aroundRow = (o) => o.drill.measure !== void 0 && lineScopes(o.x.query, o.drill.measure).size > 0;
41
+ async function abilities(o, line, offered) {
42
+ const [items, trend] = await Promise.all([itemsCan(o, line), trendCan(o)]);
43
+ const scoped = line.semantics === "expression" && aroundRow(o);
44
+ return {
45
+ breakdown: scoped ? off("FORMULA_SCOPED") : offered.length > 0 ? ON : off("NO_FIELD"),
46
+ trend: scoped ? off("FORMULA_SCOPED") : trend,
47
+ items,
48
+ explanation: line.semantics === "expression" ? ON : off("NOT_FORMULA")
49
+ };
50
+ }
51
+ /** An object without its undefined fields, so an optional field is absent rather than undefined. */
52
+ function defined(o) {
53
+ return Object.fromEntries(Object.entries(o).filter(([, v]) => v !== void 0));
54
+ }
55
+ /** A drill composed on fewer steps: a scope's, or the step before. */
56
+ const composedOn = (o, steps) => composeDrill({
57
+ model: o.x.rt.options.model,
58
+ query: o.x.query,
59
+ drill: {
60
+ ...o.drill,
61
+ steps
62
+ },
63
+ readable: readableFor(o.x),
64
+ clock: clockOf(o.x)
65
+ });
66
+ /** One scope's measures, read: the tile's measures on that scope's rows. */
67
+ async function scopeCells(o, set, scope) {
68
+ const time = o.x.query.time;
69
+ const steps = scopeSteps(o.drill.steps, scope);
70
+ if (scope === "previous" && (o.pinned.compare === void 0 || time === void 0 || steps.some((s) => "bucket" in s))) throw new EngineError("DRILL_UNSUPPORTED", "This formula compares with the period before, which a time point of the chart has none of here.");
71
+ const query = scope === "previous" && time !== void 0 && o.pinned.compare !== void 0 ? {
72
+ ...set,
73
+ time: {
74
+ dimension: time.dimension,
75
+ range: o.pinned.compare
76
+ }
77
+ } : composedOn(o, steps);
78
+ const answer = await drillRead(o, measuresOnly(query));
79
+ return query.measures.map((m) => plainCell(answer.rows[0]?.[answer.columns.findIndex((c) => c.id === m.id)]));
80
+ }
81
+ /**
82
+ * Every line on the drill: its measures from the drill's own read, each calc worked out from
83
+ * the scopes it reads (the tile's total, its time point, the period before), each read once.
84
+ */
85
+ async function linesOn(o, set, own, entry) {
86
+ const row = set.measures.map((m) => lineValue(own, m.id));
87
+ const scopes = [...lineScopes(o.x.query, entry)];
88
+ const read = await Promise.all(scopes.map((s) => scopeCells(o, set, s)));
89
+ const refs = Object.fromEntries(scopes.map((s, i) => [s, read[i]]));
90
+ const around = scopes.map((s, i) => [s, scopedLines(o.x.query, { row: read[i] ?? [] })]);
91
+ return {
92
+ row: scopedLines(o.x.query, {
93
+ row,
94
+ ...refs
95
+ }),
96
+ ...Object.fromEntries(around)
97
+ };
98
+ }
99
+ /** A summary's optional parts: the clicked value, the share, the period and the notes a link carries. */
100
+ function readOptional(o, at) {
101
+ const { model } = o.x.rt.options;
102
+ const previous = o.selections.some((s) => s.previous === true);
103
+ const flag = (on) => on ? true : void 0;
104
+ return {
105
+ clicked: o.foreign ? void 0 : o.selections.at(-1)?.clicked?.[at.entry],
106
+ operand: operandLabel(o, at.line),
107
+ column: resultColumns(model, o.x.query).find((c) => c.id === at.entry),
108
+ noun: model.sources[at.set.source]?.noun,
109
+ range: previous ? o.pinned.compare : o.pinned.range,
110
+ counted: flag(at.line.counted),
111
+ previous: flag(previous),
112
+ foreign: flag(o.foreign),
113
+ omitted: flag(o.omitted)
114
+ };
115
+ }
116
+ /** Whether a drill holds no row its viewer can see: its own count is zero, or not counted. */
117
+ const holdsNoRows = (summary) => summary.itemCount === null || numberOf(summary.itemCount) === 0;
118
+ /**
119
+ * A drill's numbers, kept apart: its line's value now and how many rows are behind it (one
120
+ * read, by the drill's own count, never the mark), its value when the chart was drawn (only for
121
+ * the viewer who drew it), its share of the step before, what it can show, and the receipt.
122
+ */
123
+ async function summarize(o, set, line, offered) {
124
+ const { model } = o.x.rt.options;
125
+ const entry = drillMeasure(set, o.drill) ?? "m1";
126
+ const asked = summaryQuery(model, set, o.drill);
127
+ const [own, can, parent] = await Promise.all([
128
+ drillRead(o, asked.query),
129
+ abilities(o, line, offered),
130
+ parentOf(o, set, line)
131
+ ]);
132
+ const scoped = await linesOn(o, set, own, entry);
133
+ const value = scoped.row.get(entry) ?? null;
134
+ const optional = {
135
+ ...readOptional(o, {
136
+ set,
137
+ line,
138
+ value,
139
+ entry
140
+ }),
141
+ share: shareFrom(value, parent),
142
+ itemCount: lineValue(own, asked.count),
143
+ rows: asked.all === void 0 ? void 0 : lineValue(own, asked.all)
144
+ };
145
+ return {
146
+ summary: {
147
+ value,
148
+ semantics: line.semantics,
149
+ can,
150
+ offered: [...offered],
151
+ receipt: own.receipt,
152
+ ...defined(optional)
153
+ },
154
+ own,
155
+ scoped
156
+ };
157
+ }
158
+ export { abilities, composedOn, holdsNoRows, lineValue, summarize };
@@ -0,0 +1,53 @@
1
+ import { drillRead, mayList } from "./drill-access.mjs";
2
+ import { HttpError } from "./http.mjs";
3
+ import { canonical } from "derive-dashboards/core";
4
+ import { KEY_FIELD, ROW_COUNT } from "derive-dashboards/core/engine";
5
+ // src/drill-targets.ts
6
+ /**
7
+ * Whether distinct values may link to another source's rows: the viewer may list its rows and
8
+ * view that source itself. The tile's own grant never stands in for a source it did not read.
9
+ */
10
+ async function mayLink(o, source) {
11
+ const { principal } = o.scope;
12
+ if (principal.channel !== "user") return false;
13
+ const can = o.x.rt.options.access.can;
14
+ const [records, own] = await Promise.all([mayList(o, source), Promise.resolve(can(principal.viewer, "view", {
15
+ kind: "source",
16
+ id: source
17
+ }))]);
18
+ return records && own;
19
+ }
20
+ /**
21
+ * Which of a page's distinct values name a row of the linked source this viewer can see: one
22
+ * read of that source under the viewer's own tenant and row rules, by its key. A value whose row
23
+ * they cannot see stays a plain value, with no link to a page that would refuse them.
24
+ */
25
+ async function visibleTargets(o, target, values) {
26
+ if (values.length === 0) return /* @__PURE__ */ new Set();
27
+ const member = `${target.source}.${KEY_FIELD}`;
28
+ const query = {
29
+ kind: "aggregate",
30
+ source: target.source,
31
+ measures: [{
32
+ id: "m1",
33
+ measure: `${target.source}.${ROW_COUNT}`
34
+ }],
35
+ dimensions: [{
36
+ id: "d1",
37
+ member
38
+ }],
39
+ filters: {
40
+ member,
41
+ op: "in",
42
+ values: [...values]
43
+ }
44
+ };
45
+ try {
46
+ const answer = await drillRead(o, query, [member]);
47
+ return new Set(answer.rows.map((row) => canonical(row[0] ?? null)));
48
+ } catch (error) {
49
+ if (error instanceof HttpError && error.status === 403) return /* @__PURE__ */ new Set();
50
+ throw error;
51
+ }
52
+ }
53
+ export { mayLink, visibleTargets };
@@ -0,0 +1,8 @@
1
+ // src/drill-totals.ts
2
+ /** A count or sum's cell as a number, an exact one from its digits; undefined for anything else. */
3
+ const numberOf = (cell) => {
4
+ if (cell === void 0 || cell === null) return void 0;
5
+ if (typeof cell === "object") return "v" in cell ? Number(cell.v) : void 0;
6
+ return typeof cell === "number" && Number.isFinite(cell) ? cell : void 0;
7
+ };
8
+ export { numberOf };
@@ -0,0 +1,175 @@
1
+ import { clockOf, drillRead } from "./drill-access.mjs";
2
+ import { otherChange } from "./drill-change.mjs";
3
+ import { pathDigest } from "./drill-identity.mjs";
4
+ import { drawnOf, sealAnswer } from "./drill-seal.mjs";
5
+ import { numberOf } from "./drill-totals.mjs";
6
+ import { isMark, parseTextTime, resolveDimension } from "derive-dashboards/core";
7
+ import { EngineError, breakdownQuery, population, trendQuery } from "derive-dashboards/core/engine";
8
+ // src/drill-views.ts
9
+ /** A group of this many rows or fewer opens on its items. */
10
+ const SMALL_GROUP = 20;
11
+ /** How many fields a first Breakdown tries past those with one value at this step. */
12
+ const FIRST_TRIES = 3;
13
+ const unsupported = (reason) => {
14
+ throw new EngineError("DRILL_UNSUPPORTED", reason);
15
+ };
16
+ /**
17
+ * The view a drill opens on when it names none (Breakdown with no field), Rules, section 5:
18
+ * a formula's Explanation, else Items for a small group, else Breakdown by the first offered field (`first` lets a Breakdown
19
+ * that finds one value try the next), else Over time, else Items.
20
+ */
21
+ function firstView(summary) {
22
+ if (summary.can.explanation.ok) return { by: "explanation" };
23
+ const count = summary.itemCount === void 0 ? void 0 : numberOf(summary.itemCount);
24
+ if (summary.can.items.ok && count !== void 0 && count <= SMALL_GROUP) return { by: "items" };
25
+ const [field] = summary.offered;
26
+ if (field !== void 0) return {
27
+ by: "breakdown",
28
+ member: field.id,
29
+ first: true
30
+ };
31
+ if (summary.can.trend.ok) return { by: "trend" };
32
+ return summary.can.explanation.ok ? { by: "explanation" } : { by: "items" };
33
+ }
34
+ /**
35
+ * The view a drill shows, normalized: Breakdown with no field picks the first view by the rules;
36
+ * a view the drill cannot show is refused with its reason.
37
+ */
38
+ function chosenView(view, summary) {
39
+ const chosen = view.by === "breakdown" && view.member === void 0 ? firstView(summary) : view;
40
+ const can = summary.can[chosen.by];
41
+ if (!can.ok) unsupported(can.reason);
42
+ if (chosen.by === "breakdown" && !summary.offered.some((f) => f.id === chosen.member)) unsupported(`"${chosen.member ?? ""}" is not a field this drill can be broken down by.`);
43
+ return chosen;
44
+ }
45
+ /** The grains Over time may draw a span at: those that give between 2 and 400 points. */
46
+ function grainsFor(range) {
47
+ if (range === void 0) return ["month", "quarter"];
48
+ const from = parseTextTime(range.from);
49
+ const to = parseTextTime(range.to);
50
+ if (from === void 0 || to === void 0) return ["month", "quarter"];
51
+ const days = (to - from) / 864e8;
52
+ return [
53
+ ["hour", 1 / 24],
54
+ ["day", 1],
55
+ ["week", 7],
56
+ ["month", 30],
57
+ ["quarter", 91],
58
+ ["year", 365]
59
+ ].filter(([, d]) => days / d >= 2 && days / d <= 400).map(([g]) => g);
60
+ }
61
+ /** An answer of a view sealed once, under this drill's execution and path. */
62
+ async function sealedView(o, answer, extra) {
63
+ const parent = await pathDigest(o.drill.steps);
64
+ const range = answer.receipt.range;
65
+ return sealAnswer(o.scope, drawnOf(answer, answer.rows, range, extra), {
66
+ exec: o.pinned.id,
67
+ parent
68
+ });
69
+ }
70
+ /** Over time: the line's population by its time field, at a grain that fits, its range and grains. */
71
+ async function trendView(o, rows, view, offered) {
72
+ const query = trendQuery(o.x.rt.options.model, rows, clockOf(o.x), view.grain);
73
+ const time = query.time;
74
+ const answer = await drillRead(o, query, time === void 0 ? [] : [time.dimension]);
75
+ const range = answer.receipt.range;
76
+ return {
77
+ by: "trend",
78
+ answer,
79
+ drill: await sealedView(o, answer, {}),
80
+ offered,
81
+ ...time?.grain === void 0 ? {} : { grain: time.grain },
82
+ ...range === void 0 ? {} : { range },
83
+ grains: grainsFor(range)
84
+ };
85
+ }
86
+ /** A Breakdown or Over time of the line's population, its answer sealed once for its clicks. */
87
+ async function groupedView(o, ask) {
88
+ const rows = population(o.x.rt.options.model, ask.set, o.drill);
89
+ const { view } = ask;
90
+ if (view.by === "trend") return {
91
+ shown: await trendView(o, rows, view, ask.offered),
92
+ view
93
+ };
94
+ const fields = view.first === true ? ask.offered.map((f) => f.id).slice(0, FIRST_TRIES) : [view.member ?? ""];
95
+ return firstTelling(o, {
96
+ line: ask.line,
97
+ offered: ask.offered,
98
+ view,
99
+ rows
100
+ }, fields);
101
+ }
102
+ /**
103
+ * The first of the fields whose Breakdown tells something: more than one group (one group is
104
+ * the step itself again). Tried one after another, since each is a read; the last is kept anyway.
105
+ */
106
+ async function firstTelling(o, ask, fields) {
107
+ const [member, ...rest] = fields;
108
+ if (member === void 0) return unsupported("There is no field to break this down by.");
109
+ const shown = await breakdownView(o, {
110
+ ...ask,
111
+ member
112
+ });
113
+ return shown.answer.rows.filter((r) => r.every((c) => !isMark(c))).length !== 1 || rest.length === 0 ? {
114
+ shown,
115
+ view: {
116
+ by: "breakdown",
117
+ member
118
+ }
119
+ } : firstTelling(o, ask, rest);
120
+ }
121
+ /** Whether a field's values are text a person can search: a string or a ref, never a number's bucket. */
122
+ function searchable(o, member) {
123
+ const def = resolveDimension(o.x.rt.options.model, o.x.query.source, member)?.dimension;
124
+ return def?.kind === "string" || def?.kind === "ref" || def?.kind === "expr" && def.type === "string";
125
+ }
126
+ /**
127
+ * A Breakdown: the top groups of a field, then Other, ranked by the line when it adds up, else
128
+ * by their rows; of the change against the tile's comparison when it has one (Other with its own
129
+ * change); narrowed by a search in a text field with more values than it draws.
130
+ */
131
+ async function breakdownView(o, ask) {
132
+ const { member } = ask;
133
+ const { answer, text, rankedBy, compared } = await readBreakdown(o, ask);
134
+ const more = answer.rows.some((r) => r.some((c) => isMark(c) && c.mark === "other"));
135
+ return {
136
+ by: "breakdown",
137
+ answer,
138
+ drill: await sealedView(o, answer, {
139
+ limited: true,
140
+ ...text === void 0 ? {} : { search: {
141
+ member,
142
+ text
143
+ } }
144
+ }),
145
+ offered: ask.offered,
146
+ member,
147
+ ...compared ? { change: true } : {},
148
+ ...(more || text !== void 0) && searchable(o, member) ? { searchable: true } : {},
149
+ ...text === void 0 ? {} : { search: text },
150
+ ...rankedBy === void 0 || compared ? {} : { rankedBy }
151
+ };
152
+ }
153
+ /** A Breakdown's read: the query for its field (searched, ranked, against the comparison) and its answer. */
154
+ async function readBreakdown(o, ask) {
155
+ const { view, member, line } = ask;
156
+ const compare = o.x.query.time?.compare;
157
+ const asked = view.member === member ? view.search : void 0;
158
+ const text = asked === "" ? void 0 : asked;
159
+ const query = breakdownQuery(ask.rows, member, o.drill, {
160
+ ...compare === void 0 ? {} : { compare },
161
+ ...text === void 0 ? {} : { search: text },
162
+ rankByRows: !line.additive
163
+ });
164
+ const read = await drillRead(o, compare === void 0 ? query : {
165
+ ...query,
166
+ total: true
167
+ }, [member]);
168
+ return {
169
+ answer: compare === void 0 ? read : otherChange(read),
170
+ text,
171
+ rankedBy: line.additive ? void 0 : query.measures.at(-1)?.id,
172
+ compared: compare !== void 0
173
+ };
174
+ }
175
+ export { chosenView, groupedView };