Skip to content

Commit 07f3224

Browse files
committed
feat: add originalQuery and totalsQuery (query with only metrics) to the analyzer output
1 parent a031fa2 commit 07f3224

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

src/__tests__/analyzer.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,25 @@ it("can generate queries for a hierarchy", () => {
710710
"tracks.track_id",
711711
"invoices.total",
712712
],
713+
originalQuery: {
714+
members: [
715+
"artists.name",
716+
"albums.title",
717+
"tracks.track_id",
718+
"customers.full_name",
719+
"customers.email",
720+
"customers.phone",
721+
"invoices.invoice_date",
722+
"invoices.total",
723+
],
724+
filters: [{ operator: "equals", member: "genres.name", value: ["Rock"] }],
725+
order: [{ member: "artists.name", direction: "asc" }],
726+
},
727+
totalsQuery: {
728+
members: ["invoices.total"],
729+
filters: [{ operator: "equals", member: "genres.name", value: ["Rock"] }],
730+
order: [{ member: "artists.name", direction: "asc" }],
731+
},
713732
queriesInfo: [
714733
{
715734
element: {
@@ -900,6 +919,25 @@ it("can generate queries for a hierarchy", () => {
900919
"invoices.invoice_date",
901920
"invoices.total",
902921
],
922+
originalQuery: {
923+
members: [
924+
"artists.name",
925+
"albums.title",
926+
"tracks.track_id",
927+
"customers.full_name",
928+
"customers.email",
929+
"customers.phone",
930+
"invoices.invoice_date",
931+
"invoices.total",
932+
],
933+
filters: [{ operator: "equals", member: "genres.name", value: ["Rock"] }],
934+
order: [{ member: "artists.name", direction: "asc" }],
935+
},
936+
totalsQuery: {
937+
members: ["invoices.total"],
938+
filters: [{ operator: "equals", member: "genres.name", value: ["Rock"] }],
939+
order: [{ member: "artists.name", direction: "asc" }],
940+
},
903941
queriesInfo: [
904942
{
905943
element: {
@@ -1056,6 +1094,22 @@ it("can generate queries for a hierarchy that wasn't present in the original que
10561094
"invoices.invoice_date",
10571095
"invoices.total",
10581096
],
1097+
originalQuery: {
1098+
members: [
1099+
"artists.name",
1100+
"albums.title",
1101+
"tracks.track_id",
1102+
"invoices.invoice_date",
1103+
"invoices.total",
1104+
],
1105+
filters: [{ operator: "equals", member: "genres.name", value: ["Rock"] }],
1106+
order: [{ member: "artists.name", direction: "asc" }],
1107+
},
1108+
totalsQuery: {
1109+
members: ["invoices.total"],
1110+
filters: [{ operator: "equals", member: "genres.name", value: ["Rock"] }],
1111+
order: [{ member: "artists.name", direction: "asc" }],
1112+
},
10591113
queriesInfo: [
10601114
{
10611115
element: {

src/lib/query-builder/analyzer.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,14 @@ export function analyzeQueryHierarchy(
186186
...queriesForHierarchy.restDimensions,
187187
...analysis.metrics,
188188
],
189-
// Repeat the last query because we will add all the rest dimensions to it
189+
originalQuery: {
190+
...analysis.query,
191+
},
192+
totalsQuery: {
193+
...analysis.query,
194+
members: [...analysis.metrics],
195+
},
196+
// Repeat the last query because we will add all the rest dimensions to it (this will be the "expanded" level)
190197
queriesInfo: [
191198
...queriesForHierarchy.queriesInfo,
192199
queriesForHierarchy.queriesInfo[
@@ -200,6 +207,9 @@ export function analyzeQueryHierarchy(
200207
})),
201208
...(analysis.query.order ?? []),
202209
];
210+
// We check for the length instead of length - 1 because we've duplicated the last query to add all the rest dimensions so the length of the array we're iterating over is one element longer thant the queriesForHierarchy.queriesInfo.length
211+
const isExpandedLevel = idx === queriesForHierarchy.queriesInfo.length;
212+
203213
return {
204214
element: queryInfo.element,
205215
keyDimensions: [
@@ -215,10 +225,7 @@ export function analyzeQueryHierarchy(
215225
...queryInfo.formatDimensions,
216226
...queryInfo.prevLevelsExtraDimensions,
217227
...queryInfo.extraDimensions,
218-
// We check for the length instead of length - 1 because we've duplicated the last query to add all the rest dimensions so the length of the array we're iterating over is one element longer thant the queriesForHierarchy.queriesInfo.length
219-
...(idx === queriesForHierarchy.queriesInfo.length
220-
? queriesForHierarchy.restDimensions
221-
: []),
228+
...(isExpandedLevel ? queriesForHierarchy.restDimensions : []),
222229
...analysis.metrics,
223230
]),
224231
],

0 commit comments

Comments
 (0)