Skip to content

Commit a915f85

Browse files
[FEATURE] Charts | Change charts time-unit to bigger timespan #164 (#204)
* [FEATURE] Charts | Change charts time-unit to bigger timespan #164 Signed-off-by: Jovan Cvetkovic <[email protected]> * [FEATURE] Charts | Change charts time-unit to bigger timespan #164 Signed-off-by: Jovan Cvetkovic <[email protected]> Signed-off-by: Jovan Cvetkovic <[email protected]>
1 parent 9abf9ee commit a915f85

File tree

5 files changed

+231
-32
lines changed

5 files changed

+231
-32
lines changed

public/pages/Alerts/containers/Alerts/Alerts.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import { FieldValueSelectionFilterConfigType } from '@elastic/eui/src/components
2222
import dateMath from '@elastic/datemath';
2323
import React, { Component } from 'react';
2424
import { ContentPanel } from '../../../../components/ContentPanel';
25-
import { getAlertsVisualizationSpec } from '../../../Overview/utils/helpers';
25+
import {
26+
getAlertsVisualizationSpec,
27+
getChartTimeUnit,
28+
getDateFormatByTimeUnit,
29+
} from '../../../Overview/utils/helpers';
2630
import moment from 'moment';
2731
import {
2832
ALERT_STATE,
@@ -70,6 +74,7 @@ export interface AlertsState {
7074
filteredAlerts: AlertItem[];
7175
detectors: { [key: string]: Detector };
7276
loading: boolean;
77+
timeUnit: string;
7378
}
7479

7580
const groupByOptions = [
@@ -93,6 +98,7 @@ export default class Alerts extends Component<AlertsProps, AlertsState> {
9398
filteredAlerts: [],
9499
detectors: {},
95100
loading: false,
101+
timeUnit: 'yearmonthdatehoursminutes',
96102
};
97103
}
98104

@@ -218,7 +224,10 @@ export default class Alerts extends Component<AlertsProps, AlertsState> {
218224
};
219225
});
220226

221-
return getAlertsVisualizationSpec(visData, this.state.groupBy);
227+
return getAlertsVisualizationSpec(visData, this.state.groupBy, {
228+
timeUnit: this.state.timeUnit,
229+
dateFormat: getDateFormatByTimeUnit(this.state.startTime, this.state.endTime),
230+
});
222231
}
223232

224233
createGroupByControl(): React.ReactNode {
@@ -297,10 +306,13 @@ export default class Alerts extends Component<AlertsProps, AlertsState> {
297306
if (recentlyUsedRanges.length > MAX_RECENTLY_USED_TIME_RANGES)
298307
recentlyUsedRanges = recentlyUsedRanges.slice(0, MAX_RECENTLY_USED_TIME_RANGES);
299308
const endTime = start === end ? DEFAULT_DATE_RANGE.end : end;
309+
310+
const timeUnit = getChartTimeUnit(start, endTime);
300311
this.setState({
301312
startTime: start,
302313
endTime: endTime,
303314
recentlyUsedRanges: recentlyUsedRanges,
315+
timeUnit,
304316
});
305317
};
306318

public/pages/Findings/containers/Findings/Findings.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ import {
2929
MAX_RECENTLY_USED_TIME_RANGES,
3030
OS_NOTIFICATION_PLUGIN,
3131
} from '../../../../utils/constants';
32-
import { getFindingsVisualizationSpec } from '../../../Overview/utils/helpers';
32+
import {
33+
getChartTimeUnit,
34+
getDateFormatByTimeUnit,
35+
getFindingsVisualizationSpec,
36+
} from '../../../Overview/utils/helpers';
3337
import { CoreServicesContext } from '../../../../components/core_services';
3438
import { Finding } from '../../models/interfaces';
3539
import { Detector } from '../../../../../models/interfaces';
36-
import { FeatureChannelList } from '../../../../../server/models/interfaces/Notifications';
40+
import { FeatureChannelList } from '../../../../../server/models/interfaces';
3741
import {
3842
getNotificationChannels,
3943
parseNotificationChannelsToOptions,
@@ -46,6 +50,7 @@ import {
4650
} from '../../../../utils/helpers';
4751
import { DetectorHit, RuleSource } from '../../../../../server/models/interfaces';
4852
import { NotificationsStart } from 'opensearch-dashboards/public';
53+
import { ruleSeverity } from '../../../Rules/utils/constants';
4954

5055
interface FindingsProps extends RouteComponentProps {
5156
detectorService: DetectorsService;
@@ -68,6 +73,7 @@ interface FindingsState {
6873
groupBy: FindingsGroupByType;
6974
filteredFindings: FindingItemType[];
7075
plugins: string[];
76+
timeUnit: string;
7177
}
7278

7379
interface FindingVisualizationData {
@@ -103,6 +109,7 @@ export default class Findings extends Component<FindingsProps, FindingsState> {
103109
groupBy: 'logType',
104110
filteredFindings: [],
105111
plugins: [],
112+
timeUnit: 'yearmonthdatehoursminutes',
106113
};
107114
}
108115

@@ -234,10 +241,12 @@ export default class Findings extends Component<FindingsProps, FindingsState> {
234241
if (recentlyUsedRanges.length > MAX_RECENTLY_USED_TIME_RANGES)
235242
recentlyUsedRanges = recentlyUsedRanges.slice(0, MAX_RECENTLY_USED_TIME_RANGES);
236243
const endTime = start === end ? DEFAULT_DATE_RANGE.end : end;
244+
const timeUnit = getChartTimeUnit(start, endTime);
237245
this.setState({
238246
startTime: start,
239247
endTime: endTime,
240248
recentlyUsedRanges: recentlyUsedRanges,
249+
timeUnit,
241250
});
242251
};
243252

@@ -256,7 +265,10 @@ export default class Findings extends Component<FindingsProps, FindingsState> {
256265
});
257266
});
258267

259-
return getFindingsVisualizationSpec(visData, this.state.groupBy);
268+
return getFindingsVisualizationSpec(visData, this.state.groupBy, {
269+
timeUnit: this.state.timeUnit,
270+
dateFormat: getDateFormatByTimeUnit(this.state.startTime, this.state.endTime),
271+
});
260272
}
261273

262274
createGroupByControl(): React.ReactNode {

public/pages/Overview/utils/constants.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,18 @@ export const summaryGroupByOptions = [
99
];
1010

1111
export const moreLink = 'https://opensearch.org/docs/latest/security-analytics/';
12+
13+
/**
14+
* Time axis' timeUnit map
15+
* for each time search unit there is a mapped chart timeUnit
16+
*/
17+
export const TimeUnitsMap: {
18+
[key: string]: string;
19+
} = {
20+
minutes: 'yearmonthdatehoursminutes',
21+
hours: 'yearmonthdatehoursminutes',
22+
days: 'yearmonthdatehours',
23+
weeks: 'yearmonthdatehours',
24+
months: 'yearmonthdatehours',
25+
years: 'yearmonthdate',
26+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { getChartTimeUnit, getDateFormatByTimeUnit } from './helpers';
2+
import { TimeUnitsMap } from './constants';
3+
4+
describe('helper utilities spec', () => {
5+
describe('tests getDateFormatByTimeUnit function', () => {
6+
const yearFormat = '%Y-%m-%d';
7+
const dayFormat = '%H:%M:%S';
8+
const fullFormat = '%Y-%m-%d %H:%M';
9+
10+
const timeFormats: {
11+
[key: string]: string;
12+
} = {
13+
'now-15m': dayFormat,
14+
'now-15h': fullFormat,
15+
'now-15d': fullFormat,
16+
'now-2M': yearFormat,
17+
'now-2y': fullFormat,
18+
};
19+
20+
it(` - function should return default format ${fullFormat} if dates are not valid`, () => {
21+
expect(getDateFormatByTimeUnit('', '')).toBe(fullFormat);
22+
});
23+
24+
for (const [start, format] of Object.entries(timeFormats)) {
25+
it(` - function should return ${format} if start date is ${start}`, () => {
26+
expect(getDateFormatByTimeUnit(start, 'now')).toBe(format);
27+
});
28+
}
29+
});
30+
31+
describe('tests getChartTimeUnit function', () => {
32+
const defaultTimeUnit = 'yearmonthdatehoursminutes';
33+
it(' - function should return default timeUnit if fn params are invalid', () => {
34+
expect(getChartTimeUnit('', '')).toBe(defaultTimeUnit);
35+
});
36+
37+
it(' - function should return default timeUnit if one is passed as param', () => {
38+
const defaultFormat = 'yearmonthdate';
39+
expect(getChartTimeUnit('', '', defaultFormat)).toBe(defaultFormat);
40+
});
41+
42+
const timeUnits: {
43+
[key: string]: string;
44+
} = {
45+
minutes: 'now-15m',
46+
hours: 'now-15h',
47+
days: 'now-15d',
48+
weeks: 'now-15w',
49+
months: 'now-5M',
50+
years: 'now-15y',
51+
};
52+
53+
for (const [unit, start] of Object.entries(timeUnits)) {
54+
it(` - function should return ${TimeUnitsMap[unit]} if unit is ${unit}`, () => {
55+
expect(getChartTimeUnit(start, 'now')).toBe(TimeUnitsMap[unit]);
56+
});
57+
}
58+
});
59+
});

0 commit comments

Comments
 (0)