derive-dashboards 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -5
- package/dist/adapter/sql-adapter.mjs +4 -4
- package/dist/body.mjs +11 -1
- package/dist/cache.d.mts +1 -1
- package/dist/cli/add.mjs +130 -0
- package/dist/cli/check.mjs +14 -1
- package/dist/cli/cli.mjs +14 -12
- package/dist/cli/config.mjs +12 -1
- package/dist/cli/doctor.mjs +147 -0
- package/dist/cli/import/dbt-schema.mjs +2 -2
- package/dist/cli/import/index.mjs +2 -2
- package/dist/cli/init.mjs +3 -2
- package/dist/cli/json.mjs +21 -0
- package/dist/cli/layers.mjs +33 -0
- package/dist/core/conformance/cases.d.mts +3 -0
- package/dist/core/conformance/cases.mjs +20 -4
- package/dist/core/conformance/check.mjs +6 -3
- package/dist/core/conformance/drill-cases.mjs +120 -0
- package/dist/core/document/layout.mjs +1 -1
- package/dist/core/document/view-layout.d.mts +14 -0
- package/dist/core/document/view-layout.mjs +157 -0
- package/dist/core/document/view.d.mts +26 -7
- package/dist/core/document/view.mjs +42 -11
- package/dist/core/engine/filter.mjs +2 -2
- package/dist/core/engine/index.d.mts +14 -2
- package/dist/core/engine/index.mjs +12 -1
- package/dist/core/engine/leaf.mjs +9 -4
- package/dist/core/engine/members.mjs +2 -2
- package/dist/core/engine/postgres.mjs +1 -1
- package/dist/core/engine/record-layout.d.mts +11 -0
- package/dist/core/engine/record-layout.mjs +7 -7
- package/dist/core/engine/records.d.mts +3 -2
- package/dist/core/engine/records.mjs +2 -1
- package/dist/core/engine/segmented.mjs +5 -2
- package/dist/core/engine/sql-records.mjs +2 -1
- package/dist/core/engine/statement.mjs +37 -3
- package/dist/core/engine/types.d.mts +3 -0
- package/dist/core/engine/version.d.mts +1 -1
- package/dist/core/engine/version.mjs +1 -1
- package/dist/core/error/codes.d.mts +33 -1
- package/dist/core/error/codes.mjs +30 -2
- package/dist/core/index.d.mts +13 -7
- package/dist/core/index.mjs +9 -5
- package/dist/core/model/catalog/check.mjs +1 -1
- package/dist/core/model/hints.d.mts +36 -0
- package/dist/core/model/member.d.mts +4 -2
- package/dist/core/model/member.mjs +19 -3
- package/dist/core/model/types.d.mts +21 -19
- package/dist/core/model/validate/drill.mjs +70 -0
- package/dist/core/model/validate/index.mjs +3 -0
- package/dist/core/model/validate/members.mjs +7 -4
- package/dist/core/model/validate/shape.mjs +150 -0
- package/dist/core/model/validate/source.mjs +5 -3
- package/dist/core/policy/bind-query.d.mts +12 -0
- package/dist/core/policy/bind-query.mjs +53 -0
- package/dist/core/query/alias.mjs +7 -1
- package/dist/core/query/drill/compose.d.mts +22 -0
- package/dist/core/query/drill/compose.mjs +85 -0
- package/dist/core/query/drill/fields.d.mts +21 -0
- package/dist/core/query/drill/fields.mjs +91 -0
- package/dist/core/query/drill/internal.d.mts +11 -0
- package/dist/core/query/drill/items.d.mts +29 -0
- package/dist/core/query/drill/items.mjs +98 -0
- package/dist/core/query/drill/line.d.mts +31 -0
- package/dist/core/query/drill/line.mjs +183 -0
- package/dist/core/query/drill/links.d.mts +33 -0
- package/dist/core/query/drill/links.mjs +31 -0
- package/dist/core/query/drill/operand.mjs +7 -0
- package/dist/core/query/drill/scopes.d.mts +22 -0
- package/dist/core/query/drill/scopes.mjs +58 -0
- package/dist/core/query/drill/selection.d.mts +37 -0
- package/dist/core/query/drill/selection.mjs +144 -0
- package/dist/core/query/drill/steps.d.mts +14 -0
- package/dist/core/query/drill/steps.mjs +35 -0
- package/dist/core/query/drill/synthetic-expr.mjs +85 -0
- package/dist/core/query/drill/synthetic.d.mts +22 -0
- package/dist/core/query/drill/synthetic.mjs +165 -0
- package/dist/core/query/drill/time.d.mts +11 -0
- package/dist/core/query/drill/time.mjs +71 -0
- package/dist/core/query/drill/types.d.mts +155 -0
- package/dist/core/query/drill/types.mjs +4 -0
- package/dist/core/query/drill/views.d.mts +62 -0
- package/dist/core/query/drill/views.mjs +257 -0
- package/dist/core/query/filter.d.mts +6 -0
- package/dist/core/query/picks.d.mts +8 -2
- package/dist/core/query/picks.mjs +11 -4
- package/dist/core/query/segments.d.mts +12 -2
- package/dist/core/query/segments.mjs +5 -2
- package/dist/core/result/columns.mjs +20 -4
- package/dist/core/result/types.d.mts +5 -1
- package/dist/core/schema/aliases.d.mts +7 -0
- package/dist/core/schema/aliases.mjs +77 -0
- package/dist/core/schema/answer-parts.mjs +117 -0
- package/dist/core/schema/answers.d.mts +2109 -128
- package/dist/core/schema/answers.mjs +11 -113
- package/dist/core/schema/commands.d.mts +2 -2
- package/dist/core/schema/document.d.mts +1 -1
- package/dist/core/schema/drill-answers.mjs +179 -0
- package/dist/core/schema/drill.d.mts +260 -0
- package/dist/core/schema/drill.mjs +107 -0
- package/dist/core/schema/index.d.mts +6 -3
- package/dist/core/schema/index.mjs +6 -3
- package/dist/core/schema/picks.d.mts +35 -0
- package/dist/core/schema/picks.mjs +23 -0
- package/dist/core/schema/query.d.mts +82 -1
- package/dist/core/schema/query.mjs +9 -5
- package/dist/core/schema/tools.mjs +1 -1
- package/dist/core/schema/view.d.mts +0 -35
- package/dist/core/schema/view.mjs +19 -24
- package/dist/core/store/schema.d.mts +5 -0
- package/dist/core/store/schema.mjs +10 -0
- package/dist/core/types/api.d.mts +21 -1
- package/dist/core/types/api.mjs +1 -1
- package/dist/core/types/drill-api.d.mts +210 -0
- package/dist/core/types/options.d.mts +21 -0
- package/dist/core/types/viewer.d.mts +5 -0
- package/dist/cursor-seal.d.mts +8 -0
- package/dist/cursor-seal.mjs +98 -0
- package/dist/dashboard-id.mjs +1 -1
- package/dist/dashboards.mjs +4 -0
- package/dist/drill-access.mjs +93 -0
- package/dist/drill-change.mjs +76 -0
- package/dist/drill-crumbs.mjs +113 -0
- package/dist/drill-distinct.mjs +130 -0
- package/dist/drill-explain.mjs +64 -0
- package/dist/drill-identity.mjs +35 -0
- package/dist/drill-item-id.mjs +35 -0
- package/dist/drill-items.mjs +132 -0
- package/dist/drill-open.mjs +169 -0
- package/dist/drill-seal.mjs +170 -0
- package/dist/drill-secret.mjs +14 -0
- package/dist/drill-share.mjs +53 -0
- package/dist/drill-summary.mjs +158 -0
- package/dist/drill-targets.mjs +53 -0
- package/dist/drill-totals.mjs +8 -0
- package/dist/drill-views.mjs +175 -0
- package/dist/drill.mjs +157 -0
- package/dist/editing.mjs +3 -3
- package/dist/endpoints/dashboards.mjs +28 -5
- package/dist/endpoints/drill.mjs +55 -0
- package/dist/endpoints/index.d.mts +245 -4
- package/dist/endpoints/index.mjs +5 -1
- package/dist/endpoints/meta.mjs +10 -1
- package/dist/endpoints/query.mjs +26 -4
- package/dist/execute.d.mts +19 -1
- package/dist/execute.mjs +34 -27
- package/dist/handler.mjs +5 -1
- package/dist/index.d.mts +3 -1
- package/dist/index.mjs +29 -2
- package/dist/llms.txt +22 -0
- package/dist/mcp/drill.mjs +61 -0
- package/dist/mcp/format.mjs +69 -11
- package/dist/mcp/index.d.mts +15 -4
- package/dist/mcp/index.mjs +13 -7
- package/dist/mcp/query.d.mts +7 -2
- package/dist/mcp/query.mjs +40 -6
- package/dist/node-postgres.d.mts +8 -4
- package/dist/node-postgres.mjs +29 -6
- package/dist/owner.mjs +3 -3
- package/dist/postgres-connection.d.mts +8 -2
- package/dist/postgres-connection.mjs +2 -1
- package/dist/postgres-store.mjs +1 -0
- package/dist/react/Canvas.mjs +28 -5
- package/dist/react/Cell.mjs +2 -1
- package/dist/react/Dashboard.d.mts +10 -5
- package/dist/react/Dashboard.mjs +62 -22
- package/dist/react/DashboardList.mjs +52 -24
- package/dist/react/DashboardListRow.mjs +49 -0
- package/dist/react/Discard.mjs +12 -4
- package/dist/react/Editing.mjs +19 -5
- package/dist/react/FilterBar.d.mts +7 -9
- package/dist/react/Grid.mjs +19 -24
- package/dist/react/Header.mjs +10 -1
- package/dist/react/History.mjs +5 -0
- package/dist/react/Parts.mjs +6 -2
- package/dist/react/ReadFailed.mjs +23 -0
- package/dist/react/ReviewPanel.mjs +13 -2
- package/dist/react/RowBody.d.mts +19 -0
- package/dist/react/RowBody.mjs +55 -0
- package/dist/react/Spaces.mjs +15 -1
- package/dist/react/Tile.d.mts +3 -1
- package/dist/react/Tile.mjs +84 -32
- package/dist/react/_virtual/_rolldown/runtime.mjs +12 -0
- package/dist/react/arrange-steps.mjs +16 -13
- package/dist/react/arrange.mjs +10 -3
- package/dist/react/builder/Calcs.mjs +35 -11
- package/dist/react/builder/FilterEditor.mjs +196 -0
- package/dist/react/builder/Filters.mjs +172 -116
- package/dist/react/builder/Formula.mjs +82 -0
- package/dist/react/builder/Groups.mjs +1 -0
- package/dist/react/builder/Preview.mjs +1 -1
- package/dist/react/builder/RangeEditor.mjs +184 -0
- package/dist/react/builder/Sections.mjs +41 -6
- package/dist/react/builder/Time.mjs +138 -60
- package/dist/react/builder/Values.mjs +85 -0
- package/dist/react/builder/WidgetBuilder.mjs +50 -49
- package/dist/react/builder/base.d.mts +5 -0
- package/dist/react/builder/base.mjs +33 -8
- package/dist/react/builder/calc.mjs +15 -1
- package/dist/react/builder/escape.d.mts +1 -0
- package/dist/react/builder/escape.mjs +62 -0
- package/dist/react/builder/filter-tree.d.mts +9 -0
- package/dist/react/builder/filter-tree.mjs +186 -0
- package/dist/react/builder/formula-parse.d.mts +18 -0
- package/dist/react/builder/formula-parse.mjs +136 -0
- package/dist/react/builder/labels.d.mts +1 -1
- package/dist/react/builder/state.d.mts +2 -1
- package/dist/react/builder/state.mjs +23 -37
- package/dist/react/builder/useBuilder.d.mts +12 -1
- package/dist/react/builder/useBuilder.mjs +6 -3
- package/dist/react/builder/words.d.mts +59 -0
- package/dist/react/builder/words.mjs +73 -0
- package/dist/react/client.d.mts +2 -0
- package/dist/react/client.mjs +1 -0
- package/dist/react/context.d.mts +28 -1
- package/dist/react/context.mjs +25 -4
- package/dist/react/drill/Bars.mjs +179 -0
- package/dist/react/drill/Controls.mjs +173 -0
- package/dist/react/drill/DashboardTile.d.mts +18 -0
- package/dist/react/drill/DashboardTile.mjs +107 -0
- package/dist/react/drill/DrillPanel.d.mts +31 -0
- package/dist/react/drill/DrillPanel.mjs +104 -0
- package/dist/react/drill/DrillTabs.mjs +52 -0
- package/dist/react/drill/Header.mjs +131 -0
- package/dist/react/drill/Items.d.mts +22 -0
- package/dist/react/drill/Items.mjs +164 -0
- package/dist/react/drill/ItemsFoot.mjs +60 -0
- package/dist/react/drill/Notes.mjs +58 -0
- package/dist/react/drill/PanelBody.mjs +74 -0
- package/dist/react/drill/PanelStates.mjs +37 -0
- package/dist/react/drill/board.d.mts +18 -0
- package/dist/react/drill/board.mjs +91 -0
- package/dist/react/drill/counts.mjs +14 -0
- package/dist/react/drill/host.mjs +26 -0
- package/dist/react/drill/item-parts.d.mts +5 -0
- package/dist/react/drill/item-parts.mjs +76 -0
- package/dist/react/drill/item-type.d.mts +26 -0
- package/dist/react/drill/item-type.mjs +7 -0
- package/dist/react/drill/lazy.d.mts +6 -0
- package/dist/react/drill/lazy.mjs +17 -0
- package/dist/react/drill/pages.mjs +48 -0
- package/dist/react/drill/panel-focus.mjs +51 -0
- package/dist/react/drill/props.d.mts +25 -0
- package/dist/react/drill/props.mjs +72 -0
- package/dist/react/drill/state.d.mts +18 -0
- package/dist/react/drill/state.mjs +68 -0
- package/dist/react/drill/useDrill.d.mts +46 -0
- package/dist/react/drill/useDrill.mjs +194 -0
- package/dist/react/drill/words.d.mts +101 -0
- package/dist/react/drill/words.mjs +79 -0
- package/dist/react/english.mjs +29 -6
- package/dist/react/filters/bar.d.mts +12 -0
- package/dist/react/filters/bar.mjs +135 -0
- package/dist/react/filters/calendar.mjs +193 -0
- package/dist/react/filters/chip.mjs +174 -0
- package/dist/react/filters/context.d.mts +22 -0
- package/dist/react/filters/context.mjs +78 -0
- package/dist/react/filters/dates.mjs +89 -0
- package/dist/react/filters/days.d.mts +36 -0
- package/dist/react/filters/days.mjs +87 -0
- package/dist/react/filters/field-step.mjs +81 -0
- package/dist/react/filters/hooks.d.mts +34 -0
- package/dist/react/filters/hooks.mjs +94 -0
- package/dist/react/filters/listbox.mjs +131 -0
- package/dist/react/filters/logic.d.mts +59 -0
- package/dist/react/filters/logic.mjs +109 -0
- package/dist/react/filters/menu-search.mjs +211 -0
- package/dist/react/filters/menu.mjs +85 -0
- package/dist/react/filters/number.mjs +127 -0
- package/dist/react/filters/numbers.d.mts +47 -0
- package/dist/react/filters/numbers.mjs +151 -0
- package/dist/react/filters/operator.mjs +104 -0
- package/dist/react/filters/phone.mjs +104 -0
- package/dist/react/filters/popover.d.mts +19 -0
- package/dist/react/filters/popover.mjs +153 -0
- package/dist/react/filters/rules.d.mts +8 -0
- package/dist/react/filters/rules.mjs +55 -0
- package/dist/react/filters/sheet.mjs +196 -0
- package/dist/react/filters/state.mjs +132 -0
- package/dist/react/filters/suggest.d.mts +47 -0
- package/dist/react/filters/suggest.mjs +97 -0
- package/dist/react/filters/values.mjs +166 -0
- package/dist/react/filters/words.d.mts +81 -0
- package/dist/react/filters/words.mjs +73 -0
- package/dist/react/format.d.mts +3 -3
- package/dist/react/format.mjs +45 -16
- package/dist/react/index.d.mts +34 -5
- package/dist/react/index.mjs +15 -4
- package/dist/react/messages.d.mts +47 -2
- package/dist/react/place.mjs +7 -3
- package/dist/react/renderers/index.d.mts +10 -2
- package/dist/react/renderers/index.mjs +116 -14
- package/dist/react/renderers/list.d.mts +22 -0
- package/dist/react/renderers/list.mjs +118 -0
- package/dist/react/review-words.mjs +1 -1
- package/dist/react/review.mjs +2 -2
- package/dist/react/scope.mjs +2 -7
- package/dist/react/series.d.mts +54 -0
- package/dist/react/series.mjs +191 -0
- package/dist/react/theme.css +2734 -1296
- package/dist/react/theme.css.d.ts +1 -0
- package/dist/react/tile-store.mjs +15 -3
- package/dist/react/useBoard.d.mts +7 -0
- package/dist/react/useBoard.mjs +4 -1
- package/dist/react/useDashboard.d.mts +4 -0
- package/dist/react/useDashboard.mjs +8 -1
- package/dist/react/useTiles.mjs +50 -15
- package/dist/react/view-grid.mjs +16 -0
- package/dist/react/view.d.mts +9 -0
- package/dist/react/view.mjs +22 -10
- package/dist/reading.d.mts +2 -0
- package/dist/reading.mjs +9 -1
- package/dist/recharts/axes.mjs +28 -0
- package/dist/recharts/axis.mjs +84 -0
- package/dist/recharts/bar.d.mts +7 -0
- package/dist/recharts/bar.mjs +140 -0
- package/dist/recharts/charts.d.mts +4 -6
- package/dist/recharts/charts.mjs +2 -114
- package/dist/recharts/index.d.mts +5 -3
- package/dist/recharts/index.mjs +6 -3
- package/dist/recharts/line.d.mts +2 -0
- package/dist/recharts/line.mjs +111 -0
- package/dist/recharts/marks.mjs +112 -0
- package/dist/recharts/parts.mjs +88 -0
- package/dist/recharts/series.d.mts +2 -22
- package/dist/recharts/series.mjs +1 -64
- package/dist/recharts/table.mjs +71 -0
- package/dist/records-read.mjs +44 -0
- package/dist/registry/filter-bar.json +26 -0
- package/dist/skills/derive-dashboards/SKILL.md +69 -0
- package/dist/store/apply.mjs +5 -5
- package/dist/store/commands.mjs +4 -4
- package/dist/store/discard.mjs +7 -7
- package/dist/store/drafts.mjs +5 -5
- package/dist/store/editor.mjs +1 -1
- package/dist/store/publish.mjs +12 -12
- package/dist/store/reader.mjs +41 -22
- package/dist/store/review.mjs +16 -16
- package/dist/store/rows.mjs +20 -20
- package/dist/store/undo.mjs +3 -3
- package/dist/store/unpublished.mjs +4 -4
- package/dist/store/versions.mjs +14 -14
- package/dist/testing/adapter-suite.mjs +4 -4
- package/dist/tiles.mjs +37 -3
- package/package.json +12 -10
- package/dist/react/FilterBar.mjs +0 -119
- package/dist/react/Records.d.mts +0 -12
- package/dist/react/Records.mjs +0 -97
- package/src/adapter/sql-adapter.ts +0 -95
- package/src/adapter/statement.ts +0 -185
- package/src/authorize.ts +0 -73
- package/src/body.ts +0 -169
- package/src/bounded.ts +0 -43
- package/src/cache.ts +0 -192
- package/src/checks.ts +0 -170
- package/src/custom-source.ts +0 -59
- package/src/dashboard-id.ts +0 -36
- package/src/dashboards.ts +0 -132
- package/src/editing.ts +0 -148
- package/src/endpoints/access.ts +0 -41
- package/src/endpoints/dashboards.ts +0 -92
- package/src/endpoints/drafts.ts +0 -138
- package/src/endpoints/index.ts +0 -21
- package/src/endpoints/meta.ts +0 -137
- package/src/endpoints/query.ts +0 -86
- package/src/endpoints/read.ts +0 -92
- package/src/endpoints/review.ts +0 -89
- package/src/endpoints/values.ts +0 -62
- package/src/endpoints/versions.ts +0 -104
- package/src/execute.ts +0 -207
- package/src/handler.ts +0 -232
- package/src/http.ts +0 -66
- package/src/index.ts +0 -163
- package/src/introspect.ts +0 -205
- package/src/mcp/api.ts +0 -59
- package/src/mcp/format.ts +0 -67
- package/src/mcp/index.ts +0 -151
- package/src/mcp/query.ts +0 -153
- package/src/mcp/resolve.ts +0 -114
- package/src/node-postgres.ts +0 -81
- package/src/owner.ts +0 -36
- package/src/postgres-connection.ts +0 -143
- package/src/postgres-store.ts +0 -82
- package/src/principal.ts +0 -39
- package/src/reading.ts +0 -56
- package/src/request-rows.ts +0 -23
- package/src/request.ts +0 -22
- package/src/storage.ts +0 -63
- package/src/store/apply.ts +0 -129
- package/src/store/commands.ts +0 -75
- package/src/store/conflicts.ts +0 -98
- package/src/store/create-tables.ts +0 -104
- package/src/store/discard.ts +0 -42
- package/src/store/drafts.ts +0 -133
- package/src/store/editor.ts +0 -28
- package/src/store/log.ts +0 -28
- package/src/store/publish.ts +0 -199
- package/src/store/publisher.ts +0 -21
- package/src/store/reader.ts +0 -139
- package/src/store/review.ts +0 -239
- package/src/store/rows.ts +0 -250
- package/src/store/tenant.ts +0 -28
- package/src/store/undo.ts +0 -56
- package/src/store/unpublished.ts +0 -33
- package/src/store/versions.ts +0 -106
- package/src/store/writer.ts +0 -19
- package/src/testing/adapter-suite.ts +0 -239
- package/src/testing/index.ts +0 -2
- package/src/tile-doc.ts +0 -46
- package/src/tiles.ts +0 -120
package/src/store/undo.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { type Author, type DashAdapter, reduceAll, type Tenant } from "@derive-to/dashboards-core";
|
|
2
|
-
import { HttpError } from "../http";
|
|
3
|
-
import { keptColumns, keptCommands } from "./commands";
|
|
4
|
-
import {
|
|
5
|
-
advanceDraft,
|
|
6
|
-
baseDoc,
|
|
7
|
-
draftOf,
|
|
8
|
-
expectRevision,
|
|
9
|
-
inTransaction,
|
|
10
|
-
now,
|
|
11
|
-
openDraft,
|
|
12
|
-
} from "./rows";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Steps the author's draft back by its last edit: the draft is the document it started from
|
|
16
|
-
* with its commands applied, so it is rebuilt without that edit's commands. They come back,
|
|
17
|
-
* so a client can apply them again as Redo. The draft's revision moves on, as for any edit.
|
|
18
|
-
*/
|
|
19
|
-
export function undo(
|
|
20
|
-
db: DashAdapter,
|
|
21
|
-
a: { tenant: Tenant; id: string; author: Author; expectedRevision: number },
|
|
22
|
-
) {
|
|
23
|
-
return inTransaction(db, async (tx) => {
|
|
24
|
-
const { row } = await openDraft(tx, a);
|
|
25
|
-
expectRevision(row, a.expectedRevision);
|
|
26
|
-
const base = baseDoc(row);
|
|
27
|
-
const kept = await keptCommands(tx, row["id"]);
|
|
28
|
-
if (kept.length === 0)
|
|
29
|
-
throw new HttpError(409, "NOTHING_TO_UNDO", "The draft has no edits left to undo.");
|
|
30
|
-
const last = Math.max(...kept.map((k) => k.revision));
|
|
31
|
-
const remaining = kept.filter((k) => k.revision !== last);
|
|
32
|
-
// The same commands on the same document applied before, in this order, so they apply again.
|
|
33
|
-
const outcome = reduceAll(
|
|
34
|
-
base,
|
|
35
|
-
remaining.map((k) => k.body),
|
|
36
|
-
);
|
|
37
|
-
if (!outcome.ok)
|
|
38
|
-
throw new HttpError(409, "EDIT_REFUSED", outcome.issue.message, {
|
|
39
|
-
issues: [outcome.issue],
|
|
40
|
-
});
|
|
41
|
-
const revision = Number(row["revision"]) + 1;
|
|
42
|
-
const set = { spec: outcome.doc, revision, ...keptColumns(remaining), updated_at: now() };
|
|
43
|
-
await advanceDraft(tx, row, set);
|
|
44
|
-
await tx.deleteMany({
|
|
45
|
-
table: "dd_dashboard_draft_command",
|
|
46
|
-
where: [
|
|
47
|
-
{ field: "draft_id", value: row["id"] },
|
|
48
|
-
{ field: "revision", value: last },
|
|
49
|
-
],
|
|
50
|
-
});
|
|
51
|
-
return {
|
|
52
|
-
draft: draftOf({ ...row, ...set }),
|
|
53
|
-
undone: kept.filter((k) => k.revision === last).map((k) => k.body),
|
|
54
|
-
};
|
|
55
|
-
});
|
|
56
|
-
}
|
package/src/store/unpublished.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { Author, DashAdapter, Tenant, UnpublishedDraft } from "@derive-to/dashboards-core";
|
|
2
|
-
import { dashboardRow, draftAuthorId, draftDoc } from "./rows";
|
|
3
|
-
import { tenantHash } from "./tenant";
|
|
4
|
-
|
|
5
|
-
/** The author's open drafts of dashboards nobody has published yet, newest first. */
|
|
6
|
-
export async function unpublished(
|
|
7
|
-
db: DashAdapter,
|
|
8
|
-
a: { tenant: Tenant; author: Author },
|
|
9
|
-
): Promise<UnpublishedDraft[]> {
|
|
10
|
-
const drafts = await db.findMany({
|
|
11
|
-
table: "dd_dashboard_draft",
|
|
12
|
-
where: [
|
|
13
|
-
{ field: "tenant_hash", value: await tenantHash(a.tenant) },
|
|
14
|
-
{ field: "author_kind", value: a.author.kind ?? "user" },
|
|
15
|
-
{ field: "author_id", value: draftAuthorId(a.author) },
|
|
16
|
-
{ field: "state", value: "open" },
|
|
17
|
-
// A draft of a published dashboard started from a version; a new one's from none.
|
|
18
|
-
{ field: "base_version", value: 0 },
|
|
19
|
-
],
|
|
20
|
-
orderBy: [{ field: "updated_at", direction: "desc" }],
|
|
21
|
-
limit: 100,
|
|
22
|
-
});
|
|
23
|
-
const rows = await Promise.all(
|
|
24
|
-
drafts.map((d) => dashboardRow(db, a.tenant, { id: String(d["dashboard_id"]) })),
|
|
25
|
-
);
|
|
26
|
-
return drafts.flatMap((d, i) => {
|
|
27
|
-
const row = rows[i];
|
|
28
|
-
if (row === null || row === undefined || row["current_version"] !== null) return [];
|
|
29
|
-
// A built-in's copy keeps serving the built-in until published; it is listed as that.
|
|
30
|
-
if (row["default_of"] !== null) return [];
|
|
31
|
-
return [{ dashboardId: String(row["id"]), title: draftDoc(d).meta.title }];
|
|
32
|
-
});
|
|
33
|
-
}
|
package/src/store/versions.ts
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Author,
|
|
3
|
-
DashAdapter,
|
|
4
|
-
Published,
|
|
5
|
-
Tenant,
|
|
6
|
-
VersionEntry,
|
|
7
|
-
} from "@derive-to/dashboards-core";
|
|
8
|
-
import { HttpError } from "../http";
|
|
9
|
-
import { advanceHead, earlier, insertVersion } from "./publish";
|
|
10
|
-
import { dashboardRow, documentOf, headDoc, inTransaction } from "./rows";
|
|
11
|
-
|
|
12
|
-
const PAGE = 50;
|
|
13
|
-
|
|
14
|
-
/** A tenant's dashboard row, or a refusal that names it. */
|
|
15
|
-
async function existing(db: DashAdapter, tenant: Tenant, id: string) {
|
|
16
|
-
const row = await dashboardRow(db, tenant, { id });
|
|
17
|
-
if (row === null) throw new HttpError(404, "NOT_FOUND", `There is no dashboard "${id}".`);
|
|
18
|
-
return row;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const optional = (value: unknown) =>
|
|
22
|
-
value === null || value === undefined ? undefined : Number(value);
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* A page of the dashboard's published versions, newest first, before a version when given:
|
|
26
|
-
* who made each, when, and from what.
|
|
27
|
-
*/
|
|
28
|
-
export async function versions(
|
|
29
|
-
db: DashAdapter,
|
|
30
|
-
a: { tenant: Tenant; id: string; before?: number; limit?: number },
|
|
31
|
-
): Promise<VersionEntry[]> {
|
|
32
|
-
const row = await existing(db, a.tenant, a.id);
|
|
33
|
-
// A dashboard nobody has published has no history, and is only its maker's: to anyone
|
|
34
|
-
// asking for its history it does not exist, as it does not to /dashboards/get.
|
|
35
|
-
if (row["current_version"] === null)
|
|
36
|
-
throw new HttpError(404, "NOT_FOUND", `There is no dashboard "${a.id}".`);
|
|
37
|
-
const rows = await db.findMany({
|
|
38
|
-
table: "dd_dashboard_version",
|
|
39
|
-
where: [
|
|
40
|
-
{ field: "dashboard_id", value: row["id"] },
|
|
41
|
-
...(a.before === undefined ? [] : [{ field: "n", op: "lt" as const, value: a.before }]),
|
|
42
|
-
],
|
|
43
|
-
orderBy: [{ field: "n", direction: "desc" }],
|
|
44
|
-
limit: Math.min(a.limit ?? PAGE, PAGE),
|
|
45
|
-
});
|
|
46
|
-
return rows.map((v) => {
|
|
47
|
-
const basedOn = optional(v["based_on"]);
|
|
48
|
-
const restoredFrom = optional(v["restored_from"]);
|
|
49
|
-
const kind = v["author_kind"] === "agent" ? "agent" : "user";
|
|
50
|
-
return {
|
|
51
|
-
version: Number(v["n"]),
|
|
52
|
-
author: { id: String(v["author_id"]), kind },
|
|
53
|
-
createdAt: String(v["created_at"]),
|
|
54
|
-
...(basedOn === undefined ? {} : { basedOn }),
|
|
55
|
-
...(restoredFrom === undefined ? {} : { restoredFrom }),
|
|
56
|
-
};
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Publishes an old version's document as the next version. The versions between stay in the
|
|
62
|
-
* history, and open drafts replay onto it when they publish, as onto any newer version.
|
|
63
|
-
*/
|
|
64
|
-
export function restore(
|
|
65
|
-
db: DashAdapter,
|
|
66
|
-
a: {
|
|
67
|
-
tenant: Tenant;
|
|
68
|
-
id: string;
|
|
69
|
-
author: Author;
|
|
70
|
-
version: number;
|
|
71
|
-
requestId: string;
|
|
72
|
-
/** The current version the caller checked the restore against; another one refuses it. */
|
|
73
|
-
expectedHead: number;
|
|
74
|
-
},
|
|
75
|
-
): Promise<Published> {
|
|
76
|
-
return inTransaction(db, async (tx) => {
|
|
77
|
-
const row = await existing(tx, a.tenant, a.id);
|
|
78
|
-
const done = await earlier(tx, row["id"], a.requestId, a.author);
|
|
79
|
-
if (done !== null) return { dashboardId: a.id, version: Number(done["n"]) };
|
|
80
|
-
const old = await tx.findOne({
|
|
81
|
-
table: "dd_dashboard_version",
|
|
82
|
-
where: [
|
|
83
|
-
{ field: "dashboard_id", value: row["id"] },
|
|
84
|
-
{ field: "n", value: a.version },
|
|
85
|
-
],
|
|
86
|
-
});
|
|
87
|
-
if (old === null)
|
|
88
|
-
throw new HttpError(404, "NOT_FOUND", `There is no version ${a.version} of "${a.id}".`);
|
|
89
|
-
const head = await headDoc(tx, row);
|
|
90
|
-
if (head.version !== a.expectedHead)
|
|
91
|
-
throw new HttpError(409, "VERSION_CONFLICT", "The dashboard changed. Reload its history.");
|
|
92
|
-
if (head.version === a.version)
|
|
93
|
-
throw new HttpError(409, "VERSION_CONFLICT", `Version ${a.version} is already current.`);
|
|
94
|
-
const doc = documentOf(old["spec"], `version ${a.version} of "${a.id}"`);
|
|
95
|
-
await insertVersion(tx, {
|
|
96
|
-
...a,
|
|
97
|
-
dashboardId: row["id"],
|
|
98
|
-
n: head.version + 1,
|
|
99
|
-
doc,
|
|
100
|
-
basedOn: head.version,
|
|
101
|
-
restoredFrom: a.version,
|
|
102
|
-
});
|
|
103
|
-
await advanceHead(tx, row["id"], head.version, doc);
|
|
104
|
-
return { dashboardId: a.id, version: head.version + 1 };
|
|
105
|
-
});
|
|
106
|
-
}
|
package/src/store/writer.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { DashAdapter, DashboardStore } from "@derive-to/dashboards-core";
|
|
2
|
-
import { draftMethods } from "./editor";
|
|
3
|
-
import { publishMethods } from "./publisher";
|
|
4
|
-
import { createReader } from "./reader";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* The store over any adapter: the read path, drafts, and publishing. Every write is one
|
|
8
|
-
* transaction on the adapter; every lookup is by the tenant's hash and checks the stored tenant.
|
|
9
|
-
*/
|
|
10
|
-
export function createStore(db: DashAdapter): DashboardStore {
|
|
11
|
-
const reader = createReader(db);
|
|
12
|
-
return {
|
|
13
|
-
get: (a) => reader.get(a),
|
|
14
|
-
list: (a) => reader.list(a),
|
|
15
|
-
getVersion: (a) => reader.getVersion(a),
|
|
16
|
-
...draftMethods(db),
|
|
17
|
-
...publishMethods(db),
|
|
18
|
-
};
|
|
19
|
-
}
|
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type DashAdapter,
|
|
3
|
-
emptyDoc,
|
|
4
|
-
type Row,
|
|
5
|
-
TABLES,
|
|
6
|
-
type TableName,
|
|
7
|
-
} from "@derive-to/dashboards-core";
|
|
8
|
-
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
9
|
-
import { createTables } from "../store/create-tables";
|
|
10
|
-
import { createReader } from "../store/reader";
|
|
11
|
-
import { tenantHash } from "../store/tenant";
|
|
12
|
-
|
|
13
|
-
/** A fresh, empty database behind the adapter under test. */
|
|
14
|
-
export type OpenAdapter = () => Promise<{ adapter: DashAdapter; close: () => Promise<void> }>;
|
|
15
|
-
|
|
16
|
-
const ACME = { org: "acme" };
|
|
17
|
-
const GLOBEX = { org: "globex" };
|
|
18
|
-
const NOW = "2026-11-12T10:00:00.000Z";
|
|
19
|
-
|
|
20
|
-
async function seedDashboard(
|
|
21
|
-
adapter: DashAdapter,
|
|
22
|
-
id: string,
|
|
23
|
-
tenant: Record<string, string>,
|
|
24
|
-
extra: Record<string, unknown> = {},
|
|
25
|
-
) {
|
|
26
|
-
const hash = await tenantHash(tenant);
|
|
27
|
-
await adapter.create({
|
|
28
|
-
table: "dd_dashboard",
|
|
29
|
-
data: {
|
|
30
|
-
id,
|
|
31
|
-
tenant,
|
|
32
|
-
tenant_hash: hash,
|
|
33
|
-
key: null,
|
|
34
|
-
title: `Title ${id}`,
|
|
35
|
-
description: null,
|
|
36
|
-
owner_id: "u1",
|
|
37
|
-
owner_kind: "user",
|
|
38
|
-
current_version: 1,
|
|
39
|
-
default_of: null,
|
|
40
|
-
archived_at: null,
|
|
41
|
-
created_at: NOW,
|
|
42
|
-
updated_at: NOW,
|
|
43
|
-
...extra,
|
|
44
|
-
},
|
|
45
|
-
});
|
|
46
|
-
await adapter.create({
|
|
47
|
-
table: "dd_dashboard_version",
|
|
48
|
-
data: {
|
|
49
|
-
id: `${id}-v1`,
|
|
50
|
-
dashboard_id: id,
|
|
51
|
-
tenant_hash: hash,
|
|
52
|
-
n: 1,
|
|
53
|
-
spec: emptyDoc(`Title ${id}`),
|
|
54
|
-
spec_version: 1,
|
|
55
|
-
author_id: "u1",
|
|
56
|
-
author_kind: "user",
|
|
57
|
-
request_id: `${id}-r1`,
|
|
58
|
-
message: null,
|
|
59
|
-
created_at: NOW,
|
|
60
|
-
},
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
type Get = () => DashAdapter;
|
|
65
|
-
|
|
66
|
-
/** The schema: every table made, and making it again changes nothing. */
|
|
67
|
-
function schemaCases(get: Get): void {
|
|
68
|
-
it("makes every table, and is safe to run twice at once", async () => {
|
|
69
|
-
await Promise.all([createTables(get()), createTables(get())]);
|
|
70
|
-
// The table names are the keys of TABLES.
|
|
71
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
|
|
72
|
-
const names = Object.keys(TABLES) as (keyof typeof TABLES)[];
|
|
73
|
-
const counts = await Promise.all(names.map((table) => get().count({ table })));
|
|
74
|
-
expect(counts.every((n) => n === 0)).toBe(true);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("reads every column of the dd_ tables from the catalog", async () => {
|
|
78
|
-
const read = await get().schema.columns();
|
|
79
|
-
const want = Object.entries(TABLES).flatMap(([table, def]) =>
|
|
80
|
-
Object.keys(def.fields).map((column) => `${table}.${column}`),
|
|
81
|
-
);
|
|
82
|
-
expect(read.map((c) => `${c.table}.${c.column}`).toSorted()).toEqual(want.toSorted());
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** Rows: typed reads and writes, and every operator. */
|
|
87
|
-
function rowCases(get: Get): void {
|
|
88
|
-
it("writes and reads rows with their types: integers, JSON, timestamps and nulls", async () => {
|
|
89
|
-
await seedDashboard(get(), "d1", ACME, { key: "ops" });
|
|
90
|
-
const row = await get().findOne({
|
|
91
|
-
table: "dd_dashboard",
|
|
92
|
-
where: [{ field: "id", value: "d1" }],
|
|
93
|
-
});
|
|
94
|
-
expect(row).toMatchObject({
|
|
95
|
-
id: "d1",
|
|
96
|
-
tenant: ACME,
|
|
97
|
-
current_version: 1,
|
|
98
|
-
created_at: NOW,
|
|
99
|
-
archived_at: null,
|
|
100
|
-
key: "ops",
|
|
101
|
-
});
|
|
102
|
-
const changed = await get().updateMany({
|
|
103
|
-
table: "dd_dashboard",
|
|
104
|
-
where: [{ field: "id", value: "d1" }],
|
|
105
|
-
set: { title: "Ops" },
|
|
106
|
-
});
|
|
107
|
-
expect(changed).toBe(1);
|
|
108
|
-
expect(
|
|
109
|
-
await get().count({ table: "dd_dashboard", where: [{ field: "title", value: "Ops" }] }),
|
|
110
|
-
).toBe(1);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it("orders, limits and filters with every operator", async () => {
|
|
114
|
-
await seedDashboard(get(), "d2", ACME);
|
|
115
|
-
await seedDashboard(get(), "d3", ACME, { archived_at: NOW });
|
|
116
|
-
const ids = await get().findMany({
|
|
117
|
-
table: "dd_dashboard",
|
|
118
|
-
where: [
|
|
119
|
-
{ field: "id", op: "in", value: ["d1", "d2", "d3"] },
|
|
120
|
-
{ field: "archived_at", op: "isNull" },
|
|
121
|
-
],
|
|
122
|
-
orderBy: [{ field: "id", direction: "desc" }],
|
|
123
|
-
limit: 1,
|
|
124
|
-
offset: 1,
|
|
125
|
-
});
|
|
126
|
-
expect(ids.map((r) => r["id"])).toEqual(["d1"]);
|
|
127
|
-
expect(
|
|
128
|
-
await get().count({
|
|
129
|
-
table: "dd_dashboard",
|
|
130
|
-
where: [{ field: "id", op: "in", value: [] }],
|
|
131
|
-
}),
|
|
132
|
-
).toBe(0);
|
|
133
|
-
expect(
|
|
134
|
-
await get().deleteMany({ table: "dd_dashboard", where: [{ field: "id", value: "d3" }] }),
|
|
135
|
-
).toBe(1);
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/** Transactions roll back whole, and a unique violation is told apart from anything else. */
|
|
140
|
-
/** An adapter's createMany, or create row by row where it has none: either writes the same rows. */
|
|
141
|
-
const createAll =
|
|
142
|
-
(adapter: DashAdapter) =>
|
|
143
|
-
async (a: { table: TableName; rows: readonly Row[] }): Promise<void> => {
|
|
144
|
-
if (adapter.createMany !== undefined) return adapter.createMany(a);
|
|
145
|
-
// oxlint-disable-next-line no-await-in-loop -- one row at a time, as create writes
|
|
146
|
-
for (const data of a.rows) await adapter.create({ table: a.table, data });
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
function batchCases(get: Get): void {
|
|
150
|
-
it("writes many rows in one call, when it can, each as create would", async () => {
|
|
151
|
-
const rows = [1, 2, 3].map((n) => ({
|
|
152
|
-
draft_id: "dr",
|
|
153
|
-
command_id: `c${String(n)}`,
|
|
154
|
-
digest: "x",
|
|
155
|
-
body: { type: "meta.set", title: `T${String(n)}` },
|
|
156
|
-
revision: 1,
|
|
157
|
-
seq: n,
|
|
158
|
-
created_at: NOW,
|
|
159
|
-
}));
|
|
160
|
-
const adapter = get();
|
|
161
|
-
await createAll(adapter)({ table: "dd_dashboard_draft_command", rows });
|
|
162
|
-
const read = await adapter.findMany({
|
|
163
|
-
table: "dd_dashboard_draft_command",
|
|
164
|
-
where: [{ field: "draft_id", value: "dr" }],
|
|
165
|
-
orderBy: [{ field: "seq", direction: "asc" }],
|
|
166
|
-
});
|
|
167
|
-
expect(read.map((r) => [r["command_id"], r["body"], r["seq"]])).toEqual(
|
|
168
|
-
rows.map((r) => [r.command_id, r.body, r.seq]),
|
|
169
|
-
);
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function transactionCases(get: Get): void {
|
|
174
|
-
it("rolls a transaction back whole and tells a unique violation apart", async () => {
|
|
175
|
-
const failing = get().transaction(async (tx) => {
|
|
176
|
-
await seedDashboard(tx, "d9", ACME);
|
|
177
|
-
throw new Error("stop");
|
|
178
|
-
});
|
|
179
|
-
await expect(failing).rejects.toThrow("stop");
|
|
180
|
-
expect(
|
|
181
|
-
await get().findOne({ table: "dd_dashboard", where: [{ field: "id", value: "d9" }] }),
|
|
182
|
-
).toBeNull();
|
|
183
|
-
const duplicate = await seedDashboard(get(), "d1", ACME).catch((error: unknown) => error);
|
|
184
|
-
expect(get().classifyError(duplicate)).toBe("unique");
|
|
185
|
-
expect(get().classifyError(new Error("other"))).toBe("other");
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/** The read path: a tenant's dashboards and versions, never another tenant's; then an upgrade of the populated database. */
|
|
190
|
-
function readerCases(get: Get): void {
|
|
191
|
-
it("reads a tenant's dashboards and versions, and never another tenant's", async () => {
|
|
192
|
-
await seedDashboard(get(), "g1", GLOBEX, { key: "ops" });
|
|
193
|
-
const reader = createReader(get());
|
|
194
|
-
expect(await reader.get({ tenant: ACME, ref: { key: "ops" } })).toMatchObject({
|
|
195
|
-
id: "d1",
|
|
196
|
-
title: "Ops",
|
|
197
|
-
currentVersion: 1,
|
|
198
|
-
});
|
|
199
|
-
expect(await reader.get({ tenant: GLOBEX, ref: { id: "d1" } })).toBeNull();
|
|
200
|
-
expect(await reader.get({ tenant: GLOBEX, ref: { key: "ops" } })).toMatchObject({ id: "g1" });
|
|
201
|
-
const page = await reader.list({ tenant: ACME, limit: 1 });
|
|
202
|
-
expect(page.items.map((d) => d.id)).toEqual(["d1"]);
|
|
203
|
-
expect(page.next).toBe("1");
|
|
204
|
-
expect(await reader.getVersion({ tenant: ACME, id: "d1", version: 1 })).toMatchObject({
|
|
205
|
-
version: 1,
|
|
206
|
-
doc: { meta: { title: "Title d1" } },
|
|
207
|
-
});
|
|
208
|
-
expect(await reader.getVersion({ tenant: GLOBEX, id: "d1", version: 1 })).toBeNull();
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
it("keeps every row when the tables are made again", async () => {
|
|
212
|
-
const before = await get().count({ table: "dd_dashboard" });
|
|
213
|
-
await createTables(get());
|
|
214
|
-
expect(await get().count({ table: "dd_dashboard" })).toBe(before);
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* The contract every store adapter must meet, run against a real database: the schema made
|
|
220
|
-
* from one definition, typed reads and writes, atomic transactions, error classes, and the dashboard read
|
|
221
|
-
* path with tenant isolation. A new adapter calls this from its own tests.
|
|
222
|
-
*/
|
|
223
|
-
export function adapterSuite(name: string, open: OpenAdapter): void {
|
|
224
|
-
// DDL on an embedded database is slow on a shared CI runner.
|
|
225
|
-
describe(`store adapter: ${name}`, { timeout: 30_000 }, () => {
|
|
226
|
-
let adapter: DashAdapter;
|
|
227
|
-
let close: () => Promise<void>;
|
|
228
|
-
beforeAll(async () => {
|
|
229
|
-
({ adapter, close } = await open());
|
|
230
|
-
}, 30_000);
|
|
231
|
-
afterAll(() => close());
|
|
232
|
-
const get = () => adapter;
|
|
233
|
-
schemaCases(get);
|
|
234
|
-
rowCases(get);
|
|
235
|
-
batchCases(get);
|
|
236
|
-
transactionCases(get);
|
|
237
|
-
readerCases(get);
|
|
238
|
-
});
|
|
239
|
-
}
|
package/src/testing/index.ts
DELETED
package/src/tile-doc.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import type { Picks, Principal } from "@derive-to/dashboards-core";
|
|
2
|
-
import { versionDoc } from "./checks";
|
|
3
|
-
import { loadDashboard } from "./dashboards";
|
|
4
|
-
import { draftDoc } from "./editing";
|
|
5
|
-
import type { Runtime } from "./execute";
|
|
6
|
-
import { HttpError } from "./http";
|
|
7
|
-
import type { userOf } from "./request";
|
|
8
|
-
|
|
9
|
-
/** Which document a tile request runs against, and who may ask for each. */
|
|
10
|
-
|
|
11
|
-
export type TileCtx = {
|
|
12
|
-
dashboardId: string;
|
|
13
|
-
version?: number;
|
|
14
|
-
picks?: Picks;
|
|
15
|
-
draft?: true;
|
|
16
|
-
merged?: true;
|
|
17
|
-
at?: number;
|
|
18
|
-
principal: Principal | null;
|
|
19
|
-
request: Request;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* The document tiles run against: the viewer's open draft, what publishing it would make now,
|
|
24
|
-
* an earlier version being previewed, or else the pinned current version. All but the last
|
|
25
|
-
* need `edit`.
|
|
26
|
-
*/
|
|
27
|
-
export async function docFor(rt: Runtime, a: TileCtx, principal: ReturnType<typeof userOf>) {
|
|
28
|
-
const sources = [a.draft, a.merged, a.at].filter((s) => s !== undefined).length;
|
|
29
|
-
if (sources > 1)
|
|
30
|
-
throw new HttpError(
|
|
31
|
-
400,
|
|
32
|
-
"BODY_INVALID",
|
|
33
|
-
"A tile runs against one document: send at most one of draft, merged and at.",
|
|
34
|
-
);
|
|
35
|
-
if (a.draft === true || a.merged === true) {
|
|
36
|
-
const as = a.draft === true ? "draft" : "merged";
|
|
37
|
-
const document = await draftDoc(rt, a.request, principal, { id: a.dashboardId, as });
|
|
38
|
-
return { id: a.dashboardId, document };
|
|
39
|
-
}
|
|
40
|
-
if (a.at !== undefined) {
|
|
41
|
-
const old = await versionDoc(rt, a.request, principal, { ...a, version: a.at });
|
|
42
|
-
return { id: a.dashboardId, document: old.doc };
|
|
43
|
-
}
|
|
44
|
-
const ref = { id: a.dashboardId, ...(a.version === undefined ? {} : { version: a.version }) };
|
|
45
|
-
return loadDashboard(rt.options, a.request, principal, ref);
|
|
46
|
-
}
|
package/src/tiles.ts
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
composeTile,
|
|
3
|
-
type Doc,
|
|
4
|
-
type Picks,
|
|
5
|
-
type Principal,
|
|
6
|
-
type TileAnswer,
|
|
7
|
-
type TileLine,
|
|
8
|
-
type TileRequest,
|
|
9
|
-
} from "@derive-to/dashboards-core";
|
|
10
|
-
import { EngineError } from "@derive-to/dashboards-core/engine";
|
|
11
|
-
import { type Execution, execute, type Runtime } from "./execute";
|
|
12
|
-
import { HttpError } from "./http";
|
|
13
|
-
import { calendarOf } from "./request";
|
|
14
|
-
|
|
15
|
-
/** What every tile of one request shares: the dashboard as pinned, the picks and one `now`. */
|
|
16
|
-
export type Ctx = {
|
|
17
|
-
rt: Runtime;
|
|
18
|
-
request: Request;
|
|
19
|
-
principal: Principal;
|
|
20
|
-
dashboardId: string;
|
|
21
|
-
doc: Doc;
|
|
22
|
-
picks: Picks;
|
|
23
|
-
now: Date;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/** One tile composed from the document by the written rules, ready to run. */
|
|
27
|
-
export function tileExecution(ctx: Ctx, tile: TileRequest): Execution {
|
|
28
|
-
const { options } = ctx.rt;
|
|
29
|
-
const composed = composeTile({
|
|
30
|
-
model: options.model,
|
|
31
|
-
doc: ctx.doc,
|
|
32
|
-
widgetId: tile.widgetId,
|
|
33
|
-
picks: ctx.picks,
|
|
34
|
-
...(tile.query === undefined ? {} : { queryName: tile.query }),
|
|
35
|
-
...(options.compose === undefined ? {} : { policy: options.compose }),
|
|
36
|
-
});
|
|
37
|
-
return {
|
|
38
|
-
rt: ctx.rt,
|
|
39
|
-
request: ctx.request,
|
|
40
|
-
principal: ctx.principal,
|
|
41
|
-
now: ctx.now,
|
|
42
|
-
calendar: calendarOf(options, ctx.doc),
|
|
43
|
-
query: composed.query,
|
|
44
|
-
lock: composed.lock,
|
|
45
|
-
coverage: composed.coverage,
|
|
46
|
-
dashboardId: ctx.dashboardId,
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** One tile by reference: composed from the document, planned under the viewer's access, run. */
|
|
51
|
-
export async function runTile(ctx: Ctx, tile: TileRequest): Promise<TileAnswer> {
|
|
52
|
-
return { widgetId: tile.widgetId, ...(await execute(tileExecution(ctx, tile))) };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** An error a client can act on, as a line; anything else ends the stream. */
|
|
56
|
-
function refusal(error: unknown): { code: string; message: string } | undefined {
|
|
57
|
-
if (error instanceof EngineError || error instanceof HttpError)
|
|
58
|
-
return { code: error.code, message: error.message };
|
|
59
|
-
return undefined;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
type Settled<T> =
|
|
63
|
-
| { i: number; value: T }
|
|
64
|
-
| { i: number; error: { code: string; message: string } }
|
|
65
|
-
| { i: number; failure: unknown };
|
|
66
|
-
|
|
67
|
-
async function settle<T>(i: number, run: () => Promise<T>): Promise<Settled<T>> {
|
|
68
|
-
try {
|
|
69
|
-
return { i, value: await run() };
|
|
70
|
-
} catch (error) {
|
|
71
|
-
const refused = refusal(error);
|
|
72
|
-
return refused === undefined ? { i, failure: error } : { i, error: refused };
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** How many jobs of one request run at once: `query.concurrency`, default 6. */
|
|
77
|
-
export function concurrencyOf(rt: Runtime): number {
|
|
78
|
-
const wanted = rt.options.query?.concurrency ?? 6;
|
|
79
|
-
return Number.isInteger(wanted) && wanted >= 1 ? wanted : 6;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Runs the jobs, at most `width` at once (each next one starts as one finishes), and yields
|
|
84
|
-
* each answer as it finishes, as an NDJSON line, then `{ done: true }`. A refused job is a line
|
|
85
|
-
* with its error and the others still answer. A reader that stops reading starts no more jobs.
|
|
86
|
-
*/
|
|
87
|
-
export async function* streamLines<T>(
|
|
88
|
-
jobs: readonly (() => Promise<T>)[],
|
|
89
|
-
width: number,
|
|
90
|
-
): AsyncGenerator<{ i: number; ok: true; result: T } | Exclude<TileLine, { ok: true }>> {
|
|
91
|
-
const running = new Map<number, Promise<Settled<T>>>();
|
|
92
|
-
let started = 0;
|
|
93
|
-
const fill = () => {
|
|
94
|
-
for (; started < jobs.length && running.size < width; started++) {
|
|
95
|
-
const job = jobs[started];
|
|
96
|
-
if (job !== undefined) running.set(started, settle(started, job));
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
fill();
|
|
100
|
-
while (running.size > 0) {
|
|
101
|
-
// The running jobs are already running; this waits for whichever finishes next.
|
|
102
|
-
// oxlint-disable-next-line eslint/no-await-in-loop
|
|
103
|
-
const settled = await Promise.race(running.values());
|
|
104
|
-
running.delete(settled.i);
|
|
105
|
-
fill();
|
|
106
|
-
if ("failure" in settled) throw settled.failure;
|
|
107
|
-
yield "error" in settled
|
|
108
|
-
? { i: settled.i, ok: false, error: settled.error }
|
|
109
|
-
: { i: settled.i, ok: true, result: settled.value };
|
|
110
|
-
}
|
|
111
|
-
yield { done: true };
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/** Each tile by reference, streamed as it finishes. */
|
|
115
|
-
export function streamTiles(ctx: Ctx, tiles: readonly TileRequest[]) {
|
|
116
|
-
return streamLines(
|
|
117
|
-
tiles.map((tile) => () => runTile(ctx, tile)),
|
|
118
|
-
concurrencyOf(ctx.rt),
|
|
119
|
-
);
|
|
120
|
-
}
|