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

Commit

Permalink
Fix Monitor Details Page (#198)
Browse files Browse the repository at this point in the history
* Fix Monitor Details Page

* fix tests and others
  • Loading branch information
lezzago authored Oct 22, 2020
1 parent c5db597 commit dcdeef0
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 323 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opendistro-alerting",
"version": "1.11.0.0",
"version": "1.11.0.1",
"description": "Kibana Alerting Plugin",
"main": "index.js",
"license": "Apache-2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import DateRangePicker from './DateRangePicker';
import {
generateFirstDataPoints,
dataPointsGenerator,
getTimeSeriesSearchRequest,
getPOISearchQuery,
} from './utils/chartHelpers';
import * as HistoryConstants from './utils/constants';
import { INDEX } from '../../../../../utils/constants';
import queryString from 'query-string';

class MonitorHistory extends PureComponent {
constructor(props) {
Expand Down Expand Up @@ -85,7 +85,7 @@ class MonitorHistory extends PureComponent {
x: plotting ends,
meta: Any meta data
*/
generatePlotData = alertsData => {
generatePlotData = (alertsData) => {
const {
timeSeriesWindow: { startTime: windowStartTime, endTime: windowEndTime },
} = this.state;
Expand Down Expand Up @@ -204,7 +204,7 @@ class MonitorHistory extends PureComponent {
});
if (resp.data.ok) {
const poiData = get(resp, 'data.resp.aggregations.alerts_over_time.buckets', []).map(
item => ({
(item) => ({
x: item.key,
y: item.doc_count,
})
Expand Down Expand Up @@ -235,32 +235,40 @@ class MonitorHistory extends PureComponent {
});
const { timeSeriesWindow } = this.state;
const { httpClient, triggers, monitorId } = this.props;
// Shall we convert this into single call and get all the data ?
const promises = triggers.map(trigger =>
httpClient.post('../api/alerting/_search', {
query: getTimeSeriesSearchRequest(
monitorId,
trigger.id,
timeSeriesWindow.startTime,
timeSeriesWindow.endTime
),
index: INDEX.ALL_ALERTS,
size: HistoryConstants.MAX_DOC_COUNT_FOR_ALERTS,
})
);
try {
//TODO:: If anyone of the call fails, this will Promise will reject automatically.
// Should we handle each trigger individually and then plot it if we get data ?
const triggersResponse = await Promise.all(promises);
const triggersData = triggersResponse.reduce((acc, triggerData, index) => {
const trigger = triggers[index];
if (triggerData.data.ok) {
return {
...acc,
[trigger.id]: this.generatePlotData(get(triggerData, 'data.resp.hits.hits', [])),
};
const params = {
size: HistoryConstants.MAX_DOC_COUNT_FOR_ALERTS,
sortField: 'start_time',
sortDirection: 'asc',
monitorIds: monitorId,
};

const resp = await httpClient.get(`../api/alerting/alerts?${queryString.stringify(params)}`);
var alerts;
if (resp.data.ok) {
alerts = resp.data.alerts;
} else {
console.log('error getting alerts:', resp);
alerts = [];
}

const triggerTemp = {};
alerts.forEach((alert) => {
if (
alert.start_time <= timeSeriesWindow.endTime &&
(alert.end_time == null || alert.end_time >= timeSeriesWindow.startTime)
) {
if (!(alert.trigger_id in triggerTemp)) {
triggerTemp[alert.trigger_id] = [];
}
triggerTemp[alert.trigger_id].push({ _source: alert });
}
}, {});
});

const triggersData = {};
triggers.forEach((trigger) => {
triggersData[trigger.id] = this.generatePlotData(get(triggerTemp, trigger.id, []));
});

this.setState({
isLoading: false,
Expand All @@ -273,7 +281,7 @@ class MonitorHistory extends PureComponent {
}
};

handleDragEnd = area => {
handleDragEnd = (area) => {
const { timeSeriesWindow } = this.state;
if ((area && area.left.getTime() === timeSeriesWindow.startTime) || this.state.isLoading)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ describe('<MonitorHistory/>', () => {
];
const httpClient = {
post: jest.fn(),
get: jest.fn(),
};
beforeEach(() => {
jest.resetAllMocks();
});
test('should show <EmptyHistory/> if no triggers found', done => {
test('should show <EmptyHistory/> if no triggers found', (done) => {
const wrapper = mount(
<MonitorHistory
httpClient={httpClient}
Expand All @@ -56,30 +57,15 @@ describe('<MonitorHistory/>', () => {
done();
});
});
test('should execute getPOIData, getAlerts on componentDidMount', done => {
test('should execute getPOIData, getAlerts on componentDidMount', (done) => {
const poiResponse = getPOIResponse(initialStartTime);
httpClient.post
.mockResolvedValueOnce(poiResponse)
.mockResolvedValueOnce({
data: {
ok: true,
resp: {
hits: {
hits: [],
},
},
},
})
.mockResolvedValueOnce({
data: {
ok: true,
resp: {
hits: {
hits: [],
},
},
},
});
httpClient.post.mockResolvedValueOnce(poiResponse);
httpClient.get.mockResolvedValue({
data: {
ok: true,
alerts: [],
},
});
const wrapper = mount(
<MonitorHistory
httpClient={httpClient}
Expand All @@ -91,12 +77,13 @@ describe('<MonitorHistory/>', () => {
process.nextTick(() => {
wrapper.update();
expect(wrapper.state().poiData).toEqual(
poiResponse.data.resp.aggregations.alerts_over_time.buckets.map(item => ({
poiResponse.data.resp.aggregations.alerts_over_time.buckets.map((item) => ({
x: item.key,
y: item.doc_count,
}))
);
expect(httpClient.post).toHaveBeenCalledTimes(3);
expect(httpClient.post).toHaveBeenCalledTimes(1);
expect(httpClient.get).toHaveBeenCalledTimes(1);
expect(wrapper.state().maxAlerts).toBe(poiResponse.data.resp.aggregations.max_alerts.value);
const triggersData = wrapper.state().triggersData;
const triggersDataKeys = Object.keys(triggersData);
Expand All @@ -110,30 +97,15 @@ describe('<MonitorHistory/>', () => {
done();
});
});
test('should get 60 mins highlight windowSize', done => {
test('should get 60 mins highlight windowSize', (done) => {
const poiResponse = getPOIResponse(initialStartTime);
httpClient.post
.mockResolvedValueOnce(poiResponse)
.mockResolvedValueOnce({
data: {
ok: true,
resp: {
hits: {
hits: [],
},
},
},
})
.mockResolvedValueOnce({
data: {
ok: true,
resp: {
hits: {
hits: [],
},
},
},
});
httpClient.post.mockResolvedValueOnce(poiResponse);
httpClient.get.mockResolvedValue({
data: {
ok: true,
alerts: [],
},
});
const wrapper = mount(
<MonitorHistory
httpClient={httpClient}
Expand All @@ -151,10 +123,10 @@ describe('<MonitorHistory/>', () => {
done();
});
});
test('should create appropriate alerts data', done => {
test('should create appropriate alerts data', (done) => {
const poiResponse = getPOIResponse(initialStartTime);
Date.now = jest.fn(() => 1539619200000);
const alerts = triggers.map(trigger => [
const alerts = triggers.map((trigger) => [
getAlertsResponse(
trigger.id,
trigger.name,
Expand All @@ -163,28 +135,13 @@ describe('<MonitorHistory/>', () => {
moment('2018-10-15T09:00:00').subtract(1, 'h')
),
]);
httpClient.post
.mockResolvedValueOnce(poiResponse)
.mockResolvedValueOnce({
data: {
ok: true,
resp: {
hits: {
hits: alerts[0],
},
},
},
})
.mockResolvedValueOnce({
data: {
ok: true,
resp: {
hits: {
hits: alerts[1],
},
},
},
});
httpClient.post.mockResolvedValueOnce(poiResponse);
httpClient.get.mockResolvedValue({
data: {
ok: true,
alerts: alerts,
},
});
const wrapper = mount(
<MonitorHistory
httpClient={httpClient}
Expand All @@ -201,10 +158,16 @@ describe('<MonitorHistory/>', () => {
done();
});
});
test('should fetch new data on timeSeriesWindow change ', done => {
test('should fetch new data on timeSeriesWindow change ', (done) => {
const poiResponse = getPOIResponse(initialStartTime);
Date.now = jest.fn(() => 1539619200000);
httpClient.post.mockResolvedValue({ data: { ok: true } }).mockResolvedValueOnce(poiResponse);
httpClient.get.mockResolvedValue({
data: {
ok: true,
alerts: [],
},
});
const wrapper = mount(
<MonitorHistory
httpClient={httpClient}
Expand Down Expand Up @@ -233,12 +196,18 @@ describe('<MonitorHistory/>', () => {
done();
});
});
test('should fall back to max scale if the max alerts are lower than threshold ', done => {
test('should fall back to max scale if the max alerts are lower than threshold ', (done) => {
const poiResponse = getPOIResponse(initialStartTime);
set(poiResponse, 'data.resp.aggregations.max_alerts.value', 3);

Date.now = jest.fn(() => 1539619200000);
httpClient.post.mockResolvedValue({ data: { ok: true } }).mockResolvedValueOnce(poiResponse);
httpClient.get.mockResolvedValue({
data: {
ok: true,
alerts: [],
},
});
const wrapper = mount(
<MonitorHistory
httpClient={httpClient}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,25 @@
exports[`<MonitorHistory/> should create appropriate alerts data 1`] = `
Object {
"1": Array [
Object {
"meta": Object {
"endTime": 1539615900000,
"startTime": 1539615600000,
},
"state": "NO_ALERTS",
"x": 1539615900000,
"x0": 1539615600000,
},
Object {
"meta": Object {
"acknowledgedTime": undefined,
"endTime": 1539616080000,
"errorsCount": 0,
"startTime": 1539615900000,
"state": "COMPLETED",
},
"state": "TRIGGERED",
"x": 1539616080000,
"x0": 1539615900000,
},
Object {
"meta": Object {
"endTime": 1539619200000,
"startTime": 1539616080000,
"startTime": 1539615600000,
},
"state": "NO_ALERTS",
"x": 1539619200000,
"x0": 1539616080000,
"x0": 1539615600000,
},
],
"2": Array [
Object {
"meta": Object {
"endTime": 1539615900000,
"startTime": 1539615600000,
},
"state": "NO_ALERTS",
"x": 1539615900000,
"x0": 1539615600000,
},
Object {
"meta": Object {
"acknowledgedTime": undefined,
"endTime": 1539616080000,
"errorsCount": 0,
"startTime": 1539615900000,
"state": "COMPLETED",
},
"state": "TRIGGERED",
"x": 1539616080000,
"x0": 1539615900000,
},
Object {
"meta": Object {
"endTime": 1539619200000,
"startTime": 1539616080000,
"startTime": 1539615600000,
},
"state": "NO_ALERTS",
"x": 1539619200000,
"x0": 1539616080000,
"x0": 1539615600000,
},
],
}
Expand Down
Loading

0 comments on commit dcdeef0

Please sign in to comment.