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,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 };
package/dist/drill.mjs ADDED
@@ -0,0 +1,157 @@
1
+ import { clockOf, mayRead, readableFor } from "./drill-access.mjs";
2
+ import { crumbs, forOpener } from "./drill-crumbs.mjs";
3
+ import { explanationView } from "./drill-explain.mjs";
4
+ import { drillItems } from "./drill-items.mjs";
5
+ import { cutTo, openDrill } from "./drill-open.mjs";
6
+ import { abilities, holdsNoRows, summarize } from "./drill-summary.mjs";
7
+ import { chosenView, groupedView } from "./drill-views.mjs";
8
+ import { wire } from "./execute.mjs";
9
+ import { resultColumns } from "derive-dashboards/core";
10
+ import { composeDrill, drillMeasure, lineFilter, lineSemantics, offeredMembers, pathFields, stepMembers } from "derive-dashboards/core/engine";
11
+ // src/drill.ts
12
+ /** The drill's query and its line, for the path as it stands. */
13
+ function composed(o) {
14
+ const { model } = o.x.rt.options;
15
+ const set = composeDrill({
16
+ model,
17
+ query: o.x.query,
18
+ drill: o.drill,
19
+ readable: readableFor(o.x),
20
+ clock: clockOf(o.x)
21
+ });
22
+ return {
23
+ set,
24
+ line: lineSemantics(model, set, o.drill)
25
+ };
26
+ }
27
+ /**
28
+ * The fields Breakdown offers this viewer at this step, each labelled as a column of the tile
29
+ * would read it: the model's preferred and small-valued fields the tile, the path and the line's
30
+ * own measure do not fix, the field an Other was cut on first, each one the viewer may read.
31
+ */
32
+ async function offeredFields(o, set) {
33
+ const { model } = o.x.rt.options;
34
+ const where = lineFilter(model, set, o.drill, { operand: false });
35
+ const path = {
36
+ ...pathFields(o.drill.steps),
37
+ where
38
+ };
39
+ const ids = offeredMembers(model, o.x.query, path, readableFor(o.x));
40
+ const allowed = await Promise.all(ids.map((id) => mayRead(o, id)));
41
+ const kept = ids.filter((_id, i) => allowed[i] === true);
42
+ const dimensions = kept.map((member, i) => ({
43
+ id: `d${i + 1}`,
44
+ member
45
+ }));
46
+ const columns = resultColumns(model, {
47
+ ...o.x.query,
48
+ dimensions
49
+ });
50
+ return kept.map((id, i) => ({
51
+ id,
52
+ label: columns.find((c) => c.id === `d${i + 1}`)?.label ?? id
53
+ }));
54
+ }
55
+ /**
56
+ * The path cut at its first step on a field this viewer may not read: a link opened with less
57
+ * access opens at the deepest step it can, and says part of it was left out.
58
+ */
59
+ async function permitted(o) {
60
+ const fields = o.selections.map((s) => [...new Set(s.steps.flatMap((step) => stepMembers(step)))]);
61
+ const first = (await Promise.all(fields.map(async (f) => (await Promise.all(f.map((m) => mayRead(o, m)))).every(Boolean)))).indexOf(false);
62
+ return first < 0 ? o : cutTo(o, first);
63
+ }
64
+ /** Whether the summary must be read for this request: asked for, or needed to pick or explain the view. */
65
+ const needsSummary = (body) => body.summary !== false || body.state.view.by === "breakdown" && body.state.view.member === void 0 || body.state.view.by === "explanation";
66
+ /**
67
+ * The drill with its summary: for a link drawn by another viewer, cut back while its last step
68
+ * holds no row this viewer can see, so nothing about rows they cannot see is shown, not even
69
+ * that there are some.
70
+ */
71
+ async function ready(o, body) {
72
+ const { set, line } = composed(o);
73
+ const offered = await offeredFields(o, set);
74
+ const summed = o.foreign || needsSummary(body) ? await summarize(o, set, line, offered) : void 0;
75
+ const empty = summed !== void 0 && holdsNoRows(summed.summary);
76
+ return o.foreign && empty && o.selections.length > 0 ? ready(cutTo(o, o.selections.length - 1), body) : {
77
+ o,
78
+ set,
79
+ line,
80
+ offered,
81
+ ...summed === void 0 ? {} : { summed }
82
+ };
83
+ }
84
+ /** The view as the drill shows it, and what it shows. */
85
+ async function shownView(r, view, page) {
86
+ const { o, set, line } = r;
87
+ if (view.by === "items") {
88
+ const entry = drillMeasure(set, o.drill) ?? "m1";
89
+ const column = r.summed?.summary.column ?? resultColumns(o.x.rt.options.model, o.x.query).find((c) => c.id === entry);
90
+ return {
91
+ shown: await drillItems(o, {
92
+ set,
93
+ line,
94
+ column,
95
+ only: view.numerator === true
96
+ }, page ?? {}),
97
+ view: view.numerator === true ? {
98
+ by: "items",
99
+ numerator: true
100
+ } : { by: "items" }
101
+ };
102
+ }
103
+ if (view.by === "explanation") {
104
+ const summed = r.summed ?? await summarize(o, set, line, r.offered);
105
+ return {
106
+ shown: await explanationView(o, summed, drillMeasure(set, o.drill) ?? "m1"),
107
+ view
108
+ };
109
+ }
110
+ const grouped = await groupedView(o, {
111
+ set,
112
+ line,
113
+ view,
114
+ offered: r.offered
115
+ });
116
+ return {
117
+ shown: {
118
+ ...grouped.shown,
119
+ answer: wire(grouped.shown.answer)
120
+ },
121
+ view: grouped.view
122
+ };
123
+ }
124
+ /**
125
+ * The rows behind a mark: the drill opened for the person asking (every check runs again), its
126
+ * numbers kept apart, its path as breadcrumbs, its state normalized, and the view it shows.
127
+ */
128
+ async function runDrill(rt, principal, request, body) {
129
+ const r = await ready(await permitted(await openDrill({
130
+ rt,
131
+ principal,
132
+ request,
133
+ state: body.state
134
+ })), body);
135
+ const can = r.summed?.summary.can ?? await abilities(r.o, r.line, r.offered);
136
+ const choice = r.summed?.summary ?? {
137
+ can,
138
+ offered: r.offered
139
+ };
140
+ const { shown, view } = await shownView(r, chosenView(body.state.view, choice), body.page);
141
+ const { first: _first, ...plain } = view;
142
+ const views = body.state.views?.slice(0, Math.max(0, r.o.path.length - 1));
143
+ const { views: _v, ...rest } = body.state;
144
+ const state = {
145
+ ...rest,
146
+ path: r.o.path,
147
+ view: plain,
148
+ ...views === void 0 || views.length === 0 ? {} : { views }
149
+ };
150
+ return forOpener({
151
+ ...r.summed === void 0 ? {} : { summary: r.summed.summary },
152
+ path: crumbs(r.o),
153
+ state,
154
+ view: shown
155
+ }, r.o.foreign);
156
+ }
157
+ export { runDrill };
package/dist/editing.mjs CHANGED
@@ -63,7 +63,7 @@ async function storedId(rt, request, user, id) {
63
63
  const row = adapter === void 0 ? null : await requestDashboardRow(request, adapter, principalTenant(user), ref);
64
64
  expectServedAs(row, ref, id);
65
65
  if ("id" in ref) return id;
66
- return row === null ? null : String(row["id"]);
66
+ return row === null ? null : String(row.id);
67
67
  }
68
68
  /**
69
69
  * The document tiles run against for the viewer's open draft: the draft, or what publishing it
@@ -109,6 +109,6 @@ async function hasOpenDraft(rt, request, user, id) {
109
109
  const stored = adapter === void 0 ? null : await storedId(rt, request, user, id);
110
110
  if (adapter === void 0 || stored === null) return false;
111
111
  const row = await draftRow(adapter, stored, editorOf(user).author);
112
- return row !== null && row["state"] === "open";
112
+ return row !== null && row.state === "open";
113
113
  }
114
114
  export { draftDoc, editorOf, hasOpenDraft, ownUnpublished, rowId, storeFor, storedId, targetOf, unpublishedFor };
@@ -1,6 +1,6 @@
1
1
  import { bodies, parseWith } from "../body.mjs";
2
- import { executeRecords } from "../execute.mjs";
3
2
  import { HttpError } from "../http.mjs";
3
+ import { executeRecords } from "../records-read.mjs";
4
4
  import { nowOf, userOf } from "../request.mjs";
5
5
  import { docFor } from "../tile-doc.mjs";
6
6
  import { runTile, streamTiles, tileExecution } from "../tiles.mjs";
@@ -8,6 +8,12 @@ import { viewDashboard } from "./access.mjs";
8
8
  import { memberValues } from "./values.mjs";
9
9
  import { createDashEndpoint, documentLock } from "derive-dashboards/core";
10
10
  // src/endpoints/dashboards.ts
11
+ /** Which document the request names, as a drill names it. */
12
+ function documentOf(a) {
13
+ if (a.merged === true) return { document: "merged" };
14
+ if (a.draft === true) return { document: { draft: true } };
15
+ return a.at === void 0 ? {} : { document: { version: a.at } };
16
+ }
11
17
  /** What every tile of one request shares: the dashboard pinned at its version (or the viewer's draft), the picks, one `now`. */
12
18
  async function tileContext(rt, a) {
13
19
  const principal = userOf(a.principal);
@@ -19,7 +25,9 @@ async function tileContext(rt, a) {
19
25
  dashboardId: loaded.id,
20
26
  doc: loaded.document,
21
27
  picks: a.picks ?? {},
22
- now: nowOf(rt.options)
28
+ now: nowOf(rt.options),
29
+ ...documentOf(a),
30
+ ..."version" in loaded ? { version: loaded.version } : {}
23
31
  };
24
32
  }
25
33
  const RECORDS_LIMIT = 50;
@@ -106,4 +114,4 @@ function runEndpoints(rt) {
106
114
  function dashboardEndpoints(rt) {
107
115
  return runEndpoints(rt);
108
116
  }
109
- export { dashboardEndpoints };
117
+ export { dashboardEndpoints, tileContext };
@@ -0,0 +1,55 @@
1
+ import { bodies, parseWith } from "../body.mjs";
2
+ import { runDrill } from "../drill.mjs";
3
+ import { HttpError } from "../http.mjs";
4
+ import { createDashEndpoint } from "derive-dashboards/core";
5
+ // src/endpoints/drill.ts
6
+ /** The dashboard a drill body starts from, before its schema checks it. */
7
+ function dashboardOf(body) {
8
+ const parsed = bodies.drill.safeParse(body);
9
+ const origin = parsed.success ? parsed.data.state.origin : void 0;
10
+ if (origin?.kind !== "dashboard") throw new HttpError(400, "BODY_INVALID", "state.origin: a dashboard's tile is required here.");
11
+ return {
12
+ kind: "dashboard",
13
+ id: origin.dashboardId
14
+ };
15
+ }
16
+ /** Refused before anything else while the server has drill-in off. */
17
+ function drillOn(rt) {
18
+ if (rt.options.drill === false) throw new HttpError(403, "DRILL_DENIED", "Drilling is off on this server (its drill option is false).");
19
+ }
20
+ /** `view` on the dashboard a drill starts from, as for its tile. */
21
+ const viewOrigin = {
22
+ kind: "can",
23
+ verb: "view",
24
+ resource: dashboardOf
25
+ };
26
+ /**
27
+ * The rows behind a mark: of a dashboard's tile by reference, or of a free query. A drill reads
28
+ * what its tile or query reads, narrowed; a field it adds (a Breakdown's) needs `view` on that
29
+ * field, and its items need `view` on the source's records.
30
+ */
31
+ function drillEndpoints(rt) {
32
+ return {
33
+ drill: createDashEndpoint("/dashboards/drill", {
34
+ method: "POST",
35
+ access: viewOrigin,
36
+ parse: (input) => {
37
+ drillOn(rt);
38
+ return parseWith(bodies.drill, input);
39
+ },
40
+ handler: ({ body, principal, request }) => runDrill(rt, principal, request, body)
41
+ }),
42
+ queryDrill: createDashEndpoint("/query/drill", {
43
+ method: "POST",
44
+ access: { kind: "signed_in" },
45
+ parse: (input) => {
46
+ drillOn(rt);
47
+ const body = parseWith(bodies.drill, input);
48
+ if (body.state.origin.kind !== "query") throw new HttpError(400, "BODY_INVALID", "state.origin: a free query is required here.");
49
+ return body;
50
+ },
51
+ handler: ({ body, principal, request }) => runDrill(rt, principal, request, body)
52
+ })
53
+ };
54
+ }
55
+ export { drillEndpoints };