Commit 85f4e696 authored by Andrew Fontaine's avatar Andrew Fontaine

Merge branch 'move-current-env-name-to-monitoring-store' into 'master'

Add current env name to monitoring store

See merge request gitlab-org/gitlab!28390
parents e147ce0f 7d352d66
...@@ -250,7 +250,7 @@ export default { ...@@ -250,7 +250,7 @@ export default {
}, },
}, },
created() { created() {
this.setEndpoints({ this.setInitialState({
metricsEndpoint: this.metricsEndpoint, metricsEndpoint: this.metricsEndpoint,
deploymentsEndpoint: this.deploymentsEndpoint, deploymentsEndpoint: this.deploymentsEndpoint,
dashboardEndpoint: this.dashboardEndpoint, dashboardEndpoint: this.dashboardEndpoint,
...@@ -258,6 +258,7 @@ export default { ...@@ -258,6 +258,7 @@ export default {
currentDashboard: this.currentDashboard, currentDashboard: this.currentDashboard,
projectPath: this.projectPath, projectPath: this.projectPath,
logsPath: this.logsPath, logsPath: this.logsPath,
currentEnvironmentName: this.currentEnvironmentName,
}); });
}, },
mounted() { mounted() {
...@@ -273,7 +274,7 @@ export default { ...@@ -273,7 +274,7 @@ export default {
'setTimeRange', 'setTimeRange',
'fetchData', 'fetchData',
'setGettingStartedEmptyState', 'setGettingStartedEmptyState',
'setEndpoints', 'setInitialState',
'setPanelGroupMetrics', 'setPanelGroupMetrics',
'filterEnvironments', 'filterEnvironments',
]), ]),
......
...@@ -64,7 +64,10 @@ export default { ...@@ -64,7 +64,10 @@ export default {
}, },
}, },
mounted() { mounted() {
this.setInitialState(); this.setInitialState({
dashboardEndpoint: removeTimeRangeParams(this.dashboardUrl),
});
this.setShowErrorBanner(false);
this.setTimeRange(this.timeRange); this.setTimeRange(this.timeRange);
this.fetchDashboard(); this.fetchDashboard();
...@@ -90,11 +93,8 @@ export default { ...@@ -90,11 +93,8 @@ export default {
fetchDashboard(dispatch, payload) { fetchDashboard(dispatch, payload) {
return dispatch(`${this.namespace}/fetchDashboard`, payload); return dispatch(`${this.namespace}/fetchDashboard`, payload);
}, },
setEndpoints(dispatch, payload) { setInitialState(dispatch, payload) {
return dispatch(`${this.namespace}/setEndpoints`, payload); return dispatch(`${this.namespace}/setInitialState`, payload);
},
setFeatureFlags(dispatch, payload) {
return dispatch(`${this.namespace}/setFeatureFlags`, payload);
}, },
setShowErrorBanner(dispatch, payload) { setShowErrorBanner(dispatch, payload) {
return dispatch(`${this.namespace}/setShowErrorBanner`, payload); return dispatch(`${this.namespace}/setShowErrorBanner`, payload);
...@@ -108,12 +108,6 @@ export default { ...@@ -108,12 +108,6 @@ export default {
this.elWidth = this.$el.clientWidth; this.elWidth = this.$el.clientWidth;
}, sidebarAnimationDuration); }, sidebarAnimationDuration);
}, },
setInitialState() {
this.setEndpoints({
dashboardEndpoint: removeTimeRangeParams(this.dashboardUrl),
});
this.setShowErrorBanner(false);
},
}, },
}; };
</script> </script>
......
...@@ -79,3 +79,28 @@ export const dateFormats = { ...@@ -79,3 +79,28 @@ export const dateFormats = {
timeOfDay: 'h:MM TT', timeOfDay: 'h:MM TT',
default: 'dd mmm yyyy, h:MMTT', default: 'dd mmm yyyy, h:MMTT',
}; };
/**
* These Vuex store properties are allowed to be
* replaced dynamically after component has been created
* and initial state has been set.
*
* Currently used in `receiveMetricsDashboardSuccess` action.
*/
export const endpointKeys = [
'metricsEndpoint',
'deploymentsEndpoint',
'dashboardEndpoint',
'dashboardsEndpoint',
'currentDashboard',
'projectPath',
'logsPath',
];
/**
* These Vuex store properties are set as soon as the
* dashboard component has been created. The values are
* passed as data-* attributes and received by dashboard
* as Vue props.
*/
export const initialStateKeys = [...endpointKeys, 'currentEnvironmentName'];
...@@ -30,8 +30,8 @@ export const setGettingStartedEmptyState = ({ commit }) => { ...@@ -30,8 +30,8 @@ export const setGettingStartedEmptyState = ({ commit }) => {
commit(types.SET_GETTING_STARTED_EMPTY_STATE); commit(types.SET_GETTING_STARTED_EMPTY_STATE);
}; };
export const setEndpoints = ({ commit }, endpoints) => { export const setInitialState = ({ commit }, initialState) => {
commit(types.SET_ENDPOINTS, endpoints); commit(types.SET_INITIAL_STATE, initialState);
}; };
export const setTimeRange = ({ commit }, timeRange) => { export const setTimeRange = ({ commit }, timeRange) => {
......
...@@ -17,6 +17,7 @@ export const RECEIVE_METRIC_RESULT_FAILURE = 'RECEIVE_METRIC_RESULT_FAILURE'; ...@@ -17,6 +17,7 @@ export const RECEIVE_METRIC_RESULT_FAILURE = 'RECEIVE_METRIC_RESULT_FAILURE';
export const SET_TIME_RANGE = 'SET_TIME_RANGE'; export const SET_TIME_RANGE = 'SET_TIME_RANGE';
export const SET_ALL_DASHBOARDS = 'SET_ALL_DASHBOARDS'; export const SET_ALL_DASHBOARDS = 'SET_ALL_DASHBOARDS';
export const SET_ENDPOINTS = 'SET_ENDPOINTS'; export const SET_ENDPOINTS = 'SET_ENDPOINTS';
export const SET_INITIAL_STATE = 'SET_INITIAL_STATE';
export const SET_GETTING_STARTED_EMPTY_STATE = 'SET_GETTING_STARTED_EMPTY_STATE'; export const SET_GETTING_STARTED_EMPTY_STATE = 'SET_GETTING_STARTED_EMPTY_STATE';
export const SET_NO_DATA_EMPTY_STATE = 'SET_NO_DATA_EMPTY_STATE'; export const SET_NO_DATA_EMPTY_STATE = 'SET_NO_DATA_EMPTY_STATE';
export const SET_SHOW_ERROR_BANNER = 'SET_SHOW_ERROR_BANNER'; export const SET_SHOW_ERROR_BANNER = 'SET_SHOW_ERROR_BANNER';
......
...@@ -3,7 +3,7 @@ import pick from 'lodash/pick'; ...@@ -3,7 +3,7 @@ import pick from 'lodash/pick';
import * as types from './mutation_types'; import * as types from './mutation_types';
import { mapToDashboardViewModel, normalizeQueryResult } from './utils'; import { mapToDashboardViewModel, normalizeQueryResult } from './utils';
import { BACKOFF_TIMEOUT } from '../../lib/utils/common_utils'; import { BACKOFF_TIMEOUT } from '../../lib/utils/common_utils';
import { metricStates } from '../constants'; import { endpointKeys, initialStateKeys, metricStates } from '../constants';
import httpStatusCodes from '~/lib/utils/http_status'; import httpStatusCodes from '~/lib/utils/http_status';
/** /**
...@@ -150,19 +150,11 @@ export default { ...@@ -150,19 +150,11 @@ export default {
state: emptyStateFromError(error), state: emptyStateFromError(error),
}); });
}, },
[types.SET_INITIAL_STATE](state, initialState = {}) {
Object.assign(state, pick(initialState, initialStateKeys));
},
[types.SET_ENDPOINTS](state, endpoints = {}) { [types.SET_ENDPOINTS](state, endpoints = {}) {
const endpointKeys = [ Object.assign(state, pick(endpoints, endpointKeys));
'metricsEndpoint',
'deploymentsEndpoint',
'dashboardEndpoint',
'dashboardsEndpoint',
'currentDashboard',
'projectPath',
'logsPath',
];
Object.entries(pick(endpoints, endpointKeys)).forEach(([key, value]) => {
state[key] = value;
});
}, },
[types.SET_TIME_RANGE](state, timeRange) { [types.SET_TIME_RANGE](state, timeRange) {
state.timeRange = timeRange; state.timeRange = timeRange;
......
...@@ -88,11 +88,17 @@ describe('Dashboard', () => { ...@@ -88,11 +88,17 @@ describe('Dashboard', () => {
expect(findEnvironmentsDropdown().exists()).toBe(true); expect(findEnvironmentsDropdown().exists()).toBe(true);
}); });
it('sets endpoints: logs path', () => { it('sets initial state', () => {
expect(store.dispatch).toHaveBeenCalledWith( expect(store.dispatch).toHaveBeenCalledWith('monitoringDashboard/setInitialState', {
'monitoringDashboard/setEndpoints', currentDashboard: '',
expect.objectContaining({ logsPath: propsData.logsPath }), currentEnvironmentName: 'production',
); dashboardEndpoint: 'https://invalid',
dashboardsEndpoint: 'https://invalid',
deploymentsEndpoint: null,
logsPath: '/path/to/logs',
metricsEndpoint: 'http://test.host/monitoring/mock',
projectPath: '/path/to/project',
});
}); });
}); });
......
...@@ -26,9 +26,8 @@ describe('MetricEmbed', () => { ...@@ -26,9 +26,8 @@ describe('MetricEmbed', () => {
beforeEach(() => { beforeEach(() => {
actions = { actions = {
setFeatureFlags: jest.fn(), setInitialState: jest.fn(),
setShowErrorBanner: jest.fn(), setShowErrorBanner: jest.fn(),
setEndpoints: jest.fn(),
setTimeRange: jest.fn(), setTimeRange: jest.fn(),
fetchDashboard: jest.fn(), fetchDashboard: jest.fn(),
}; };
......
...@@ -16,7 +16,7 @@ import { ...@@ -16,7 +16,7 @@ import {
fetchEnvironmentsData, fetchEnvironmentsData,
fetchPrometheusMetrics, fetchPrometheusMetrics,
fetchPrometheusMetric, fetchPrometheusMetric,
setEndpoints, setInitialState,
filterEnvironments, filterEnvironments,
setGettingStartedEmptyState, setGettingStartedEmptyState,
duplicateSystemDashboard, duplicateSystemDashboard,
...@@ -208,14 +208,14 @@ describe('Monitoring store actions', () => { ...@@ -208,14 +208,14 @@ describe('Monitoring store actions', () => {
}); });
}); });
describe('Set endpoints', () => { describe('Set initial state', () => {
let mockedState; let mockedState;
beforeEach(() => { beforeEach(() => {
mockedState = storeState(); mockedState = storeState();
}); });
it('should commit SET_ENDPOINTS mutation', done => { it('should commit SET_INITIAL_STATE mutation', done => {
testAction( testAction(
setEndpoints, setInitialState,
{ {
metricsEndpoint: 'additional_metrics.json', metricsEndpoint: 'additional_metrics.json',
deploymentsEndpoint: 'deployments.json', deploymentsEndpoint: 'deployments.json',
...@@ -223,7 +223,7 @@ describe('Monitoring store actions', () => { ...@@ -223,7 +223,7 @@ describe('Monitoring store actions', () => {
mockedState, mockedState,
[ [
{ {
type: types.SET_ENDPOINTS, type: types.SET_INITIAL_STATE,
payload: { payload: {
metricsEndpoint: 'additional_metrics.json', metricsEndpoint: 'additional_metrics.json',
deploymentsEndpoint: 'deployments.json', deploymentsEndpoint: 'deployments.json',
......
...@@ -86,6 +86,58 @@ describe('Monitoring mutations', () => { ...@@ -86,6 +86,58 @@ describe('Monitoring mutations', () => {
expect(typeof stateCopy.deploymentData[0]).toEqual('object'); expect(typeof stateCopy.deploymentData[0]).toEqual('object');
}); });
}); });
describe('SET_INITIAL_STATE', () => {
it('should set all the endpoints', () => {
mutations[types.SET_INITIAL_STATE](stateCopy, {
metricsEndpoint: 'additional_metrics.json',
deploymentsEndpoint: 'deployments.json',
dashboardEndpoint: 'dashboard.json',
projectPath: '/gitlab-org/gitlab-foss',
currentEnvironmentName: 'production',
});
expect(stateCopy.metricsEndpoint).toEqual('additional_metrics.json');
expect(stateCopy.deploymentsEndpoint).toEqual('deployments.json');
expect(stateCopy.dashboardEndpoint).toEqual('dashboard.json');
expect(stateCopy.projectPath).toEqual('/gitlab-org/gitlab-foss');
expect(stateCopy.currentEnvironmentName).toEqual('production');
});
it('should not remove previously set properties', () => {
const defaultLogsPath = stateCopy.logsPath;
mutations[types.SET_INITIAL_STATE](stateCopy, {
logsPath: defaultLogsPath,
});
mutations[types.SET_INITIAL_STATE](stateCopy, {
dashboardEndpoint: 'dashboard.json',
});
mutations[types.SET_INITIAL_STATE](stateCopy, {
projectPath: '/gitlab-org/gitlab-foss',
});
mutations[types.SET_INITIAL_STATE](stateCopy, {
currentEnvironmentName: 'canary',
});
expect(stateCopy).toMatchObject({
logsPath: defaultLogsPath,
dashboardEndpoint: 'dashboard.json',
projectPath: '/gitlab-org/gitlab-foss',
currentEnvironmentName: 'canary',
});
});
it('should not update unknown properties', () => {
mutations[types.SET_INITIAL_STATE](stateCopy, {
dashboardEndpoint: 'dashboard.json',
someOtherProperty: 'some invalid value', // someOtherProperty is not allowed
});
expect(stateCopy.dashboardEndpoint).toBe('dashboard.json');
expect(stateCopy.someOtherProperty).toBeUndefined();
});
});
describe('SET_ENDPOINTS', () => { describe('SET_ENDPOINTS', () => {
it('should set all the endpoints', () => { it('should set all the endpoints', () => {
mutations[types.SET_ENDPOINTS](stateCopy, { mutations[types.SET_ENDPOINTS](stateCopy, {
......
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