Skip to content

Commit

Permalink
feat: add originalQuery and totalsQuery (query with only metrics) to …
Browse files Browse the repository at this point in the history
…the analyzer output
  • Loading branch information
retro committed Sep 4, 2024
1 parent e07ce4a commit 75f2cf2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
54 changes: 54 additions & 0 deletions src/__tests__/analyzer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,25 @@ it("can generate queries for a hierarchy", () => {
"tracks.track_id",
"invoices.total",
],
originalQuery: {
members: [
"artists.name",
"albums.title",
"tracks.track_id",
"customers.full_name",
"customers.email",
"customers.phone",
"invoices.invoice_date",
"invoices.total",
],
filters: [{ operator: "equals", member: "genres.name", value: ["Rock"] }],
order: [{ member: "artists.name", direction: "asc" }],
},
totalsQuery: {
members: ["invoices.total"],
filters: [{ operator: "equals", member: "genres.name", value: ["Rock"] }],
order: [{ member: "artists.name", direction: "asc" }],
},
queriesInfo: [
{
element: {
Expand Down Expand Up @@ -900,6 +919,25 @@ it("can generate queries for a hierarchy", () => {
"invoices.invoice_date",
"invoices.total",
],
originalQuery: {
members: [
"artists.name",
"albums.title",
"tracks.track_id",
"customers.full_name",
"customers.email",
"customers.phone",
"invoices.invoice_date",
"invoices.total",
],
filters: [{ operator: "equals", member: "genres.name", value: ["Rock"] }],
order: [{ member: "artists.name", direction: "asc" }],
},
totalsQuery: {
members: ["invoices.total"],
filters: [{ operator: "equals", member: "genres.name", value: ["Rock"] }],
order: [{ member: "artists.name", direction: "asc" }],
},
queriesInfo: [
{
element: {
Expand Down Expand Up @@ -1056,6 +1094,22 @@ it("can generate queries for a hierarchy that wasn't present in the original que
"invoices.invoice_date",
"invoices.total",
],
originalQuery: {
members: [
"artists.name",
"albums.title",
"tracks.track_id",
"invoices.invoice_date",
"invoices.total",
],
filters: [{ operator: "equals", member: "genres.name", value: ["Rock"] }],
order: [{ member: "artists.name", direction: "asc" }],
},
totalsQuery: {
members: ["invoices.total"],
filters: [{ operator: "equals", member: "genres.name", value: ["Rock"] }],
order: [{ member: "artists.name", direction: "asc" }],
},
queriesInfo: [
{
element: {
Expand Down
17 changes: 12 additions & 5 deletions src/lib/query-builder/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,14 @@ export function analyzeQueryHierarchy(
...queriesForHierarchy.restDimensions,
...analysis.metrics,
],
// Repeat the last query because we will add all the rest dimensions to it
originalQuery: {
...analysis.query,
},
totalsQuery: {
...analysis.query,
members: [...analysis.metrics],
},
// Repeat the last query because we will add all the rest dimensions to it (this will be the "expanded" level)
queriesInfo: [
...queriesForHierarchy.queriesInfo,
queriesForHierarchy.queriesInfo[
Expand All @@ -200,6 +207,9 @@ export function analyzeQueryHierarchy(
})),
...(analysis.query.order ?? []),
];
// 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
const isExpandedLevel = idx === queriesForHierarchy.queriesInfo.length;

return {
element: queryInfo.element,
keyDimensions: [
Expand All @@ -215,10 +225,7 @@ export function analyzeQueryHierarchy(
...queryInfo.formatDimensions,
...queryInfo.prevLevelsExtraDimensions,
...queryInfo.extraDimensions,
// 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
...(idx === queriesForHierarchy.queriesInfo.length
? queriesForHierarchy.restDimensions
: []),
...(isExpandedLevel ? queriesForHierarchy.restDimensions : []),
...analysis.metrics,
]),
],
Expand Down

0 comments on commit 75f2cf2

Please sign in to comment.