From 6ce88f946dd207c3307d60c5daa9e548bb0019f5 Mon Sep 17 00:00:00 2001 From: ebb-tide Date: Thu, 31 May 2018 16:45:33 -0700 Subject: [PATCH] Set Dashboard time range from query string middleware Co-authored-by: Deniz Kusefoglu Co-authored-by: Jared Scheib --- ui/src/dashboards/actions/index.js | 8 --- ui/src/dashboards/containers/DashboardPage.js | 9 +--- ui/src/dashboards/reducers/dashTimeV1.js | 7 --- ui/src/shared/data/timeRanges.js | 9 ++++ ui/src/shared/middleware/queryStringConfig.js | 50 ++++++++++++++++--- .../dashboards/reducers/dashTimeV1.test.js | 13 +---- 6 files changed, 55 insertions(+), 41 deletions(-) diff --git a/ui/src/dashboards/actions/index.js b/ui/src/dashboards/actions/index.js index 16a640d4d..81e0ec3b1 100644 --- a/ui/src/dashboards/actions/index.js +++ b/ui/src/dashboards/actions/index.js @@ -45,14 +45,6 @@ export const loadDeafaultDashTimeV1 = dashboardID => ({ }, }) -export const addDashTimeV1 = (dashboardID, timeRange) => ({ - type: 'ADD_DASHBOARD_TIME_V1', - payload: { - dashboardID, - timeRange, - }, -}) - export const setDashTimeV1 = (dashboardID, timeRange) => ({ type: 'SET_DASHBOARD_TIME_V1', payload: { diff --git a/ui/src/dashboards/containers/DashboardPage.js b/ui/src/dashboards/containers/DashboardPage.js index 814457fec..b9d937372 100644 --- a/ui/src/dashboards/containers/DashboardPage.js +++ b/ui/src/dashboards/containers/DashboardPage.js @@ -49,6 +49,7 @@ import { TEMP_VAR_DASHBOARD_TIME, TEMP_VAR_UPPER_DASHBOARD_TIME, } from 'shared/constants' +import {FORMAT_INFLUXQL, defaultTimeRange} from 'src/shared/data/timeRanges' import { notifyDashboardNotFound, notifyInvalidTempVarValueInURLQuery, @@ -57,14 +58,6 @@ import {colorsStringSchema, colorsNumberSchema} from 'shared/schemas' import {ErrorHandling} from 'src/shared/decorators/errors' import {OverlayContext} from 'src/shared/components/OverlayTechnology' -const FORMAT_INFLUXQL = 'influxql' -const defaultTimeRange = { - upper: null, - lower: 'now() - 15m', - seconds: 900, - format: FORMAT_INFLUXQL, -} - @ErrorHandling class DashboardPage extends Component { constructor(props) { diff --git a/ui/src/dashboards/reducers/dashTimeV1.js b/ui/src/dashboards/reducers/dashTimeV1.js index 4d8c81d27..b1b6c82f6 100644 --- a/ui/src/dashboards/reducers/dashTimeV1.js +++ b/ui/src/dashboards/reducers/dashTimeV1.js @@ -5,13 +5,6 @@ const initialState = { const dashTimeV1 = (state = initialState, action) => { switch (action.type) { - case 'ADD_DASHBOARD_TIME_V1': { - const {dashboardID, timeRange} = action.payload - const ranges = [...state.ranges, {dashboardID, timeRange}] - - return {...state, ranges} - } - case 'DELETE_DASHBOARD': { const {dashboardID} = action.payload const ranges = state.ranges.filter(r => r.dashboardID !== dashboardID) diff --git a/ui/src/shared/data/timeRanges.js b/ui/src/shared/data/timeRanges.js index 1631bcd93..dd52fbcd7 100644 --- a/ui/src/shared/data/timeRanges.js +++ b/ui/src/shared/data/timeRanges.js @@ -72,3 +72,12 @@ export const timeRanges = [ menuOption: 'Past 30d', }, ] + +export const FORMAT_INFLUXQL = 'influxql' + +export const defaultTimeRange = { + upper: null, + lower: 'now() - 15m', + seconds: 900, + format: FORMAT_INFLUXQL, +} diff --git a/ui/src/shared/middleware/queryStringConfig.js b/ui/src/shared/middleware/queryStringConfig.js index e72d898b5..943fcde39 100644 --- a/ui/src/shared/middleware/queryStringConfig.js +++ b/ui/src/shared/middleware/queryStringConfig.js @@ -2,13 +2,51 @@ import queryString from 'query-string' import {enablePresentationMode} from 'src/shared/actions/app' +import {setDashTimeV1} from 'src/dashboards/actions' +import {timeRanges, defaultTimeRange} from 'src/shared/data/timeRanges' +import idNormalizer, {TYPE_ID} from 'src/normalizers/id' -export const queryStringConfig = () => dispatch => action => { - dispatch(action) - const urlQueries = queryString.parse(window.location.search) +export const queryStringConfig = store => { + let prevPath + return dispatch => action => { + dispatch(action) + const urlQueries = queryString.parse(window.location.search) - // Presentation Mode - if (urlQueries.present === 'true') { - dispatch(enablePresentationMode()) + // Presentation Mode + if (urlQueries.present === 'true') { + dispatch(enablePresentationMode()) + } + + const dashboardRegex = /\/sources\/\d+\/dashboards\/(\d+)/ + if (dashboardRegex.test(window.location.pathname)) { + const currentPath = window.location.pathname + const dashboardID = currentPath.match(dashboardRegex)[1] + if (currentPath !== prevPath) { + const {dashTimeV1} = store.getState() + + const foundTimeRange = dashTimeV1.ranges.find( + r => r.dashboardID === idNormalizer(TYPE_ID, dashboardID) + ) + + let timeRange = foundTimeRange || defaultTimeRange + + // if lower and upper in urlQueries. + // and if valid. + + if (urlQueries.upper) { + timeRange = { + ...timeRange, + upper: urlQueries.upper, + lower: urlQueries.lower, + } + } else { + timeRange = timeRanges.find(t => t.lower === urlQueries.lower) + } + if (foundTimeRange) { + dispatch(setDashTimeV1(+dashboardID, timeRange)) + } + } + prevPath = currentPath + } } } diff --git a/ui/test/dashboards/reducers/dashTimeV1.test.js b/ui/test/dashboards/reducers/dashTimeV1.test.js index 9a42f2b82..66b4d3de8 100644 --- a/ui/test/dashboards/reducers/dashTimeV1.test.js +++ b/ui/test/dashboards/reducers/dashTimeV1.test.js @@ -1,9 +1,5 @@ import reducer from 'src/dashboards/reducers/dashTimeV1' -import { - addDashTimeV1, - setDashTimeV1, - deleteDashboard, -} from 'src/dashboards/actions/index' +import {setDashTimeV1, deleteDashboard} from 'src/dashboards/actions/index' const emptyState = undefined const dashboardID = 1 @@ -18,13 +14,6 @@ describe('Dashboards.Reducers.DashTimeV1', () => { expect(actual).toEqual(expected) }) - it('can add a dashboard time', () => { - const actual = reducer(emptyState, addDashTimeV1(dashboardID, timeRange)) - const expected = [{dashboardID, timeRange}] - - expect(actual.ranges).toEqual(expected) - }) - it('can delete a dashboard time range', () => { const state = { ranges: [{dashboardID, timeRange}],