derive-dashboards 0.1.0 → 0.1.1

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 (301) hide show
  1. package/README.md +4 -3
  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/check.mjs +14 -1
  6. package/dist/cli/import/dbt-schema.mjs +2 -2
  7. package/dist/cli/import/index.mjs +2 -2
  8. package/dist/core/conformance/cases.d.mts +3 -0
  9. package/dist/core/conformance/cases.mjs +20 -4
  10. package/dist/core/conformance/check.mjs +6 -3
  11. package/dist/core/conformance/drill-cases.mjs +120 -0
  12. package/dist/core/document/view.d.mts +26 -7
  13. package/dist/core/document/view.mjs +42 -11
  14. package/dist/core/engine/index.d.mts +14 -2
  15. package/dist/core/engine/index.mjs +12 -1
  16. package/dist/core/engine/members.mjs +1 -1
  17. package/dist/core/engine/postgres.mjs +1 -1
  18. package/dist/core/engine/record-layout.d.mts +11 -0
  19. package/dist/core/engine/record-layout.mjs +7 -7
  20. package/dist/core/engine/records.d.mts +3 -2
  21. package/dist/core/engine/records.mjs +2 -1
  22. package/dist/core/engine/segmented.mjs +5 -2
  23. package/dist/core/engine/sql-records.mjs +2 -1
  24. package/dist/core/engine/statement.mjs +37 -3
  25. package/dist/core/engine/types.d.mts +3 -0
  26. package/dist/core/engine/version.d.mts +1 -1
  27. package/dist/core/engine/version.mjs +1 -1
  28. package/dist/core/error/codes.d.mts +31 -0
  29. package/dist/core/error/codes.mjs +28 -1
  30. package/dist/core/index.d.mts +9 -5
  31. package/dist/core/index.mjs +5 -3
  32. package/dist/core/model/catalog/check.mjs +1 -1
  33. package/dist/core/model/hints.d.mts +36 -0
  34. package/dist/core/model/member.d.mts +4 -2
  35. package/dist/core/model/member.mjs +16 -3
  36. package/dist/core/model/types.d.mts +8 -6
  37. package/dist/core/model/validate/drill.mjs +70 -0
  38. package/dist/core/model/validate/index.mjs +3 -0
  39. package/dist/core/model/validate/shape.mjs +150 -0
  40. package/dist/core/model/validate/source.mjs +5 -3
  41. package/dist/core/query/alias.mjs +7 -1
  42. package/dist/core/query/drill/compose.d.mts +22 -0
  43. package/dist/core/query/drill/compose.mjs +85 -0
  44. package/dist/core/query/drill/fields.d.mts +21 -0
  45. package/dist/core/query/drill/fields.mjs +91 -0
  46. package/dist/core/query/drill/internal.d.mts +11 -0
  47. package/dist/core/query/drill/items.d.mts +29 -0
  48. package/dist/core/query/drill/items.mjs +98 -0
  49. package/dist/core/query/drill/line.d.mts +31 -0
  50. package/dist/core/query/drill/line.mjs +183 -0
  51. package/dist/core/query/drill/links.d.mts +33 -0
  52. package/dist/core/query/drill/links.mjs +31 -0
  53. package/dist/core/query/drill/operand.mjs +7 -0
  54. package/dist/core/query/drill/scopes.d.mts +22 -0
  55. package/dist/core/query/drill/scopes.mjs +58 -0
  56. package/dist/core/query/drill/selection.d.mts +37 -0
  57. package/dist/core/query/drill/selection.mjs +144 -0
  58. package/dist/core/query/drill/steps.d.mts +14 -0
  59. package/dist/core/query/drill/steps.mjs +35 -0
  60. package/dist/core/query/drill/synthetic-expr.mjs +85 -0
  61. package/dist/core/query/drill/synthetic.d.mts +22 -0
  62. package/dist/core/query/drill/synthetic.mjs +165 -0
  63. package/dist/core/query/drill/time.d.mts +11 -0
  64. package/dist/core/query/drill/time.mjs +71 -0
  65. package/dist/core/query/drill/types.d.mts +155 -0
  66. package/dist/core/query/drill/types.mjs +4 -0
  67. package/dist/core/query/drill/views.d.mts +62 -0
  68. package/dist/core/query/drill/views.mjs +257 -0
  69. package/dist/core/query/segments.d.mts +12 -2
  70. package/dist/core/query/segments.mjs +3 -1
  71. package/dist/core/result/columns.mjs +17 -4
  72. package/dist/core/result/types.d.mts +5 -1
  73. package/dist/core/schema/answer-parts.mjs +117 -0
  74. package/dist/core/schema/answers.d.mts +2108 -128
  75. package/dist/core/schema/answers.mjs +10 -113
  76. package/dist/core/schema/commands.d.mts +2 -2
  77. package/dist/core/schema/document.d.mts +1 -1
  78. package/dist/core/schema/drill-answers.mjs +179 -0
  79. package/dist/core/schema/drill.d.mts +260 -0
  80. package/dist/core/schema/drill.mjs +107 -0
  81. package/dist/core/schema/index.d.mts +4 -2
  82. package/dist/core/schema/index.mjs +4 -2
  83. package/dist/core/schema/picks.d.mts +35 -0
  84. package/dist/core/schema/picks.mjs +23 -0
  85. package/dist/core/schema/query.d.mts +1 -1
  86. package/dist/core/schema/tools.mjs +1 -1
  87. package/dist/core/schema/view.d.mts +0 -35
  88. package/dist/core/schema/view.mjs +19 -24
  89. package/dist/core/store/schema.d.mts +5 -0
  90. package/dist/core/store/schema.mjs +10 -0
  91. package/dist/core/types/api.d.mts +15 -1
  92. package/dist/core/types/api.mjs +1 -1
  93. package/dist/core/types/drill-api.d.mts +210 -0
  94. package/dist/core/types/options.d.mts +21 -0
  95. package/dist/core/types/viewer.d.mts +5 -0
  96. package/dist/cursor-seal.d.mts +8 -0
  97. package/dist/cursor-seal.mjs +98 -0
  98. package/dist/dashboard-id.mjs +1 -1
  99. package/dist/drill-access.mjs +93 -0
  100. package/dist/drill-change.mjs +76 -0
  101. package/dist/drill-crumbs.mjs +113 -0
  102. package/dist/drill-distinct.mjs +130 -0
  103. package/dist/drill-explain.mjs +64 -0
  104. package/dist/drill-identity.mjs +35 -0
  105. package/dist/drill-item-id.mjs +35 -0
  106. package/dist/drill-items.mjs +132 -0
  107. package/dist/drill-open.mjs +169 -0
  108. package/dist/drill-seal.mjs +170 -0
  109. package/dist/drill-secret.mjs +14 -0
  110. package/dist/drill-share.mjs +53 -0
  111. package/dist/drill-summary.mjs +158 -0
  112. package/dist/drill-targets.mjs +53 -0
  113. package/dist/drill-totals.mjs +8 -0
  114. package/dist/drill-views.mjs +175 -0
  115. package/dist/drill.mjs +157 -0
  116. package/dist/editing.mjs +2 -2
  117. package/dist/endpoints/dashboards.mjs +11 -3
  118. package/dist/endpoints/drill.mjs +55 -0
  119. package/dist/endpoints/index.d.mts +233 -2
  120. package/dist/endpoints/index.mjs +5 -1
  121. package/dist/endpoints/query.mjs +26 -4
  122. package/dist/execute.d.mts +19 -1
  123. package/dist/execute.mjs +15 -17
  124. package/dist/index.d.mts +3 -1
  125. package/dist/index.mjs +29 -2
  126. package/dist/mcp/drill.mjs +61 -0
  127. package/dist/mcp/format.mjs +27 -3
  128. package/dist/mcp/index.d.mts +15 -4
  129. package/dist/mcp/index.mjs +13 -7
  130. package/dist/mcp/query.d.mts +7 -2
  131. package/dist/mcp/query.mjs +7 -3
  132. package/dist/node-postgres.d.mts +8 -4
  133. package/dist/node-postgres.mjs +29 -6
  134. package/dist/owner.mjs +3 -3
  135. package/dist/postgres-connection.d.mts +8 -2
  136. package/dist/postgres-connection.mjs +2 -1
  137. package/dist/react/Dashboard.d.mts +10 -5
  138. package/dist/react/Dashboard.mjs +51 -23
  139. package/dist/react/DashboardList.mjs +30 -2
  140. package/dist/react/Discard.mjs +7 -2
  141. package/dist/react/Grid.mjs +12 -5
  142. package/dist/react/Header.mjs +9 -0
  143. package/dist/react/Parts.mjs +6 -2
  144. package/dist/react/ReviewPanel.mjs +13 -2
  145. package/dist/react/RowBody.d.mts +19 -0
  146. package/dist/react/RowBody.mjs +55 -0
  147. package/dist/react/Tile.d.mts +1 -1
  148. package/dist/react/Tile.mjs +54 -30
  149. package/dist/react/builder/state.d.mts +1 -1
  150. package/dist/react/client.d.mts +2 -0
  151. package/dist/react/client.mjs +1 -0
  152. package/dist/react/context.d.mts +15 -1
  153. package/dist/react/context.mjs +19 -4
  154. package/dist/react/drill/Bars.mjs +179 -0
  155. package/dist/react/drill/Controls.mjs +173 -0
  156. package/dist/react/drill/DashboardTile.d.mts +18 -0
  157. package/dist/react/drill/DashboardTile.mjs +107 -0
  158. package/dist/react/drill/DrillPanel.d.mts +31 -0
  159. package/dist/react/drill/DrillPanel.mjs +104 -0
  160. package/dist/react/drill/DrillTabs.mjs +52 -0
  161. package/dist/react/drill/Header.mjs +131 -0
  162. package/dist/react/drill/Items.d.mts +22 -0
  163. package/dist/react/drill/Items.mjs +164 -0
  164. package/dist/react/drill/ItemsFoot.mjs +60 -0
  165. package/dist/react/drill/Notes.mjs +58 -0
  166. package/dist/react/drill/PanelBody.mjs +74 -0
  167. package/dist/react/drill/PanelStates.mjs +37 -0
  168. package/dist/react/drill/board.d.mts +18 -0
  169. package/dist/react/drill/board.mjs +91 -0
  170. package/dist/react/drill/counts.mjs +14 -0
  171. package/dist/react/drill/host.mjs +26 -0
  172. package/dist/react/drill/item-parts.d.mts +5 -0
  173. package/dist/react/drill/item-parts.mjs +76 -0
  174. package/dist/react/drill/item-type.d.mts +26 -0
  175. package/dist/react/drill/item-type.mjs +7 -0
  176. package/dist/react/drill/lazy.d.mts +6 -0
  177. package/dist/react/drill/lazy.mjs +17 -0
  178. package/dist/react/drill/pages.mjs +48 -0
  179. package/dist/react/drill/panel-focus.mjs +51 -0
  180. package/dist/react/drill/props.d.mts +25 -0
  181. package/dist/react/drill/props.mjs +72 -0
  182. package/dist/react/drill/state.d.mts +18 -0
  183. package/dist/react/drill/state.mjs +68 -0
  184. package/dist/react/drill/useDrill.d.mts +46 -0
  185. package/dist/react/drill/useDrill.mjs +194 -0
  186. package/dist/react/drill/words.d.mts +98 -0
  187. package/dist/react/drill/words.mjs +77 -0
  188. package/dist/react/english.mjs +9 -1
  189. package/dist/react/format.d.mts +3 -3
  190. package/dist/react/format.mjs +34 -12
  191. package/dist/react/index.d.mts +27 -4
  192. package/dist/react/index.mjs +10 -3
  193. package/dist/react/messages.d.mts +14 -1
  194. package/dist/react/renderers/index.d.mts +10 -2
  195. package/dist/react/renderers/index.mjs +54 -14
  196. package/dist/react/renderers/list.d.mts +22 -0
  197. package/dist/react/renderers/list.mjs +118 -0
  198. package/dist/react/review-words.mjs +1 -1
  199. package/dist/react/review.mjs +2 -2
  200. package/dist/react/scope.mjs +2 -7
  201. package/dist/react/series.d.mts +37 -0
  202. package/dist/react/series.mjs +95 -0
  203. package/dist/react/theme.css +1823 -1296
  204. package/dist/react/theme.css.d.ts +1 -0
  205. package/dist/react/tile-store.mjs +15 -3
  206. package/dist/react/useBoard.d.mts +5 -0
  207. package/dist/react/useBoard.mjs +4 -1
  208. package/dist/react/useDashboard.d.mts +1 -0
  209. package/dist/react/useDashboard.mjs +2 -1
  210. package/dist/react/useTiles.mjs +50 -15
  211. package/dist/react/view.d.mts +9 -0
  212. package/dist/react/view.mjs +22 -10
  213. package/dist/reading.d.mts +2 -0
  214. package/dist/reading.mjs +9 -1
  215. package/dist/recharts/charts.d.mts +0 -1
  216. package/dist/recharts/charts.mjs +128 -34
  217. package/dist/recharts/index.d.mts +2 -2
  218. package/dist/recharts/index.mjs +2 -2
  219. package/dist/recharts/marks.mjs +83 -0
  220. package/dist/recharts/series.d.mts +2 -22
  221. package/dist/recharts/series.mjs +1 -64
  222. package/dist/records-read.mjs +44 -0
  223. package/dist/store/apply.mjs +5 -5
  224. package/dist/store/commands.mjs +4 -4
  225. package/dist/store/discard.mjs +7 -7
  226. package/dist/store/drafts.mjs +5 -5
  227. package/dist/store/editor.mjs +1 -1
  228. package/dist/store/publish.mjs +12 -12
  229. package/dist/store/reader.mjs +41 -22
  230. package/dist/store/review.mjs +16 -16
  231. package/dist/store/rows.mjs +20 -20
  232. package/dist/store/undo.mjs +3 -3
  233. package/dist/store/unpublished.mjs +4 -4
  234. package/dist/store/versions.mjs +14 -14
  235. package/dist/testing/adapter-suite.mjs +4 -4
  236. package/dist/tiles.mjs +37 -3
  237. package/package.json +8 -10
  238. package/dist/react/Records.d.mts +0 -12
  239. package/dist/react/Records.mjs +0 -97
  240. package/src/adapter/sql-adapter.ts +0 -95
  241. package/src/adapter/statement.ts +0 -185
  242. package/src/authorize.ts +0 -73
  243. package/src/body.ts +0 -169
  244. package/src/bounded.ts +0 -43
  245. package/src/cache.ts +0 -192
  246. package/src/checks.ts +0 -170
  247. package/src/custom-source.ts +0 -59
  248. package/src/dashboard-id.ts +0 -36
  249. package/src/dashboards.ts +0 -132
  250. package/src/editing.ts +0 -148
  251. package/src/endpoints/access.ts +0 -41
  252. package/src/endpoints/dashboards.ts +0 -92
  253. package/src/endpoints/drafts.ts +0 -138
  254. package/src/endpoints/index.ts +0 -21
  255. package/src/endpoints/meta.ts +0 -137
  256. package/src/endpoints/query.ts +0 -86
  257. package/src/endpoints/read.ts +0 -92
  258. package/src/endpoints/review.ts +0 -89
  259. package/src/endpoints/values.ts +0 -62
  260. package/src/endpoints/versions.ts +0 -104
  261. package/src/execute.ts +0 -207
  262. package/src/handler.ts +0 -232
  263. package/src/http.ts +0 -66
  264. package/src/index.ts +0 -163
  265. package/src/introspect.ts +0 -205
  266. package/src/mcp/api.ts +0 -59
  267. package/src/mcp/format.ts +0 -67
  268. package/src/mcp/index.ts +0 -151
  269. package/src/mcp/query.ts +0 -153
  270. package/src/mcp/resolve.ts +0 -114
  271. package/src/node-postgres.ts +0 -81
  272. package/src/owner.ts +0 -36
  273. package/src/postgres-connection.ts +0 -143
  274. package/src/postgres-store.ts +0 -82
  275. package/src/principal.ts +0 -39
  276. package/src/reading.ts +0 -56
  277. package/src/request-rows.ts +0 -23
  278. package/src/request.ts +0 -22
  279. package/src/storage.ts +0 -63
  280. package/src/store/apply.ts +0 -129
  281. package/src/store/commands.ts +0 -75
  282. package/src/store/conflicts.ts +0 -98
  283. package/src/store/create-tables.ts +0 -104
  284. package/src/store/discard.ts +0 -42
  285. package/src/store/drafts.ts +0 -133
  286. package/src/store/editor.ts +0 -28
  287. package/src/store/log.ts +0 -28
  288. package/src/store/publish.ts +0 -199
  289. package/src/store/publisher.ts +0 -21
  290. package/src/store/reader.ts +0 -139
  291. package/src/store/review.ts +0 -239
  292. package/src/store/rows.ts +0 -250
  293. package/src/store/tenant.ts +0 -28
  294. package/src/store/undo.ts +0 -56
  295. package/src/store/unpublished.ts +0 -33
  296. package/src/store/versions.ts +0 -106
  297. package/src/store/writer.ts +0 -19
  298. package/src/testing/adapter-suite.ts +0 -239
  299. package/src/testing/index.ts +0 -2
  300. package/src/tile-doc.ts +0 -46
  301. package/src/tiles.ts +0 -120
@@ -0,0 +1,132 @@
1
+ import { drillRead, readableColumns, running } from "./drill-access.mjs";
2
+ import { distinctItems } from "./drill-distinct.mjs";
3
+ import { fingerprint } from "./drill-identity.mjs";
4
+ import { itemId, keyHidden, linked } from "./drill-item-id.mjs";
5
+ import { lineValue } from "./drill-summary.mjs";
6
+ import { readPage } from "./records-read.mjs";
7
+ import { resolveDimension } from "derive-dashboards/core";
8
+ import { countQuery, flagCell, itemsShape, population, storedCell } from "derive-dashboards/core/engine";
9
+ // src/drill-items.ts
10
+ /** A column as Items show it: the line's value labelled, typed and formatted as the line, the flag as its numerator, a number with its model format. */
11
+ function columnOf(model, source, c, r) {
12
+ if (c.member === r.line.value && r.line.stored !== void 0 && r.column !== void 0) {
13
+ const unit = r.column.unit;
14
+ return {
15
+ member: c.member,
16
+ label: r.column.label,
17
+ type: r.column.type,
18
+ ...unit === void 0 ? {} : { unit }
19
+ };
20
+ }
21
+ if (c.member === r.line.numerator?.flag) return {
22
+ member: c.member,
23
+ label: r.line.numerator.label,
24
+ type: "boolean"
25
+ };
26
+ const format = resolveDimension(model, source, c.member)?.dimension.format;
27
+ return format === void 0 ? c : {
28
+ ...c,
29
+ format
30
+ };
31
+ }
32
+ /** A page of records as items: each with its source, id, key (unless hidden), values and link. */
33
+ async function itemsOf(o, x, page, r) {
34
+ const by = await fingerprint(o.x.rt, o.scope.principal);
35
+ const read = await readPage(x, {
36
+ ...page,
37
+ by
38
+ });
39
+ const { source } = x.query;
40
+ const { model } = x.rt.options;
41
+ const hide = keyHidden(model, source);
42
+ const names = model.sources[source]?.key ?? [];
43
+ const keys = read.keys ?? [];
44
+ const stored = r.line.stored;
45
+ const cell = (member, v) => {
46
+ if (member === r.line.numerator?.flag) return flagCell(v);
47
+ return member === r.line.value && stored !== void 0 ? storedCell(stored.numeric, v) : v;
48
+ };
49
+ const items = await Promise.all(read.rows.map(async (row, n) => {
50
+ const key = keys[n] ?? [];
51
+ const item = {
52
+ source,
53
+ id: await itemId(o, source, key, hide),
54
+ ...hide ? {} : { key: Object.fromEntries(names.map((k, i) => [k, key[i] ?? null])) },
55
+ values: Object.fromEntries(read.columns.map((c, i) => [c.member, cell(c.member, row[i] ?? null)]))
56
+ };
57
+ return linked(o, item);
58
+ }));
59
+ return {
60
+ source,
61
+ columns: read.columns.map((c) => columnOf(model, source, c, r)),
62
+ items,
63
+ ...read.next === void 0 ? {} : { next: read.next }
64
+ };
65
+ }
66
+ /** A ratio's numerator: its label and flag, how many rows it counts, and whether the list shows only those. */
67
+ async function numeratorOf(o, set, line, only) {
68
+ const numerator = line.numerator;
69
+ if (numerator === void 0) return {};
70
+ const { model } = o.x.rt.options;
71
+ const counted = await drillRead(o, countQuery(population(model, set, o.drill, true)));
72
+ return { numerator: {
73
+ ...numerator,
74
+ count: lineValue(counted, "m1"),
75
+ ...only ? { only: true } : {},
76
+ ...numerator.flag === void 0 ? { unmarked: true } : {}
77
+ } };
78
+ }
79
+ /**
80
+ * A page of a drill's Items, as its line's kind reads them (Rules, section 2): the rows of its
81
+ * population (a count's own rows, a sum's with the summed field, an average's or percentile's
82
+ * rows with a value sorted by it, a ratio's denominator rows, each marked when its numerator
83
+ * counts it, or only those), or a distinct count's values. Only columns the viewer may read
84
+ * are listed.
85
+ */
86
+ async function drillItems(o, ask, page) {
87
+ const { model } = o.x.rt.options;
88
+ const { set, line } = ask;
89
+ const rows = population(model, set, o.drill, ask.only && line.numerator !== void 0);
90
+ if (line.semantics === "distinct" && line.value !== void 0) return distinctItems(o, rows, line.value, page);
91
+ const { title, badge, sorted, fixed, ...shape } = itemsShape(model, set, o.drill, line);
92
+ const columns = await readableColumns(o, set.source, shape.columns);
93
+ const after = page.after === void 0 ? {} : { after: page.after };
94
+ const read = {
95
+ limit: page.limit ?? 50,
96
+ shape: {
97
+ ...shape,
98
+ columns
99
+ },
100
+ ...after
101
+ };
102
+ const [listed, numerator] = await Promise.all([itemsOf(o, running(o.x, rows), read, {
103
+ line,
104
+ column: ask.column
105
+ }), page.after === void 0 ? numeratorOf(o, set, line, ask.only) : Promise.resolve({})]);
106
+ return {
107
+ ...itemsView(listed, {
108
+ title,
109
+ badge,
110
+ sorted,
111
+ fixed
112
+ }, line),
113
+ ...numerator
114
+ };
115
+ }
116
+ /** The Items view from a page: the title and badge only where their column is listed. */
117
+ function itemsView(listed, hints, line) {
118
+ const { title, badge, sorted, fixed } = hints;
119
+ const shown = (member) => member !== void 0 && listed.columns.some((c) => c.member === member);
120
+ return {
121
+ by: "items",
122
+ ...listed,
123
+ ...title !== void 0 && shown(title) ? { title } : {},
124
+ ...badge !== void 0 && shown(badge.member) ? { badge } : {},
125
+ ...line.value === void 0 ? {} : { value: line.value },
126
+ ...sorted === void 0 ? {} : { sorted },
127
+ ...fixed.length === 0 ? {} : { fixed },
128
+ ...line.percentile === void 0 ? {} : { percentile: line.percentile },
129
+ sort: line.ranked === true ? "value" : "records"
130
+ };
131
+ }
132
+ export { drillItems };
@@ -0,0 +1,169 @@
1
+ import { fingerprint, pathDigest, revisionOf } from "./drill-identity.mjs";
2
+ import { isRowRef, openExecution, openRow, openSelection, sealSelection } from "./drill-seal.mjs";
3
+ import { tileContext } from "./endpoints/dashboards.mjs";
4
+ import { freeExecution } from "./endpoints/query.mjs";
5
+ import { HttpError } from "./http.mjs";
6
+ import { tileExecution } from "./tiles.mjs";
7
+ import { DRILL_REASONS } from "derive-dashboards/core";
8
+ import { EngineError, keptPath } from "derive-dashboards/core/engine";
9
+ // src/drill-open.ts
10
+ /** The tile or query a drill starts from, composed as its answer was. */
11
+ async function originExecution(a, principal) {
12
+ const { origin } = a.state;
13
+ if (origin.kind === "query") return freeExecution(a.rt, principal, a.request, origin.query);
14
+ const document = origin.document;
15
+ const ctx = await tileContext(a.rt, {
16
+ dashboardId: origin.dashboardId,
17
+ picks: origin.picks,
18
+ ...document !== void 0 && "draft" in document ? { draft: true } : {},
19
+ ...document !== void 0 && "version" in document ? { at: document.version } : {},
20
+ principal,
21
+ request: a.request
22
+ });
23
+ return tileExecution(ctx, {
24
+ widgetId: origin.widgetId,
25
+ ...origin.query === void 0 ? {} : { query: origin.query }
26
+ });
27
+ }
28
+ /**
29
+ * The execution as the answer was drawn: its pinned range and calendar, so the drill does not
30
+ * move; for a mark of the period before, that period's range. It reads the drill's model: the
31
+ * source's own fields and a drill's (`drillModel`).
32
+ */
33
+ /** Whether a query's range is relative to now ("last 30 days"). */
34
+ const isRelative = (time) => time !== void 0 && "last" in time.range;
35
+ function pinnedTo(x, at, previous) {
36
+ const options = {
37
+ ...x.rt.options,
38
+ model: x.rt.drillModel
39
+ };
40
+ const pinned = {
41
+ ...x,
42
+ rt: {
43
+ ...x.rt,
44
+ options,
45
+ cache: void 0
46
+ },
47
+ calendar: at.calendar
48
+ };
49
+ const time = x.query.time;
50
+ if (!previous && at.now !== void 0 && isRelative(time)) return {
51
+ ...pinned,
52
+ now: new Date(at.now)
53
+ };
54
+ const range = previous ? at.compare : at.range;
55
+ if (time === void 0 || range === void 0) return pinned;
56
+ return {
57
+ ...pinned,
58
+ query: {
59
+ ...x.query,
60
+ time: {
61
+ dimension: time.dimension,
62
+ range,
63
+ ...time.grain === void 0 ? {} : { grain: time.grain },
64
+ ...previous || time.compare === void 0 ? {} : { compare: time.compare }
65
+ }
66
+ }
67
+ };
68
+ }
69
+ /** A settled read's value, or its error thrown. */
70
+ function settled(result) {
71
+ if (result.status === "fulfilled") return result.value;
72
+ throw result.reason;
73
+ }
74
+ /** Refused unless a signed-in viewer drills a source the model lets drill. */
75
+ function checkDrillable(a, x) {
76
+ if (a.rt.options.model.sources[x.query.source]?.drill === false) throw new HttpError(403, "DRILL_DENIED", DRILL_REASONS.SOURCE_OFF);
77
+ }
78
+ const stale = () => new HttpError(409, "DRILL_STALE", "The dashboard changed since this drill was opened: open it again from the chart.");
79
+ const notThisView = () => new EngineError("SELECTION_INVALID", "This link's drill is out of date: open it again from the chart.");
80
+ /**
81
+ * Each step of the path opened: a sealed selection, or a row of a sealed answer (a click not yet
82
+ * normalized). Each must be drawn under this execution, after the very steps before it.
83
+ */
84
+ async function openPath(scope, entries, exec) {
85
+ const opened = await Promise.all(entries.map((e) => isRowRef(e) ? openRow(scope, e) : openSelection(scope, e)));
86
+ const digests = await Promise.all(opened.map((_s, i) => pathDigest(opened.slice(0, i).flatMap((s) => s.steps))));
87
+ opened.forEach((s, i) => {
88
+ if (s.exec !== exec || s.parent !== digests[i]) throw notThisView();
89
+ });
90
+ return opened;
91
+ }
92
+ /**
93
+ * The path normalized and sealed again where it changed: each kept selection bound to the steps
94
+ * now before it, so the next request opens it as it stands.
95
+ */
96
+ function resealed(scope, kept, entries) {
97
+ return Promise.all(kept.map(async (s, i) => {
98
+ const parent = await pathDigest(kept.slice(0, i).flatMap((k) => k.steps));
99
+ const entry = entries[i];
100
+ if (entry !== void 0 && !isRowRef(entry) && parent === s.parent) return entry;
101
+ return sealSelection(scope, {
102
+ ...s,
103
+ parent
104
+ });
105
+ }));
106
+ }
107
+ /**
108
+ * A drill opened for the person asking: refused on a public link and on a source the model
109
+ * keeps from drilling; its origin composed under their access; its execution and path opened
110
+ * (each sealed for this tenant and origin, each step bound to the execution and the steps
111
+ * before it); its path normalized so it never fixes a field twice. A dashboard whose tile
112
+ * changed since the drill was drawn (another version, an edited draft) is DRILL_STALE. A link
113
+ * drawn by another viewer opens without anything they saw.
114
+ */
115
+ async function openDrill(a) {
116
+ if (a.rt.options.drill === false) throw new HttpError(403, "DRILL_DENIED", "Drilling is off on this server (its drill option is false).");
117
+ const principal = a.principal;
118
+ if (principal?.channel !== "user") throw new HttpError(403, "DRILL_DENIED", "Drilling needs a signed-in viewer; a public link does not drill.");
119
+ const scope = {
120
+ rt: a.rt,
121
+ principal,
122
+ origin: a.state.origin
123
+ };
124
+ const [origin, execution, by] = await Promise.allSettled([
125
+ originExecution(a, principal),
126
+ openExecution(scope, a.state.execution),
127
+ fingerprint(a.rt, principal)
128
+ ]);
129
+ const x = settled(origin);
130
+ checkDrillable(a, x);
131
+ const at = settled(execution);
132
+ if (at.revision !== void 0 && at.revision !== await revisionOf(x.query)) throw stale();
133
+ const all = await openPath(scope, a.state.path, at.id);
134
+ const foreign = at.by !== settled(by);
135
+ const mine = foreign ? all.map(({ clicked: _c, ...s }) => s) : all;
136
+ const indexes = keptPath(mine);
137
+ const kept = indexes.flatMap((i) => mine[i] ?? []);
138
+ const entries = indexes.flatMap((i) => a.state.path[i] ?? []);
139
+ return {
140
+ x: pinnedTo(x, at, kept.some((s) => s.previous === true)),
141
+ scope,
142
+ pinned: at,
143
+ execution: a.state.execution,
144
+ drill: {
145
+ steps: kept.flatMap((s) => s.steps),
146
+ ...a.state.entry === void 0 ? {} : { measure: a.state.entry }
147
+ },
148
+ path: await resealed(scope, kept, entries),
149
+ selections: kept,
150
+ foreign,
151
+ omitted: false,
152
+ reads: /* @__PURE__ */ new Map()
153
+ };
154
+ }
155
+ /** The drill cut back to its first `steps` selections, the rest left out: what a viewer with less access opens. */
156
+ function cutTo(o, steps) {
157
+ const kept = o.selections.slice(0, steps);
158
+ return {
159
+ ...o,
160
+ drill: {
161
+ ...o.drill,
162
+ steps: kept.flatMap((s) => s.steps)
163
+ },
164
+ selections: kept,
165
+ path: o.path.slice(0, steps),
166
+ omitted: true
167
+ };
168
+ }
169
+ export { cutTo, openDrill };
@@ -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 };