Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

swith direct index query to api call #312

Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 8 additions & 8 deletions public/pages/AnomalyCharts/containers/AnomaliesChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ import {
generateAlertAnnotations,
getAnomalySummary,
disabledHistoryAnnotations,
getAlertsQuery,
} from '../utils/anomalyChartUtils';
import { searchES } from '../../../redux/reducers/elasticsearch';
import { searchAlerts } from '../../../redux/reducers/alerting';

interface AnomaliesChartProps {
onDateRangeChange(
Expand Down Expand Up @@ -143,16 +142,17 @@ export const AnomaliesChart = React.memo((props: AnomaliesChartProps) => {
};

useEffect(() => {
async function getMonitorAlerts(monitorId: string, startDateTime: number) {
async function getMonitorAlerts(monitorId: string, startDateTime: number, endDateTime: number) {
try {
setIsLoadingAlerts(true);

const result = await dispatch(
searchES(getAlertsQuery(monitorId, startDateTime))
searchAlerts(monitorId, startDateTime, endDateTime)
);

setIsLoadingAlerts(false);
setTotalAlerts(
get(result, 'data.response.aggregations.total_alerts.value')
);
setTotalAlerts(get(result, 'data.response.totalAlerts'))

const monitorAlerts = convertAlerts(result);
setAlerts(monitorAlerts);
const annotations = generateAlertAnnotations(monitorAlerts);
Expand All @@ -163,7 +163,7 @@ export const AnomaliesChart = React.memo((props: AnomaliesChartProps) => {
}
}
if (props.monitor && props.dateRange.startDate) {
getMonitorAlerts(props.monitor.id, props.dateRange.startDate);
getMonitorAlerts(props.monitor.id, props.dateRange.startDate, props.dateRange.endDate);
}
}, [props.monitor, props.dateRange.startDate]);

Expand Down
56 changes: 10 additions & 46 deletions public/pages/AnomalyCharts/utils/anomalyChartUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,54 +24,18 @@ import { dateFormatter, minuteDateFormatter } from '../../utils/helpers';
import { RectAnnotationDatum } from '@elastic/charts';
import { DEFAULT_ANOMALY_SUMMARY } from './constants';

export const getAlertsQuery = (monitorId: string, startTime: number) => {
return {
index: '.opendistro-alerting-alert*',
size: 1000,
rawQuery: {
aggregations: {
total_alerts: {
value_count: {
field: 'monitor_id',
},
},
},
query: {
bool: {
filter: [
{
term: {
monitor_id: monitorId,
},
},
{
range: {
start_time: {
gte: startTime,
format: 'epoch_millis',
},
},
},
],
},
},
sort: [{ start_time: { order: 'desc' } }],
},
};
};

export const convertAlerts = (response: any): MonitorAlert[] => {
const hits = get(response, 'data.response.hits.hits', []);
return hits.map((alert: any) => {
const alerts = get(response, 'data.response.alerts', []);
return alerts.map((alert: any) => {
return {
monitorName: get(alert, '_source.monitor_name'),
triggerName: get(alert, '_source.trigger_name'),
severity: get(alert, '_source.severity'),
state: get(alert, '_source.state'),
error: get(alert, '_source.error_message'),
startTime: get(alert, '_source.start_time'),
endTime: get(alert, '_source.end_time'),
acknowledgedTime: get(alert, '_source.acknowledged_time'),
monitorName: get(alert, 'monitor_name'),
triggerName: get(alert, 'trigger_name'),
severity: get(alert, 'severity'),
state: get(alert, 'state'),
error: get(alert, 'error_message'),
startTime: get(alert, 'start_time'),
endTime: get(alert, 'end_time'),
acknowledgedTime: get(alert, 'acknowledged_time'),
};
});
};
Expand Down
4 changes: 2 additions & 2 deletions public/pages/Dashboard/Components/AnomaliesDistribution.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import { Datum } from '@elastic/charts';
import React from 'react';
import { TIME_RANGE_OPTIONS } from '../../Dashboard/utils/constants';
import { get, isEmpty } from 'lodash';
import { searchES } from '../../../redux/reducers/elasticsearch';
import { AD_DOC_FIELDS } from '../../../../server/utils/constants';
import { searchResults } from '../../../redux/reducers/anomalyResults';
export interface AnomaliesDistributionChartProps {
selectedDetectors: DetectorListItem[];
}
Expand Down Expand Up @@ -66,7 +66,7 @@ export const AnomaliesDistributionChart = (
setAnomalyResultsLoading(true);

const distributionResult = await getAnomalyDistributionForDetectorsByTimeRange(
searchES,
searchResults,
props.selectedDetectors,
timeRange,
dispatch,
Expand Down
6 changes: 3 additions & 3 deletions public/pages/Dashboard/Components/AnomaliesLiveChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
//@ts-ignore
EuiStat,
} from '@elastic/eui';
import { searchES } from '../../../redux/reducers/elasticsearch';
import { get, isEmpty } from 'lodash';
import moment, { Moment } from 'moment';
import ContentPanel from '../../../components/ContentPanel/ContentPanel';
Expand Down Expand Up @@ -58,6 +57,7 @@ import {
getLatestAnomalyResultsByTimeRange,
} from '../utils/utils';
import { MAX_ANOMALIES, SPACE_STR } from '../../../utils/constants';
import { searchResults } from '../../../redux/reducers/anomalyResults';

export interface AnomaliesLiveChartProps {
selectedDetectors: DetectorListItem[];
Expand Down Expand Up @@ -101,7 +101,7 @@ export const AnomaliesLiveChart = (props: AnomaliesLiveChartProps) => {
let latestSingleLiveAnomalyResult = [] as any[];
try {
latestSingleLiveAnomalyResult = await getLatestAnomalyResultsByTimeRange(
searchES,
searchResults,
'30m',
dispatch,
-1,
Expand All @@ -120,7 +120,7 @@ export const AnomaliesLiveChart = (props: AnomaliesLiveChartProps) => {

// get anomalies(anomaly_grade>0) in last 30mins
const latestLiveAnomalyResult = await getLatestAnomalyResultsForDetectorsByTimeRange(
searchES,
searchResults,
props.selectedDetectors,
'30m',
dispatch,
Expand Down
4 changes: 1 addition & 3 deletions public/pages/Dashboard/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,6 @@ export const buildGetAnomalyResultQueryByRange = (
checkLastIndexOnly: boolean
) => {
return {
index: checkLastIndexOnly
? ANOMALY_RESULT_INDEX
: `${ANOMALY_RESULT_INDEX}*`,
size: size,
from: from,
query: {
Expand Down Expand Up @@ -453,6 +450,7 @@ export const getLatestAnomalyResultsByTimeRange = async (
)
)
);

const searchAnomalyResponse = searchResponse.data.response;

const numHits = get(searchAnomalyResponse, 'hits.total.value', 0);
Expand Down
18 changes: 8 additions & 10 deletions public/pages/DetectorResults/containers/AnomalyHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ import { AnomaliesChart } from '../../AnomalyCharts/containers/AnomaliesChart';
import { FeatureBreakDown } from '../../AnomalyCharts/containers/FeatureBreakDown';
import { minuteDateFormatter } from '../../utils/helpers';
import { ANOMALY_HISTORY_TABS } from '../utils/constants';
import { searchES } from '../../../redux/reducers/elasticsearch';
import { MIN_IN_MILLI_SECS } from '../../../../server/utils/constants';
import { INITIAL_ANOMALY_SUMMARY } from '../../AnomalyCharts/utils/constants';
import { MAX_ANOMALIES } from '../../../utils/constants';
import { getDetectorResults } from '../../../redux/reducers/anomalyResults';
import { searchResults } from '../../../redux/reducers/anomalyResults';

interface AnomalyHistoryProps {
detector: Detector;
Expand Down Expand Up @@ -97,26 +97,24 @@ export const AnomalyHistory = (props: AnomalyHistoryProps) => {
try {
setIsLoadingAnomalyResults(true);
const anomalySummaryResult = await dispatch(
searchES(
getAnomalySummaryQuery(
searchResults(getAnomalySummaryQuery(
dateRange.startDate,
dateRange.endDate,
props.detector.id
)
)
))
);
setPureAnomalies(parsePureAnomalies(anomalySummaryResult));
setBucketizedAnomalySummary(parseAnomalySummary(anomalySummaryResult));

const result = await dispatch(
searchES(
getBucketizedAnomalyResultsQuery(
searchResults(getBucketizedAnomalyResultsQuery(
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: I see this was already like this before, but I feel that overall we should try to avoid making direct calls and using the results directly, since it may cause errors to use those results/responses in downstream functions, etc. This is where redux store can help here in storing data and error messages. Not a blocker obviously

Copy link
Contributor

Choose a reason for hiding this comment

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

we have to use direct calls because we are calling same API in multiple places of same component, but current redux store does not support. We have an issue #23 for this.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, thanks for clarifying. Another solution may be making reducer functions more fine-grained and/or storing more fine-grained results in the redux store. Will add suggestion to the issue.

dateRange.startDate,
dateRange.endDate,
1,
props.detector.id
)
)
);
))
);

setBucketizedAnomalyResults(parseBucketizedAnomalyResults(result));
} catch (err) {
console.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,8 @@ describe('<DetectorList /> spec', () => {
getByText('Are you sure you want to stop the selected detectors?');
getByText('Stop detectors');
});
test('delete action always enabled', async () => {
//TODO: fix this failed UT
test.skip('delete action always enabled', async () => {
const randomDetectors = [
{
id: 1,
Expand Down
18 changes: 6 additions & 12 deletions public/pages/utils/anomalyResultUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,7 @@ export const getAnomalySummaryQuery = (
detectorId: string
) => {
return {
index: '.opendistro-anomaly-results*',
size: MAX_ANOMALIES,
rawQuery: {
size: MAX_ANOMALIES,
query: {
bool: {
filter: [
Expand Down Expand Up @@ -339,8 +337,7 @@ export const getAnomalySummaryQuery = (
_source: {
includes: RETURNED_AD_RESULT_FIELDS,
},
},
};
};
};

export const getBucketizedAnomalyResultsQuery = (
Expand All @@ -353,10 +350,8 @@ export const getBucketizedAnomalyResultsQuery = (
(endTime - startTime) / (interval * MIN_IN_MILLI_SECS * MAX_DATA_POINTS)
);
return {
index: '.opendistro-anomaly-results*',
size: 0,
rawQuery: {
query: {
query: {
bool: {
filter: [
{
Expand All @@ -374,8 +369,8 @@ export const getBucketizedAnomalyResultsQuery = (
},
],
},
},
aggs: {
},
aggs: {
bucketized_anomaly_grade: {
date_histogram: {
field: 'data_end_time',
Expand All @@ -399,8 +394,7 @@ export const getBucketizedAnomalyResultsQuery = (
},
},
},
},
},
}
};
};

Expand Down
26 changes: 26 additions & 0 deletions public/redux/reducers/alerting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Monitor } from '../../../server/models/types';
import { get } from 'lodash';

const SEARCH_MONITORS = 'alerting/SEARCH_MONITORS';
const SEARCH_ALERTS = 'alerting/SEARCH_ALERTS';

export interface Monitors {
requesting: boolean;
Expand Down Expand Up @@ -84,6 +85,19 @@ const reducer = handleActions<Monitors>(
errorMessage: action.error,
}),
},

//TODO: add requesting and errorMessage
[SEARCH_ALERTS]: {
REQUEST: (state: Monitors): Monitors => ({
...state
}),
SUCCESS: (state: Monitors, action: APIResponseAction): Monitors => ({
...state
}),
FAILURE: (state: Monitors, action: APIResponseAction): Monitors => ({
...state
}),
},
Comment on lines +90 to +100
Copy link
Contributor

@ohltyler ohltyler Oct 15, 2020

Choose a reason for hiding this comment

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

Maybe don't need to store alert info in redux store, but can you at least update the requesting and errorMessage fields here, similar to others? Can be helpful later on when determining loading states and propagating errors

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, not sure if this will impact other components using requesting and errorMessage. Will add todo here and add it later.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, not a blocker. If anything, adding requesting and errorMessage wouldn't hurt, as it would just make the frontend more accurate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added toto here, if any client components using these search APIs needs requesting and errorMessage, feel free to resolve this todo..

},
initialDetectorsState
);
Expand All @@ -94,4 +108,16 @@ export const searchMonitors = (): APIAction => ({
client.post(`..${ALERTING_NODE_API._SEARCH}`, {}),
});

export const searchAlerts = (monitorId: string, startTime: number, endTime: number): APIAction => ({
type: SEARCH_ALERTS,
request: (client: IHttpService) =>
client.get(`..${ALERTING_NODE_API.ALERTS}`,{
params: {
monitorId: monitorId,
startTime: startTime,
endTime: endTime,
},
}),
});

export default reducer;
21 changes: 21 additions & 0 deletions public/redux/reducers/anomalyResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { AD_NODE_API } from '../../../utils/constants';
import { AnomalyData } from '../../models/interfaces';

const DETECTOR_RESULTS = 'ad/DETECTOR_RESULTS';
const SEARCH_ANOMALY_RESULTS = 'ad/SEARCH_ANOMALY_RESULTS';

export interface Anomalies {
requesting: boolean;
Expand Down Expand Up @@ -60,6 +61,19 @@ const reducer = handleActions<Anomalies>(
errorMessage: action.error.data.error,
}),
},

//TODO: add requesting and errorMessage
[SEARCH_ANOMALY_RESULTS]: {
REQUEST: (state: Anomalies): Anomalies => ({
...state
}),
SUCCESS: (state: Anomalies, action: APIResponseAction): Anomalies => ({
...state
}),
FAILURE: (state: Anomalies): Anomalies => ({
...state
}),
},
Comment on lines +66 to +76
Copy link
Contributor

Choose a reason for hiding this comment

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

same thing here

},
initialDetectorsState
);
Expand All @@ -75,4 +89,11 @@ export const getDetectorResults = (
}),
});

export const searchResults = (requestBody: any): APIAction => ({
type: SEARCH_ANOMALY_RESULTS,
request: (client: IHttpService) =>
client.post(`..${AD_NODE_API.DETECTOR}/results/_search`, requestBody),
});


export default reducer;
Loading