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,257 @@
1
+ import { EngineError } from "../../error/refusal.mjs";
2
+ import { drillMeasure, lineMeasure, operandSet, rowsMeasure } from "./line.mjs";
3
+ import { ROW_COUNT, minName } from "./synthetic.mjs";
4
+ // src/query/drill/views.ts
5
+ /** Breakdown draws the top groups and puts the rest in Other. */
6
+ const BREAKDOWN_TOP = 10;
7
+ const anded = (set, parts) => {
8
+ const kept = parts.filter((f) => f !== void 0);
9
+ const [only, ...more] = kept;
10
+ const { filters: _f, ...rest } = set;
11
+ if (only === void 0) return rest;
12
+ return {
13
+ ...rest,
14
+ filters: more.length === 0 ? only : { all: kept }
15
+ };
16
+ };
17
+ /** Filters ANDed into one, or none. */
18
+ const andAll = (parts) => {
19
+ const kept = parts.filter((f) => f !== void 0);
20
+ const [only, ...more] = kept;
21
+ return more.length === 0 ? only : { all: kept };
22
+ };
23
+ /**
24
+ * The population a line reads: the rows behind the drill that the line counts. It ANDs the
25
+ * line's own filter, its measure's `where` (a ratio's denominator's; with `numerator`, its
26
+ * numerator's too), and, for a measure of a field, that the field is set: read by a predicate
27
+ * of the drill's own (`dd_set_...`), whether or not the field may be shown, so the population
28
+ * never depends on what a viewer may see. Breakdown, Over time and Items all read it, and the
29
+ * drill law checks it against the oracle. A comparison is not part of it.
30
+ */
31
+ function population(model, set, drill, numerator) {
32
+ const time = set.time === void 0 ? {} : { time: {
33
+ dimension: set.time.dimension,
34
+ range: set.time.range
35
+ } };
36
+ return anded({
37
+ ...set,
38
+ ...time
39
+ }, [set.filters, lineFilter(model, set, drill, { numerator: numerator === true })]);
40
+ }
41
+ /** A line's own filter, its rows' `where`, its operand set, and its numerator's `where`; none for a formula. */
42
+ function lineFilter(model, set, drill, only) {
43
+ const line = lineMeasure(model, set, drill);
44
+ if (line === void 0) return void 0;
45
+ const rows = rowsMeasure(model, set.source, line);
46
+ if (rows === void 0) throw new EngineError("SELECTION_INVALID", "The ratio's denominator is not a measure of its source.");
47
+ const where = "where" in rows.def ? rows.def.where : void 0;
48
+ const operand = only?.operand === false ? void 0 : operandFilter(model, set.source, rows.name);
49
+ const extra = only?.numerator === true ? numeratorWhere(model, set.source, line.def) : void 0;
50
+ return andAll([
51
+ line.entry.filter,
52
+ where,
53
+ operand,
54
+ extra
55
+ ]);
56
+ }
57
+ /** The filter "the operand is set", for a measure that counts only rows with a value. */
58
+ function operandFilter(model, source, name) {
59
+ const isSet = operandSet(model, source, name);
60
+ return isSet === void 0 ? void 0 : {
61
+ member: isSet,
62
+ op: "equals",
63
+ values: [true]
64
+ };
65
+ }
66
+ /** A ratio's numerator `where`, for the Only filter. */
67
+ function numeratorWhere(model, source, def) {
68
+ if (def.kind !== "ratio") return;
69
+ const top = model.sources[source]?.measures[def.numerator];
70
+ const where = top !== void 0 && "where" in top ? top.where : void 0;
71
+ return andAll([where, operandFilter(model, source, def.numerator)]);
72
+ }
73
+ /** The row count over a filter, as one more measure of a query. */
74
+ const counted = (set, id, filter) => ({
75
+ id,
76
+ measure: `${set.source}.${ROW_COUNT}`,
77
+ ...filter === void 0 ? {} : { filter }
78
+ });
79
+ /**
80
+ * The line's value and how many rows are behind it, in one read: the drill's query with the row
81
+ * count over the line's population (`dd_rows` with the line's filter) as one more measure, and,
82
+ * for a line that counts only rows whose field is set, the rows whether or not it is (`all`).
83
+ * Aliases are returned so each count is read by name.
84
+ */
85
+ function summaryQuery(model, set, drill) {
86
+ const count = `m${set.measures.length + 1}`;
87
+ const filter = lineFilter(model, set, drill);
88
+ const loose = lineFilter(model, set, drill, { operand: false });
89
+ const measures = [...set.measures, counted(set, count, filter)];
90
+ if (JSON.stringify(loose) === JSON.stringify(filter)) return {
91
+ query: {
92
+ ...set,
93
+ measures
94
+ },
95
+ count
96
+ };
97
+ const all = `m${set.measures.length + 2}`;
98
+ return {
99
+ query: {
100
+ ...set,
101
+ measures: [...measures, counted(set, all, loose)]
102
+ },
103
+ count,
104
+ all
105
+ };
106
+ }
107
+ /**
108
+ * The step before a sum's value, with its least row value, so a share of it is shown only
109
+ * when no row can be negative: a parent of 50 made of 100 and -50 has no 200% child.
110
+ */
111
+ function parentQuery(model, parent, drill) {
112
+ const line = lineMeasure(model, parent, drill);
113
+ const name = line?.def.kind === "sum" ? minName(line.name) : void 0;
114
+ const measure = name === void 0 ? void 0 : model.sources[parent.source]?.measures[name];
115
+ if (line === void 0 || measure === void 0) return { query: parent };
116
+ const least = `m${parent.measures.length + 1}`;
117
+ const entry = {
118
+ id: least,
119
+ measure: `${parent.source}.${name}`,
120
+ ...line.entry.filter === void 0 ? {} : { filter: line.entry.filter }
121
+ };
122
+ return {
123
+ query: {
124
+ ...parent,
125
+ measures: [...parent.measures, entry]
126
+ },
127
+ least
128
+ };
129
+ }
130
+ /** The order a Breakdown ranks its groups by: the line when it adds up, else the row count. */
131
+ function ranking(rows, by, byRows) {
132
+ if (!byRows) return {
133
+ measures: rows.measures,
134
+ order: [{
135
+ by,
136
+ dir: "desc"
137
+ }]
138
+ };
139
+ const count = `m${rows.measures.length + 1}`;
140
+ return {
141
+ measures: [...rows.measures, {
142
+ id: count,
143
+ measure: `${rows.source}.${ROW_COUNT}`
144
+ }],
145
+ order: [{
146
+ by: count,
147
+ dir: "desc"
148
+ }]
149
+ };
150
+ }
151
+ /**
152
+ * The rows behind a drill broken down by one field: its top groups, then Other, ranked by the
153
+ * line when it adds up, else by the rows behind each (Rules, section 6: never by an average).
154
+ * With a comparison, each group's value now, before (`c1`) and its change (`c2`), the groups
155
+ * that moved most first (`c3`). With a search, only the groups whose value contains it.
156
+ */
157
+ function breakdownQuery(rows, member, drill, options = {}) {
158
+ const by = drillMeasure(rows, drill) ?? "m1";
159
+ const base = {
160
+ ...options.search === void 0 || options.search === "" ? rows : anded(rows, [rows.filters, {
161
+ member,
162
+ op: "contains",
163
+ values: [options.search]
164
+ }]),
165
+ dimensions: [{
166
+ id: "d1",
167
+ member
168
+ }],
169
+ limit: BREAKDOWN_TOP,
170
+ other: true
171
+ };
172
+ if (options.compare === void 0 || rows.time === void 0 || by.startsWith("c")) return {
173
+ ...base,
174
+ ...ranking(rows, by, options.rankByRows === true)
175
+ };
176
+ const now = { ref: by };
177
+ const before = {
178
+ ref: by,
179
+ of: "previous"
180
+ };
181
+ return {
182
+ ...base,
183
+ time: {
184
+ ...rows.time,
185
+ compare: options.compare
186
+ },
187
+ calc: [
188
+ {
189
+ id: "c1",
190
+ formula: before,
191
+ label: "Before"
192
+ },
193
+ {
194
+ id: "c2",
195
+ formula: {
196
+ op: "-",
197
+ args: [now, before]
198
+ },
199
+ label: "Change"
200
+ },
201
+ {
202
+ id: "c3",
203
+ formula: {
204
+ op: "abs",
205
+ args: [{
206
+ op: "-",
207
+ args: [now, before]
208
+ }]
209
+ },
210
+ label: "Size of change"
211
+ }
212
+ ],
213
+ order: [{
214
+ by: "c3",
215
+ dir: "desc"
216
+ }]
217
+ };
218
+ }
219
+ /** How many rows a population holds: every source's rows counted (`dd_rows`, in a drill's model). */
220
+ function countQuery(rows) {
221
+ const { calc: _c, order: _o, limit: _l, other: _ot, total: _t, dimensions: _d, ...rest } = rows;
222
+ return {
223
+ ...rest,
224
+ measures: [{
225
+ id: "m1",
226
+ measure: `${rows.source}.${ROW_COUNT}`
227
+ }]
228
+ };
229
+ }
230
+ /**
231
+ * A distinct count's items, one page: its distinct values (never "not set": a distinct count
232
+ * skips it), each with its row count, most rows first then by value, so pages never overlap.
233
+ * It reads `take` past `skip`, and one more to say whether a page follows.
234
+ */
235
+ function distinctQuery(rows, field, page) {
236
+ const all = countQuery(rows);
237
+ return {
238
+ ...anded(all, [all.filters, {
239
+ member: field,
240
+ op: "set",
241
+ values: []
242
+ }]),
243
+ dimensions: [{
244
+ id: "d1",
245
+ member: field
246
+ }],
247
+ order: [{
248
+ by: "m1",
249
+ dir: "desc"
250
+ }, {
251
+ by: "d1",
252
+ dir: "asc"
253
+ }],
254
+ limit: page.skip + page.take + 1
255
+ };
256
+ }
257
+ export { breakdownQuery, countQuery, distinctQuery, lineFilter, parentQuery, population, summaryQuery };
@@ -1,6 +1,16 @@
1
- import { Cell } from "./types.mjs";
1
+ import { Cuts } from "./drill/types.mjs";
2
+ import { Cell, WireQuery } from "./types.mjs";
2
3
  /**
3
4
  * Each limited dimension's values in rank order, by its index: one more than its limit when
4
5
  * more exist, so a query can tell that the limit left something out.
5
6
  */
6
- export type Kept = ReadonlyMap<number, readonly Cell[]>;
7
+ export type Kept = ReadonlyMap<number, readonly Cell[]>;
8
+ /** What each limited dimension kept, as a drill's Other reads it: the values, cut to the limit. */
9
+ export declare const segmentCuts: (query: WireQuery, kept: Kept) => Cuts;
10
+ /** The indexes of the dimensions with a limit of their own. */
11
+ export declare const limitedDimensions: (query: WireQuery) => number[];
12
+ /**
13
+ * The query that ranks one dimension's values alone, over the whole range, by its `rankBy`:
14
+ * by default the first measure descending. Ties go by the value, as in every ranking.
15
+ */
16
+ export declare function rankQuery(query: WireQuery, index: number): WireQuery;
@@ -6,6 +6,8 @@ function keptOf(query, index, ranked) {
6
6
  const limit = query.dimensions?.[index]?.limit;
7
7
  return limit === void 0 ? ranked : ranked.slice(0, limit);
8
8
  }
9
+ /** What each limited dimension kept, as a drill's Other reads it: the values, cut to the limit. */
10
+ const segmentCuts = (query, kept) => ({ segments: [...kept].map(([i, ranked]) => [i, keptOf(query, i, ranked)]) });
9
11
  /** Whether any dimension has a limit of its own. */
10
12
  const hasSegments = (query) => (query.dimensions ?? []).some((d) => d.limit !== void 0);
11
13
  /** The indexes of the dimensions with a limit of their own. */
@@ -179,4 +181,4 @@ function segmentOthers(query, folded) {
179
181
  ...before === void 0 ? {} : { before }
180
182
  };
181
183
  }
182
- export { foldedColumn, hasSegments, keptQuery, limitedDimensions, rankQuery, segmentOtherQuery, segmentOthers, segmentSplit, uncutQuery };
184
+ export { foldedColumn, hasSegments, keptQuery, limitedDimensions, rankQuery, segmentCuts, segmentOtherQuery, segmentOthers, segmentSplit, uncutQuery };
@@ -11,6 +11,18 @@ const lastPart = (id) => id.split(".").at(-1) ?? id;
11
11
  function memberLabel(id, label) {
12
12
  return label ?? words(lastPart(id));
13
13
  }
14
+ /** How a measure's values read: its unit, and a ratio the model reads as a percent of one. */
15
+ function readsAs(def) {
16
+ const unit = def !== void 0 && "unit" in def ? def.unit : void 0;
17
+ const percent = def?.kind === "ratio" && def.format === "percent";
18
+ return {
19
+ ...unit === void 0 ? {} : { unit },
20
+ ...percent ? {
21
+ format: "percent",
22
+ fraction: true
23
+ } : {}
24
+ };
25
+ }
14
26
  /** A measure's cell type: counts are integers, percentiles float, the rest as declared. */
15
27
  function measureType(model, source, def) {
16
28
  if (def.kind === "count") return "int";
@@ -53,7 +65,8 @@ function dimensionColumns(model, query) {
53
65
  return {
54
66
  id: d.id,
55
67
  label,
56
- type: dimensionType(resolved?.dimension)
68
+ type: dimensionType(resolved?.dimension),
69
+ member: d.member
57
70
  };
58
71
  });
59
72
  }
@@ -64,21 +77,21 @@ function dimensionColumns(model, query) {
64
77
  */
65
78
  function resultColumns(model, query) {
66
79
  const grain = query.time?.grain;
67
- const bucket = grain === void 0 ? [] : [{
80
+ const bucket = grain === void 0 || query.time === void 0 ? [] : [{
68
81
  id: "t",
69
82
  label: words(grain),
70
83
  type: "time",
84
+ member: query.time.dimension,
71
85
  grain
72
86
  }];
73
87
  const dimensions = dimensionColumns(model, query);
74
88
  const measures = query.measures.map((m) => {
75
89
  const def = resolveMeasure(model, query.source, m.measure);
76
- const unit = def !== void 0 && "unit" in def && def.unit !== void 0 ? def.unit : void 0;
77
90
  return {
78
91
  id: m.id,
79
92
  label: m.label ?? memberLabel(m.measure, def?.label),
80
93
  type: def === void 0 ? "float" : measureType(model, query.source, def),
81
- ...unit === void 0 ? {} : { unit }
94
+ ...readsAs(def)
82
95
  };
83
96
  });
84
97
  const types = new Map(measures.map((c) => [c.id, c.type]));
@@ -9,12 +9,16 @@ export type ResultColumn = {
9
9
  label: string;
10
10
  type: "string" | "number" | "boolean" | "time" | "int" | "decimal" | "float";
11
11
  unit?: Unit;
12
+ /** On a dimension or the time bucket: the member it groups by, so a click can drill into it. */
13
+ member?: string;
12
14
  /** On the time bucket column: the grain its labels name. */
13
15
  grain?: Grain;
14
- /** On a calc's column: how its values read. */
16
+ /** On a calc's column, or a ratio the model reads as a percent: how its values read. */
15
17
  format?: Format;
16
18
  /** On a calc that reads the comparison period: a change, shown with its sign (+3.2%). */
17
19
  signed?: true;
20
+ /** On a ratio read as a percent: its values are fractions of one (0.55 reads 55%), not points. */
21
+ fraction?: true;
18
22
  };
19
23
  /** A column of a records page: the member it shows, labelled and typed from the model. */
20
24
  export type RecordColumn = {
@@ -0,0 +1,117 @@
1
+ import { issue } from "./issue.mjs";
2
+ import { grain, op, primitive } from "./query.mjs";
3
+ import { z } from "zod";
4
+ // src/schema/answer-parts.ts
5
+ const cell = z.union([
6
+ z.string(),
7
+ z.number(),
8
+ z.boolean(),
9
+ z.null(),
10
+ z.looseObject({
11
+ t: z.enum(["int", "decimal"]),
12
+ v: z.string()
13
+ })
14
+ ]);
15
+ const resultCell = z.union([cell, z.looseObject({ mark: z.enum(["other", "all"]) })]);
16
+ const rows = z.array(z.array(resultCell).readonly()).readonly();
17
+ const columnType = z.enum([
18
+ "string",
19
+ "number",
20
+ "boolean",
21
+ "time",
22
+ "int",
23
+ "decimal",
24
+ "float"
25
+ ]);
26
+ const column = z.looseObject({
27
+ id: z.string(),
28
+ label: z.string(),
29
+ type: columnType,
30
+ unit: z.string().exactOptional(),
31
+ member: z.string().exactOptional(),
32
+ grain: grain.exactOptional(),
33
+ format: z.enum([
34
+ "number",
35
+ "percent",
36
+ "currency",
37
+ "duration"
38
+ ]).exactOptional(),
39
+ signed: z.literal(true).exactOptional(),
40
+ fraction: z.literal(true).exactOptional()
41
+ });
42
+ const resolvedRange = z.looseObject({
43
+ from: z.string(),
44
+ to: z.string()
45
+ });
46
+ const boundFilter = z.lazy(() => z.union([
47
+ z.looseObject({ all: z.array(boundFilter).readonly() }),
48
+ z.looseObject({ any: z.array(boundFilter).readonly() }),
49
+ z.looseObject({ not: boundFilter }),
50
+ z.looseObject({
51
+ member: z.string(),
52
+ op,
53
+ values: z.array(primitive).readonly()
54
+ }),
55
+ z.looseObject({ some: z.looseObject({
56
+ join: z.string(),
57
+ where: boundFilter
58
+ }) }),
59
+ z.looseObject({ none: z.looseObject({
60
+ join: z.string(),
61
+ where: boundFilter
62
+ }) })
63
+ ]));
64
+ const receipt = z.looseObject({
65
+ queryHash: z.string(),
66
+ provider: z.string(),
67
+ trust: z.enum(["builtin", "external"]),
68
+ sources: z.array(z.string()).readonly(),
69
+ answeredBy: z.string(),
70
+ cache: z.enum([
71
+ "hit",
72
+ "stale",
73
+ "miss",
74
+ "off"
75
+ ]),
76
+ range: resolvedRange.exactOptional(),
77
+ calendar: z.looseObject({
78
+ timezone: z.string(),
79
+ weekStart: z.union([
80
+ z.literal(0),
81
+ z.literal(1),
82
+ z.literal(6)
83
+ ]),
84
+ fiscalYearStartMonth: z.number()
85
+ }),
86
+ dataThrough: z.string(),
87
+ computedAt: z.string(),
88
+ consistency: z.enum([
89
+ "snapshot",
90
+ "statement",
91
+ "mixed"
92
+ ]),
93
+ durationMs: z.number(),
94
+ rowsReturned: z.number(),
95
+ filter: boundFilter,
96
+ coverage: z.looseObject({
97
+ applied: z.array(z.string()).readonly(),
98
+ dropped: z.array(z.string()).readonly()
99
+ }),
100
+ warnings: z.array(issue).readonly()
101
+ });
102
+ const result = z.looseObject({
103
+ rows,
104
+ columns: z.array(column).readonly(),
105
+ compare: z.looseObject({
106
+ rows,
107
+ range: resolvedRange
108
+ }).exactOptional(),
109
+ status: z.enum(["complete", "truncated"]),
110
+ receipt
111
+ });
112
+ const recordColumn = z.looseObject({
113
+ member: z.string(),
114
+ label: z.string(),
115
+ type: columnType
116
+ });
117
+ export { cell, column, receipt, recordColumn, result };