Skip to content

Commit

Permalink
[FEATURE] Set default time range to 24 hrs and share the same setting…
Browse files Browse the repository at this point in the history
… for all UI pages opensearch-project#253

[FEATURE] Charts should show the entire time range selected in the filter opensearch-project#258

Signed-off-by: Jovan Cvetkovic <[email protected]>
  • Loading branch information
jovancvetkovic3006 committed Dec 24, 2022
1 parent c6d5dbe commit 4d9db71
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 14 deletions.
5 changes: 3 additions & 2 deletions public/pages/Findings/containers/Findings/Findings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import {
getChartTimeUnit,
getDateFormatByTimeUnit,
getDomainRange,
getFindingsVisualizationSpec,
} from '../../../Overview/utils/helpers';
import { CoreServicesContext } from '../../../../components/core_services';
Expand All @@ -51,7 +52,6 @@ import {
} from '../../../../utils/helpers';
import { DetectorHit, RuleSource } from '../../../../../server/models/interfaces';
import { NotificationsStart } from 'opensearch-dashboards/public';
import { ruleSeverity } from '../../../Rules/utils/constants';

interface FindingsProps extends RouteComponentProps {
detectorService: DetectorsService;
Expand Down Expand Up @@ -111,7 +111,7 @@ class Findings extends Component<FindingsProps, FindingsState> {
groupBy: 'logType',
filteredFindings: [],
plugins: [],
timeUnit: 'yearmonthdatehoursminutes',
timeUnit: getChartTimeUnit(DEFAULT_DATE_RANGE.start, DEFAULT_DATE_RANGE.end),
};
}

Expand Down Expand Up @@ -273,6 +273,7 @@ class Findings extends Component<FindingsProps, FindingsState> {
return getFindingsVisualizationSpec(visData, this.state.groupBy, {
timeUnit: this.state.timeUnit,
dateFormat: getDateFormatByTimeUnit(this.state.startTime, this.state.endTime),
domain: getDomainRange([this.state.startTime, this.state.endTime]),
});
}

Expand Down
91 changes: 80 additions & 11 deletions public/pages/Overview/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import { SummaryData } from '../components/Widgets/Summary';
import dateMath from '@elastic/datemath';
import { TimeUnitsMap } from './constants';
import _ from 'lodash';
import { DEFAULT_DATE_RANGE } from '../../../utils/constants';

export type DateOpts = {
timeUnit: string;
dateFormat: string;
domain?: number[];
};

/**
Expand Down Expand Up @@ -45,7 +47,7 @@ const addInteractiveLegends = (layer: any) => _.defaultsDeep(layer, legendSelect
function getVisualizationSpec(description: string, data: any, layers: any[]): TopLevelSpec {
return {
config: {
view: { stroke: null },
view: { stroke: 'transparent' },
legend: {
labelColor: '#343741',
titleColor: '#1a1c21',
Expand Down Expand Up @@ -143,6 +145,15 @@ export function getOverviewVisualizationSpec(
);
}

export const getDomainRange = (
range: string[] = [DEFAULT_DATE_RANGE.start, DEFAULT_DATE_RANGE.end]
): number[] => {
return [
dateMath.parse(range[0])?.toDate().getTime(),
dateMath.parse(range[1])?.toDate().getTime(),
];
};

/**
* Returns chart x-axis date format based on time span
* @param start
Expand Down Expand Up @@ -186,21 +197,48 @@ export function getFindingsVisualizationSpec(
dateOpts: DateOpts = {
timeUnit: 'yearmonthdatehoursminutes',
dateFormat: '%Y-%m-%d %H:%M',
domain: [DEFAULT_DATE_RANGE.start, DEFAULT_DATE_RANGE.end],
}
) {
console.log('TIME UNIT', dateOpts.timeUnit);
// dateOpts.timeUnit = 'yearmonthdatehours';
return getVisualizationSpec('Findings data overview', visualizationData, [
addInteractiveLegends({
mark: 'bar',
mark: {
type: 'bar',
strokeWidth: 1,
stroke: 'white',
clip: true,
},
encoding: {
tooltip: [{ field: 'finding', aggregate: 'sum', type: 'quantitative', title: 'Findings' }],
tooltip: [
{
field: 'finding',
aggregate: 'sum',
type: 'quantitative',
title: 'Findings',
},
{
field: 'time',
type: 'temporal',
title: 'Time',
format: '%Y-%m-%d %H:%M',
},
],
x: {
timeUnit: dateOpts.timeUnit,
field: 'time',
title: '',
type: 'temporal',
axis: {
grid: false,
format: dateOpts.dateFormat,
},
scale: {
type: 'time',
field: 'time',
domain: dateOpts.domain,
},
},
y: {
aggregate: 'sum',
Expand Down Expand Up @@ -328,22 +366,53 @@ export function getChartTimeUnit(
): string {
const startMoment = dateMath.parse(start);
const endMoment = dateMath.parse(end);
let minUnit: string = 'minutes';
let timeUnit: string = TimeUnitsMap[minUnit];
let timeUnit: string = 'yearmonthdatehoursminutes';

if (!startMoment || !endMoment) return defaultUnit;

try {
const timeDiff = endMoment.diff(startMoment);
const momentTimeDiff = moment.duration(timeDiff);

const timeUnits: string[] = ['years', 'months', 'days', 'hours', 'minutes'];
for (const unit of timeUnits) {
// @ts-ignore
if (momentTimeDiff._data[unit]) {
timeUnit = TimeUnitsMap[unit];
break;
console.log('TIME UNIT', momentTimeDiff);
const { years, months, days, hours, minutes, seconds } = momentTimeDiff._data;

if (!years) {
if (!months) {
if (!days) {
if (!hours) {
if (minutes < 60) {
timeUnit = 'yearmonthdatehoursminutes';
}
} else {
if (hours < 60) {
timeUnit = 'yearmonthdatehours';
}
}
} else {
if (days === 1) {
timeUnit = 'yearmonthdatehours';
} else if (days <= 14) {
timeUnit = 'yearmonthdate';
} else {
timeUnit = 'yearmonthdate';
}
}
} else {
if (months <= 6) {
timeUnit = 'yearmonthdate';
} else {
timeUnit = 'yearmonth';
}
}
} else if (years <= 6) {
timeUnit = 'yearmonth';

if (years > 2) {
timeUnit = 'yearquarter';
}
} else {
timeUnit = 'year';
}
} catch (e) {
console.error(`Time diff can't be calculated for dates: ${start} and ${end}`);
Expand Down
2 changes: 1 addition & 1 deletion public/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DETECTOR_TYPES } from '../pages/Detectors/utils/constants';

export const DATE_MATH_FORMAT = 'YYYY-MM-DDTHH:mm:ss.SSSZ';
export const MAX_RECENTLY_USED_TIME_RANGES = 5;
export const DEFAULT_DATE_RANGE = { start: 'now-15m', end: 'now' };
export const DEFAULT_DATE_RANGE = { start: 'now-24h', end: 'now' };

export const PLUGIN_NAME = 'opensearch_security_analytics_dashboards';
export const OS_NOTIFICATION_PLUGIN = 'opensearch-notifications';
Expand Down

0 comments on commit 4d9db71

Please sign in to comment.