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/editing.ts
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type Author,
|
|
3
|
-
type DashboardStore,
|
|
4
|
-
type Doc,
|
|
5
|
-
type Draft,
|
|
6
|
-
type DraftTarget,
|
|
7
|
-
type Principal,
|
|
8
|
-
principalTenant,
|
|
9
|
-
type Viewer,
|
|
10
|
-
} from "@derive-to/dashboards-core";
|
|
11
|
-
import { expectServedAs, parseDashboardId } from "./dashboard-id";
|
|
12
|
-
import { adapterFor } from "./dashboards";
|
|
13
|
-
import type { Runtime } from "./execute";
|
|
14
|
-
import { HttpError } from "./http";
|
|
15
|
-
import { dashboardResource } from "./owner";
|
|
16
|
-
import { requestDashboardRow } from "./request-rows";
|
|
17
|
-
import { draftClosed, draftRow } from "./store/rows";
|
|
18
|
-
import { createStore } from "./store/writer";
|
|
19
|
-
|
|
20
|
-
/** What the draft endpoints share: the store, how a client's id maps to a row, and who edits. */
|
|
21
|
-
|
|
22
|
-
type User = Principal & { viewer: Viewer };
|
|
23
|
-
|
|
24
|
-
/** The store for this request; editing without one is refused with what to configure. */
|
|
25
|
-
export function storeFor(rt: Runtime, request: Request): DashboardStore {
|
|
26
|
-
const adapter = adapterFor(rt.options, request);
|
|
27
|
-
if (adapter === undefined)
|
|
28
|
-
throw new HttpError(
|
|
29
|
-
400,
|
|
30
|
-
"STORE_REQUIRED",
|
|
31
|
-
"Editing dashboards needs a store: pass `database` to deriveDashboards.",
|
|
32
|
-
);
|
|
33
|
-
return createStore(adapter);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* What a draft of `id` starts from: a built-in by its key, or a stored dashboard by the one id
|
|
38
|
-
* it answers to (a built-in's copy is reached only by its built-in id).
|
|
39
|
-
*/
|
|
40
|
-
export async function targetOf(
|
|
41
|
-
rt: Runtime,
|
|
42
|
-
request: Request,
|
|
43
|
-
user: User,
|
|
44
|
-
id: string,
|
|
45
|
-
): Promise<DraftTarget> {
|
|
46
|
-
const ref = parseDashboardId(id);
|
|
47
|
-
if ("id" in ref) {
|
|
48
|
-
await storedId(rt, request, user, id);
|
|
49
|
-
return ref;
|
|
50
|
-
}
|
|
51
|
-
const { key } = ref;
|
|
52
|
-
const code = (rt.options.dashboards ?? []).find((d) => d.key === key);
|
|
53
|
-
if (code === undefined) throw new HttpError(404, "NOT_FOUND", `There is no dashboard "${id}".`);
|
|
54
|
-
return { builtIn: key, doc: code.document };
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Who edits, and in which tenant, from the request's principal: a person, or an agent and whom it acts for. */
|
|
58
|
-
export function editorOf(user: User): { tenant: Record<string, string>; author: Author } {
|
|
59
|
-
const { agent } = user.viewer;
|
|
60
|
-
const author: Author =
|
|
61
|
-
agent === undefined
|
|
62
|
-
? { id: user.viewer.id, kind: "user" }
|
|
63
|
-
: { id: user.viewer.id, kind: "agent", for: agent.for };
|
|
64
|
-
return { tenant: principalTenant(user), author };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** The stored row id behind the id a client uses: a built-in's copy is found by its key. */
|
|
68
|
-
export async function rowId(rt: Runtime, request: Request, user: User, id: string) {
|
|
69
|
-
const stored = await storedId(rt, request, user, id);
|
|
70
|
-
if (stored === null) throw draftClosed();
|
|
71
|
-
return stored;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* The stored row id behind a client's id, or null for a built-in nobody has customized. A
|
|
76
|
-
* built-in's copy named by its stored id is refused: it answers only to its built-in id.
|
|
77
|
-
*/
|
|
78
|
-
export async function storedId(rt: Runtime, request: Request, user: User, id: string) {
|
|
79
|
-
const ref = parseDashboardId(id);
|
|
80
|
-
const adapter = adapterFor(rt.options, request);
|
|
81
|
-
const row =
|
|
82
|
-
adapter === undefined
|
|
83
|
-
? null
|
|
84
|
-
: await requestDashboardRow(request, adapter, principalTenant(user), ref);
|
|
85
|
-
expectServedAs(row, ref, id);
|
|
86
|
-
if ("id" in ref) return id;
|
|
87
|
-
return row === null ? null : String(row["id"]);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* The document tiles run against for the viewer's open draft: the draft, or what publishing it
|
|
92
|
-
* would make now. Refused without `edit`.
|
|
93
|
-
*/
|
|
94
|
-
export async function draftDoc(
|
|
95
|
-
rt: Runtime,
|
|
96
|
-
request: Request,
|
|
97
|
-
user: User,
|
|
98
|
-
a: { id: string; as: "draft" | "merged" },
|
|
99
|
-
): Promise<Doc> {
|
|
100
|
-
const resource = await dashboardResource(rt.options, request, user, a.id);
|
|
101
|
-
const can = await rt.options.access.can(user.viewer, "edit", resource);
|
|
102
|
-
if (!can) throw new HttpError(403, "FORBIDDEN", "Running a draft needs edit on its dashboard.");
|
|
103
|
-
const doc = await storeFor(rt, request).draftDocument({
|
|
104
|
-
...editorOf(user),
|
|
105
|
-
id: await rowId(rt, request, user, a.id),
|
|
106
|
-
as: a.as,
|
|
107
|
-
});
|
|
108
|
-
if (doc === null) throw draftClosed();
|
|
109
|
-
return doc;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* A dashboard the caller made and has not published: served to them alone, from their draft.
|
|
114
|
-
* Null when there is none, so the caller's refusal stands.
|
|
115
|
-
*/
|
|
116
|
-
export function unpublishedFor(
|
|
117
|
-
rt: Runtime,
|
|
118
|
-
request: Request,
|
|
119
|
-
user: User,
|
|
120
|
-
id: string,
|
|
121
|
-
): Promise<Draft | null> {
|
|
122
|
-
const adapter = adapterFor(rt.options, request);
|
|
123
|
-
if (adapter === undefined || "key" in parseDashboardId(id)) return Promise.resolve(null);
|
|
124
|
-
return createStore(adapter).getDraft({ ...editorOf(user), id });
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/** The caller's own new dashboards, for the list: only they see them until they publish. */
|
|
128
|
-
export async function ownUnpublished(rt: Runtime, request: Request, user: User) {
|
|
129
|
-
const adapter = adapterFor(rt.options, request);
|
|
130
|
-
if (adapter === undefined) return [];
|
|
131
|
-
const drafts = await createStore(adapter).unpublished(editorOf(user));
|
|
132
|
-
return drafts.map((d) => ({
|
|
133
|
-
id: d.dashboardId,
|
|
134
|
-
title: d.title,
|
|
135
|
-
version: 0,
|
|
136
|
-
unpublished: true as const,
|
|
137
|
-
}));
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/** Whether the caller has an open draft of this dashboard, so a client offers to resume it. */
|
|
141
|
-
export async function hasOpenDraft(rt: Runtime, request: Request, user: User, id: string) {
|
|
142
|
-
const adapter = adapterFor(rt.options, request);
|
|
143
|
-
const stored = adapter === undefined ? null : await storedId(rt, request, user, id);
|
|
144
|
-
if (adapter === undefined || stored === null) return false;
|
|
145
|
-
// `storedId` found the row in the viewer's tenant (read once per request): only the draft is left.
|
|
146
|
-
const row = await draftRow(adapter, stored, editorOf(user).author);
|
|
147
|
-
return row !== null && row["state"] === "open";
|
|
148
|
-
}
|
package/src/endpoints/access.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import type { EndpointAccess, Resource } from "@derive-to/dashboards-core";
|
|
2
|
-
import { HttpError } from "../http";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* The id a body names in a field, before its schema checks it. A body without one is refused
|
|
6
|
-
* here as malformed, so the host's `can` is never asked about an empty id.
|
|
7
|
-
*/
|
|
8
|
-
function idIn(body: unknown, field: string): string {
|
|
9
|
-
const value: unknown =
|
|
10
|
-
typeof body === "object" && body !== null
|
|
11
|
-
? Object.getOwnPropertyDescriptor(body, field)?.value
|
|
12
|
-
: undefined;
|
|
13
|
-
if (typeof value === "string" && value !== "") return value;
|
|
14
|
-
throw new HttpError(400, "BODY_INVALID", `${field}: a non-empty id is required.`);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** The dashboard a body names. */
|
|
18
|
-
const dashboardIn = (body: unknown): Resource => ({
|
|
19
|
-
kind: "dashboard",
|
|
20
|
-
id: idIn(body, "dashboardId"),
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
/** `view` on the dashboard the body names. The body is checked by the endpoint's own schema after. */
|
|
24
|
-
export const viewDashboard: EndpointAccess = { kind: "can", verb: "view", resource: dashboardIn };
|
|
25
|
-
|
|
26
|
-
/** `edit` on the dashboard the body names: drafts, and tiles run against one. */
|
|
27
|
-
export const editDashboard: EndpointAccess = { kind: "can", verb: "edit", resource: dashboardIn };
|
|
28
|
-
|
|
29
|
-
/** `edit` on the tenant: making a new dashboard. */
|
|
30
|
-
export const editTenant: EndpointAccess = {
|
|
31
|
-
kind: "can",
|
|
32
|
-
verb: "edit",
|
|
33
|
-
resource: () => ({ kind: "tenant" }),
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
/** `view` on the member the body names. */
|
|
37
|
-
export const viewMember: EndpointAccess = {
|
|
38
|
-
kind: "can",
|
|
39
|
-
verb: "view",
|
|
40
|
-
resource: (body: unknown): Resource => ({ kind: "member", id: idIn(body, "member") }),
|
|
41
|
-
};
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { createDashEndpoint, documentLock, type ValuesResponse } from "@derive-to/dashboards-core";
|
|
2
|
-
import { bodies, parseWith } from "../body";
|
|
3
|
-
import { executeRecords, type Runtime } from "../execute";
|
|
4
|
-
import { HttpError } from "../http";
|
|
5
|
-
import { nowOf, userOf } from "../request";
|
|
6
|
-
import { docFor, type TileCtx } from "../tile-doc";
|
|
7
|
-
import { type Ctx, runTile, streamTiles, tileExecution } from "../tiles";
|
|
8
|
-
import { viewDashboard } from "./access";
|
|
9
|
-
import { memberValues } from "./values";
|
|
10
|
-
|
|
11
|
-
/** What every tile of one request shares: the dashboard pinned at its version (or the viewer's draft), the picks, one `now`. */
|
|
12
|
-
async function tileContext(rt: Runtime, a: TileCtx): Promise<Ctx> {
|
|
13
|
-
const principal = userOf(a.principal);
|
|
14
|
-
const loaded = await docFor(rt, a, principal);
|
|
15
|
-
return {
|
|
16
|
-
rt,
|
|
17
|
-
request: a.request,
|
|
18
|
-
principal,
|
|
19
|
-
dashboardId: loaded.id,
|
|
20
|
-
doc: loaded.document,
|
|
21
|
-
picks: a.picks ?? {},
|
|
22
|
-
now: nowOf(rt.options),
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const RECORDS_LIMIT = 50;
|
|
27
|
-
|
|
28
|
-
/** A dashboard filter control's values: its own list, or the values of its member the viewer can see under the lock. */
|
|
29
|
-
function controlValues(
|
|
30
|
-
rt: Runtime,
|
|
31
|
-
ctx: Ctx,
|
|
32
|
-
a: { controlId: string; search?: string; limit?: number },
|
|
33
|
-
): Promise<ValuesResponse> {
|
|
34
|
-
const control = ctx.doc.controls.find((c) => c.id === a.controlId);
|
|
35
|
-
if (control?.kind !== "filter")
|
|
36
|
-
throw new HttpError(404, "NOT_FOUND", `There is no filter control "${a.controlId}".`);
|
|
37
|
-
const own = control.values;
|
|
38
|
-
if (typeof own === "object") return Promise.resolve({ values: [...own], more: false });
|
|
39
|
-
const x = {
|
|
40
|
-
rt,
|
|
41
|
-
request: ctx.request,
|
|
42
|
-
principal: ctx.principal,
|
|
43
|
-
now: ctx.now,
|
|
44
|
-
lock: documentLock(ctx.doc),
|
|
45
|
-
coverage: { applied: [], dropped: [] },
|
|
46
|
-
dashboardId: ctx.dashboardId,
|
|
47
|
-
};
|
|
48
|
-
return memberValues(x, { ...a, member: control.member });
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** Running a dashboard's tiles, records and control values by reference. */
|
|
52
|
-
function runEndpoints(rt: Runtime) {
|
|
53
|
-
return {
|
|
54
|
-
tile: createDashEndpoint("/dashboards/tile", {
|
|
55
|
-
method: "POST",
|
|
56
|
-
access: viewDashboard,
|
|
57
|
-
parse: (input) => parseWith(bodies.tile, input),
|
|
58
|
-
handler: async ({ body, principal, request }) =>
|
|
59
|
-
runTile(await tileContext(rt, { ...body, principal, request }), body),
|
|
60
|
-
}),
|
|
61
|
-
tiles: createDashEndpoint("/dashboards/tiles", {
|
|
62
|
-
method: "POST",
|
|
63
|
-
access: viewDashboard,
|
|
64
|
-
respond: "ndjson",
|
|
65
|
-
parse: (input) => parseWith(bodies.tiles, input),
|
|
66
|
-
handler: async ({ body, principal, request }) =>
|
|
67
|
-
streamTiles(await tileContext(rt, { ...body, principal, request }), body.tiles),
|
|
68
|
-
}),
|
|
69
|
-
records: createDashEndpoint("/dashboards/records", {
|
|
70
|
-
method: "POST",
|
|
71
|
-
access: viewDashboard,
|
|
72
|
-
parse: (input) => parseWith(bodies.records, input),
|
|
73
|
-
handler: async ({ body, principal, request }) => {
|
|
74
|
-
const x = tileExecution(await tileContext(rt, { ...body, principal, request }), body);
|
|
75
|
-
const after = body.after === undefined ? {} : { after: body.after };
|
|
76
|
-
return executeRecords(x, { limit: body.limit ?? RECORDS_LIMIT, ...after });
|
|
77
|
-
},
|
|
78
|
-
}),
|
|
79
|
-
controlValues: createDashEndpoint("/dashboards/control-values", {
|
|
80
|
-
method: "POST",
|
|
81
|
-
access: viewDashboard,
|
|
82
|
-
parse: (input) => parseWith(bodies.controlValues, input),
|
|
83
|
-
handler: async ({ body, principal, request }) =>
|
|
84
|
-
controlValues(rt, await tileContext(rt, { ...body, principal, request }), body),
|
|
85
|
-
}),
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** Dashboards by reference: listed, read, and their tiles, records and control values run. */
|
|
90
|
-
export function dashboardEndpoints(rt: Runtime) {
|
|
91
|
-
return runEndpoints(rt);
|
|
92
|
-
}
|
package/src/endpoints/drafts.ts
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { createDashEndpoint, type Doc, emptyDoc } from "@derive-to/dashboards-core";
|
|
2
|
-
import { bodies, parseWith } from "../body";
|
|
3
|
-
import { checkCommands, checkDocument, versionDoc } from "../checks";
|
|
4
|
-
import { servedAs } from "../dashboard-id";
|
|
5
|
-
import { editorOf, rowId, storeFor, targetOf } from "../editing";
|
|
6
|
-
import type { Runtime } from "../execute";
|
|
7
|
-
import { HttpError } from "../http";
|
|
8
|
-
import { dashboardResource } from "../owner";
|
|
9
|
-
import { userOf } from "../request";
|
|
10
|
-
import { editDashboard, editTenant } from "./access";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* A new dashboard's first document: empty, or a copy of a published version of another one,
|
|
14
|
-
* read by the one rule for earlier versions (`versionDoc`) and with every query one the caller
|
|
15
|
-
* may read, titled as they asked.
|
|
16
|
-
*/
|
|
17
|
-
async function firstDoc(
|
|
18
|
-
rt: Runtime,
|
|
19
|
-
request: Request,
|
|
20
|
-
user: ReturnType<typeof userOf>,
|
|
21
|
-
body: { title: string; from?: { dashboardId: string; version: number } },
|
|
22
|
-
): Promise<Doc> {
|
|
23
|
-
if (body.from === undefined) return emptyDoc(body.title);
|
|
24
|
-
const old = await versionDoc(rt, request, user, body.from);
|
|
25
|
-
await checkDocument(
|
|
26
|
-
rt,
|
|
27
|
-
user.viewer,
|
|
28
|
-
{ kind: "tenant" },
|
|
29
|
-
{ restored: old.doc, current: undefined },
|
|
30
|
-
);
|
|
31
|
-
return { ...old.doc, meta: { ...old.doc.meta, title: body.title } };
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** Opening a draft of a dashboard, or of a new one. */
|
|
35
|
-
function startEndpoints(rt: Runtime) {
|
|
36
|
-
return {
|
|
37
|
-
startDraft: createDashEndpoint("/dashboards/draft/start", {
|
|
38
|
-
method: "POST",
|
|
39
|
-
access: editDashboard,
|
|
40
|
-
parse: (input) => parseWith(bodies.dashboard, input),
|
|
41
|
-
handler: async ({ body, principal, request }) => {
|
|
42
|
-
const user = userOf(principal);
|
|
43
|
-
const draft = await storeFor(rt, request).startDraft({
|
|
44
|
-
...editorOf(user),
|
|
45
|
-
target: await targetOf(rt, request, user, body.dashboardId),
|
|
46
|
-
});
|
|
47
|
-
return servedAs(draft, body.dashboardId);
|
|
48
|
-
},
|
|
49
|
-
}),
|
|
50
|
-
newDashboard: createDashEndpoint("/dashboards/new", {
|
|
51
|
-
method: "POST",
|
|
52
|
-
access: editTenant,
|
|
53
|
-
parse: (input) => parseWith(bodies.newDashboard, input),
|
|
54
|
-
handler: async ({ body, principal, request }) => {
|
|
55
|
-
const user = userOf(principal);
|
|
56
|
-
const doc = await firstDoc(rt, request, user, body);
|
|
57
|
-
return storeFor(rt, request).startDraft({
|
|
58
|
-
...editorOf(user),
|
|
59
|
-
target: {
|
|
60
|
-
create: doc,
|
|
61
|
-
...(body.requestId === undefined ? {} : { requestId: body.requestId }),
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
},
|
|
65
|
-
}),
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/** Applying a batch of commands to the editor's open draft. */
|
|
70
|
-
function applyEndpoint(rt: Runtime) {
|
|
71
|
-
return createDashEndpoint("/dashboards/draft/apply", {
|
|
72
|
-
method: "POST",
|
|
73
|
-
access: editDashboard,
|
|
74
|
-
parse: (input) => parseWith(bodies.applyDraft, input),
|
|
75
|
-
handler: async ({ body, principal, request }) => {
|
|
76
|
-
const user = userOf(principal);
|
|
77
|
-
const ids = body.commands.map((c) => c.commandId);
|
|
78
|
-
const repeated = ids.find((cid, i) => ids.indexOf(cid) !== i);
|
|
79
|
-
if (repeated !== undefined)
|
|
80
|
-
throw new HttpError(
|
|
81
|
-
400,
|
|
82
|
-
"DUPLICATE_COMMAND_ID",
|
|
83
|
-
`The command id "${repeated}" is used twice in one batch; give each command its own.`,
|
|
84
|
-
);
|
|
85
|
-
const resource = await dashboardResource(rt.options, request, user, body.dashboardId);
|
|
86
|
-
await checkCommands(rt, user.viewer, resource, body.commands);
|
|
87
|
-
const applied = await storeFor(rt, request).applyDraft({
|
|
88
|
-
...editorOf(user),
|
|
89
|
-
id: await rowId(rt, request, user, body.dashboardId),
|
|
90
|
-
expectedRevision: body.expectedRevision,
|
|
91
|
-
commands: body.commands,
|
|
92
|
-
});
|
|
93
|
-
return { ...applied, draft: servedAs(applied.draft, body.dashboardId) };
|
|
94
|
-
},
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** Publishing the draft as the next version, or dropping it. */
|
|
99
|
-
function closeEndpoints(rt: Runtime) {
|
|
100
|
-
return {
|
|
101
|
-
publishDraft: createDashEndpoint("/dashboards/draft/publish", {
|
|
102
|
-
method: "POST",
|
|
103
|
-
access: editDashboard,
|
|
104
|
-
parse: (input) => parseWith(bodies.publishDraft, input),
|
|
105
|
-
handler: async ({ body, principal, request }) => {
|
|
106
|
-
const user = userOf(principal);
|
|
107
|
-
const editor = editorOf(user);
|
|
108
|
-
const published = await storeFor(rt, request).publish({
|
|
109
|
-
...editor,
|
|
110
|
-
id: await rowId(rt, request, user, body.dashboardId),
|
|
111
|
-
expectedRevision: body.expectedRevision,
|
|
112
|
-
requestId: body.requestId,
|
|
113
|
-
head: body.head,
|
|
114
|
-
});
|
|
115
|
-
return servedAs(published, body.dashboardId);
|
|
116
|
-
},
|
|
117
|
-
}),
|
|
118
|
-
discardDraft: createDashEndpoint("/dashboards/draft/discard", {
|
|
119
|
-
method: "POST",
|
|
120
|
-
access: editDashboard,
|
|
121
|
-
parse: (input) => parseWith(bodies.discardDraft, input),
|
|
122
|
-
handler: async ({ body, principal, request }) => {
|
|
123
|
-
const user = userOf(principal);
|
|
124
|
-
const { removed } = await storeFor(rt, request).discardDraft({
|
|
125
|
-
...editorOf(user),
|
|
126
|
-
id: await rowId(rt, request, user, body.dashboardId),
|
|
127
|
-
expectedRevision: body.expectedRevision,
|
|
128
|
-
});
|
|
129
|
-
return { discarded: true as const, removed };
|
|
130
|
-
},
|
|
131
|
-
}),
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/** Starting, editing, publishing and discarding a draft; and making a new dashboard. */
|
|
136
|
-
export function draftEndpoints(rt: Runtime) {
|
|
137
|
-
return { ...startEndpoints(rt), applyDraft: applyEndpoint(rt), ...closeEndpoints(rt) };
|
|
138
|
-
}
|
package/src/endpoints/index.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { Runtime } from "../execute";
|
|
2
|
-
import { dashboardEndpoints } from "./dashboards";
|
|
3
|
-
import { draftEndpoints } from "./drafts";
|
|
4
|
-
import { metaEndpoints } from "./meta";
|
|
5
|
-
import { queryEndpoints } from "./query";
|
|
6
|
-
import { readEndpoints } from "./read";
|
|
7
|
-
import { reviewEndpoints } from "./review";
|
|
8
|
-
import { versionEndpoints } from "./versions";
|
|
9
|
-
|
|
10
|
-
/** The endpoints every install has, by name. Each declares who may call it. */
|
|
11
|
-
export function coreEndpoints(rt: Runtime) {
|
|
12
|
-
const meta = metaEndpoints(rt);
|
|
13
|
-
const dashboards = { ...readEndpoints(rt), ...dashboardEndpoints(rt) };
|
|
14
|
-
const drafts = { ...draftEndpoints(rt), ...reviewEndpoints(rt) };
|
|
15
|
-
const query = queryEndpoints(rt);
|
|
16
|
-
const history = versionEndpoints(rt);
|
|
17
|
-
const names = [meta, dashboards, drafts, query, history].flatMap((g) => Object.keys(g));
|
|
18
|
-
const repeated = names.find((name, i) => names.indexOf(name) !== i);
|
|
19
|
-
if (repeated !== undefined) throw new Error(`Two core endpoints are named "${repeated}".`);
|
|
20
|
-
return { ...meta, ...dashboards, ...drafts, ...query, ...history };
|
|
21
|
-
}
|
package/src/endpoints/meta.ts
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createDashEndpoint,
|
|
3
|
-
type DimensionDef,
|
|
4
|
-
type MeasureDef,
|
|
5
|
-
type MetaMember,
|
|
6
|
-
memberLabel,
|
|
7
|
-
type SourceDef,
|
|
8
|
-
type Viewer,
|
|
9
|
-
} from "@derive-to/dashboards-core";
|
|
10
|
-
import { toolSchemas } from "@derive-to/dashboards-core/schema";
|
|
11
|
-
import { bodies, bodySchemas, parseWith } from "../body";
|
|
12
|
-
import type { Runtime } from "../execute";
|
|
13
|
-
import { nowOf, userOf } from "../request";
|
|
14
|
-
import { viewMember } from "./access";
|
|
15
|
-
import { memberValues } from "./values";
|
|
16
|
-
|
|
17
|
-
function dimensionType(def: DimensionDef): MetaMember["type"] {
|
|
18
|
-
if (def.kind === "number" || def.kind === "boolean" || def.kind === "time") return def.kind;
|
|
19
|
-
return def.kind === "expr" ? def.type : "string";
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function measureType(def: MeasureDef): MetaMember["type"] {
|
|
23
|
-
if (def.kind === "count") return "int";
|
|
24
|
-
if (def.kind === "percentile" || def.kind === "ratio") return "number";
|
|
25
|
-
return def.numeric === "float" ? "number" : "decimal";
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
type Member =
|
|
29
|
-
| { id: string; kind: "measure"; def: MeasureDef }
|
|
30
|
-
| { id: string; kind: "dimension"; def: DimensionDef };
|
|
31
|
-
|
|
32
|
-
/** A member as the viewer's meta lists it: label, type, and a measure's aggregate and unit. */
|
|
33
|
-
function memberMeta(m: Member): MetaMember & { kind: Member["kind"] } {
|
|
34
|
-
const base = { id: m.id, label: memberLabel(m.id, m.def.label) };
|
|
35
|
-
if (m.kind === "measure")
|
|
36
|
-
return {
|
|
37
|
-
kind: "measure",
|
|
38
|
-
...base,
|
|
39
|
-
type: measureType(m.def),
|
|
40
|
-
aggregate: m.def.kind,
|
|
41
|
-
...unitOf(m.def),
|
|
42
|
-
};
|
|
43
|
-
return { kind: "dimension", ...base, type: dimensionType(m.def), ...staticValues(m.def) };
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** The joins a query can group by: to one row each, never to many. */
|
|
47
|
-
function oneRowJoins(source: SourceDef) {
|
|
48
|
-
return Object.entries(source.joins ?? {})
|
|
49
|
-
.filter(([, j]) => j.relationship !== "one_to_many")
|
|
50
|
-
.map(([name, j]) => ({ name, label: memberLabel(name), source: j.to }));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function unitOf(def: MeasureDef): Pick<MetaMember, "unit"> {
|
|
54
|
-
return "unit" in def && def.unit !== undefined ? { unit: def.unit } : {};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** A dimension's own list of values, when the model gives one. */
|
|
58
|
-
function staticValues(def: DimensionDef): Pick<MetaMember, "values"> {
|
|
59
|
-
if (def.kind === "string" && def.values !== undefined)
|
|
60
|
-
return { values: def.values === "lookup" ? "lookup" : def.values };
|
|
61
|
-
return def.kind === "bucket" ? { values: def.labels } : {};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* What a viewer may use: every source and member `can` allows, never hidden or sensitive ones
|
|
66
|
-
* (unless the host allows the sensitive one), with labels, types and value lists. Never a
|
|
67
|
-
* table, column or expression.
|
|
68
|
-
*/
|
|
69
|
-
async function metaFor(rt: Runtime, viewer: Viewer) {
|
|
70
|
-
const { options } = rt;
|
|
71
|
-
const can = (kind: "source" | "member", id: string) =>
|
|
72
|
-
Promise.resolve(options.access.can(viewer, "view", { kind, id }));
|
|
73
|
-
const shown = (id: string, m: { hidden?: boolean; sensitive?: boolean }) =>
|
|
74
|
-
m.hidden !== true && (m.sensitive !== true || options.access.sensitive?.(viewer, id) === true);
|
|
75
|
-
const sources = await Promise.all(
|
|
76
|
-
Object.entries(options.model.sources).map(async ([name, source]) => {
|
|
77
|
-
if (!(await can("source", name))) return null;
|
|
78
|
-
const members = [
|
|
79
|
-
...Object.entries(source.measures).map(([n, m]) => ({
|
|
80
|
-
id: `${name}.${n}`,
|
|
81
|
-
def: m,
|
|
82
|
-
kind: "measure" as const,
|
|
83
|
-
})),
|
|
84
|
-
...Object.entries(source.dimensions).map(([n, d]) => ({
|
|
85
|
-
id: `${name}.${n}`,
|
|
86
|
-
def: d,
|
|
87
|
-
kind: "dimension" as const,
|
|
88
|
-
})),
|
|
89
|
-
].filter((m) => shown(m.id, m.def));
|
|
90
|
-
const allowed = await Promise.all(members.map((m) => can("member", m.id)));
|
|
91
|
-
const meta = members.filter((_m, i) => allowed[i] === true).map((m) => memberMeta(m));
|
|
92
|
-
return {
|
|
93
|
-
name,
|
|
94
|
-
label: memberLabel(name),
|
|
95
|
-
measures: meta.filter((m) => m.kind === "measure").map(({ kind: _k, ...m }) => m),
|
|
96
|
-
dimensions: meta.filter((m) => m.kind === "dimension").map(({ kind: _k, ...m }) => m),
|
|
97
|
-
joins: oneRowJoins(source),
|
|
98
|
-
};
|
|
99
|
-
}),
|
|
100
|
-
);
|
|
101
|
-
return { sources: sources.filter((s) => s !== null) };
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export function metaEndpoints(rt: Runtime) {
|
|
105
|
-
return {
|
|
106
|
-
meta: createDashEndpoint("/meta", {
|
|
107
|
-
method: "GET",
|
|
108
|
-
access: { kind: "signed_in" },
|
|
109
|
-
parse: (input) => parseWith(bodies.list, input),
|
|
110
|
-
handler: ({ principal }) => metaFor(rt, userOf(principal).viewer),
|
|
111
|
-
}),
|
|
112
|
-
/** The shapes an agent sends, as JSON Schema, to build its tools from. The same for everyone. */
|
|
113
|
-
schemas: createDashEndpoint("/meta/schemas", {
|
|
114
|
-
method: "GET",
|
|
115
|
-
access: { kind: "signed_in" },
|
|
116
|
-
parse: (input) => parseWith(bodies.list, input),
|
|
117
|
-
handler: () => ({ ...toolSchemas(), bodies: bodySchemas() }),
|
|
118
|
-
}),
|
|
119
|
-
memberValues: createDashEndpoint("/meta/values", {
|
|
120
|
-
method: "POST",
|
|
121
|
-
access: viewMember,
|
|
122
|
-
parse: (input) => parseWith(bodies.memberValues, input),
|
|
123
|
-
handler: ({ body, principal, request }) =>
|
|
124
|
-
memberValues(
|
|
125
|
-
{
|
|
126
|
-
rt,
|
|
127
|
-
request,
|
|
128
|
-
principal: userOf(principal),
|
|
129
|
-
now: nowOf(rt.options),
|
|
130
|
-
lock: [],
|
|
131
|
-
coverage: { applied: [], dropped: [] },
|
|
132
|
-
},
|
|
133
|
-
body,
|
|
134
|
-
),
|
|
135
|
-
}),
|
|
136
|
-
};
|
|
137
|
-
}
|
package/src/endpoints/query.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import type { Principal } from "@derive-to/dashboards-core";
|
|
2
|
-
import {
|
|
3
|
-
createDashEndpoint,
|
|
4
|
-
queryClosure,
|
|
5
|
-
recordDimensions,
|
|
6
|
-
type WireQuery,
|
|
7
|
-
} from "@derive-to/dashboards-core";
|
|
8
|
-
import { authorizeFree } from "../authorize";
|
|
9
|
-
import { bodies, parseWith } from "../body";
|
|
10
|
-
import { type Execution, execute, executeRecords, type Runtime } from "../execute";
|
|
11
|
-
import { calendarOf, nowOf, userOf } from "../request";
|
|
12
|
-
import { concurrencyOf, streamLines } from "../tiles";
|
|
13
|
-
|
|
14
|
-
const RECORDS_LIMIT = 50;
|
|
15
|
-
|
|
16
|
-
/** A free query, authorized for this viewer as one: `view` on every source and member it reads. */
|
|
17
|
-
async function freeExecution(
|
|
18
|
-
rt: Runtime,
|
|
19
|
-
principal: Principal | null,
|
|
20
|
-
request: Request,
|
|
21
|
-
query: WireQuery,
|
|
22
|
-
): Promise<Execution> {
|
|
23
|
-
const user = userOf(principal);
|
|
24
|
-
await authorizeFree(rt.options, user.viewer, query);
|
|
25
|
-
return {
|
|
26
|
-
rt,
|
|
27
|
-
request,
|
|
28
|
-
principal: user,
|
|
29
|
-
now: nowOf(rt.options),
|
|
30
|
-
calendar: calendarOf(rt.options),
|
|
31
|
-
query,
|
|
32
|
-
lock: [],
|
|
33
|
-
coverage: { applied: [], dropped: [] },
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** Free queries: the widget builder's preview, an agent's question. A different grant from viewing a dashboard. */
|
|
38
|
-
export function queryEndpoints(rt: Runtime) {
|
|
39
|
-
return {
|
|
40
|
-
queryRun: createDashEndpoint("/query/run", {
|
|
41
|
-
method: "POST",
|
|
42
|
-
access: { kind: "signed_in" },
|
|
43
|
-
parse: (input) => parseWith(bodies.query, input, "QUERY_INVALID"),
|
|
44
|
-
handler: async ({ body, principal, request }) =>
|
|
45
|
-
execute(await freeExecution(rt, principal, request, body.query)),
|
|
46
|
-
}),
|
|
47
|
-
queryBatch: createDashEndpoint("/query/batch", {
|
|
48
|
-
method: "POST",
|
|
49
|
-
access: { kind: "signed_in" },
|
|
50
|
-
respond: "ndjson",
|
|
51
|
-
parse: (input) => parseWith(bodies.queryBatch, input, "QUERY_INVALID"),
|
|
52
|
-
handler: ({ body, principal, request }) =>
|
|
53
|
-
streamLines(
|
|
54
|
-
body.queries.map(
|
|
55
|
-
(q) => async () => execute(await freeExecution(rt, principal, request, q)),
|
|
56
|
-
),
|
|
57
|
-
concurrencyOf(rt),
|
|
58
|
-
),
|
|
59
|
-
}),
|
|
60
|
-
queryRecords: createDashEndpoint("/query/records", {
|
|
61
|
-
method: "POST",
|
|
62
|
-
access: { kind: "signed_in" },
|
|
63
|
-
parse: (input) => parseWith(bodies.queryRecords, input, "QUERY_INVALID"),
|
|
64
|
-
handler: async ({ body, principal, request }) => {
|
|
65
|
-
// The rows show the source's record columns, so the viewer needs view on those too.
|
|
66
|
-
const columns = recordDimensions(rt.options.model, body.query.source).map((n, i) => ({
|
|
67
|
-
id: `d${i + 1}` as const,
|
|
68
|
-
member: `${body.query.source}.${n}`,
|
|
69
|
-
}));
|
|
70
|
-
await authorizeFree(rt.options, userOf(principal).viewer, {
|
|
71
|
-
...body.query,
|
|
72
|
-
dimensions: columns,
|
|
73
|
-
});
|
|
74
|
-
const x = await freeExecution(rt, principal, request, body.query);
|
|
75
|
-
const after = body.after === undefined ? {} : { after: body.after };
|
|
76
|
-
return executeRecords(x, { limit: body.limit ?? RECORDS_LIMIT, ...after });
|
|
77
|
-
},
|
|
78
|
-
}),
|
|
79
|
-
queryValidate: createDashEndpoint("/query/validate", {
|
|
80
|
-
method: "POST",
|
|
81
|
-
access: { kind: "signed_in" },
|
|
82
|
-
parse: (input) => parseWith(bodies.query, input, "QUERY_INVALID"),
|
|
83
|
-
handler: ({ body }) => ({ issues: queryClosure(rt.options.model, body.query).issues }),
|
|
84
|
-
}),
|
|
85
|
-
};
|
|
86
|
-
}
|