Skip to content

Commit

Permalink
PMM-9987: subtracting-a-minute-for-qan (#1356)
Browse files Browse the repository at this point in the history
* PMM-9987-subtracting-a-minute-for-qan: fix time update logic, removing double requests, subtract minute

* PMM-9987-subtracting-a-minute-for-qan: no subtracting from no 'now' mode

* PMM-9987-subtracting-a-minute-for-qan: subtract one minute in tests
  • Loading branch information
solovevayaroslavna authored Jul 29, 2022
1 parent 843168b commit 4f85f1b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
5 changes: 0 additions & 5 deletions pmm-app/src/pmm-qan/panel/provider/provider.tools.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getLocationSrv } from '@grafana/runtime';
import { ParseQueryParamDate } from 'shared/components/helpers/time-parameters-parser';
import {
ALL_VARIABLE_TEXT,
AUTO_VARIABLE_TEXT,
Expand Down Expand Up @@ -135,10 +134,6 @@ export const refreshGrafanaVariables = (state) => {
};

export const parseURL = (query) => ({
from: ParseQueryParamDate.transform(query.get('from') || 'now-12h', 'from'),
to: ParseQueryParamDate.transform(query.get('to') || 'now', 'to')
.utc()
.format('YYYY-MM-DDTHH:mm:ssZ'),
columns: JSON.parse(query.get('columns')) || DEFAULT_COLUMNS,
labels: setFilters(query),
pageNumber: query.get('page_number') || DEFAULT_PAGE_NUMBER,
Expand Down
30 changes: 27 additions & 3 deletions pmm-app/src/pmm-qan/panel/provider/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect, useState, useRef } from 'react';
import { omit, isEqual } from 'lodash';
import { parseURL, refreshGrafanaVariables, setLabels } from './provider.tools';
import { QueryAnalyticsContext } from './provider.types';
import { ParseQueryParamDate } from '../../../shared/components/helpers/time-parameters-parser';

const initialState = {} as QueryAnalyticsContext;

Expand Down Expand Up @@ -135,13 +136,21 @@ export const UrlParametersProvider = ({ timeRange, children }) => {

const query = new URLSearchParams(window.location.search);
const searchRef = useRef<string| null>(null);
const [fromTimeMomentValue, setFromTimeMomentValue] = useState(ParseQueryParamDate.transform(
query.get('from') || 'now-12h', 'from',
).utc().format('YYYY-MM-DDTHH:mm:ssZ'));
const [toTimeMomentValue, setToTimeMomentValue] = useState(ParseQueryParamDate.transform(
query.get('to') || 'now', 'to',
).utc().format('YYYY-MM-DDTHH:mm:ssZ'));

const [panelState, setContext] = useState({
...parseURL(query),
rawTime: {
from: timeRange.raw.from,
to: timeRange.raw.to,
},
from: fromTimeMomentValue,
to: toTimeMomentValue,
search: searchRef.current,
});

Expand Down Expand Up @@ -173,14 +182,29 @@ export const UrlParametersProvider = ({ timeRange, children }) => {
const [to, setTo] = useState(getAbsoluteTime(timeRange.raw.to));
const [previousState, setPreviousState] = useState(panelState);

useEffect(() => {
const newFrom = getAbsoluteTime(timeRange.raw.from);
const newTo = getAbsoluteTime(timeRange.raw.to);

if (!((from === newFrom) && (to === newTo))) {
if (newTo === 'now') {
setToTimeMomentValue(timeRange.to.utc().subtract(1, 'minute').format('YYYY-MM-DDTHH:mm:ssZ'));
setFromTimeMomentValue(timeRange.from.utc().subtract(1, 'minute').format('YYYY-MM-DDTHH:mm:ssZ'));
} else {
setToTimeMomentValue(timeRange.to.utc().format('YYYY-MM-DDTHH:mm:ssZ'));
setFromTimeMomentValue(timeRange.from.utc().format('YYYY-MM-DDTHH:mm:ssZ'));
}
}
}, [timeRange, from, to]);

useEffect(() => {
const newFrom = getAbsoluteTime(timeRange.raw.from);
const newTo = getAbsoluteTime(timeRange.raw.to);

const newState = {
...panelState,
from: timeRange.from.utc().format('YYYY-MM-DDTHH:mm:ssZ'),
to: timeRange.to.utc().format('YYYY-MM-DDTHH:mm:ssZ'),
from: fromTimeMomentValue,
to: toTimeMomentValue,
rawTime: {
from: newFrom,
to: newTo,
Expand All @@ -207,7 +231,7 @@ export const UrlParametersProvider = ({ timeRange, children }) => {
setFrom(newFrom);
setTo(newTo);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [timeRange, from, to]);
}, [fromTimeMomentValue, toTimeMomentValue]);

const wrapAction = (key) => (...value) => setContext(actions[key](...value));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ describe('Time parameters parser::', () => {
const result2 = ParseQueryParamDate.transform('now/d', 'to');
const result3 = ParseQueryParamDate.transform('now/d', 'from');

expect(result1.format('YYYY-MM-DDTHH:mm:ss.000')).toStrictEqual('2020-11-15T00:00:59.000');
expect(result2.format('YYYY-MM-DDTHH:mm:ss.000')).toStrictEqual('2020-11-22T23:59:59.000');
expect(result3.format('YYYY-MM-DDTHH:mm:ss.000')).toStrictEqual('2020-11-22T00:00:00.000');
expect(result1.format('YYYY-MM-DDTHH:mm:ss.000')).toStrictEqual('2020-11-14T23:59:59.000');
expect(result2.format('YYYY-MM-DDTHH:mm:ss.000')).toStrictEqual('2020-11-22T23:58:59.000');
expect(result3.format('YYYY-MM-DDTHH:mm:ss.000')).toStrictEqual('2020-11-21T23:59:00.000');
});

it('date is now', () => {
const result1 = ParseQueryParamDate.transform('now');

expect(result1.toISOString()).toStrictEqual('2020-11-22T00:00:00.000Z');
expect(result1.toISOString()).toStrictEqual('2020-11-21T23:59:00.000Z');
});

it('date is incorrect', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ export class ParseQueryParamDate {
}

if (date === 'now') {
return nowFunc();
return nowFunc().subtract(1, 'minute');
}

if (date.length > 4 && date.startsWith('now')) {
// Calculate complex time range, for example 'Week to date'
// from=now-5d&to=now-6M ... from=now/w&to=now/w
// https://grafana.com/docs/grafana/latest/dashboards/time-range-controls/
parsedDate = getComplexTimeValue(date, nowFunc, edge);
parsedDate = getComplexTimeValue(date, nowFunc, edge).subtract(1, 'minute');
} else {
// expect unix timestamp in milliseconds
const isNum = /^\d+$/.test(date);
Expand Down

0 comments on commit 4f85f1b

Please sign in to comment.