Skip to content

Commit

Permalink
update annotation for query with custom granularity (if it was used) …
Browse files Browse the repository at this point in the history
…with granularity details
  • Loading branch information
KSDaemon committed Sep 24, 2024
1 parent 574d533 commit c4bee3b
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions packages/cubejs-api-gateway/src/helpers/prepareAnnotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import { MemberType } from '../types/strings';
import { MemberType as MemberTypeEnum } from '../types/enums';
import { MemberExpression } from '../types/query';

type GranularityMeta = {
name: string;
title: string;
interval: string;
offset?: string;
origin?: string;
};

/**
* Annotation item for cube's member.
*/
Expand All @@ -23,6 +31,7 @@ type ConfigItem = {
meta: any;
drillMembers?: any[];
drillMembersGrouped?: any;
granularities?: GranularityMeta[];
};

/**
Expand Down Expand Up @@ -50,7 +59,10 @@ const annotation = (
...(memberType === MemberTypeEnum.MEASURES ? {
drillMembers: config.drillMembers,
drillMembersGrouped: config.drillMembersGrouped
} : {})
} : {}),
...(memberType === MemberTypeEnum.DIMENSIONS && config.granularities ? {
granularities: config.granularities || [],
} : {}),
}];
};

Expand Down Expand Up @@ -81,25 +93,38 @@ function prepareAnnotation(metaConfig: MetaConfig[], query: any) {
(query.timeDimensions || [])
.filter(td => !!td.granularity)
.map(
td => [
annotation(
td => {
const an = annotation(
configMap,
MemberTypeEnum.DIMENSIONS,
)(
`${td.dimension}.${td.granularity}`
)
].concat(
);

if (an && an[1].granularities) {
// No need to send all the granularities defined, only those make sense for this query
an[1].granularities = an[1].granularities.filter(g => g.name === td.granularity);
}

// TODO: deprecated: backward compatibility for
// referencing time dimensions without granularity
dimensions.indexOf(td.dimension) === -1
? [
annotation(
configMap,
MemberTypeEnum.DIMENSIONS
)(td.dimension)
]
: []
).filter(a => !!a)
if (dimensions.indexOf(td.dimension) !== -1) {
return [an].filter(a => !!a);
}

const dimWithoutGranularity = annotation(
configMap,
MemberTypeEnum.DIMENSIONS
)(td.dimension);

if (dimWithoutGranularity && dimWithoutGranularity[1].granularities) {
// no need to populate granularities here
dimWithoutGranularity[1].granularities = undefined;
}

return [an].concat([dimWithoutGranularity])
.filter(a => !!a);
}
)
)
),
Expand Down

0 comments on commit c4bee3b

Please sign in to comment.