Skip to content

Commit b2eeba2

Browse files
committed
feat: refactor query configuration types and update widget data handling for resource and steps queries
1 parent 372e238 commit b2eeba2

7 files changed

Lines changed: 139 additions & 73 deletions

File tree

custom/model/dashboard.types.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ export type QueryOrderByItem = {
117117
direction?: 'asc' | 'desc'
118118
}
119119

120-
export type QueryConfig = {
120+
export type ResourceQueryConfig = {
121+
source?: 'resource'
121122
resource: string
122123
select?: QuerySelectItem[]
123124
sparkline?: {
@@ -139,17 +140,31 @@ export type QueryConfig = {
139140
formatting?: Record<string, JsonValue>
140141
}
141142

142-
export type FunnelQueryConfig = {
143-
steps: FunnelQueryStep[]
143+
export type StepsQueryStepConfig =
144+
| {
145+
name: string
146+
resource: string
147+
metric: QueryAggregateSelectItem
148+
filters?: FilterExpression
149+
}
150+
| {
151+
name: string
152+
resource: string
153+
select: QueryAggregateSelectItem[]
154+
filters?: FilterExpression
155+
}
156+
157+
export type StepsQueryConfig = {
158+
source: 'steps'
159+
steps: StepsQueryStepConfig[]
144160
calcs?: QueryCalcSelectItem[]
161+
order_by?: QueryOrderByItem[]
162+
limit?: number
163+
offset?: number
164+
formatting?: Record<string, JsonValue>
145165
}
146166

147-
export type FunnelQueryStep = {
148-
name: string
149-
resource: string
150-
metric: QueryAggregateSelectItem
151-
filters?: FilterExpression
152-
}
167+
export type QueryConfig = ResourceQueryConfig | StepsQueryConfig
153168

154169
export type FieldRef = string | {
155170
field: string
@@ -246,7 +261,7 @@ export type TableWidgetConfig = WidgetBaseConfig & {
246261
export type ChartDashboardWidgetConfig = WidgetBaseConfig & {
247262
target: 'chart'
248263
chart: ChartWidgetConfig
249-
query: QueryConfig | FunnelQueryConfig
264+
query: QueryConfig
250265
}
251266

252267
export type KpiCardWidgetConfig = WidgetBaseConfig & {

custom/skills/adminforth-dashboard/SKILL.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ Use resource, not resourceId.
151151

152152
## Query shape rules
153153

154-
Use dashboard_configure_funnel_chart_widget for funnel charts and set query.steps.
155-
Do not use query.steps for kpi_card, gauge_card, table, pivot_table, line, bar, stacked bar, pie, or histogram charts.
154+
All chart widgets, including funnel charts, use the same query shape.
155+
Use a single-resource query by default.
156156

157157
For kpi_card and normal charts, use:
158158
- query.resource
@@ -162,6 +162,29 @@ For kpi_card and normal charts, use:
162162
- optional query.order_by
163163
- optional query.calcs
164164

165+
For multi-resource charts or widgets, use the general steps source:
166+
167+
query:
168+
source: steps
169+
steps:
170+
- name: Leads
171+
resource: leads
172+
metric:
173+
agg: count
174+
as: value
175+
- name: Customers
176+
resource: orders
177+
metric:
178+
agg: count_distinct
179+
field: customer_id
180+
as: value
181+
182+
Each step may use either:
183+
- metric for one aggregate
184+
- select for multiple aggregate fields
185+
186+
Do not use bare query.steps without source: steps.
187+
165188
## Date range rules
166189

167190
Use only query.filters for time ranges.
@@ -203,7 +226,7 @@ select raw token totals:
203226
then query.calcs:
204227
- calculate total_spend from those aliases and lookup variables
205228

206-
For today vs yesterday KPI, use multiple aggregate select items with filters and distinct aliases, then calcs. Do not use query.steps.
229+
For today vs yesterday KPI, use multiple aggregate select items with filters and distinct aliases, then calcs.
207230

208231
## Calc variables
209232

schema/api.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { toJSONSchema, z } from 'zod'
22
import {
33
BarChartSchema,
44
FunnelChartSchema,
5-
FunnelQueryConfigSchema,
65
GaugeCardViewConfigSchema,
76
HistogramChartSchema,
87
KpiCardViewConfigSchema,
@@ -153,7 +152,7 @@ const ConfigurableHistogramChartWidgetConfigSchema = WidgetEditableBaseSchema.ex
153152
const ConfigurableFunnelChartWidgetConfigSchema = WidgetEditableBaseSchema.extend({
154153
target: z.literal('chart'),
155154
chart: FunnelChartSchema,
156-
query: FunnelQueryConfigSchema,
155+
query: QueryConfigSchema,
157156
})
158157

159158
const ConfigurablePivotTableWidgetConfigSchema = WidgetEditableBaseSchema.extend({

schema/widget.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export {
3333
} from './widgets/table.js'
3434
export {
3535
EmptyWidgetConfigSchema,
36-
FunnelQueryConfigSchema,
3736
QueryConfigSchema,
3837
WidgetEditableBaseSchema,
3938
} from './widgets/common.js'

schema/widgets/charts.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { z } from 'zod'
22
import {
33
ChartFieldRefSchema,
4-
FunnelQueryConfigSchema,
54
QueryConfigSchema,
65
WidgetBaseSchema,
76
} from './common.js'
@@ -100,7 +99,7 @@ export const HistogramChartWidgetConfigSchema = WidgetBaseSchema.extend({
10099
export const FunnelChartWidgetConfigSchema = WidgetBaseSchema.extend({
101100
target: z.literal('chart'),
102101
chart: FunnelChartSchema,
103-
query: FunnelQueryConfigSchema,
102+
query: QueryConfigSchema,
104103
})
105104

106105
export const ChartWidgetTargetConfigSchema = z.union([

schema/widgets/common.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ export const QueryCalcItemSchema = z.object({
140140
as: z.string(),
141141
}).strict()
142142

143-
export const QueryConfigSchema = z.object({
143+
const ResourceQueryConfigSchema = z.object({
144+
source: z.literal('resource').optional(),
144145
resource: z.string(),
145146
select: z.array(QuerySelectItemSchema).optional(),
146147
sparkline: z.object({
@@ -159,18 +160,36 @@ export const QueryConfigSchema = z.object({
159160
formatting: z.record(z.string(), z.unknown()).optional(),
160161
}).strict()
161162

162-
const FunnelQueryStepSchema = z.object({
163+
const StepsQueryMetricStepSchema = z.object({
163164
name: z.string(),
164165
resource: z.string(),
165166
metric: QueryAggregateSelectItemSchema,
166167
filters: FilterExpressionSchema.optional(),
167168
}).strict()
168169

169-
export const FunnelQueryConfigSchema = z.object({
170-
steps: z.array(FunnelQueryStepSchema).min(1),
171-
calcs: z.array(QueryCalcItemSchema).optional(),
170+
const StepsQuerySelectStepSchema = z.object({
171+
name: z.string(),
172+
resource: z.string(),
173+
select: z.array(QueryAggregateSelectItemSchema).min(1),
174+
filters: FilterExpressionSchema.optional(),
172175
}).strict()
173176

177+
export const QueryConfigSchema = z.union([
178+
ResourceQueryConfigSchema,
179+
z.object({
180+
source: z.literal('steps'),
181+
steps: z.array(z.union([
182+
StepsQueryMetricStepSchema,
183+
StepsQuerySelectStepSchema,
184+
])).min(1),
185+
calcs: z.array(QueryCalcItemSchema).optional(),
186+
order_by: z.array(QueryOrderByItemSchema).optional(),
187+
limit: z.number().int().positive().optional(),
188+
offset: z.number().int().nonnegative().optional(),
189+
formatting: z.record(z.string(), z.unknown()).optional(),
190+
}).strict(),
191+
])
192+
174193
export const WidgetPersistedFieldsSchema = z.object({
175194
id: z.string(),
176195
group_id: z.string(),

0 commit comments

Comments
 (0)