Skip to content

Commit

Permalink
Use groupBy when groupings is not populated correctly (elastic#189672)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdelemme authored Sep 19, 2024
1 parent 18afcae commit c509747
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
* 2.0.
*/
import * as t from 'io-ts';
import { allOrAnyStringOrArray } from '../../schema';

const getSLOInstancesParamsSchema = t.type({
path: t.type({ id: t.string }),
});

const getSLOInstancesResponseSchema = t.type({
groupBy: t.union([t.string, t.array(t.string)]),
groupBy: allOrAnyStringOrArray,
instances: t.array(t.string),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import * as t from 'io-ts';
import { indicatorSchema, objectiveSchema } from '../../schema';
import { dateType } from '../../schema/common';
import { allOrAnyStringOrArray, dateType } from '../../schema/common';

const getPreviewDataParamsSchema = t.type({
body: t.intersection([
Expand All @@ -20,7 +20,7 @@ const getPreviewDataParamsSchema = t.type({
t.partial({
objective: objectiveSchema,
instanceId: t.string,
groupBy: t.string,
groupBy: allOrAnyStringOrArray,
remoteName: t.string,
groupings: t.record(t.string, t.unknown),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function useGetPreviewData({
remoteName,
}: {
isValid: boolean;
groupBy?: string;
groupBy?: string | string[];
instanceId?: string;
remoteName?: string;
groupings?: Record<string, unknown>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function EventsChartPanel({ slo, range, selectedTabId, onBrushed }: Props
const { isLoading, data } = useGetPreviewData({
range,
isValid: true,
groupBy: slo.groupBy,
indicator: slo.indicator,
groupings: slo.groupings,
instanceId: slo.instanceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { ALL_VALUE } from '@kbn/slo-schema';
import {
getSLOPipelineId,
SLO_INGEST_PIPELINE_INDEX_NAME_PREFIX,
Expand Down Expand Up @@ -43,47 +44,24 @@ export const getSLOPipelineTemplate = (slo: SLODefinition) => ({
},
},
{
script: {
description: 'Generated the instanceId field for SLO rollup data',
source: `
// This function will recursively collect all the values of a HashMap of HashMaps
Collection collectValues(HashMap subject) {
Collection values = new ArrayList();
// Iterate through the values
for(Object value: subject.values()) {
// If the value is a HashMap, recurse
if (value instanceof HashMap) {
values.addAll(collectValues((HashMap) value));
} else {
values.add(String.valueOf(value));
}
}
return values;
}
// Create the string builder
StringBuilder instanceId = new StringBuilder();
if (ctx["slo"]["groupings"] == null) {
ctx["slo"]["instanceId"] = "*";
} else {
// Get the values as a collection
Collection values = collectValues(ctx["slo"]["groupings"]);
// Convert to a list and sort
List sortedValues = new ArrayList(values);
Collections.sort(sortedValues);
// Create comma delimited string
for(String instanceValue: sortedValues) {
instanceId.append(instanceValue);
instanceId.append(",");
}
// Assign the slo.instanceId
ctx["slo"]["instanceId"] = instanceId.length() > 0 ? instanceId.substring(0, instanceId.length() - 1) : "*";
}
`,
dot_expander: {
path: 'slo.groupings',
field: '*',
ignore_failure: true,
if: 'ctx.slo.groupings != null',
},
},
{
set: {
description: 'Generated the instanceId field based on the groupings field',
field: 'slo.instanceId',
value:
[slo.groupBy].flat().includes(ALL_VALUE) || [slo.groupBy].flat().length === 0
? ALL_VALUE
: [slo.groupBy]
.flat()
.map((field) => `{{{slo.groupings.${field}}}}`)
.join(','),
},
},
],
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface Options {
interval: string;
instanceId?: string;
remoteName?: string;
groupBy?: string;
groupBy?: string | string[];
groupings?: Record<string, unknown>;
}
export class GetPreviewData {
Expand Down Expand Up @@ -516,15 +516,20 @@ export class GetPreviewData {

private getGroupingsFilter(options: Options, filter: estypes.QueryDslQueryContainer[]) {
const groupingsKeys = Object.keys(options.groupings || []);

if (groupingsKeys.length) {
groupingsKeys.forEach((key) => {
filter.push({
term: { [key]: options.groupings?.[key] },
});
});
} else if (options.instanceId !== ALL_VALUE && options.groupBy) {
filter.push({
term: { [options.groupBy]: options.instanceId },
} else if (options.instanceId && options.instanceId !== ALL_VALUE && options.groupBy) {
const instanceIdPart = options.instanceId.split(',');
const groupByPart = Array.isArray(options.groupBy) ? options.groupBy : [options.groupBy];
groupByPart.forEach((groupBy, index) => {
filter.push({
term: { [groupBy]: instanceIdPart[index] },
});
});
}
}
Expand Down

0 comments on commit c509747

Please sign in to comment.