Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SLO] Fix bug with 'View events' feature to filter on group-by fields #178260

Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
* 2.0.
*/
import { DiscoverStart } from '@kbn/discover-plugin/public';
import { kqlWithFiltersSchema, SLOWithSummaryResponse } from '@kbn/slo-schema';
import { ALL_VALUE, kqlWithFiltersSchema, SLOWithSummaryResponse } from '@kbn/slo-schema';
import { Filter, FilterStateStore, TimeRange } from '@kbn/es-query';
import { i18n } from '@kbn/i18n';
import { v4 } from 'uuid';
import { isEmpty } from 'lodash';
import { buildEsQuery } from '../build_es_query';

function createDiscoverLocator(
Expand All @@ -16,6 +18,7 @@ function createDiscoverLocator(
showGood = false,
timeRange?: TimeRange
) {
const indexId = v4();
const filters: Filter[] = [];

if (kqlWithFiltersSchema.is(slo.indicator.params.filter)) {
Expand All @@ -33,8 +36,15 @@ function createDiscoverLocator(
const goodFilters = kqlWithFiltersSchema.is(slo.indicator.params.good)
? slo.indicator.params.good.filters
: [];
const totalKuery = kqlWithFiltersSchema.is(slo.indicator.params.total)
? slo.indicator.params.total.kqlQuery
: slo.indicator.params.total;
const totalFilters = kqlWithFiltersSchema.is(slo.indicator.params.total)
? slo.indicator.params.total.filters
: [];
const customGoodFilter = buildEsQuery({ kuery: goodKuery, filters: goodFilters });
const customBadFilter = { bool: { must_not: customGoodFilter } };
const customTotalFilter = buildEsQuery({ kuery: totalKuery, filters: totalFilters });
const customBadFilter = { bool: { filter: customTotalFilter, must_not: customGoodFilter } };

filters.push({
$state: { store: FilterStateStore.APP_STATE },
Expand All @@ -44,8 +54,8 @@ function createDiscoverLocator(
defaultMessage: 'Good events',
}),
disabled: !showGood,
index: `${slo.indicator.params.index}-id`,
value: JSON.stringify(customGoodFilter),
index: indexId,
},
query: customGoodFilter as Record<string, any>,
});
Expand All @@ -58,13 +68,45 @@ function createDiscoverLocator(
defaultMessage: 'Bad events',
}),
disabled: !showBad,
index: `${slo.indicator.params.index}-id`,
value: JSON.stringify(customBadFilter),
index: indexId,
},
query: customBadFilter as Record<string, any>,
});
}

const groupBy = [slo.groupBy].flat();

if (
!isEmpty(slo.groupings) &&
groupBy.length > 0 &&
groupBy.every((field) => field === ALL_VALUE) === false
) {
groupBy.forEach((field) => {
filters.push({
meta: {
disabled: false,
negate: false,
alias: null,
key: field,
params: {
query: slo.groupings[field],
},
type: 'phrase',
index: indexId,
},
$state: {
store: FilterStateStore.APP_STATE,
},
query: {
match_phrase: {
[field]: slo.groupings[field],
},
},
});
});
}

const timeFieldName =
slo.indicator.type !== 'sli.apm.transactionDuration' &&
slo.indicator.type !== 'sli.apm.transactionErrorRate' &&
Expand All @@ -80,7 +122,7 @@ function createDiscoverLocator(
},
filters,
dataViewSpec: {
id: `${slo.indicator.params.index}-id`,
id: indexId,
title: slo.indicator.params.index,
timeFieldName,
},
Expand Down