Skip to content

Commit

Permalink
#OBS-I275: fix: alerts metric fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JeraldJF committed Nov 8, 2024
1 parent a6c4c33 commit b260464
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ const QueryBuilder = (props: any) => {
if (!_.isEmpty(existingState)) {
const operatorType = _.get(existingState, "operator")
const thresholdValue = _.get(existingState, "threshold")
const metricId = _.get(existingState, "id") || ""

if (_.includes(["within_range", "outside_range"], operatorType)) {
return { ..._.omit(existingState, ["threshold"]), threshold_from: _.get(thresholdValue, 0), threshold_to: _.get(thresholdValue, 1) }
return { ..._.omit(existingState, ["threshold"]), threshold_from: _.get(thresholdValue, 0), threshold_to: _.get(thresholdValue, 1), metric: metricId }
}
return { ..._.omit(existingState, ["threshold_from", "threshold_to"]), threshold: _.get(thresholdValue, 0) }
return { ..._.omit(existingState, ["threshold_from", "threshold_to"]), threshold: _.get(thresholdValue, 0), metric: metricId }
}
return {}
});
Expand Down Expand Up @@ -68,18 +70,18 @@ const QueryBuilder = (props: any) => {
if (selectedSubComponent) {
const filteredMetrics = _.filter(supportedMetrics, (supportedMetric) => supportedMetric.subComponent == selectedSubComponent);
return _.map(filteredMetrics, (supportedMetric) => {
const { alias, metric } = supportedMetric;
const { alias, id } = supportedMetric;
return {
label: alias,
value: metric
value: id
};
});
}
return _.map(supportedMetrics, (supportedMetric) => {
const { alias, metric } = supportedMetric;
const { alias, id } = supportedMetric;
return {
label: alias,
value: metric
value: id
};
});
};
Expand Down Expand Up @@ -264,7 +266,7 @@ const QueryBuilder = (props: any) => {
</Grid>
{runQuery && (
<Grid item xs={12}>
<RunQuery random={Math.random()} handleClose={updateRunQuery} queryBuilderContext={value} />
<RunQuery random={Math.random()} handleClose={updateRunQuery} queryBuilderContext={value} component={_.get(components, _.get(value, 'category'))} />
</Grid>
)}
</>
Expand Down
5 changes: 3 additions & 2 deletions web-console-v2/src/pages/alertManager/components/RunQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { useEffect, useState } from "react";
import Loader from "components/Loader";

const RunQuery = (props: any) => {
const { handleClose, queryBuilderContext } = props;
const { handleClose, queryBuilderContext, component } = props;
const { metric, threshold, threshold_from, threshold_to, operator } = queryBuilderContext
const metricValue = _.filter(component, field => _.get(field, "id") == metric)
const [metadata, setMetadata] = useState<Record<string, any> | null>(null);
const [loading, setLoading] = useState(false)

Expand Down Expand Up @@ -139,7 +140,7 @@ const RunQuery = (props: any) => {
headers: {},
body: {},
params: {
query: metric,
query: _.get(metricValue, [0, "metric"]) || metric,
step: '5m',
start: dayjs().unix(),
end: dayjs().subtract(1, 'day').unix()
Expand Down
16 changes: 10 additions & 6 deletions web-console-v2/src/pages/alertManager/services/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { LocalizationProvider, DateTimePicker } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { searchAlert } from 'services/alerts';
import { fetchChannels } from 'services/notificationChannels';
import { getMetricsGroupedByComponents } from './queryBuilder';

export const validateForm = (config: Record<string, any>) => {
if (!config) return false;
Expand Down Expand Up @@ -196,10 +197,10 @@ const DatePicker = (props: any) => {
<DateTimePicker
label={label}
value={date}
onChange={(newValue:any) => {
onChange={(newValue: any) => {
setDate(newValue, alertId);
}}
renderInput={(params:any) => <TextField {...params} />}
renderInput={(params: any) => <TextField {...params} />}
/>
);
};
Expand Down Expand Up @@ -240,7 +241,7 @@ export const asyncValidation = () => {
export const getStatusComponent = (props: any) => {
return () => {
const { statusData, setSilenceStatus, fetchDataHandler, toggleFilter, removeFilter } = props;
return _.map(statusData, (value:any, status:any) => (
return _.map(statusData, (value: any, status: any) => (
<Chip
key={status}
size="small"
Expand Down Expand Up @@ -326,7 +327,7 @@ export const getStatusColor = (value: string) => {
return _.get(valueToColorMapping, value?.toLowerCase()) || 'success';
};

export const transformRulePayload = (formData: Record<string, any>) => {
export const transformRulePayload = async (formData: Record<string, any>) => {
const {
name,
description,
Expand All @@ -340,7 +341,10 @@ export const transformRulePayload = (formData: Record<string, any>) => {
context,
severity
} = formData;
const { threshold, threshold_from, threshold_to, operator } = queryBuilderContext
const { threshold, threshold_from, threshold_to, operator, metric } = queryBuilderContext
const components = await getMetricsGroupedByComponents();
const selectedComponent = _.get(components, category)
const metricValue = _.filter(selectedComponent, field => _.get(field, "id") == metric)
const updatedThreshold = !_.includes(["within_range", "outside_range"], operator) ? [threshold] : [threshold_from, threshold_to]
const rulePayload = {
name,
Expand All @@ -356,7 +360,7 @@ export const transformRulePayload = (formData: Record<string, any>) => {
type: category
},
metadata: {
queryBuilderContext: { ...queryBuilderContext, threshold: updatedThreshold }
queryBuilderContext: { ...queryBuilderContext, threshold: updatedThreshold, metric: _.get(metricValue, [0, "metric"]), id: metric }
},
context: {
...context
Expand Down
2 changes: 1 addition & 1 deletion web-console-v2/src/pages/alertManager/views/AddRule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const AddAlertrules = () => {
}, []);

const addAlertRule: any = async () => {
const rulePayload = transformRulePayload({ ...formData, context: { alertType: 'CUSTOM' } });
const rulePayload = await transformRulePayload({ ...formData, context: { alertType: 'CUSTOM' } });
return addAlert(rulePayload);
};

Expand Down
4 changes: 2 additions & 2 deletions web-console-v2/src/pages/alertManager/views/EditRule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ const EditRule = () => {
}
], [ruleMetadata]);

const editAlertRule = () => {
const rulePayload = transformRulePayload({ ...formData, context: { alertType: alertType } });
const editAlertRule = async () => {
const rulePayload = await transformRulePayload({ ...formData, context: { alertType: alertType } });
return editAlert({ id: id, data: rulePayload });
};

Expand Down

0 comments on commit b260464

Please sign in to comment.