From 34306fc85d88d7580566a9ffea8e169cb5dcd714 Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Tue, 24 Sep 2019 14:30:29 -0400 Subject: [PATCH] [Uptime] [Blocker] Move absolute date parsing to URL params hook (#46066) (#46093) * Move absolute date parsing to URL params hook. * Clean up naming. * Update code to avoid putting dynamically-computed values in URL. * Add test for new helper function. --- .../common/constants/client_defaults.ts | 3 ++ .../monitor_charts.test.tsx.snap | 4 +-- .../__tests__/monitor_charts.test.tsx | 13 ++++++++ .../functional/charts/checks_chart.tsx | 11 +++---- .../functional/charts/duration_chart.tsx | 16 ++++------ .../functional/charts/snapshot_histogram.tsx | 12 ++++---- .../components/functional/monitor_charts.tsx | 14 ++++----- .../use_url_params.test.tsx.snap | 2 +- .../hooks/__tests__/use_url_params.test.tsx | 5 ++++ .../public/lib/helper/stringify_url_params.ts | 2 +- .../get_supported_url_params.test.ts.snap | 14 +++++++-- .../get_supported_url_params.test.ts | 19 ++++++++++-- .../__tests__/parse_absolute_date.test.ts | 30 +++++++++++++++++++ .../url_params/get_supported_url_params.ts | 13 ++++++++ .../helper/url_params/parse_absolute_date.ts | 15 ++++++++++ .../plugins/uptime/public/pages/monitor.tsx | 2 +- .../plugins/uptime/public/pages/overview.tsx | 28 +++++------------ 17 files changed, 143 insertions(+), 60 deletions(-) create mode 100644 x-pack/legacy/plugins/uptime/public/lib/helper/url_params/__tests__/parse_absolute_date.test.ts create mode 100644 x-pack/legacy/plugins/uptime/public/lib/helper/url_params/parse_absolute_date.ts diff --git a/x-pack/legacy/plugins/uptime/common/constants/client_defaults.ts b/x-pack/legacy/plugins/uptime/common/constants/client_defaults.ts index 30ca486b278e2..21e866e991944 100644 --- a/x-pack/legacy/plugins/uptime/common/constants/client_defaults.ts +++ b/x-pack/legacy/plugins/uptime/common/constants/client_defaults.ts @@ -5,6 +5,9 @@ */ export const CLIENT_DEFAULTS = { + ABSOLUTE_DATE_RANGE_START: 0, + // 15 minutes + ABSOLUTE_DATE_RANGE_END: 1000 * 60 * 15, // 60 seconds AUTOREFRESH_INTERVAL: 60 * 1000, // polling defaults to "on" diff --git a/x-pack/legacy/plugins/uptime/public/components/functional/__tests__/__snapshots__/monitor_charts.test.tsx.snap b/x-pack/legacy/plugins/uptime/public/components/functional/__tests__/__snapshots__/monitor_charts.test.tsx.snap index 6f17de1e26c21..1272e939e4b43 100644 --- a/x-pack/legacy/plugins/uptime/public/components/functional/__tests__/__snapshots__/monitor_charts.test.tsx.snap +++ b/x-pack/legacy/plugins/uptime/public/components/functional/__tests__/__snapshots__/monitor_charts.test.tsx.snap @@ -65,8 +65,8 @@ exports[`MonitorCharts component renders the component without errors 1`] = ` { + let dateMathSpy: any; + const MOCK_DATE_VALUE = 20; + + beforeEach(() => { + dateMathSpy = jest.spyOn(DateMath, 'parse'); + dateMathSpy.mockReturnValue(MOCK_DATE_VALUE); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + const chartResponse: { monitorChartsData: MonitorChart } = { monitorChartsData: { locationDurationLines: [ diff --git a/x-pack/legacy/plugins/uptime/public/components/functional/charts/checks_chart.tsx b/x-pack/legacy/plugins/uptime/public/components/functional/charts/checks_chart.tsx index d1f29d3309497..c7ac0a59e4434 100644 --- a/x-pack/legacy/plugins/uptime/public/components/functional/charts/checks_chart.tsx +++ b/x-pack/legacy/plugins/uptime/public/components/functional/charts/checks_chart.tsx @@ -16,13 +16,13 @@ import { timeFormatter, } from '@elastic/charts'; import { EuiPanel, EuiTitle } from '@elastic/eui'; -import React, { useContext } from 'react'; +import React from 'react'; import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; import { StatusData } from '../../../../common/graphql/types'; import { getChartDateLabel } from '../../../lib/helper'; -import { UptimeSettingsContext } from '../../../contexts'; import { getColorsMap } from './get_colors_map'; +import { useUrlParams } from '../../../hooks'; interface ChecksChartProps { /** @@ -47,7 +47,8 @@ interface ChecksChartProps { export const ChecksChart = ({ dangerColor, status, successColor }: ChecksChartProps) => { const upSeriesSpecId = getSpecId('Up'); const downSeriesSpecId = getSpecId('Down'); - const { absoluteStartDate, absoluteEndDate } = useContext(UptimeSettingsContext); + const [getUrlParams] = useUrlParams(); + const { absoluteDateRangeStart: min, absoluteDateRangeEnd: max } = getUrlParams(); const upString = i18n.translate('xpack.uptime.monitorCharts.checkStatus.series.upCountLabel', { defaultMessage: 'Up count', @@ -71,12 +72,12 @@ export const ChecksChart = ({ dangerColor, status, successColor }: ChecksChartPr - + { - const { absoluteStartDate, absoluteEndDate } = useContext(UptimeSettingsContext); + const [getUrlParams] = useUrlParams(); + const { absoluteDateRangeStart: min, absoluteDateRangeEnd: max } = getUrlParams(); // this id is used for the line chart representing the average duration length const averageSpecId = getSpecId('average-'); @@ -99,16 +99,12 @@ export const DurationChart = ({ - + diff --git a/x-pack/legacy/plugins/uptime/public/components/functional/monitor_charts.tsx b/x-pack/legacy/plugins/uptime/public/components/functional/monitor_charts.tsx index 5dffe94045fb6..1d18919d5c473 100644 --- a/x-pack/legacy/plugins/uptime/public/components/functional/monitor_charts.tsx +++ b/x-pack/legacy/plugins/uptime/public/components/functional/monitor_charts.tsx @@ -7,14 +7,13 @@ import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import React, { Fragment, useContext } from 'react'; -import DateMath from '@elastic/datemath'; -import { Moment } from 'moment'; import { MonitorChart } from '../../../common/graphql/types'; import { UptimeGraphQLQueryProps, withUptimeGraphQL } from '../higher_order'; import { monitorChartsQuery } from '../../queries'; import { DurationChart } from './charts'; import { UptimeSettingsContext } from '../../contexts'; import { SnapshotHistogram } from './charts/snapshot_histogram'; +import { useUrlParams } from '../../hooks'; interface MonitorChartsQueryResult { monitorChartsData?: MonitorChart; @@ -47,10 +46,9 @@ export const MonitorChartsComponent = ({ } = data; const { colors } = useContext(UptimeSettingsContext); - const parseDateRange = (r: string): number => { - const parsed: Moment | undefined = DateMath.parse(r); - return parsed ? parsed.valueOf() : 0; - }; + const [getUrlParams] = useUrlParams(); + const { absoluteDateRangeStart, absoluteDateRangeEnd } = getUrlParams(); + return ( @@ -64,8 +62,8 @@ export const MonitorChartsComponent = ({
- {"autorefreshInterval":60000,"autorefreshIsPaused":false,"dateRangeStart":"now-19d","dateRangeEnd":"now-1m","filters":"","search":"","selectedPingStatus":"down","statusFilter":""} + {"absoluteDateRangeStart":20,"absoluteDateRangeEnd":20,"autorefreshInterval":60000,"autorefreshIsPaused":false,"dateRangeStart":"now-19d","dateRangeEnd":"now-1m","filters":"","search":"","selectedPingStatus":"down","statusFilter":""}