Skip to content

Commit

Permalink
code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
drewdaemon committed Jun 16, 2022
1 parent 7c5eb48 commit 3ca5e4f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ interface BaseOperationDefinitionProps<C extends BaseIndexPatternColumn, P = {}>
*/
helpComponentTitle?: string;
/**
* Optimizes EsAggs expression. Invoked only once per operation type. May mutate arguments.
* Optimizes EsAggs expression. Invoked only once per operation type.
*/
optimizeEsAggs?: (
aggs: ExpressionAstExpressionBuilder[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ export const percentileOperation: OperationDefinition<
}
).toAst();
},
// TODO - there may be a way to get around including aggExpressionToEsAggsIdMap
optimizeEsAggs: (aggs, esAggsIdMap, aggExpressionToEsAggsIdMap) => {
optimizeEsAggs: (_aggs, _esAggsIdMap, aggExpressionToEsAggsIdMap) => {
let aggs = [..._aggs];
const esAggsIdMap = { ..._esAggsIdMap };

const percentileExpressionsByArgs: Record<string, ExpressionAstExpressionBuilder[]> = {};

// group percentile dimensions by differentiating parameters
Expand All @@ -171,8 +173,8 @@ export const percentileOperation: OperationDefinition<

// collapse them into a single esAggs expression builder
Object.values(percentileExpressionsByArgs).forEach((expressionBuilders) => {
if (!(expressionBuilders.length > 1)) {
// don't need to optimize if there's only one
if (expressionBuilders.length <= 1) {
// don't need to optimize if there aren't more than one
return;
}

Expand Down Expand Up @@ -203,18 +205,15 @@ export const percentileOperation: OperationDefinition<

const duplicateExpressionBuilder = percentileToBuilder[percentile];

if (
!aggExpressionToEsAggsIdMap.has(builder) ||
!aggExpressionToEsAggsIdMap.has(duplicateExpressionBuilder)
) {
const idForDuplicate = aggExpressionToEsAggsIdMap.get(duplicateExpressionBuilder);
const idForThisOne = aggExpressionToEsAggsIdMap.get(builder);

if (!idForDuplicate || !idForThisOne) {
throw new Error(
"Couldn't find esAggs ID for percentile expression builder... this should never happen."
);
}

const idForDuplicate = aggExpressionToEsAggsIdMap.get(duplicateExpressionBuilder)!;
const idForThisOne = aggExpressionToEsAggsIdMap.get(builder)!;

esAggsIdMap[idForDuplicate].push(...esAggsIdMap[idForThisOne]);

delete esAggsIdMap[idForThisOne];
Expand Down
22 changes: 12 additions & 10 deletions x-pack/plugins/lens/public/indexpattern_datasource/to_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ declare global {
}
}

// esAggs column ID manipulation functions
const extractEsAggId = (id: string) => id.split('.')[0].split('-')[2];
const updatePositionIndex = (currentId: string, newIndex: number) => {
const [fullId, percentile] = currentId.split('.');
const idParts = fullId.split('-');
idParts[1] = String(newIndex);
return idParts.join('-') + (percentile ? `.${percentile}` : '');
};

function getExpressionForLayer(
layer: IndexPatternLayer,
indexPattern: IndexPattern,
Expand Down Expand Up @@ -219,19 +228,12 @@ function getExpressionForLayer(
*/

const updatedEsAggsIdMap: Record<string, OriginalColumn[]> = {};
const extractEsAggId = (id: string) => id.split('.')[0].split('-')[2];
const updatePositionIndex = (currentId: string, newIndex: number) => {
const [fullId, percentile] = currentId.split('.');
const idParts = fullId.split('-');
idParts[1] = String(newIndex);
return idParts.join('-') + (percentile ? `.${percentile}` : '');
};
let counter = 0;

const esAggsIds = Object.keys(esAggsIdMap);
aggs.forEach((builder) => {
const esAggId = builder.functions[0].getArgument('id')?.[0];
const matchingEsAggColumnIds = Object.keys(esAggsIdMap).filter(
(id) => extractEsAggId(id) === esAggId
);
const matchingEsAggColumnIds = esAggsIds.filter((id) => extractEsAggId(id) === esAggId);

matchingEsAggColumnIds.forEach((currentId) => {
const currentColumn = esAggsIdMap[currentId][0];
Expand Down

0 comments on commit 3ca5e4f

Please sign in to comment.