From 0c7f9f2bae260585a2bbf8c149f771bdfa7ecd90 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9goire=20Aubert?= Date: Tue, 18 Jul 2017 11:01:43 +0200 Subject: [PATCH] SONAR-9403 Display project activity custom graphs on the project dashboard too --- .../apps/overview/components/OverviewApp.js | 9 ++--- .../js/apps/overview/events/PreviewGraph.js | 36 ++++++++++++------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/overview/components/OverviewApp.js b/server/sonar-web/src/main/js/apps/overview/components/OverviewApp.js index ca6094836ef..8ca0ce8f45c 100644 --- a/server/sonar-web/src/main/js/apps/overview/components/OverviewApp.js +++ b/server/sonar-web/src/main/js/apps/overview/components/OverviewApp.js @@ -33,9 +33,9 @@ import { getAllTimeMachineData } from '../../../api/time-machine'; import { enhanceMeasuresWithMetrics } from '../../../helpers/measures'; import { getLeakPeriod } from '../../../helpers/periods'; import { TooltipsContainer } from '../../../components/mixins/tooltips-mixin'; -import { getGraph } from '../../../helpers/storage'; +import { getCustomGraph, getGraph } from '../../../helpers/storage'; import { METRICS, HISTORY_METRICS_LIST } from '../utils'; -import { GRAPHS_METRICS_DISPLAYED } from '../../projectActivity/utils'; +import { getDisplayedHistoryMetrics } from '../../projectActivity/utils'; import type { Component, History, MeasuresList, Period } from '../types'; import '../styles.css'; @@ -101,10 +101,11 @@ export default class OverviewApp extends React.PureComponent { } loadHistory(component: Component) { - let graphMetrics = GRAPHS_METRICS_DISPLAYED[getGraph()]; + let graphMetrics = getDisplayedHistoryMetrics(getGraph(), getCustomGraph()); if (!graphMetrics || graphMetrics.length <= 0) { - graphMetrics = GRAPHS_METRICS_DISPLAYED['overview']; + graphMetrics = getDisplayedHistoryMetrics('overview', []); } + const metrics = uniq(HISTORY_METRICS_LIST.concat(graphMetrics)); return getAllTimeMachineData(component.key, metrics).then(r => { if (this.mounted) { diff --git a/server/sonar-web/src/main/js/apps/overview/events/PreviewGraph.js b/server/sonar-web/src/main/js/apps/overview/events/PreviewGraph.js index 171c6d87975..73eba033873 100644 --- a/server/sonar-web/src/main/js/apps/overview/events/PreviewGraph.js +++ b/server/sonar-web/src/main/js/apps/overview/events/PreviewGraph.js @@ -21,10 +21,10 @@ import React from 'react'; import { minBy } from 'lodash'; import { AutoSizer } from 'react-virtualized'; -import { generateSeries, GRAPHS_METRICS_DISPLAYED } from '../../projectActivity/utils'; -import { getGraph } from '../../../helpers/storage'; import AdvancedTimeline from '../../../components/charts/AdvancedTimeline'; import PreviewGraphTooltips from './PreviewGraphTooltips'; +import { generateSeries, getDisplayedHistoryMetrics } from '../../projectActivity/utils'; +import { getCustomGraph, getGraph } from '../../../helpers/storage'; import { formatMeasure, getShortType } from '../../../helpers/measures'; import type { Serie } from '../../../components/charts/AdvancedTimeline'; import type { History, Metric } from '../types'; @@ -37,6 +37,7 @@ type Props = { }; type State = { + customMetrics: Array, graph: string, metricsType: string, selectedDate: ?Date, @@ -54,12 +55,14 @@ export default class PreviewGraph extends React.PureComponent { constructor(props: Props) { super(props); const graph = getGraph(); - const metricsType = this.getMetricType(props.metrics, graph); + const customMetrics = getCustomGraph(); + const metricsType = this.getMetricType(props.metrics, graph, customMetrics); this.state = { + customMetrics, graph, metricsType, selectedDate: null, - series: this.getSeries(props.history, graph, metricsType), + series: this.getSeries(props.history, graph, customMetrics, metricsType), tooltipIdx: null, tooltipXPos: null }; @@ -68,11 +71,13 @@ export default class PreviewGraph extends React.PureComponent { componentWillReceiveProps(nextProps: Props) { if (nextProps.history !== this.props.history || nextProps.metrics !== this.props.metrics) { const graph = getGraph(); - const metricsType = this.getMetricType(nextProps.metrics, graph); + const customMetrics = getCustomGraph(); + const metricsType = this.getMetricType(nextProps.metrics, graph, customMetrics); this.setState({ + customMetrics, graph, metricsType, - series: this.getSeries(nextProps.history, graph, metricsType) + series: this.getSeries(nextProps.history, graph, customMetrics, metricsType) }); } } @@ -80,20 +85,25 @@ export default class PreviewGraph extends React.PureComponent { formatValue = (tick: number | string) => formatMeasure(tick, getShortType(this.state.metricsType)); - getDisplayedMetrics = (graph: string): Array => { - const metrics: Array = GRAPHS_METRICS_DISPLAYED[graph]; + getDisplayedMetrics = (graph: string, customMetrics: Array): Array => { + const metrics: Array = getDisplayedHistoryMetrics(graph, customMetrics); if (!metrics || metrics.length <= 0) { - return GRAPHS_METRICS_DISPLAYED['overview']; + return getDisplayedHistoryMetrics('overview', customMetrics); } return metrics; }; - getSeries = (history: ?History, graph: string, metricsType: string) => { + getSeries = ( + history: ?History, + graph: string, + customMetrics: Array, + metricsType: string + ) => { const myHistory = history; if (!myHistory) { return []; } - const metrics = this.getDisplayedMetrics(graph); + const metrics = this.getDisplayedMetrics(graph, customMetrics); const firstValid = minBy( metrics.map(metric => myHistory[metric].find(p => p.value || p.value === 0)), 'date' @@ -107,8 +117,8 @@ export default class PreviewGraph extends React.PureComponent { return generateSeries(measureHistory, graph, metricsType, metrics); }; - getMetricType = (metrics: Array, graph: string) => { - const metricKey = this.getDisplayedMetrics(graph)[0]; + getMetricType = (metrics: Array, graph: string, customMetrics: Array) => { + const metricKey = this.getDisplayedMetrics(graph, customMetrics)[0]; const metric = metrics.find(metric => metric.key === metricKey); return metric ? metric.type : 'INT'; }; -- 2.39.5