Commit c83361e5 authored by Miguel Rincon's avatar Miguel Rincon

Added fix to time window selection

- Use key in the time window state instead of entire object
- Extend specs to cover more cases
parent 20fc1535
import { __ } from '~/locale';
export const defaultTimeWindow = 'thirtyMinutes';
export const defaultTimeWindow = 'oneHour';
export const timeWindows = {
thirtyMinutes: {
oneHour: {
label: __('1 hour'),
seconds: 60 * 60,
},
threeHours: {
fourHours: {
label: __('4 hours'),
seconds: 60 * 60 * 4,
},
......@@ -23,7 +23,7 @@ export const timeWindows = {
label: __('Past week'),
seconds: 60 * 60 * 24 * 7,
},
pastMonth: {
twoWeeks: {
label: __('2 weeks'),
seconds: 60 * 60 * 24 * 15,
},
......
......@@ -7,6 +7,7 @@ import { s__ } from '~/locale';
import * as types from './mutation_types';
import { getTimeRange } from '../utils';
import { timeWindows } from '../constants';
const requestLogsUntilData = params =>
backOff((next, stop) => {
......@@ -40,8 +41,8 @@ export const setSearch = ({ dispatch, commit }, searchQuery) => {
dispatch('fetchLogs');
};
export const setTimeWindow = ({ dispatch, commit }, timeWindow) => {
commit(types.SET_TIME_WINDOW, timeWindow);
export const setTimeWindow = ({ dispatch, commit }, timeWindowKey) => {
commit(types.SET_TIME_WINDOW, timeWindowKey);
dispatch('fetchLogs');
};
......@@ -74,7 +75,9 @@ export const fetchLogs = ({ commit, state }) => {
};
if (state.timeWindow.current) {
const { start, end } = getTimeRange(state.timeWindow.current.seconds);
const { current } = state.timeWindow;
const { start, end } = getTimeRange(timeWindows[current].seconds);
params.start = start;
params.end = end;
}
......
......@@ -16,8 +16,8 @@ export default {
state.enableAdvancedQuerying = enableAdvancedQuerying;
},
/** Time Range data */
[types.SET_TIME_WINDOW](state, timeWindow) {
state.timeWindow.current = timeWindow;
[types.SET_TIME_WINDOW](state, timeWindowKey) {
state.timeWindow.current = timeWindowKey;
},
/** Environments data */
......
......@@ -11,6 +11,7 @@ import {
fetchLogs,
} from 'ee/logs/stores/actions';
import { getTimeRange } from 'ee/logs/utils';
import { timeWindows } from 'ee/logs/constants';
import axios from '~/lib/utils/axios_utils';
import flash from '~/flash';
......@@ -34,6 +35,7 @@ describe('Logs Store actions', () => {
let state;
let mock;
const mockThirtyMinutesSeconds = 3600;
const mockThirtyMinutes = {
start: '2020-01-09T18:06:20.000Z',
end: '2020-01-09T18:36:20.000Z',
......@@ -172,17 +174,26 @@ describe('Logs Store actions', () => {
{ type: types.RECEIVE_LOGS_DATA_SUCCESS, payload: mockLogsResult },
],
[],
done,
() => {
expect(getTimeRange).toHaveBeenCalledWith(mockThirtyMinutesSeconds);
done();
},
);
});
it('should commit logs and pod data when there is pod name defined and a non-default date range', done => {
const mockOneDay = { start: '2020-01-08T18:41:39.000Z', end: '2020-01-09T18:41:39.000Z' };
const mockOneDaySeconds = timeWindows.oneDay.seconds;
const mockOneDay = {
start: '2020-01-08T18:41:39.000Z',
end: '2020-01-09T18:41:39.000Z',
};
getTimeRange.mockReturnValueOnce(mockOneDay);
state.projectPath = mockProjectPath;
state.environments.current = mockEnvName;
state.pods.current = mockPodName;
state.timeWindow.current = 'oneDay';
const endpoint = `/${mockProjectPath}/-/logs/k8s.json`;
......@@ -210,7 +221,10 @@ describe('Logs Store actions', () => {
{ type: types.RECEIVE_LOGS_DATA_SUCCESS, payload: mockLogsResult },
],
[],
done,
() => {
expect(getTimeRange).toHaveBeenCalledWith(mockOneDaySeconds);
done();
},
);
});
......
......@@ -136,6 +136,15 @@ describe('Logs Store Mutations', () => {
});
});
describe('SET_TIME_WINDOW', () => {
it('sets a time window Key', () => {
const mockKey = 'fourHours';
mutations[types.SET_TIME_WINDOW](state, mockKey);
expect(state.timeWindow.current).toEqual(mockKey);
});
});
describe('REQUEST_PODS_DATA', () => {
it('receives log data error and stops loading', () => {
mutations[types.REQUEST_PODS_DATA](state);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment