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'; import { __ } from '~/locale';
export const defaultTimeWindow = 'thirtyMinutes'; export const defaultTimeWindow = 'oneHour';
export const timeWindows = { export const timeWindows = {
thirtyMinutes: { oneHour: {
label: __('1 hour'), label: __('1 hour'),
seconds: 60 * 60, seconds: 60 * 60,
}, },
threeHours: { fourHours: {
label: __('4 hours'), label: __('4 hours'),
seconds: 60 * 60 * 4, seconds: 60 * 60 * 4,
}, },
...@@ -23,7 +23,7 @@ export const timeWindows = { ...@@ -23,7 +23,7 @@ export const timeWindows = {
label: __('Past week'), label: __('Past week'),
seconds: 60 * 60 * 24 * 7, seconds: 60 * 60 * 24 * 7,
}, },
pastMonth: { twoWeeks: {
label: __('2 weeks'), label: __('2 weeks'),
seconds: 60 * 60 * 24 * 15, seconds: 60 * 60 * 24 * 15,
}, },
......
...@@ -7,6 +7,7 @@ import { s__ } from '~/locale'; ...@@ -7,6 +7,7 @@ import { s__ } from '~/locale';
import * as types from './mutation_types'; import * as types from './mutation_types';
import { getTimeRange } from '../utils'; import { getTimeRange } from '../utils';
import { timeWindows } from '../constants';
const requestLogsUntilData = params => const requestLogsUntilData = params =>
backOff((next, stop) => { backOff((next, stop) => {
...@@ -40,8 +41,8 @@ export const setSearch = ({ dispatch, commit }, searchQuery) => { ...@@ -40,8 +41,8 @@ export const setSearch = ({ dispatch, commit }, searchQuery) => {
dispatch('fetchLogs'); dispatch('fetchLogs');
}; };
export const setTimeWindow = ({ dispatch, commit }, timeWindow) => { export const setTimeWindow = ({ dispatch, commit }, timeWindowKey) => {
commit(types.SET_TIME_WINDOW, timeWindow); commit(types.SET_TIME_WINDOW, timeWindowKey);
dispatch('fetchLogs'); dispatch('fetchLogs');
}; };
...@@ -74,7 +75,9 @@ export const fetchLogs = ({ commit, state }) => { ...@@ -74,7 +75,9 @@ export const fetchLogs = ({ commit, state }) => {
}; };
if (state.timeWindow.current) { 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.start = start;
params.end = end; params.end = end;
} }
......
...@@ -16,8 +16,8 @@ export default { ...@@ -16,8 +16,8 @@ export default {
state.enableAdvancedQuerying = enableAdvancedQuerying; state.enableAdvancedQuerying = enableAdvancedQuerying;
}, },
/** Time Range data */ /** Time Range data */
[types.SET_TIME_WINDOW](state, timeWindow) { [types.SET_TIME_WINDOW](state, timeWindowKey) {
state.timeWindow.current = timeWindow; state.timeWindow.current = timeWindowKey;
}, },
/** Environments data */ /** Environments data */
......
...@@ -11,6 +11,7 @@ import { ...@@ -11,6 +11,7 @@ import {
fetchLogs, fetchLogs,
} from 'ee/logs/stores/actions'; } from 'ee/logs/stores/actions';
import { getTimeRange } from 'ee/logs/utils'; import { getTimeRange } from 'ee/logs/utils';
import { timeWindows } from 'ee/logs/constants';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import flash from '~/flash'; import flash from '~/flash';
...@@ -34,6 +35,7 @@ describe('Logs Store actions', () => { ...@@ -34,6 +35,7 @@ describe('Logs Store actions', () => {
let state; let state;
let mock; let mock;
const mockThirtyMinutesSeconds = 3600;
const mockThirtyMinutes = { const mockThirtyMinutes = {
start: '2020-01-09T18:06:20.000Z', start: '2020-01-09T18:06:20.000Z',
end: '2020-01-09T18:36:20.000Z', end: '2020-01-09T18:36:20.000Z',
...@@ -172,17 +174,26 @@ describe('Logs Store actions', () => { ...@@ -172,17 +174,26 @@ describe('Logs Store actions', () => {
{ type: types.RECEIVE_LOGS_DATA_SUCCESS, payload: mockLogsResult }, { 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 => { 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); getTimeRange.mockReturnValueOnce(mockOneDay);
state.projectPath = mockProjectPath; state.projectPath = mockProjectPath;
state.environments.current = mockEnvName; state.environments.current = mockEnvName;
state.pods.current = mockPodName; state.pods.current = mockPodName;
state.timeWindow.current = 'oneDay';
const endpoint = `/${mockProjectPath}/-/logs/k8s.json`; const endpoint = `/${mockProjectPath}/-/logs/k8s.json`;
...@@ -210,7 +221,10 @@ describe('Logs Store actions', () => { ...@@ -210,7 +221,10 @@ describe('Logs Store actions', () => {
{ type: types.RECEIVE_LOGS_DATA_SUCCESS, payload: mockLogsResult }, { type: types.RECEIVE_LOGS_DATA_SUCCESS, payload: mockLogsResult },
], ],
[], [],
done, () => {
expect(getTimeRange).toHaveBeenCalledWith(mockOneDaySeconds);
done();
},
); );
}); });
......
...@@ -136,6 +136,15 @@ describe('Logs Store Mutations', () => { ...@@ -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', () => { describe('REQUEST_PODS_DATA', () => {
it('receives log data error and stops loading', () => { it('receives log data error and stops loading', () => {
mutations[types.REQUEST_PODS_DATA](state); 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