-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UI-9384 - Preserve KPI titles when migrated KPI widgets from 4.3 to 5.0
- Loading branch information
Showing
6 changed files
with
185 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { dataModelsForTests } from "@activeviam/data-model-5.0"; | ||
import { getMigratedKpiTitles } from "./getMigratedKpiTitles"; | ||
import { legacyKpi } from "./__test_resources__/legacyKpi"; | ||
|
||
const cube = dataModelsForTests.sandbox.catalogs[0].cubes[0]; | ||
|
||
describe("getMigratedKpiTitles", () => { | ||
it("returns the migrated KPI titles corresponding to the legacy KPI state, ready to be used in Atoti UI 5.0", () => { | ||
const migratedKpiTitles = getMigratedKpiTitles(legacyKpi, { | ||
cube, | ||
mapping: { | ||
columns: [{ type: "allMeasures" }], | ||
measures: [{ type: "measure", measureName: "contributors.COUNT" }], | ||
rows: [ | ||
{ | ||
type: "hierarchy", | ||
dimensionName: "Currency", | ||
hierarchyName: "Currency", | ||
levelName: "Currency", | ||
}, | ||
], | ||
}, | ||
}); | ||
expect(migratedKpiTitles).toMatchInlineSnapshot(` | ||
{ | ||
"[Measures].[contributors.COUNT],[Currency].[Currency].[AllMember].[EUR, USD]": "Hello World", | ||
} | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { | ||
Cube, | ||
DataVisualizationWidgetMapping, | ||
KpiWidgetState, | ||
MdxUnknownCompoundIdentifier, | ||
parse, | ||
} from "@activeviam/activeui-sdk-5.0"; | ||
import { getSpecificCompoundIdentifier, quote } from "@activeviam/mdx-5.0"; | ||
|
||
interface LegacyKpiTitle { | ||
title: string; | ||
tuple: { [hierarchyUniqueName: string]: string[] }; | ||
} | ||
|
||
/** | ||
* Returns the legacy KPI titles attached to `legacyKpiState`. | ||
*/ | ||
function _getLegacyKpiTitles( | ||
// Legacy widget states are not typed. | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
legacyKpiState: any, | ||
{ cube }: { cube: Cube }, | ||
): LegacyKpiTitle[] { | ||
const locations = | ||
legacyKpiState?.value?.body?.configuration?.featuredValues?.locations; | ||
|
||
if (!locations) { | ||
return []; | ||
} | ||
|
||
const legacyTitles: LegacyKpiTitle[] = []; | ||
|
||
for (const tupleKey in locations) { | ||
const { title } = locations[tupleKey]; | ||
const tuple: { | ||
[hierarchyUniqueName: string]: string[]; | ||
} = {}; | ||
const memberUniqueNames = tupleKey.split(/,(?![^\[]*\])/); | ||
for (const memberUniqueName of memberUniqueNames) { | ||
const compoundIdentifier = | ||
parse<MdxUnknownCompoundIdentifier>(memberUniqueName); | ||
const specificCompoundIdentifier = getSpecificCompoundIdentifier( | ||
compoundIdentifier, | ||
{ cube }, | ||
); | ||
if (specificCompoundIdentifier.type === "measure") { | ||
tuple[`[Measures].[Measures]`] = [ | ||
specificCompoundIdentifier.measureName, | ||
]; | ||
} else if (specificCompoundIdentifier.type === "member") { | ||
tuple[ | ||
`[${specificCompoundIdentifier.dimensionName}].[${specificCompoundIdentifier.hierarchyName}]` | ||
] = specificCompoundIdentifier.path; | ||
} | ||
} | ||
legacyTitles.push({ title, tuple }); | ||
} | ||
|
||
return legacyTitles; | ||
} | ||
|
||
/** | ||
* Returns the migrated KPI widget titles corresponding to legacyKpiState. | ||
*/ | ||
export function getMigratedKpiTitles( | ||
// Legacy widget states are not typed. | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
legacyKpiState: any, | ||
{ mapping, cube }: { mapping: DataVisualizationWidgetMapping; cube: Cube }, | ||
): KpiWidgetState["titles"] { | ||
const legacyTitles = _getLegacyKpiTitles(legacyKpiState, { cube }); | ||
const migratedTitles: KpiWidgetState["titles"] = {}; | ||
|
||
const memberUniqueNames: string[] = []; | ||
legacyTitles.forEach(({ title, tuple }) => { | ||
// Atoti UI 5.0 KPI widgets expect tuple keys to express members in the following order: | ||
// - column fields first | ||
// - then row fields | ||
const fields = [...(mapping.columns ?? []), ...(mapping.rows ?? [])]; | ||
fields.forEach((field) => { | ||
if (field.type === "allMeasures") { | ||
const measureName = tuple[`[Measures].[Measures]`]?.[0]; | ||
if (measureName) { | ||
memberUniqueNames.push(`[Measures].[${measureName}]`); | ||
} | ||
} else if (field.type === "hierarchy") { | ||
const hierarchyUniqueName = `[${field.dimensionName}].[${field.hierarchyName}]`; | ||
const namePath = tuple[hierarchyUniqueName]; | ||
if (namePath) { | ||
memberUniqueNames.push( | ||
`${hierarchyUniqueName}.${quote(...namePath)}`, | ||
); | ||
} | ||
} | ||
}); | ||
const tupleKey = memberUniqueNames.join(","); | ||
migratedTitles[tupleKey] = title; | ||
}); | ||
|
||
return migratedTitles; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters