From 9e9b54fbe1e332e8aa7a13d2ce670fee2fde41f0 Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Wed, 20 Jan 2016 14:39:06 +0100 Subject: [PATCH] use new WS on code, projects and overview pages --- .../sonar-web/src/main/js/api/components.js | 32 ++++++++-------- .../src/main/js/apps/code/actions/index.js | 8 +++- .../main/js/apps/code/components/Component.js | 16 ++++---- .../apps/code/components/ComponentDetach.js | 2 +- .../apps/code/components/ComponentMeasure.js | 4 +- .../js/apps/code/components/ComponentName.js | 2 +- .../js/apps/code/components/ComponentPin.js | 2 +- .../code/components/ComponentQualityGate.js | 6 +-- .../js/apps/code/components/SourceViewer.js | 4 +- .../src/main/js/apps/code/reducers/index.js | 6 +-- .../components/domain-bubble-chart.js | 38 ++++++++++--------- .../overview/components/domain-treemap.js | 6 +-- .../overview/components/ncloc-distribution.js | 6 +-- .../tests/apps/code/components-test.js | 4 +- .../sonar-web/tests/apps/code/store-test.js | 18 ++++----- 15 files changed, 84 insertions(+), 70 deletions(-) diff --git a/server/sonar-web/src/main/js/api/components.js b/server/sonar-web/src/main/js/api/components.js index 3db794b855e..89a6e35ffec 100644 --- a/server/sonar-web/src/main/js/api/components.js +++ b/server/sonar-web/src/main/js/api/components.js @@ -46,27 +46,29 @@ export function createProject (data) { } export function getChildren (componentKey, metrics = []) { - const url = baseUrl + '/api/resources/index'; - const data = { resource: componentKey, metrics: metrics.join(','), depth: 1 }; - return getJSON(url, data); + const url = baseUrl + '/api/measures/component_tree'; + const data = { + baseComponentKey: componentKey, + metricKeys: metrics.join(','), + strategy: 'children' + }; + return getJSON(url, data).then(r => r.components); } -export function getFiles (componentKey, metrics = []) { - // due to the limitation of the WS we can not ask qualifiers=FIL, - // in this case the WS does not return measures - // so the filtering by a qualifier is done manually - - const url = baseUrl + '/api/resources/index'; - const data = { resource: componentKey, metrics: metrics.join(','), depth: -1 }; - return getJSON(url, data).then(r => { - return r.filter(component => component.qualifier === 'FIL'); +export function getFiles (componentKey, metrics = [], additional = {}) { + const url = baseUrl + '/api/measures/component_tree'; + const data = Object.assign({}, additional, { + baseComponentKey: componentKey, + metricKeys: metrics.join(','), + strategy: 'leaves' }); + return getJSON(url, data).then(r => r.components); } export function getComponent (componentKey, metrics = []) { - const url = baseUrl + '/api/resources/index'; - const data = { resource: componentKey, metrics: metrics.join(',') }; - return getJSON(url, data).then(r => r[0]); + const url = baseUrl + '/api/measures/component'; + const data = { componentKey, metricKeys: metrics.join(',') }; + return getJSON(url, data).then(r => r.component); } export function getTree (baseComponentKey, options = {}) { diff --git a/server/sonar-web/src/main/js/apps/code/actions/index.js b/server/sonar-web/src/main/js/apps/code/actions/index.js index fcf52337263..e39f46d0327 100644 --- a/server/sonar-web/src/main/js/apps/code/actions/index.js +++ b/server/sonar-web/src/main/js/apps/code/actions/index.js @@ -22,6 +22,7 @@ import { pushPath } from 'redux-simple-router'; import { getChildren, getComponent, getTree, getBreadcrumbs } from '../../../api/components'; import { translate } from '../../../helpers/l10n'; +import { getComponentUrl } from '../../../helpers/urls'; const METRICS = [ @@ -192,7 +193,12 @@ export function browse (componentKey) { dispatch(startFetching()); return retrieveComponent(componentKey, bucket) .then(([component, children, breadcrumbs]) => { - dispatch(browseAction(component, children, breadcrumbs)); + if (component.refKey) { + window.location = getComponentUrl(component.refKey); + return new Promise(); + } else { + dispatch(browseAction(component, children, breadcrumbs)); + } }) .then(() => dispatch(pushPath(getPath(componentKey)))) .then(() => dispatch(stopFetching())) diff --git a/server/sonar-web/src/main/js/apps/code/components/Component.js b/server/sonar-web/src/main/js/apps/code/components/Component.js index ea6e17a23f5..ef1b1e561bb 100644 --- a/server/sonar-web/src/main/js/apps/code/components/Component.js +++ b/server/sonar-web/src/main/js/apps/code/components/Component.js @@ -67,13 +67,15 @@ class Component extends React.Component { let componentAction = null; - switch (component.qualifier) { - case 'FIL': - case 'UTS': - componentAction = ; - break; - default: - componentAction = ; + if (!component.refKey) { + switch (component.qualifier) { + case 'FIL': + case 'UTS': + componentAction = ; + break; + default: + componentAction = ; + } } return ( diff --git a/server/sonar-web/src/main/js/apps/code/components/ComponentDetach.js b/server/sonar-web/src/main/js/apps/code/components/ComponentDetach.js index 9666577ed34..c2a18b6defd 100644 --- a/server/sonar-web/src/main/js/apps/code/components/ComponentDetach.js +++ b/server/sonar-web/src/main/js/apps/code/components/ComponentDetach.js @@ -27,7 +27,7 @@ const ComponentDetach = ({ component }) => ( + href={getComponentUrl(component.refKey || component.key)}/> ); diff --git a/server/sonar-web/src/main/js/apps/code/components/ComponentMeasure.js b/server/sonar-web/src/main/js/apps/code/components/ComponentMeasure.js index df7e1812d32..7845e5d1178 100644 --- a/server/sonar-web/src/main/js/apps/code/components/ComponentMeasure.js +++ b/server/sonar-web/src/main/js/apps/code/components/ComponentMeasure.js @@ -24,10 +24,10 @@ import { formatMeasure } from '../../../helpers/measures'; const ComponentMeasure = ({ component, metricKey, metricType }) => { - const measure = _.findWhere(component.msr, { key: metricKey }); + const measure = _.findWhere(component.measures, { metric: metricKey }); return ( - {measure ? formatMeasure(measure.val, metricType) : ''} + {measure ? formatMeasure(measure.value, metricType) : ''} ); }; diff --git a/server/sonar-web/src/main/js/apps/code/components/ComponentName.js b/server/sonar-web/src/main/js/apps/code/components/ComponentName.js index 77405448839..dde1849ce4b 100644 --- a/server/sonar-web/src/main/js/apps/code/components/ComponentName.js +++ b/server/sonar-web/src/main/js/apps/code/components/ComponentName.js @@ -62,7 +62,7 @@ const Component = ({ component, previous, onBrowse }) => { {component.name.substr(prefix.length)} ) : component.name; - const canBrowse = !!onBrowse && !component.copy; + const canBrowse = !!onBrowse; return ( diff --git a/server/sonar-web/src/main/js/apps/code/components/ComponentPin.js b/server/sonar-web/src/main/js/apps/code/components/ComponentPin.js index d51a1dd0a18..9e3484cdbdc 100644 --- a/server/sonar-web/src/main/js/apps/code/components/ComponentPin.js +++ b/server/sonar-web/src/main/js/apps/code/components/ComponentPin.js @@ -27,7 +27,7 @@ import { translate } from '../../../helpers/l10n'; const ComponentPin = ({ component }) => { const handleClick = (e) => { e.preventDefault(); - Workspace.openComponent({ uuid: component.uuid || component.id }); + Workspace.openComponent({ uuid: component.id }); }; return ( diff --git a/server/sonar-web/src/main/js/apps/code/components/ComponentQualityGate.js b/server/sonar-web/src/main/js/apps/code/components/ComponentQualityGate.js index 040e243e74f..9fb381e6833 100644 --- a/server/sonar-web/src/main/js/apps/code/components/ComponentQualityGate.js +++ b/server/sonar-web/src/main/js/apps/code/components/ComponentQualityGate.js @@ -25,13 +25,13 @@ import { translate } from '../../../helpers/l10n'; const METRIC = 'alert_status'; const ComponentQualityGate = ({ component }) => { - const measure = _.findWhere(component.msr, { key: METRIC }); + const measure = _.findWhere(component.measures, { metric: METRIC }); return measure ? ( - + ) : ; }; diff --git a/server/sonar-web/src/main/js/apps/code/components/SourceViewer.js b/server/sonar-web/src/main/js/apps/code/components/SourceViewer.js index 8645c362148..ddedbf2617d 100644 --- a/server/sonar-web/src/main/js/apps/code/components/SourceViewer.js +++ b/server/sonar-web/src/main/js/apps/code/components/SourceViewer.js @@ -28,7 +28,7 @@ export default class SourceViewer extends Component { } shouldComponentUpdate (nextProps) { - return nextProps.component.uuid !== this.props.component.uuid; + return nextProps.component.id !== this.props.component.id; } componentWillUpdate () { @@ -46,7 +46,7 @@ export default class SourceViewer extends Component { renderSourceViewer () { this.sourceViewer = new BaseSourceViewer(); this.sourceViewer.render().$el.appendTo(this.refs.container); - this.sourceViewer.open(this.props.component.uuid); + this.sourceViewer.open(this.props.component.id); } destroySourceViewer () { diff --git a/server/sonar-web/src/main/js/apps/code/reducers/index.js b/server/sonar-web/src/main/js/apps/code/reducers/index.js index 728782c97e7..eb7c09c8301 100644 --- a/server/sonar-web/src/main/js/apps/code/reducers/index.js +++ b/server/sonar-web/src/main/js/apps/code/reducers/index.js @@ -28,9 +28,9 @@ function hasSourceCode (component) { } function selectCoverageMetric (component) { - const coverage = _.findWhere(component.msr, { key: 'coverage' }); - const itCoverage = _.findWhere(component.msr, { key: 'it_coverage' }); - const overallCoverage = _.findWhere(component.msr, { key: 'overall_coverage' }); + const coverage = _.findWhere(component.measures, { metric: 'coverage' }); + const itCoverage = _.findWhere(component.measures, { metric: 'it_coverage' }); + const overallCoverage = _.findWhere(component.measures, { metric: 'overall_coverage' }); if (coverage != null && itCoverage != null && overallCoverage != null) { return 'overall_coverage'; diff --git a/server/sonar-web/src/main/js/apps/overview/components/domain-bubble-chart.js b/server/sonar-web/src/main/js/apps/overview/components/domain-bubble-chart.js index e7ae61e7ca8..6554987adf6 100644 --- a/server/sonar-web/src/main/js/apps/overview/components/domain-bubble-chart.js +++ b/server/sonar-web/src/main/js/apps/overview/components/domain-bubble-chart.js @@ -32,7 +32,7 @@ const BUBBLES_LIMIT = 500; function getMeasure (component, metric) { - return component.measures[metric] || 0; + return Number(component.measures[metric]) || 0; } @@ -53,28 +53,29 @@ export class DomainBubbleChart extends React.Component { } requestFiles () { - let metrics = [].concat(this.props.xMetric, this.props.yMetric, this.props.sizeMetrics); - return getFiles(this.props.component.key, metrics).then(r => { + const metrics = [].concat(this.props.xMetric, this.props.yMetric, this.props.sizeMetrics); + const options = { + s: 'metric', + metricSort: this.props.sizeMetrics, + asc: false, + ps: BUBBLES_LIMIT + }; + return getFiles(this.props.component.key, metrics, options).then(r => { let files = r.map(file => { let measures = {}; - (file.msr || []).forEach(measure => { - measures[measure.key] = measure.val; + (file.measures || []).forEach(measure => { + measures[measure.metric] = measure.value; }); return _.extend(file, { measures }); }); this.setState({ loading: false, - files: this.limitFiles(files), + files: files, total: files.length }); }); } - limitFiles (files) { - const comparator = file => -1 * this.getSizeMetricsValue(file); - return _.sortBy(files, comparator).slice(0, BUBBLES_LIMIT); - } - getMetricObject (metrics, metricKey) { return _.findWhere(metrics, { key: metricKey }); } @@ -96,15 +97,17 @@ export class DomainBubbleChart extends React.Component { /* eslint max-len: 0 */ let inner = [ component.name, - `${this.state.xMetric.name}: ${formatMeasure(getMeasure(component, this.props.xMetric), this.state.xMetric.type)}`, - `${this.state.yMetric.name}: ${formatMeasure(getMeasure(component, this.props.yMetric), this.state.yMetric.type)}`, + `${this.state.xMetric.name}: ${formatMeasure(getMeasure(component, this.props.xMetric), + this.state.xMetric.type)}`, + `${this.state.yMetric.name}: ${formatMeasure(getMeasure(component, this.props.yMetric), + this.state.yMetric.type)}`, `${sizeMetricsTitle}: ${formatMeasure(this.getSizeMetricsValue(component), sizeMetricsType)}` ].join('
'); return `
${inner}
`; } - handleBubbleClick (uuid) { - Workspace.openComponent({ uuid }); + handleBubbleClick (id) { + Workspace.openComponent({ uuid: id }); } renderLoading () { @@ -123,7 +126,7 @@ export class DomainBubbleChart extends React.Component { x: getMeasure(component, this.props.xMetric), y: getMeasure(component, this.props.yMetric), size: this.getSizeMetricsValue(component), - link: component.uuid, + link: component.id, tooltip: this.getTooltip(component) }; }); @@ -160,7 +163,8 @@ export class DomainBubbleChart extends React.Component { {this.state.xMetric.name} {this.state.total > BUBBLES_LIMIT && -
{translateWithParameters('overview.chart.files.limit_message', BUBBLES_LIMIT)}
} +
{translateWithParameters('overview.chart.files.limit_message', + BUBBLES_LIMIT)}
} ; } diff --git a/server/sonar-web/src/main/js/apps/overview/components/domain-treemap.js b/server/sonar-web/src/main/js/apps/overview/components/domain-treemap.js index 4ea27a3523a..3ea605187df 100644 --- a/server/sonar-web/src/main/js/apps/overview/components/domain-treemap.js +++ b/server/sonar-web/src/main/js/apps/overview/components/domain-treemap.js @@ -51,12 +51,12 @@ export class DomainTreemap extends React.Component { return getChildren(componentKey, metrics).then(r => { let components = r.map(component => { let measures = {}; - (component.msr || []).forEach(measure => { - measures[measure.key] = measure.val; + (component.measures || []).forEach(measure => { + measures[measure.metric] = measure.value; }); return _.extend(component, { measures, - key: component.copy ? `${component.copy}` : component.key + key: component.refKey || component.key }); }); this.setState({ loading: false, components }); diff --git a/server/sonar-web/src/main/js/apps/overview/components/ncloc-distribution.js b/server/sonar-web/src/main/js/apps/overview/components/ncloc-distribution.js index d0c6e5f793e..dfe8cff2dbc 100644 --- a/server/sonar-web/src/main/js/apps/overview/components/ncloc-distribution.js +++ b/server/sonar-web/src/main/js/apps/overview/components/ncloc-distribution.js @@ -50,8 +50,8 @@ export const NclocDistribution = React.createClass({ return getChildren(this.props.component.key, metrics).then(r => { let components = r.map(component => { let measures = {}; - (component.msr || []).forEach(measure => { - measures[measure.key] = measure.val; + (component.measures || []).forEach(measure => { + measures[measure.metric] = measure.value; }); return _.extend(component, { measures }); }); @@ -76,7 +76,7 @@ export const NclocDistribution = React.createClass({ let data = this.state.components.map((component, index) => { return { - x: parseInt(component.measures[METRIC], 10), + x: component.measures[METRIC] ? parseInt(component.measures[METRIC], 10) : 0, y: index, value: component.name, component: component diff --git a/server/sonar-web/tests/apps/code/components-test.js b/server/sonar-web/tests/apps/code/components-test.js index a31591cf24e..9613032c75f 100644 --- a/server/sonar-web/tests/apps/code/components-test.js +++ b/server/sonar-web/tests/apps/code/components-test.js @@ -21,14 +21,14 @@ chai.use(sinonChai); const measures = [ - { key: 'ncloc', val: 9757 } + { metric: 'ncloc', value: 9757 } ]; const exampleComponent = { uuid: 'A1', key: 'A', name: 'AA', qualifier: 'TRK', - msr: measures + measures: measures }; const exampleComponent2 = { uuid: 'B2', key: 'B' }; const exampleComponent3 = { uuid: 'C3', key: 'C' }; diff --git a/server/sonar-web/tests/apps/code/store-test.js b/server/sonar-web/tests/apps/code/store-test.js index 613eee6517e..de5ddbfadf1 100644 --- a/server/sonar-web/tests/apps/code/store-test.js +++ b/server/sonar-web/tests/apps/code/store-test.js @@ -143,8 +143,8 @@ describe('Code :: Store', () => { it('should be set to "coverage"', () => { const componentWithCoverage = { ...exampleComponent, - msr: [ - { key: 'coverage', val: 13 } + measures: [ + { metric: 'coverage', value: 13 } ] }; @@ -155,8 +155,8 @@ describe('Code :: Store', () => { it('should be set to "it_coverage"', () => { const componentWithCoverage = { ...exampleComponent, - msr: [ - { key: 'it_coverage', val: 13 } + measures: [ + { metric: 'it_coverage', value: 13 } ] }; @@ -167,10 +167,10 @@ describe('Code :: Store', () => { it('should be set to "overall_coverage"', () => { const componentWithCoverage = { ...exampleComponent, - msr: [ - { key: 'coverage', val: 11 }, - { key: 'it_coverage', val: 12 }, - { key: 'overall_coverage', val: 13 } + measures: [ + { metric: 'coverage', value: 11 }, + { metric: 'it_coverage', value: 12 }, + { metric: 'overall_coverage', value: 13 } ] }; @@ -181,7 +181,7 @@ describe('Code :: Store', () => { it('should fallback to "it_coverage"', () => { const componentWithCoverage = { ...exampleComponent, - msr: [] + measures: [] }; expect(current(initialState, initComponentAction(componentWithCoverage)).coverageMetric) -- 2.39.5