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/cache.ts
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
bindPolicy,
|
|
3
|
-
canonical,
|
|
4
|
-
type DashOptions,
|
|
5
|
-
type Model,
|
|
6
|
-
namedPolicy,
|
|
7
|
-
type Principal,
|
|
8
|
-
policyBinding,
|
|
9
|
-
principalTenant,
|
|
10
|
-
queryClosure,
|
|
11
|
-
type SecondaryStorage,
|
|
12
|
-
type SourceName,
|
|
13
|
-
viaJoins,
|
|
14
|
-
type WireQuery,
|
|
15
|
-
} from "@derive-to/dashboards-core";
|
|
16
|
-
import { ENGINE_VERSION } from "@derive-to/dashboards-core/engine";
|
|
17
|
-
import type { Answered } from "./execute";
|
|
18
|
-
import { type Entries, entriesIn, memoryStorage } from "./storage";
|
|
19
|
-
|
|
20
|
-
export async function sha256(text: string): Promise<string> {
|
|
21
|
-
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
|
|
22
|
-
return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** The parents a source's rows are visible through, all the way up. */
|
|
26
|
-
function viaParents(model: Model, source: SourceName, seen: Set<SourceName>): void {
|
|
27
|
-
const def = model.sources[source];
|
|
28
|
-
if (seen.has(source) || def === undefined) return;
|
|
29
|
-
seen.add(source);
|
|
30
|
-
for (const join of viaJoins(def)) {
|
|
31
|
-
const to = def.joins?.[join]?.to;
|
|
32
|
-
if (to !== undefined) viaParents(model, to, seen);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Every source an answer can depend on: what the query reads, what its lock and required filters are on, and their via parents. */
|
|
37
|
-
export function manifest(model: Model, query: WireQuery, on: readonly SourceName[]): SourceName[] {
|
|
38
|
-
const seen = new Set<SourceName>();
|
|
39
|
-
for (const source of [...queryClosure(model, query).sources, ...on])
|
|
40
|
-
viaParents(model, source, seen);
|
|
41
|
-
return [...seen].toSorted();
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* The bound row policy of each source the answer reads (its manifest: the query's sources, the
|
|
46
|
-
* lock's, and every parent they are visible through): two viewers share an entry when those
|
|
47
|
-
* bind alike, whatever the policies of sources it does not read.
|
|
48
|
-
*/
|
|
49
|
-
function boundPolicies(
|
|
50
|
-
model: Model,
|
|
51
|
-
principal: Principal,
|
|
52
|
-
sources: readonly SourceName[],
|
|
53
|
-
): Record<string, unknown> {
|
|
54
|
-
return Object.fromEntries(
|
|
55
|
-
sources.flatMap((name): [string, unknown][] => {
|
|
56
|
-
const def = model.sources[name];
|
|
57
|
-
if (def === undefined) return [];
|
|
58
|
-
const policy = policyBinding(def);
|
|
59
|
-
if (typeof policy === "object" && "via" in policy) return [[name, "via"]];
|
|
60
|
-
const rules = typeof policy === "string" ? namedPolicy(model, policy) : policy;
|
|
61
|
-
return [[name, bindPolicy(rules, principal)]];
|
|
62
|
-
}),
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
type Entry = { at: number; value: Answered };
|
|
67
|
-
|
|
68
|
-
/** What decides an answer, besides the rows: everything that goes into its cache key. */
|
|
69
|
-
type CacheKey = {
|
|
70
|
-
principal: Principal;
|
|
71
|
-
query: WireQuery;
|
|
72
|
-
lock: unknown;
|
|
73
|
-
required: unknown;
|
|
74
|
-
calendar: unknown;
|
|
75
|
-
maxRows: number;
|
|
76
|
-
/** `now` floored to the quantum; only a relative range reads it. */
|
|
77
|
-
now: number;
|
|
78
|
-
/** The sources whose epochs the entry depends on. */
|
|
79
|
-
sources: readonly SourceName[];
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
const epochKey = (tenant: Record<string, string>, source: string) =>
|
|
83
|
-
`dd:epoch:${canonical(tenant)}:${source}`;
|
|
84
|
-
|
|
85
|
-
export type Cache = {
|
|
86
|
-
answer(key: CacheKey, compute: () => Promise<Answered>): Promise<Answered>;
|
|
87
|
-
/** Drops a tenant's cached answers that read any of `sources` (every source when absent). */
|
|
88
|
-
invalidate(tenant: Record<string, string>, sources?: readonly SourceName[]): Promise<void>;
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const marked = (value: Answered, cache: "hit" | "stale" | "miss"): Answered => ({
|
|
92
|
-
...value,
|
|
93
|
-
receipt: { ...value.receipt, cache, answeredBy: cache === "miss" ? "base" : "cache" },
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Answers keyed by everything that decides them. Each source's epoch is in the key, so a write
|
|
98
|
-
* invalidates by bumping the epochs of the sources it changed; old entries are never read again
|
|
99
|
-
* and expire on their own.
|
|
100
|
-
*/
|
|
101
|
-
class ResultCache implements Cache {
|
|
102
|
-
readonly #options: DashOptions;
|
|
103
|
-
readonly #ttl: number;
|
|
104
|
-
readonly #stale: number;
|
|
105
|
-
readonly #storage: SecondaryStorage;
|
|
106
|
-
readonly #entries: Entries<Entry>;
|
|
107
|
-
readonly #inFlight = new Map<string, Promise<Answered>>();
|
|
108
|
-
readonly #modelHash: Promise<string>;
|
|
109
|
-
|
|
110
|
-
constructor(options: DashOptions, cache: NonNullable<DashOptions["cache"]>) {
|
|
111
|
-
this.#options = options;
|
|
112
|
-
this.#ttl = cache.ttl ?? 60;
|
|
113
|
-
this.#stale = cache.staleTtl ?? 0;
|
|
114
|
-
this.#storage = options.secondaryStorage ?? memoryStorage();
|
|
115
|
-
this.#entries = entriesIn<Entry>(options.secondaryStorage);
|
|
116
|
-
this.#modelHash = sha256(canonical(options.model));
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async #keyOf(k: CacheKey): Promise<string> {
|
|
120
|
-
const tenant = principalTenant(k.principal);
|
|
121
|
-
const epochs = await Promise.all(
|
|
122
|
-
k.sources.map(async (s) => (await this.#storage.get(epochKey(tenant, s))) ?? "0"),
|
|
123
|
-
);
|
|
124
|
-
const relative = k.query.time !== undefined && "last" in k.query.time.range;
|
|
125
|
-
const parts = {
|
|
126
|
-
v: ENGINE_VERSION,
|
|
127
|
-
model: await this.#modelHash,
|
|
128
|
-
tenant,
|
|
129
|
-
policy: boundPolicies(this.#options.model, k.principal, k.sources),
|
|
130
|
-
query: k.query,
|
|
131
|
-
lock: k.lock,
|
|
132
|
-
required: k.required,
|
|
133
|
-
calendar: k.calendar,
|
|
134
|
-
maxRows: k.maxRows,
|
|
135
|
-
...(relative ? { now: k.now } : {}),
|
|
136
|
-
epochs: Object.fromEntries(k.sources.map((s, i) => [s, epochs[i]])),
|
|
137
|
-
};
|
|
138
|
-
return `dd:result:${await sha256(canonical(parts))}`;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/** Computes once per key at a time, stores the answer, and hands every waiter the same one. */
|
|
142
|
-
#refresh(key: string, compute: () => Promise<Answered>): Promise<Answered> {
|
|
143
|
-
const running = this.#inFlight.get(key);
|
|
144
|
-
if (running !== undefined) return running;
|
|
145
|
-
const task = (async () => {
|
|
146
|
-
try {
|
|
147
|
-
const value = await compute();
|
|
148
|
-
await this.#entries.set(key, { at: Date.now(), value }, this.#ttl + this.#stale);
|
|
149
|
-
return value;
|
|
150
|
-
} finally {
|
|
151
|
-
this.#inFlight.delete(key);
|
|
152
|
-
}
|
|
153
|
-
})();
|
|
154
|
-
this.#inFlight.set(key, task);
|
|
155
|
-
return task;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/** A stale answer's refresh runs where the host runs background work, and never fails a request. */
|
|
159
|
-
#background(task: Promise<unknown>): void {
|
|
160
|
-
const quiet = task.catch(() => null);
|
|
161
|
-
this.#options.advanced?.backgroundTasks?.handler(quiet);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
async answer(k: CacheKey, compute: () => Promise<Answered>): Promise<Answered> {
|
|
165
|
-
const key = await this.#keyOf(k);
|
|
166
|
-
const entry = await this.#entries.get(key);
|
|
167
|
-
if (entry !== null) {
|
|
168
|
-
const age = (Date.now() - entry.at) / 1000;
|
|
169
|
-
if (age < this.#ttl) return marked(entry.value, "hit");
|
|
170
|
-
if (age < this.#ttl + this.#stale) {
|
|
171
|
-
this.#background(this.#refresh(key, compute));
|
|
172
|
-
return marked(entry.value, "stale");
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
return marked(await this.#refresh(key, compute), "miss");
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
async invalidate(tenant: Record<string, string>, sources?: readonly SourceName[]): Promise<void> {
|
|
179
|
-
const names = sources ?? Object.keys(this.#options.model.sources);
|
|
180
|
-
await Promise.all(
|
|
181
|
-
names.map(async (s) => {
|
|
182
|
-
const current = Number((await this.#storage.get(epochKey(tenant, s))) ?? "0");
|
|
183
|
-
await this.#storage.set(epochKey(tenant, s), String(current + 1));
|
|
184
|
-
}),
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/** The result cache, or undefined when the host did not ask for one. */
|
|
190
|
-
export function createCache(options: DashOptions): Cache | undefined {
|
|
191
|
-
return options.cache === undefined ? undefined : new ResultCache(options, options.cache);
|
|
192
|
-
}
|
package/src/checks.ts
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type Command,
|
|
3
|
-
type Control,
|
|
4
|
-
type Doc,
|
|
5
|
-
lockChanged,
|
|
6
|
-
type Principal,
|
|
7
|
-
parseMemberId,
|
|
8
|
-
QueryReferenceError,
|
|
9
|
-
type Resource,
|
|
10
|
-
resolveDimension,
|
|
11
|
-
toWire,
|
|
12
|
-
type Viewer,
|
|
13
|
-
type Widget,
|
|
14
|
-
} from "@derive-to/dashboards-core";
|
|
15
|
-
import { authorizeFree } from "./authorize";
|
|
16
|
-
import { editorOf, storedId, storeFor } from "./editing";
|
|
17
|
-
import type { Runtime } from "./execute";
|
|
18
|
-
import { HttpError } from "./http";
|
|
19
|
-
import { dashboardResource } from "./owner";
|
|
20
|
-
|
|
21
|
-
/** The checks an edit passes: every query readable by the editor, a lock changed only with `manage`. */
|
|
22
|
-
|
|
23
|
-
type User = Principal & { viewer: Viewer };
|
|
24
|
-
|
|
25
|
-
/** A stored query as the engine reads it; one that names a missing formula part is refused. */
|
|
26
|
-
function wireOf(query: Parameters<typeof toWire>[0]) {
|
|
27
|
-
try {
|
|
28
|
-
return toWire(query).wire;
|
|
29
|
-
} catch (error) {
|
|
30
|
-
if (error instanceof QueryReferenceError)
|
|
31
|
-
throw new HttpError(400, "QUERY_REFERENCE", error.message);
|
|
32
|
-
throw error;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Every query a batch adds or changes, each named query of a new widget included, must resolve against the model and be one the
|
|
38
|
-
* editor may read: nobody builds a widget over data they cannot see. A lock needs `manage`.
|
|
39
|
-
*/
|
|
40
|
-
export async function checkCommands(
|
|
41
|
-
rt: Runtime,
|
|
42
|
-
viewer: Viewer,
|
|
43
|
-
dashboard: Resource,
|
|
44
|
-
commands: readonly Command[],
|
|
45
|
-
) {
|
|
46
|
-
const queries = commands.flatMap((c) =>
|
|
47
|
-
c.type === "widget.setQuery"
|
|
48
|
-
? [c.query]
|
|
49
|
-
: c.type === "widget.add"
|
|
50
|
-
? Object.values(c.widget.queries)
|
|
51
|
-
: [],
|
|
52
|
-
);
|
|
53
|
-
// oxlint-disable-next-line no-await-in-loop -- one check per query, in order
|
|
54
|
-
for (const query of queries) await authorizeFree(rt.options, viewer, wireOf(query));
|
|
55
|
-
await checkFilters(
|
|
56
|
-
rt,
|
|
57
|
-
viewer,
|
|
58
|
-
commands.flatMap((c) => (c.type === "control.add" ? [c.control] : [])),
|
|
59
|
-
);
|
|
60
|
-
if (commands.some((c) => c.type === "lock.set" || c.type === "lock.remove")) {
|
|
61
|
-
const can = await rt.options.access.can(viewer, "manage", dashboard);
|
|
62
|
-
if (!can) throw new HttpError(403, "FORBIDDEN", "Changing a dashboard's lock needs manage.");
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Every filter a batch adds narrows a field the model has and the editor may read: a filter on
|
|
68
|
-
* a field that does not exist would never apply, and one on a hidden field would show its values.
|
|
69
|
-
*/
|
|
70
|
-
async function checkFilters(rt: Runtime, viewer: Viewer, controls: readonly Control[]) {
|
|
71
|
-
const members = controls.flatMap((c) => (c.kind === "filter" ? [c.member] : []));
|
|
72
|
-
for (const member of members) {
|
|
73
|
-
const ref = parseMemberId(member);
|
|
74
|
-
const known =
|
|
75
|
-
ref !== undefined && resolveDimension(rt.options.model, ref.source, member) !== undefined;
|
|
76
|
-
if (!known)
|
|
77
|
-
throw new HttpError(
|
|
78
|
-
400,
|
|
79
|
-
"QUERY_REFERENCE",
|
|
80
|
-
`A filter names "${member}", which is not a field of the model.`,
|
|
81
|
-
);
|
|
82
|
-
// oxlint-disable-next-line no-await-in-loop -- one check per filter, in order
|
|
83
|
-
const can = await rt.options.access.can(viewer, "view", { kind: "member", id: member });
|
|
84
|
-
if (!can) throw new HttpError(403, "FORBIDDEN", `A filter on "${member}" needs view on it.`);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** Widgets brought back whole (a put-back on review): every query in them must be one the editor may read. */
|
|
89
|
-
export async function checkWidgets(
|
|
90
|
-
rt: Runtime,
|
|
91
|
-
viewer: Viewer,
|
|
92
|
-
dashboard: Resource,
|
|
93
|
-
widgets: readonly Widget[],
|
|
94
|
-
) {
|
|
95
|
-
const commands = widgets.map(
|
|
96
|
-
(widget, i): Command => ({
|
|
97
|
-
commandId: String(i),
|
|
98
|
-
type: "widget.add",
|
|
99
|
-
tab: "",
|
|
100
|
-
id: String(i),
|
|
101
|
-
widget,
|
|
102
|
-
at: { x: 0, y: 0, w: 1, h: 1 },
|
|
103
|
-
}),
|
|
104
|
-
);
|
|
105
|
-
await checkCommands(rt, viewer, dashboard, commands);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* A whole document an editor brings back, as a restore does: every query in it must be one they
|
|
110
|
-
* may read, and if its lock differs from the current one, they need `manage`, exactly as if
|
|
111
|
-
* they had made those changes by hand.
|
|
112
|
-
*/
|
|
113
|
-
export async function checkDocument(
|
|
114
|
-
rt: Runtime,
|
|
115
|
-
viewer: Viewer,
|
|
116
|
-
dashboard: Resource,
|
|
117
|
-
docs: { restored: Doc; current: Doc | undefined },
|
|
118
|
-
) {
|
|
119
|
-
const { restored: doc, current } = docs;
|
|
120
|
-
const queries = Object.values(doc.widgets).flatMap((w) => Object.values(w.queries));
|
|
121
|
-
// oxlint-disable-next-line no-await-in-loop -- one check per query, in order
|
|
122
|
-
for (const query of queries) await authorizeFree(rt.options, viewer, wireOf(query));
|
|
123
|
-
await checkFilters(rt, viewer, doc.controls);
|
|
124
|
-
if (current !== undefined && lockChanged(current, doc)) {
|
|
125
|
-
const can = await rt.options.access.can(viewer, "manage", dashboard);
|
|
126
|
-
if (!can)
|
|
127
|
-
throw new HttpError(
|
|
128
|
-
403,
|
|
129
|
-
"FORBIDDEN",
|
|
130
|
-
"That version has a different lock; restoring it needs manage.",
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* An earlier published version of a dashboard: the one rule for reading one, whether to
|
|
137
|
-
* preview it, run its tiles or copy it. It needs `edit`, and `manage` when its lock differs
|
|
138
|
-
* from the current one, so it never shows more than the person could see by restoring it.
|
|
139
|
-
*/
|
|
140
|
-
export async function versionDoc(
|
|
141
|
-
rt: Runtime,
|
|
142
|
-
request: Request,
|
|
143
|
-
user: User,
|
|
144
|
-
ref: { dashboardId: string; version: number },
|
|
145
|
-
) {
|
|
146
|
-
const resource = await dashboardResource(rt.options, request, user, ref.dashboardId);
|
|
147
|
-
if (!(await rt.options.access.can(user.viewer, "edit", resource)))
|
|
148
|
-
throw new HttpError(403, "FORBIDDEN", "Reading an earlier version needs edit.");
|
|
149
|
-
const id = await storedId(rt, request, user, ref.dashboardId);
|
|
150
|
-
const store = storeFor(rt, request);
|
|
151
|
-
const { tenant } = editorOf(user);
|
|
152
|
-
const old = id === null ? null : await store.getVersion({ tenant, id, version: ref.version });
|
|
153
|
-
if (id === null)
|
|
154
|
-
throw new HttpError(404, "NOT_FOUND", `"${ref.dashboardId}" has no published versions.`);
|
|
155
|
-
if (old === null)
|
|
156
|
-
throw new HttpError(
|
|
157
|
-
404,
|
|
158
|
-
"NOT_FOUND",
|
|
159
|
-
`There is no version ${String(ref.version)} of "${ref.dashboardId}".`,
|
|
160
|
-
);
|
|
161
|
-
const current = await store.get({ tenant, ref: { id } });
|
|
162
|
-
const lockDiffers = current !== null && lockChanged(current.doc, old.doc);
|
|
163
|
-
if (lockDiffers && !(await rt.options.access.can(user.viewer, "manage", resource)))
|
|
164
|
-
throw new HttpError(
|
|
165
|
-
403,
|
|
166
|
-
"FORBIDDEN",
|
|
167
|
-
"That version has a different lock; seeing it needs manage.",
|
|
168
|
-
);
|
|
169
|
-
return old;
|
|
170
|
-
}
|
package/src/custom-source.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CustomSource,
|
|
3
|
-
GroupedRead,
|
|
4
|
-
HostRows,
|
|
5
|
-
ReadContext,
|
|
6
|
-
RecordsRead,
|
|
7
|
-
} from "@derive-to/dashboards-core";
|
|
8
|
-
import { boundedCall } from "./bounded";
|
|
9
|
-
|
|
10
|
-
type Rows = Promise<HostRows>;
|
|
11
|
-
|
|
12
|
-
/** What a host writes for its own data layer: the grouped read and, optionally, records. */
|
|
13
|
-
export type CustomSourceOptions = {
|
|
14
|
-
/**
|
|
15
|
-
* One grouped read: the rows of `request.source` passing `request.where`, grouped by the
|
|
16
|
-
* bucket and dimensions, each measure aggregated. Every access rule is already in `where`:
|
|
17
|
-
* apply it exactly, or the viewer reads rows they may not.
|
|
18
|
-
*/
|
|
19
|
-
answer: (request: GroupedRead, context: ReadContext) => Rows;
|
|
20
|
-
/** The rows behind a tile. Without it, records of these sources are refused with a reason. */
|
|
21
|
-
records?: (request: RecordsRead, context: ReadContext) => Rows;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* A connection that is the host's own data layer (its SQL, ClickHouse, an API, a cache): the
|
|
26
|
-
* library still does dashboards, editing, caching, the UI, agents, totals, ranking, Other,
|
|
27
|
-
* comparisons and formulas, and asks it only grouped reads and records. The host owns safety
|
|
28
|
-
* on this path: run `derive-dashboards check --conformance` against it.
|
|
29
|
-
*/
|
|
30
|
-
export function customSource(options: CustomSourceOptions): CustomSource {
|
|
31
|
-
const { answer, records } = options;
|
|
32
|
-
return { kind: "custom", answer, ...(records === undefined ? {} : { records }) };
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** Whether a value is a custom source. */
|
|
36
|
-
export function isCustomSource(handle: unknown): handle is CustomSource {
|
|
37
|
-
return (
|
|
38
|
-
typeof handle === "object" &&
|
|
39
|
-
handle !== null &&
|
|
40
|
-
"kind" in handle &&
|
|
41
|
-
handle.kind === "custom" &&
|
|
42
|
-
"answer" in handle &&
|
|
43
|
-
typeof handle.answer === "function"
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** A custom source whose every call starts inside the deadline and ends at it or on abort. */
|
|
48
|
-
export function boundedSource(source: CustomSource): CustomSource {
|
|
49
|
-
const { records } = source;
|
|
50
|
-
return {
|
|
51
|
-
kind: "custom",
|
|
52
|
-
answer: (request, context) => boundedCall(context, () => source.answer(request, context)),
|
|
53
|
-
...(records === undefined
|
|
54
|
-
? {}
|
|
55
|
-
: {
|
|
56
|
-
records: (request, context) => boundedCall(context, () => records(request, context)),
|
|
57
|
-
}),
|
|
58
|
-
};
|
|
59
|
-
}
|
package/src/dashboard-id.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { Row } from "@derive-to/dashboards-core";
|
|
2
|
-
import { HttpError } from "./http";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* What a client's dashboard id means: the one owner of that rule. A built-in is named by
|
|
6
|
-
* `default:<key>`, and so is its tenant's stored copy once someone customized it; any other
|
|
7
|
-
* id is a stored dashboard's own id. A dashboard answers to exactly one id, so the host's
|
|
8
|
-
* `can`, which is asked about the id a client sent, is always asked about the dashboard it gets.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const BUILT_IN = "default:";
|
|
12
|
-
|
|
13
|
-
export type DashboardRef = { key: string } | { id: string };
|
|
14
|
-
|
|
15
|
-
/** A client's id as a reference to a built-in (by key) or a stored dashboard (by id). */
|
|
16
|
-
export function parseDashboardId(id: string): DashboardRef {
|
|
17
|
-
return id.startsWith(BUILT_IN) ? { key: id.slice(BUILT_IN.length) } : { id };
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/** The id clients use for a built-in, customized or not. */
|
|
21
|
-
export const builtInId = (key: string) => `${BUILT_IN}${key}`;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Refuses a stored row reached by an id it does not answer to: a built-in's copy by its
|
|
25
|
-
* internal id. It is not found, as for any id that names nothing.
|
|
26
|
-
*/
|
|
27
|
-
export function expectServedAs(row: Row | null, ref: DashboardRef, id: string): void {
|
|
28
|
-
if (row === null || "key" in ref) return;
|
|
29
|
-
if (row["default_of"] !== null && row["default_of"] !== undefined)
|
|
30
|
-
throw new HttpError(404, "NOT_FOUND", `There is no dashboard "${id}".`);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** An answer that names a dashboard, naming it by the id the client used. */
|
|
34
|
-
export function servedAs<T extends { dashboardId: string }>(answer: T, id: string): T {
|
|
35
|
-
return { ...answer, dashboardId: id };
|
|
36
|
-
}
|
package/src/dashboards.ts
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type DashAdapter,
|
|
3
|
-
type DashboardSummary,
|
|
4
|
-
type DashOptions,
|
|
5
|
-
type Doc,
|
|
6
|
-
type Principal,
|
|
7
|
-
principalTenant,
|
|
8
|
-
} from "@derive-to/dashboards-core";
|
|
9
|
-
import { builtInId, parseDashboardId } from "./dashboard-id";
|
|
10
|
-
import { HttpError } from "./http";
|
|
11
|
-
import { requestDashboardRow } from "./request-rows";
|
|
12
|
-
import { createReader, storedHead } from "./store/reader";
|
|
13
|
-
|
|
14
|
-
/** A dashboard as the runtime serves it: its id, the version pinned for this request, its document. */
|
|
15
|
-
export type Loaded = { id: string; version: number; document: Doc };
|
|
16
|
-
|
|
17
|
-
const PAGE = 500;
|
|
18
|
-
const MOST = 5000;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* A tenant's published dashboards, every page of them up to a bound, so access is checked on
|
|
22
|
-
* all of them rather than on the first page only. The list reads rows, never documents.
|
|
23
|
-
*/
|
|
24
|
-
async function allStored(adapter: DashAdapter, tenant: Record<string, string>) {
|
|
25
|
-
const reader = createReader(adapter);
|
|
26
|
-
const items: DashboardSummary[] = [];
|
|
27
|
-
let cursor: string | undefined;
|
|
28
|
-
do {
|
|
29
|
-
// oxlint-disable-next-line no-await-in-loop -- each page starts where the last ended
|
|
30
|
-
const page = await reader.list({
|
|
31
|
-
tenant,
|
|
32
|
-
limit: PAGE,
|
|
33
|
-
...(cursor === undefined ? {} : { cursor }),
|
|
34
|
-
});
|
|
35
|
-
items.push(...page.items);
|
|
36
|
-
cursor = page.next;
|
|
37
|
-
} while (cursor !== undefined && items.length < MOST);
|
|
38
|
-
return items;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** The store adapter for this request, when the host gave one. */
|
|
42
|
-
export function adapterFor(options: DashOptions, request: Request): DashAdapter | undefined {
|
|
43
|
-
const { database } = options;
|
|
44
|
-
return typeof database === "function" ? database(request) : database;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Every dashboard a tenant sees: its stored ones at their current versions, and each code-defined
|
|
49
|
-
* one it has not customized, at version 0.
|
|
50
|
-
*/
|
|
51
|
-
/** A dashboard in the list, with who made it when it is stored, for `can` to read. */
|
|
52
|
-
type Listed = {
|
|
53
|
-
id: string;
|
|
54
|
-
title: string;
|
|
55
|
-
version: number;
|
|
56
|
-
owner?: { id: string; kind: "user" | "agent" };
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export async function listDashboards(
|
|
60
|
-
options: DashOptions,
|
|
61
|
-
request: Request,
|
|
62
|
-
principal: Principal,
|
|
63
|
-
): Promise<Listed[]> {
|
|
64
|
-
const adapter = adapterFor(options, request);
|
|
65
|
-
const stored = adapter === undefined ? [] : await allStored(adapter, principalTenant(principal));
|
|
66
|
-
const customized = new Set(
|
|
67
|
-
stored.flatMap((d) => (d.defaultOf === undefined ? [] : [d.defaultOf])),
|
|
68
|
-
);
|
|
69
|
-
const defaults = (options.dashboards ?? [])
|
|
70
|
-
.filter((d) => !customized.has(d.key))
|
|
71
|
-
.map((d) => ({ id: builtInId(d.key), title: d.document.meta.title, version: 0 }));
|
|
72
|
-
return [
|
|
73
|
-
// A customized built-in keeps the id clients know it by.
|
|
74
|
-
...stored.map((d) => ({
|
|
75
|
-
id: d.defaultOf === undefined ? d.id : builtInId(d.defaultOf),
|
|
76
|
-
title: d.title,
|
|
77
|
-
version: d.currentVersion,
|
|
78
|
-
// A built-in's copy belongs to nobody, whoever first pressed Edit.
|
|
79
|
-
...(d.defaultOf === undefined ? { owner: { id: d.ownerId, kind: d.ownerKind } } : {}),
|
|
80
|
-
})),
|
|
81
|
-
...defaults,
|
|
82
|
-
];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
async function find(
|
|
86
|
-
options: DashOptions,
|
|
87
|
-
request: Request,
|
|
88
|
-
principal: Principal,
|
|
89
|
-
id: string,
|
|
90
|
-
): Promise<Loaded> {
|
|
91
|
-
const adapter = adapterFor(options, request);
|
|
92
|
-
const ref = parseDashboardId(id);
|
|
93
|
-
// The row the request's access check already read, then its current version.
|
|
94
|
-
const tenant = principalTenant(principal);
|
|
95
|
-
const stored =
|
|
96
|
-
adapter === undefined
|
|
97
|
-
? null
|
|
98
|
-
: await storedHead(adapter, await requestDashboardRow(request, adapter, tenant, ref), tenant);
|
|
99
|
-
// A built-in's copy answers only to its built-in id, never to its stored one.
|
|
100
|
-
if (stored !== null && ("key" in ref || stored.defaultOf === undefined))
|
|
101
|
-
return { id, version: stored.currentVersion, document: stored.doc };
|
|
102
|
-
const key = "key" in ref ? ref.key : undefined;
|
|
103
|
-
const code =
|
|
104
|
-
key === undefined ? undefined : (options.dashboards ?? []).find((d) => d.key === key);
|
|
105
|
-
if (code === undefined)
|
|
106
|
-
throw new HttpError(
|
|
107
|
-
404,
|
|
108
|
-
"NOT_FOUND",
|
|
109
|
-
`There is no dashboard "${id}". A built-in one's id is "default:" and its key; /dashboards/list gives every id.`,
|
|
110
|
-
);
|
|
111
|
-
return { id, version: 0, document: code.document };
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* A dashboard by id, pinned at its current version for the rest of the request: the tenant's
|
|
116
|
-
* stored row, or for `default:<key>` the tenant's customized copy if it has one, else the
|
|
117
|
-
* code-defined document at version 0. A `version` the client names is an expectation: a
|
|
118
|
-
* different current version is a 409, so a client never runs tiles against a dashboard that
|
|
119
|
-
* changed under it.
|
|
120
|
-
*/
|
|
121
|
-
export async function loadDashboard(
|
|
122
|
-
options: DashOptions,
|
|
123
|
-
request: Request,
|
|
124
|
-
principal: Principal,
|
|
125
|
-
ref: { id: string; version?: number },
|
|
126
|
-
): Promise<Loaded> {
|
|
127
|
-
const loaded = await find(options, request, principal, ref.id);
|
|
128
|
-
const expected = ref.version;
|
|
129
|
-
if (expected !== undefined && expected !== loaded.version)
|
|
130
|
-
throw new HttpError(409, "VERSION_CONFLICT", "The dashboard changed. Reload it.");
|
|
131
|
-
return loaded;
|
|
132
|
-
}
|