From f19752352309f50f99510a5d44c2c6f3d2f63ec9 Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Wed, 18 May 2016 16:18:13 +0200 Subject: [PATCH] SONAR-7223 "Best Values" are not displayed properly (#994) --- .../source-viewer/measures-overlay.js | 125 +++++++++--------- .../src/main/js/widgets/old/treemap.js | 69 +++++----- 2 files changed, 99 insertions(+), 95 deletions(-) diff --git a/server/sonar-web/src/main/js/components/source-viewer/measures-overlay.js b/server/sonar-web/src/main/js/components/source-viewer/measures-overlay.js index 2fbcd936ab1..1442ac49793 100644 --- a/server/sonar-web/src/main/js/components/source-viewer/measures-overlay.js +++ b/server/sonar-web/src/main/js/components/source-viewer/measures-overlay.js @@ -21,21 +21,21 @@ import $ from 'jquery'; import _ from 'underscore'; import ModalView from '../common/modals'; import Template from './templates/source-viewer-measures.hbs'; +import { getMeasures } from '../../api/measures'; +import { getMetrics } from '../../api/metrics'; +import { formatMeasure } from '../../helpers/measures'; export default ModalView.extend({ template: Template, testsOrder: ['ERROR', 'FAILURE', 'OK', 'SKIPPED'], initialize () { - const that = this; + this.testsScroll = 0; const requests = [this.requestMeasures(), this.requestIssues()]; if (this.model.get('isUnitTest')) { requests.push(this.requestTests()); } - this.testsScroll = 0; - $.when.apply($, requests).done(function () { - that.render(); - }); + Promise.all(requests).then(() => this.render()); }, events () { @@ -121,9 +121,9 @@ export default ModalView.extend({ calcAdditionalMeasures (measures) { measures.issuesRemediationEffort = - (measures.sqale_index_raw || 0) + - (measures.reliability_remediation_effort_raw || 0) + - (measures.security_remediation_effort_raw || 0); + (Number(measures.sqale_index_raw) || 0) + + (Number(measures.reliability_remediation_effort_raw) || 0) + + (Number(measures.security_remediation_effort_raw) || 0); if (measures.lines_to_cover && measures.uncovered_lines) { measures.covered_lines = measures.lines_to_cover_raw - measures.uncovered_lines_raw; @@ -156,70 +156,77 @@ export default ModalView.extend({ }, requestMeasures () { - const that = this; - const url = window.baseUrl + '/api/resources'; - const metrics = this.getMetrics(); - const options = { - resource: this.model.key(), - metrics: _.pluck(metrics, 'key').join() - }; - return $.get(url, options).done(function (data) { - const measuresList = data[0].msr || []; - let measures = that.model.get('measures') || {}; - measuresList.forEach(function (m) { - const metric = _.findWhere(metrics, { key: m.key }); - metric.value = m.frmt_val || m.data; - measures[m.key] = m.frmt_val || m.data; - measures[m.key + '_raw'] = m.val; - }); - measures = that.calcAdditionalMeasures(measures); - that.model.set({ - measures, - measuresToDisplay: that.prepareMetrics(metrics) + return getMetrics().then(metrics => { + const metricsToRequest = metrics + .filter(metric => metric.type !== 'DATA' && !metric.hidden) + .map(metric => metric.key); + + return getMeasures(this.model.key(), metricsToRequest).then(measures => { + let nextMeasures = this.model.get('measures') || {}; + measures.forEach(measure => { + const metric = metrics.find(metric => metric.key === measure.metric); + nextMeasures[metric.key] = formatMeasure(measure.value, metric.type); + nextMeasures[metric.key + '_raw'] = measure.value; + metric.value = nextMeasures[metric.key]; + }); + nextMeasures = this.calcAdditionalMeasures(nextMeasures); + this.model.set({ + measures: nextMeasures, + measuresToDisplay: this.prepareMetrics(metrics) + }); }); }); }, requestIssues () { - const that = this; - const url = window.baseUrl + '/api/issues/search'; - const options = { - componentUuids: this.model.id, - resolved: false, - ps: 1, - facets: 'types,severities,tags' - }; - return $.get(url, options).done(function (data) { - const typesFacet = data.facets.find(facet => facet.property === 'types').values; - const typesOrder = ['BUG', 'VULNERABILITY', 'CODE_SMELL']; - const sortedTypesFacet = _.sortBy(typesFacet, function (v) { - return typesOrder.indexOf(v.val); - }); + return new Promise(resolve => { + const that = this; + const url = window.baseUrl + '/api/issues/search'; + const options = { + componentUuids: this.model.id, + resolved: false, + ps: 1, + facets: 'types,severities,tags' + }; - const severitiesFacet = data.facets.find(facet => facet.property === 'severities').values; - const sortedSeveritiesFacet = _.sortBy(severitiesFacet, facet => window.severityComparator(facet.val)); + $.get(url, options).done(function (data) { + const typesFacet = data.facets.find(facet => facet.property === 'types').values; + const typesOrder = ['BUG', 'VULNERABILITY', 'CODE_SMELL']; + const sortedTypesFacet = _.sortBy(typesFacet, function (v) { + return typesOrder.indexOf(v.val); + }); - const tagsFacet = data.facets.find(facet => facet.property === 'tags').values; + const severitiesFacet = data.facets.find(facet => facet.property === 'severities').values; + const sortedSeveritiesFacet = _.sortBy(severitiesFacet, facet => window.severityComparator(facet.val)); - that.model.set({ - tagsFacet, - typesFacet: sortedTypesFacet, - severitiesFacet: sortedSeveritiesFacet, - issuesCount: data.total + const tagsFacet = data.facets.find(facet => facet.property === 'tags').values; + + that.model.set({ + tagsFacet, + typesFacet: sortedTypesFacet, + severitiesFacet: sortedSeveritiesFacet, + issuesCount: data.total + }); + + resolve(); }); }); }, requestTests () { - const that = this; - const url = window.baseUrl + '/api/tests/list'; - const options = { testFileId: this.model.id }; - return $.get(url, options).done(function (data) { - that.model.set({ tests: data.tests }); - that.testSorting = 'status'; - that.testAsc = true; - that.sortTests(function (test) { - return `${that.testsOrder.indexOf(test.status)}_______${test.name}`; + return new Promise(resolve => { + const that = this; + const url = window.baseUrl + '/api/tests/list'; + const options = { testFileId: this.model.id }; + + $.get(url, options).done(function (data) { + that.model.set({ tests: data.tests }); + that.testSorting = 'status'; + that.testAsc = true; + that.sortTests(function (test) { + return `${that.testsOrder.indexOf(test.status)}_______${test.name}`; + }); + resolve(); }); }); }, diff --git a/server/sonar-web/src/main/js/widgets/old/treemap.js b/server/sonar-web/src/main/js/widgets/old/treemap.js index 090deb10886..83224b2bbde 100644 --- a/server/sonar-web/src/main/js/widgets/old/treemap.js +++ b/server/sonar-web/src/main/js/widgets/old/treemap.js @@ -17,9 +17,11 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import $ from 'jquery'; import _ from 'underscore'; + import { translate } from '../../helpers/l10n'; +import { formatMeasure } from '../../helpers/measures'; +import { getChildren } from '../../api/components'; (function () { @@ -296,31 +298,35 @@ import { translate } from '../../helpers/l10n'; } }; - Treemap.prototype.formatComponents = function (data) { - const that = this; - const components = _.filter(data, function (component) { - const hasSizeMetric = function () { - return _.findWhere(component.msr, { - key: that.sizeMetric.key + Treemap.prototype.formatComponents = function (components, metrics) { + const nextComponents = components + .filter(component => { + const sizeMeasure = component.measures.find(measure => measure.metric === this.sizeMetric.key); + return sizeMeasure != null; + }) + .map(component => { + const measures = component.measures.map(measure => { + const metric = metrics.find(metric => metric.key === measure.metric); + return { ...measure, metricType: metric.type }; + }); + return { ...component, measures }; }); - }; - return _.isArray(component.msr) && component.msr.length > 0 && hasSizeMetric(); - }); - if (_.isArray(components) && components.length > 0) { - return components.map(function (component) { + + if (nextComponents.length > 0) { + return nextComponents.map(component => { const measures = {}; - component.msr.forEach(function (measure) { - measures[measure.key] = { - val: measure.val, - fval: measure.frmt_val, - text: measure.text, - data: measure.data + + component.measures.forEach(measure => { + measures[measure.metric] = { + val: measure.value, + fval: formatMeasure(measure.value, measure.metricType) }; }); + return { - key: component.copy != null ? component.copy : component.key, + key: component.refKey != null ? component.refKey : component.key, name: component.name, - longName: component.lname, + longName: component.name, qualifier: component.qualifier, measures }; @@ -329,24 +335,15 @@ import { translate } from '../../helpers/l10n'; }; Treemap.prototype.requestChildren = function (d) { - const that = this; - const metrics = this.metricsPriority().join(','); - const RESOURCES_URL = window.baseUrl + '/api/resources/index'; - return $.get(RESOURCES_URL, { - resource: d.key, - depth: 1, - metrics - }).done(function (r) { - let components = that.formatComponents(r); + return getChildren(d.key, this.metricsPriority(), { additionalFields: 'metrics' }).then(r => { + let components = this.formatComponents(r.components, r.metrics); if (components != null) { - components = _.sortBy(components, function (component) { - return -that.sizeMetric.value(component); - }); - components = _.initial(components, components.length - that.options().maxItems - 1); - that.updateTreemap(components, components.length > that.options().maxItems); - return that.addToBreadcrumbs(_.extend(d, { + components = _.sortBy(components, component => -this.sizeMetric.value(component)); + components = _.initial(components, components.length - this.options().maxItems - 1); + this.updateTreemap(components, components.length > this.options().maxItems); + this.addToBreadcrumbs(_.extend(d, { components, - maxResultsReached: that.maxResultsReached() + maxResultsReached: this.maxResultsReached() })); } }); -- 2.39.5