From: Grégoire Aubert Date: Tue, 25 Jul 2017 13:19:38 +0000 (+0200) Subject: Add metrics redux store X-Git-Tag: 6.6-RC1~658 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a202c2a3e4b38e5d8aad0533324d6291c9b4c121;p=sonarqube.git Add metrics redux store --- diff --git a/server/sonar-web/src/main/js/store/metrics/actions.js b/server/sonar-web/src/main/js/store/metrics/actions.js new file mode 100644 index 00000000000..d76e54233fb --- /dev/null +++ b/server/sonar-web/src/main/js/store/metrics/actions.js @@ -0,0 +1,42 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +// @flow + +export type Metric = { + custom?: boolean, + decimalScale?: number, + description?: string, + direction?: number, + domain?: string, + hidden?: boolean, + key: string, + name: string, + qualitative?: boolean, + type: string +}; + +export type Metrics = Array; + +export const RECEIVE_METRICS = 'RECEIVE_METRICS'; + +export const receiveMetrics = (metrics: Metrics) => ({ + type: RECEIVE_METRICS, + metrics +}); diff --git a/server/sonar-web/src/main/js/store/metrics/reducer.js b/server/sonar-web/src/main/js/store/metrics/reducer.js new file mode 100644 index 00000000000..0c0950c6864 --- /dev/null +++ b/server/sonar-web/src/main/js/store/metrics/reducer.js @@ -0,0 +1,49 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +// @flow +import { combineReducers } from 'redux'; +import { keyBy } from 'lodash'; +import { RECEIVE_METRICS } from './actions'; +import type { Metric } from './actions'; + +type StateByKey = { [string]: Metric }; +type StateKeys = Array; +type State = { byKey: StateByKey, keys: StateKeys }; + +const byKey = (state: StateByKey = {}, action = {}) => { + if (action.type === RECEIVE_METRICS) { + return keyBy(action.metrics, 'key'); + } + return state; +}; + +const keys = (state: StateKeys = [], action = {}) => { + if (action.type === RECEIVE_METRICS) { + return action.metrics.map(f => f.key); + } + + return state; +}; + +export default combineReducers({ byKey, keys }); + +export const getMetrics = (state: State) => state.keys; + +export const getMetricByKey = (state: State, key: string) => state.byKey[key]; diff --git a/server/sonar-web/src/main/js/store/rootActions.js b/server/sonar-web/src/main/js/store/rootActions.js index a3d3902cf1e..b4558241c65 100644 --- a/server/sonar-web/src/main/js/store/rootActions.js +++ b/server/sonar-web/src/main/js/store/rootActions.js @@ -22,8 +22,10 @@ import { getGlobalNavigation, getComponentNavigation } from '../api/nav'; import { getComponentData } from '../api/components'; import * as auth from '../api/auth'; import { getOrganizations } from '../api/organizations'; +import { getMetrics } from '../api/metrics'; import { receiveLanguages } from './languages/actions'; import { receiveComponents } from './components/actions'; +import { receiveMetrics } from './metrics/actions'; import { addGlobalErrorMessage } from './globalMessages/duck'; import { parseError } from '../apps/code/utils'; import { setAppState } from './appState/duck'; @@ -39,6 +41,10 @@ export const fetchLanguages = () => dispatch => { return getLanguages().then(languages => dispatch(receiveLanguages(languages)), onFail(dispatch)); }; +export const fetchMetrics = () => dispatch => { + return getMetrics().then(metrics => dispatch(receiveMetrics(metrics)), onFail(dispatch)); +}; + export const fetchOrganizations = (organizations?: Array) => dispatch => getOrganizations(organizations).then( r => dispatch(receiveOrganizations(r.organizations)), diff --git a/server/sonar-web/src/main/js/store/rootReducer.js b/server/sonar-web/src/main/js/store/rootReducer.js index 76355147ac9..e38f544dabf 100644 --- a/server/sonar-web/src/main/js/store/rootReducer.js +++ b/server/sonar-web/src/main/js/store/rootReducer.js @@ -24,6 +24,7 @@ import users, * as fromUsers from './users/reducer'; import favorites, * as fromFavorites from './favorites/duck'; import languages, * as fromLanguages from './languages/reducer'; import measures, * as fromMeasures from './measures/reducer'; +import metrics, * as fromMetrics from './metrics/reducer'; import notifications, * as fromNotifications from './notifications/duck'; import organizations, * as fromOrganizations from './organizations/duck'; import organizationsMembers, * as fromOrganizationsMembers from './organizationsMembers/reducer'; @@ -42,6 +43,7 @@ export default combineReducers({ favorites, languages, measures, + metrics, notifications, organizations, organizationsMembers, @@ -87,6 +89,10 @@ export const getComponentMeasure = (state, componentKey, metricKey) => export const getComponentMeasures = (state, componentKey) => fromMeasures.getComponentMeasures(state.measures, componentKey); +export const getMetrics = state => fromMetrics.getMetrics(state.metrics); + +export const getMetricByKey = (state, key) => fromMetrics.getMetricByKey(state.metrics, key); + export const getGlobalNotifications = state => fromNotifications.getGlobal(state.notifications); export const getProjectsWithNotifications = state =>