Evan Shen
08/18/2021, 10:38 PMavailableItems() factory from IWorkspaceCatalog using the forInsight chained method, but it expects an IInsightDefinition and that’s not a kind of object I have unless I create a new insight. Is there a way to do something like forInsight but with an insight identifier? Or get an IInsightDefinition from an identifier? If not that then what’s the best way to accomplish what I’m trying to do? I should note that I will want to do this for multiple insights at a time so I’m hoping it won’t require too many consecutive calls.Dan Homola
08/19/2021, 7:26 AMgetInsight function in the insigts part of the Analytical workspace. Assuming you have the instance of the Analytical backend stored in a variable called backend, this should be doing what you are trying to do:
import { idRef } from "@gooddata/sdk-model";
async function getAvailableAttributesForInsight(id) {
const [catalog, insight] = await Promise.all([
backend.workspace("WORKSPACE_ID").catalog().load(),
backend.workspace("WORKSPACE_ID").insights().getInsight(idRef(id)),
]);
return catalog.availableItems().forInsight(insight).forTypes(["attribute"]).load();
}
// or for multiple insights
async function getAvailableAttributesForInsights(ids) {
const [catalog, insights] = await Promise.all([
backend.workspace("WORKSPACE_ID").catalog().load(),
Promise.all(ids.map((id) => backend.workspace("WORKSPACE_ID").insights().getInsight(idRef(id)))),
]);
const availableAttributes = await Promise.all(
insights.map((insight) =>
catalog.availableItems().forInsight(insight).forTypes(["attribute"]).load(),
),
);
// return the results as object mapping id -> available attributes
return ids.reduce((acc, id, index) => {
acc[id] = availableAttributes[index];
return acc;
}, {});
}
The functions shown try to do as much network requests in parallel as possible.
Hope this helps!Evan Shen
08/19/2021, 7:16 PMDan Homola
08/20/2021, 7:48 AM// load the whole catalog once
const catalog = await backend.workspace("WORKSPACE_ID").catalog().load();
// just for illustration, these can be parallelized by Promise.all or other means
const insight1 = await backend.workspace("WORKSPACE_ID").insights().getInsight(idRef("INSIGHT1_ID"));
const insight2 = await backend.workspace("WORKSPACE_ID").insights().getInsight(idRef("INSIGHT2_ID"));
// you can reuse the `catalog` object so that the preloaded data is reused as well and only data needed to filter them
// according to the individual insights is downloaded
const insight1AvailableAttrs = await catalog.availableItems().forInsight(insight1).forTypes(["attribute"]).load();
const insight2AvailableAttrs = await catalog.availableItems().forInsight(insight2).forTypes(["attribute"]).load();
Does this help you or am I missing something?Evan Shen
08/20/2021, 5:14 PMDan Homola
08/23/2021, 7:22 AM