Skip to content

Commit

Permalink
Merge pull request #5108 from gooddata/dho-cq-603-rollup-ui-3
Browse files Browse the repository at this point in the history
fix: add native total handling to tiger converters
  • Loading branch information
no23reason authored Jul 10, 2024
2 parents 9abe7cc + 30b7b2a commit e5fd960
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// (C) 2022 GoodData Corporation
// (C) 2022-2024 GoodData Corporation
import {
isMeasureGroupIdentifier,
DataValue,
IExecutionDefinition,
IResultHeader,
isResultTotalHeader,
TotalType,
} from "@gooddata/sdk-model";
import { DimensionHeader, ExecutionResultGrandTotal } from "@gooddata/api-client-tiger";
import isEmpty from "lodash/isEmpty.js";
Expand Down Expand Up @@ -155,7 +156,7 @@ function transformGrandTotal(
const data = transformedTotals[totalType];
return {
data,
defIdx: totalDefinitions.findIndex((def) => def.type === totalType),
defIdx: totalDefinitions.findIndex((def) => def.type === stringToTotalType(totalType)),
};
})
.filter((total) => total.defIdx !== -1)
Expand Down Expand Up @@ -183,3 +184,25 @@ function getHeadersAtPosition(
return headerGroups.map((group) => group[coord]);
});
}

/**
* Convert a string total type to a TotalType so that it is safe to compare with total definitions.
*/
function stringToTotalType(type: string): TotalType {
switch (type.toLowerCase()) {
case "sum":
return "sum";
case "max":
return "max";
case "min":
return "min";
case "avg":
return "avg";
case "med":
return "med";
case "native":
return "nat";
default:
throw new Error(`Unknown total type: ${type}`);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// (C) 2007-2022 GoodData Corporation
// (C) 2007-2024 GoodData Corporation
import isEqual from "lodash/isEqual.js";
import {
bucketsFind,
Expand All @@ -18,7 +18,7 @@ import flatMap from "lodash/flatMap.js";
import { Total, TotalDimension, TotalFunctionEnum } from "@gooddata/api-client-tiger";
import { dimensionLocalIdentifier } from "./DimensionsConverter.js";

const TOTAL_ORDER: TotalFunctionEnum[] = ["SUM", "MAX", "MIN", "AVG", "MED"];
const TOTAL_ORDER: TotalFunctionEnum[] = ["SUM", "MAX", "MIN", "AVG", "MED", "NATIVE"];

const ATTRIBUTE = "attribute";
const COLUMNS = "columns";
Expand All @@ -38,6 +38,7 @@ function enrichTotalWithMeasureIndex(total: Total, measures: IMeasure[]) {
total,
};
}

/**
* Extracts total definitions from execution definition dimensions and converts them into total specifications for
* Tiger AFM. Execution definition defines totals by a measure, aggregation function, and the attribute for whose
Expand Down Expand Up @@ -383,8 +384,10 @@ function convertTotalType(type: TotalType): TotalFunctionEnum {
if (type === "med") {
return TotalFunctionEnum.MED;
}
// type === "nat"
throw new Error("Tiger backend does not support native totals.");
if (type === "nat") {
return TotalFunctionEnum.NATIVE;
}
throw new Error(`Unknown total type "${type}".`);
}

function getAttributeBucketIdentifiers(attribute: IAttribute[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,10 @@ describe("convertTotals", () => {
["two subtotals and marginal total", Test12],
["row total and column subtotal", Test13],
["column total and row subtotal", Test14],
["native total", Test15],
["subtotals with non-sorted totals", Test16],
];
const ErrorScenarios: Array<[string, IExecutionDefinition]> = [["native total", Test15]];

it.each(Scenarios)("should correctly convert %s", (_desc, def) => {
expect(convertTotals(def)).toMatchSnapshot();
});
it.each(ErrorScenarios)("should fail on converting %s", (_desc, def) => {
expect(() => convertTotals(def)).toThrowErrorMatchingSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,24 @@ exports[`convertTotals > should correctly convert multiple totals with various f
]
`;

exports[`convertTotals > should correctly convert native total 1`] = `
[
{
"function": "NATIVE",
"localIdentifier": "total_nat_m1_by_localAttr1_0",
"metric": "m1",
"totalDimensions": [
{
"dimensionIdentifier": "dim_1",
"totalDimensionItems": [
"measureGroup",
],
},
],
},
]
`;

exports[`convertTotals > should correctly convert no totals 1`] = `[]`;

exports[`convertTotals > should correctly convert row total and column subtotal 1`] = `
Expand Down Expand Up @@ -607,5 +625,3 @@ exports[`convertTotals > should correctly convert two totals with different func
},
]
`;

exports[`convertTotals > should fail on converting native total 1`] = `[Error: Tiger backend does not support native totals.]`;
3 changes: 2 additions & 1 deletion libs/sdk-ui-pivot/src/locales.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// (C) 2019-2023 GoodData Corporation
// (C) 2019-2024 GoodData Corporation
import { MessageDescriptor, defineMessages } from "react-intl";

//NOTE: Follow up ticket for move all messages: https://gooddata.atlassian.net/browse/FET-1050
Expand All @@ -9,6 +9,7 @@ export const messages: Record<string, MessageDescriptor> = defineMessages({
max: { id: "visualizations.totals.dropdown.title.max" },
nat: { id: "visualizations.totals.dropdown.title.nat" },
med: { id: "visualizations.totals.dropdown.title.med" },
native: { id: "visualizations.totals.dropdown.title.nat" },
"disabled.mvf": { id: "visualizations.totals.dropdown.tooltip.nat.disabled.mvf" },
"disabled.ranking": { id: "visualizations.totals.dropdown.tooltip.nat.disabled.ranking" },
});

0 comments on commit e5fd960

Please sign in to comment.