Skip to content
This repository was archived by the owner on Sep 4, 2024. It is now read-only.

Commit 055f583

Browse files
committed
[feat] Changed the data structure for queries to enable multiple group bys
1 parent 213205d commit 055f583

File tree

6 files changed

+40
-20
lines changed

6 files changed

+40
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file.
66
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
77

88
## [Unreleased]
9+
### Changed
10+
- Changed the data structure for queries to enable multiple group bys
911

1012
## [0.0.68] 2023-05-29
1113
### Changed

src/commands/deploy/handlers/validators.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,26 @@ const querySchema = object({
106106
.default(undefined)
107107
.noUnknown(true)
108108
.strict(),
109-
groupBy: object({
110-
type: string().oneOf(groupByTypes).min(1).required(),
109+
groupBys: array()
110+
.of(
111+
object({
112+
type: string().oneOf(groupByTypes).min(1).required(),
113+
value: string().min(1).required(),
114+
})
115+
.noUnknown(true)
116+
.strict(),
117+
)
118+
.optional(),
119+
orderBy: object({
111120
value: string().min(1).required(),
112-
orderBy: string().min(1).optional(),
113-
limit: number().min(1).optional(),
114121
order: string().oneOf(["ASC", "DESC"]).optional(),
115122
})
116123
.nullable()
117124
.optional()
118125
.default(undefined)
119126
.noUnknown(true)
120127
.strict(),
128+
limit: number().min(1).optional(),
121129
})
122130
.noUnknown(true)
123131
.required()
@@ -350,7 +358,7 @@ function validateQueries(queries: any[] = []) {
350358
const res = await querySchema.validate(item);
351359
const filters = res.properties.parameters.filters;
352360
const calculations = res.properties.parameters.calculations;
353-
const groupBy = res.properties.parameters.groupBy;
361+
const orderBy = res.properties.parameters.orderBy;
354362

355363
filters?.forEach((filter) => {
356364
parseFilter(filter);
@@ -364,8 +372,8 @@ function validateQueries(queries: any[] = []) {
364372
throw new Error("Aliases must me unique across all calculation / visualisation .");
365373
}
366374

367-
if (groupBy?.orderBy && !calcs?.some((c) => getCalculationAlias(c) === groupBy?.orderBy)) {
368-
throw new Error("The orderBy field of the groupBy must be present in the calculations / visualisations.");
375+
if (orderBy?.value && !calcs?.some((c) => getCalculationAlias(c) === orderBy?.value)) {
376+
throw new Error("The orderBy must be present in the visualisations.");
369377
}
370378
} catch (error) {
371379
const message = `query: ${item.id}: ${error}`;

src/commands/pull/handlers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ function convertQuery(query: Query): DeploymentQuery {
153153
matchCase: query.parameters.needle.matchCase,
154154
}
155155
: undefined,
156-
groupBy: query.parameters.groupBy,
156+
groupBys: query.parameters.groupBys,
157+
orderBy: query.parameters.orderBy,
158+
limit: query.parameters.limit,
157159
filterCombination: query.parameters.filterCombination,
158160
filters: query.parameters.filters?.map((filter) => `${filter.key} ${filter.operation} ${filter.value}`),
159161
calculations: query.parameters.calculations?.map((calculation) => {

src/commands/query/handlers/handlers.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,14 @@ async function interactive(input: { queryId: string; service: string; format: Ou
9595
// only numeric
9696
applicableKeys.filter((keySet) => keySet.type === "number"),
9797
);
98-
let groupBy;
98+
let groupBys;
99+
let orderBy;
100+
let limit;
99101
if (calculations.length) {
100-
groupBy = await promptGroupBy(applicableKeys, calculations);
102+
const res = await promptGroupBy(applicableKeys, calculations);
103+
groupBys = res?.type ? [{ value: res?.value, type: res?.type }] : undefined;
104+
orderBy = res?.orderBy ? { value: res?.orderBy, order: res?.order } : undefined;
105+
limit = res?.limit ? limit : undefined;
101106
}
102107

103108
const filters = await promptFilters(applicableKeys);
@@ -121,7 +126,9 @@ async function interactive(input: { queryId: string; service: string; format: Ou
121126
...filter,
122127
operation: filter.operator,
123128
})),
124-
groupBy,
129+
groupBys,
130+
orderBy,
131+
limit,
125132
},
126133
config,
127134
});

src/services/api/paths/queries.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ export interface QueryParameters {
55
filters?: Array<QueryFilter>;
66
filterCombination: "AND" | "OR";
77
calculations?: QueryCalculation[];
8-
groupBy?: QueryGroupBy;
8+
groupBys?: QueryGroupBy[];
9+
orderBy?: {
10+
value: string;
11+
order?: "ASC" | "DESC";
12+
};
13+
limit?: number;
914
needle?: SearchNeedle;
1015
}
1116

@@ -19,9 +24,6 @@ export interface QueryFilter {
1924
export interface QueryGroupBy {
2025
type: "string" | "number" | "boolean";
2126
value: string;
22-
orderBy?: string;
23-
limit?: number;
24-
order?: "ASC" | "DESC";
2527
}
2628

2729
export interface QueryCalculation {

src/services/api/paths/query-runs.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,12 @@ export interface QueryRunCreateParams {
7171
isRegex: boolean;
7272
matchCase: boolean;
7373
};
74-
groupBy?: {
75-
limit: number;
76-
order: string;
77-
orderBy: string;
74+
groupBys?: {
7875
type: string;
7976
value: string;
80-
};
77+
}[];
78+
orderBy?: { value: string; order: string };
79+
limit?: number;
8180
};
8281
config: UserConfig;
8382
}

0 commit comments

Comments
 (0)