]> source.dussan.org Git - sonarqube.git/commitdiff
Add metrics redux store
authorGrégoire Aubert <gregoire.aubert@sonarsource.com>
Tue, 25 Jul 2017 13:19:38 +0000 (15:19 +0200)
committerGrégoire Aubert <gregoire.aubert@sonarsource.com>
Mon, 14 Aug 2017 09:44:44 +0000 (11:44 +0200)
server/sonar-web/src/main/js/store/metrics/actions.js [new file with mode: 0644]
server/sonar-web/src/main/js/store/metrics/reducer.js [new file with mode: 0644]
server/sonar-web/src/main/js/store/rootActions.js
server/sonar-web/src/main/js/store/rootReducer.js

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 (file)
index 0000000..d76e542
--- /dev/null
@@ -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<Metric>;
+
+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 (file)
index 0000000..0c0950c
--- /dev/null
@@ -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<string>;
+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];
index a3d3902cf1e5de1a44b0314a4c9aa85f93e5cf3e..b4558241c65fcc59465ac5e545d24baa5976ba6e 100644 (file)
@@ -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<string>) => dispatch =>
   getOrganizations(organizations).then(
     r => dispatch(receiveOrganizations(r.organizations)),
index 76355147ac91e533b54b1130cf0a9770e1ca4a2e..e38f544dabff2394fe58882359944559846a6ea9 100644 (file)
@@ -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 =>