mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[Metrics UI][Rules] Standardize NOW as startedAt from executor options (#128020)
* [Metrics UI][Rules] Standardize NOW as startedAt from executor options * Removing moment
This commit is contained in:
parent
c8b327c52c
commit
fe64578670
5 changed files with 21 additions and 25 deletions
|
@ -7,14 +7,13 @@
|
|||
|
||||
import { ElasticsearchClient } from 'kibana/server';
|
||||
import { mapValues } from 'lodash';
|
||||
import moment from 'moment';
|
||||
import { Logger } from '@kbn/logging';
|
||||
import { InventoryMetricConditions } from '../../../../common/alerting/metrics';
|
||||
import { InfraTimerangeInput } from '../../../../common/http_api';
|
||||
import { InventoryItemType } from '../../../../common/inventory_models/types';
|
||||
import { LogQueryFields } from '../../metrics/types';
|
||||
import { InfraSource } from '../../sources';
|
||||
import { calcualteFromBasedOnMetric } from './lib/calculate_from_based_on_metric';
|
||||
import { calculateFromBasedOnMetric } from './lib/calculate_from_based_on_metric';
|
||||
import { getData } from './lib/get_data';
|
||||
|
||||
type ConditionResult = InventoryMetricConditions & {
|
||||
|
@ -34,7 +33,7 @@ export const evaluateCondition = async ({
|
|||
compositeSize,
|
||||
filterQuery,
|
||||
lookbackSize,
|
||||
startTime,
|
||||
executionTimestamp,
|
||||
logger,
|
||||
}: {
|
||||
condition: InventoryMetricConditions;
|
||||
|
@ -45,16 +44,14 @@ export const evaluateCondition = async ({
|
|||
compositeSize: number;
|
||||
filterQuery?: string;
|
||||
lookbackSize?: number;
|
||||
startTime?: number;
|
||||
executionTimestamp: Date;
|
||||
logger: Logger;
|
||||
}): Promise<Record<string, ConditionResult>> => {
|
||||
const { metric, customMetric } = condition;
|
||||
|
||||
const to = startTime ? moment(startTime) : moment();
|
||||
|
||||
const timerange = {
|
||||
to: to.valueOf(),
|
||||
from: calcualteFromBasedOnMetric(to, condition, nodeType, metric, customMetric),
|
||||
to: executionTimestamp.valueOf(),
|
||||
from: calculateFromBasedOnMetric(executionTimestamp, condition, nodeType, metric, customMetric),
|
||||
interval: `${condition.timeSize}${condition.timeUnit}`,
|
||||
forceInterval: true,
|
||||
} as InfraTimerangeInput;
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
import { i18n } from '@kbn/i18n';
|
||||
import { ALERT_REASON, ALERT_RULE_PARAMETERS } from '@kbn/rule-data-utils';
|
||||
import { first, get } from 'lodash';
|
||||
import moment from 'moment';
|
||||
import {
|
||||
ActionGroup,
|
||||
ActionGroupIdsOf,
|
||||
|
@ -98,8 +97,8 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) =
|
|||
group: '*',
|
||||
alertState: stateToAlertMessage[AlertStates.ERROR],
|
||||
reason,
|
||||
timestamp: startedAt.toISOString(),
|
||||
viewInAppUrl,
|
||||
timestamp: moment().toISOString(),
|
||||
value: null,
|
||||
metric: mapToConditionsLookup(criteria, (c) => c.metric),
|
||||
});
|
||||
|
@ -128,6 +127,7 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) =
|
|||
esClient: services.scopedClusterClient.asCurrentUser,
|
||||
compositeSize,
|
||||
filterQuery,
|
||||
executionTimestamp: startedAt,
|
||||
logger,
|
||||
})
|
||||
)
|
||||
|
@ -218,8 +218,8 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) =
|
|||
group,
|
||||
alertState: stateToAlertMessage[nextState],
|
||||
reason,
|
||||
timestamp: startedAt.toISOString(),
|
||||
viewInAppUrl,
|
||||
timestamp: moment().toISOString(),
|
||||
value: mapToConditionsLookup(results, (result) =>
|
||||
formatMetric(result[group].metric, result[group].currentValue)
|
||||
),
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { Moment } from 'moment';
|
||||
import moment from 'moment';
|
||||
import { InventoryMetricConditions } from '../../../../../common/alerting/metrics';
|
||||
import { SnapshotCustomMetricInput } from '../../../../../common/http_api';
|
||||
import { findInventoryModel } from '../../../../../common/inventory_models';
|
||||
|
@ -15,8 +15,8 @@ import {
|
|||
} from '../../../../../common/inventory_models/types';
|
||||
import { isRate } from './is_rate';
|
||||
|
||||
export const calcualteFromBasedOnMetric = (
|
||||
to: Moment,
|
||||
export const calculateFromBasedOnMetric = (
|
||||
to: Date,
|
||||
condition: InventoryMetricConditions,
|
||||
nodeType: InventoryItemType,
|
||||
metric: SnapshotMetricType,
|
||||
|
@ -25,11 +25,10 @@ export const calcualteFromBasedOnMetric = (
|
|||
const inventoryModel = findInventoryModel(nodeType);
|
||||
const metricAgg = inventoryModel.metrics.snapshot[metric];
|
||||
if (isRate(metricAgg, customMetric)) {
|
||||
return to
|
||||
.clone()
|
||||
return moment(to)
|
||||
.subtract(condition.timeSize * 2, condition.timeUnit)
|
||||
.valueOf();
|
||||
} else {
|
||||
return to.clone().subtract(condition.timeSize, condition.timeUnit).valueOf();
|
||||
return moment(to).subtract(condition.timeSize, condition.timeUnit).valueOf();
|
||||
}
|
||||
};
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
import { i18n } from '@kbn/i18n';
|
||||
import { ALERT_REASON } from '@kbn/rule-data-utils';
|
||||
import { first, isEqual, last } from 'lodash';
|
||||
import moment from 'moment';
|
||||
import {
|
||||
ActionGroupIdsOf,
|
||||
AlertInstanceContext as AlertContext,
|
||||
|
@ -67,7 +66,7 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) =>
|
|||
MetricThresholdAlertContext,
|
||||
MetricThresholdAllowedActionGroups
|
||||
>(async function (options) {
|
||||
const { services, params, state } = options;
|
||||
const { services, params, state, startedAt } = options;
|
||||
const { criteria } = params;
|
||||
if (criteria.length === 0) throw new Error('Cannot execute an alert with 0 conditions');
|
||||
const { alertWithLifecycle, savedObjectsClient } = services;
|
||||
|
@ -94,7 +93,7 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) =>
|
|||
const { fromKueryExpression } = await import('@kbn/es-query');
|
||||
fromKueryExpression(params.filterQueryText);
|
||||
} catch (e) {
|
||||
const timestamp = moment().toISOString();
|
||||
const timestamp = startedAt.toISOString();
|
||||
const actionGroupId = FIRED_ACTIONS.id; // Change this to an Error action group when able
|
||||
const reason = buildInvalidQueryAlertReason(params.filterQueryText);
|
||||
const alert = alertFactory(UNGROUPED_FACTORY_KEY, reason);
|
||||
|
@ -138,7 +137,8 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) =>
|
|||
params as EvaluatedRuleParams,
|
||||
config,
|
||||
prevGroups,
|
||||
compositeSize
|
||||
compositeSize,
|
||||
{ end: startedAt.valueOf() }
|
||||
);
|
||||
|
||||
// Because each alert result has the same group definitions, just grab the groups from the first one.
|
||||
|
@ -225,7 +225,7 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) =>
|
|||
|
||||
if (reason) {
|
||||
const firstResult = first(alertResults);
|
||||
const timestamp = (firstResult && firstResult[group].timestamp) ?? moment().toISOString();
|
||||
const timestamp = (firstResult && firstResult[group].timestamp) ?? startedAt.toISOString();
|
||||
const actionGroupId =
|
||||
nextState === AlertStates.OK
|
||||
? RecoveredActionGroup.id
|
||||
|
|
|
@ -93,7 +93,7 @@ export default function ({ getService }: FtrProviderContext) {
|
|||
source,
|
||||
logQueryFields: void 0,
|
||||
compositeSize: 10000,
|
||||
startTime: DATES['8.0.0'].hosts_only.max,
|
||||
executionTimestamp: new Date(DATES['8.0.0'].hosts_only.max),
|
||||
logger,
|
||||
};
|
||||
|
||||
|
@ -451,7 +451,7 @@ export default function ({ getService }: FtrProviderContext) {
|
|||
it('should work FOR LAST 1 minute', async () => {
|
||||
const results = await evaluateCondition({
|
||||
...baseOptions,
|
||||
startTime: DATES['8.0.0'].pods_only.max,
|
||||
executionTimestamp: new Date(DATES['8.0.0'].pods_only.max),
|
||||
nodeType: 'pod' as InventoryItemType,
|
||||
condition: {
|
||||
...baseCondition,
|
||||
|
@ -492,7 +492,7 @@ export default function ({ getService }: FtrProviderContext) {
|
|||
it('should work FOR LAST 5 minute', async () => {
|
||||
const results = await evaluateCondition({
|
||||
...baseOptions,
|
||||
startTime: DATES['8.0.0'].pods_only.max,
|
||||
executionTimestamp: new Date(DATES['8.0.0'].pods_only.max),
|
||||
logQueryFields: { indexPattern: 'metricbeat-*' },
|
||||
nodeType: 'pod',
|
||||
condition: {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue