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

Fix the charts and group by section on the Log Threshold alert detail page #155327

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import {
ALERT_CONTEXT,
ALERT_EVALUATION_THRESHOLD,
ALERT_EVALUATION_VALUE,
ALERT_EVALUATION_VALUES,
Expand All @@ -19,6 +20,7 @@ export const legacyExperimentalFieldMap = {
required: false,
},
[ALERT_EVALUATION_VALUE]: { type: 'scaled_float', scaling_factor: 100, required: false },
[ALERT_CONTEXT]: { type: 'object', array: false, required: false },
[ALERT_EVALUATION_VALUES]: {
type: 'scaled_float',
scaling_factor: 100,
Expand Down
3 changes: 3 additions & 0 deletions packages/kbn-rule-data-utils/src/technical_field_names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const EVENT_MODULE = 'event.module' as const;
const ALERT_BUILDING_BLOCK_TYPE = `${ALERT_NAMESPACE}.building_block_type` as const;
const ALERT_EVALUATION_THRESHOLD = `${ALERT_NAMESPACE}.evaluation.threshold` as const;
const ALERT_EVALUATION_VALUE = `${ALERT_NAMESPACE}.evaluation.value` as const;
const ALERT_CONTEXT = `${ALERT_NAMESPACE}.context` as const;
const ALERT_EVALUATION_VALUES = `${ALERT_NAMESPACE}.evaluation.values` as const;

// Fields pertaining to the rule associated with the alert
Expand Down Expand Up @@ -133,6 +134,7 @@ const fields = {
ALERT_RULE_CONSUMER,
ALERT_RULE_PRODUCER,
ALERT_REASON,
ALERT_CONTEXT,
ALERT_RISK_SCORE,
ALERT_CASE_IDS,
ALERT_RULE_AUTHOR,
Expand Down Expand Up @@ -194,6 +196,7 @@ export {
ALERT_BUILDING_BLOCK_TYPE,
ALERT_EVALUATION_THRESHOLD,
ALERT_EVALUATION_VALUE,
ALERT_CONTEXT,
ALERT_EVALUATION_VALUES,
ALERT_RULE_EXCEPTIONS_LIST,
ALERT_RULE_NAMESPACE_FIELD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ const LogsRatioChart: React.FC<ChartProps> = ({
const barSeries = useMemo(() => {
return series.flatMap(({ points, id }) => points.map((point) => ({ ...point, groupBy: id })));
}, [series]);

if (isLoading) {
return <LoadingState />;
} else if (hasError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import React, { useEffect, useState } from 'react';
import { EuiFlexGroup, EuiFlexItem, EuiSpacer } from '@elastic/eui';
import { LIGHT_THEME } from '@elastic/charts';
import { EuiPanel } from '@elastic/eui';
import { ALERT_END, ALERT_EVALUATION_VALUE, ALERT_START } from '@kbn/rule-data-utils';
import {
ALERT_CONTEXT,
ALERT_END,
ALERT_EVALUATION_VALUE,
ALERT_START,
} from '@kbn/rule-data-utils';
import moment from 'moment';
import { useTheme } from '@emotion/react';
import { EuiTitle } from '@elastic/eui';
Expand All @@ -20,6 +25,7 @@ import {
} from '@kbn/observability-alert-details';
import { useEuiTheme } from '@elastic/eui';
import { UI_SETTINGS } from '@kbn/data-plugin/public';
import { get } from 'lodash';
import { useKibanaContextForPlugin } from '../../../../hooks/use_kibana';
import { getChartGroupNames } from '../../../../../common/utils/get_chart_group_names';
import {
Expand Down Expand Up @@ -61,7 +67,9 @@ const AlertDetailsAppSection = ({
rule.params.groupBy?.reduce(
(selectedFields: Record<string, any>, field) => ({
...selectedFields,
...{ [field]: alert.fields[field] },
...{
[field]: get(alert.fields[ALERT_CONTEXT], ['groupByKeys', ...field.split('.')], null),
},
}),
{}
) || {};
Expand Down Expand Up @@ -232,7 +240,9 @@ const AlertDetailsAppSection = ({
rule &&
rule.params.criteria.length === 1 && (
<EuiFlexItem>
<LogsHistoryChart rule={rule} />
<LogsHistoryChart
rule={{ ...rule, params: { ...rule.params, timeSize: 12, timeUnit: 'h' } }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

/>
</EuiFlexItem>
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const useChartPreviewData = ({
let seriesQueryB = ratio[1].data.series[0].points;
let seriesId = 'ratio';
// When groupBy and a filter is applied, return the ratio only for the filtered grouped-by
if (ruleParams.groupBy.length && filterSeriesByGroupName) {
if (ruleParams.groupBy?.length && filterSeriesByGroupName) {
seriesId = filterSeriesByGroupName;
seriesQueryA =
ratio[0].data.series.find((series) => series.id === filterSeriesByGroupName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { i18n } from '@kbn/i18n';
import {
ALERT_CONTEXT,
ALERT_EVALUATION_THRESHOLD,
ALERT_EVALUATION_VALUE,
ALERT_REASON,
Expand Down Expand Up @@ -86,7 +87,7 @@ export type LogThresholdAlertFactory = (
value: number,
threshold: number,
actions?: Array<{ actionGroup: LogThresholdActionGroups; context: AlertContext }>,
additionalContext?: AdditionalContext
rootLevelContext?: AdditionalContext
) => LogThresholdAlert;
export type LogThresholdAlertLimit = RuleExecutorServices<
LogThresholdAlertState,
Expand Down Expand Up @@ -134,15 +135,21 @@ export const createLogThresholdExecutor = (libs: InfraBackendLibs) =>
value,
threshold,
actions,
additionalContext
rootLevelContext
) => {
const alertContext =
actions != null
? actions.reduce((next, action) => ({ ...next, ...action.context }), {})
: {};

const alert = alertWithLifecycle({
id,
fields: {
[ALERT_EVALUATION_THRESHOLD]: threshold,
[ALERT_EVALUATION_VALUE]: value,
[ALERT_REASON]: reason,
...flattenAdditionalContext(additionalContext),
[ALERT_CONTEXT]: alertContext,
...flattenAdditionalContext(rootLevelContext),
},
});

Expand Down