Skip to content

Commit 52adbe7

Browse files
committed
Connect dashboard to the Sonar web server
1 parent 348fa66 commit 52adbe7

File tree

4 files changed

+204
-8
lines changed

4 files changed

+204
-8
lines changed

src/main/js/App.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import MetricsSummaryTab from './components/MetricsSummaryTab';
33

4-
class App extends Component {
4+
class App extends React.Component {
5+
6+
state = {
7+
project: ''
8+
};
9+
10+
componentDidMount() {
11+
console.log('App:'+this.props.project);
12+
this.setState({project:this.props.project});
13+
}
14+
515
render(){
6-
return (<MetricsSummaryTab/>);
16+
return (<MetricsSummaryTab project={this.state.project}/>);
717
}
18+
819
}
920

1021
export default App;

src/main/js/api.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,69 @@ return getJSON('/api/project_analyses/search', {
9292
}
9393
});
9494
}
95+
96+
export function findMetricsSummaryMeasures(project) {
97+
98+
return getJSON('/api/project_analyses/search', {
99+
project: project.key,
100+
p: 1,
101+
ps: 500,
102+
}).then(function (responseAnalyses) {
103+
const numberOfAnalyses = responseAnalyses.analyses.length;
104+
if (numberOfAnalyses > 0) {
105+
106+
return getJSON('/api/measures/search_history', {
107+
component: project.key,
108+
metrics: "alert_status,bugs,vulnerabilities,code_smells,reliability_rating,security_rating,sqale_rating",
109+
ps: 1000
110+
}).then(function (responseMetrics) {
111+
var data = [];
112+
var numberOfVersions=0;
113+
114+
for (let i = 0; i < numberOfAnalyses; i++) {
115+
let analysis = responseAnalyses.analyses[i];
116+
for (let j = 0; j < analysis.events.length; j++) {
117+
if (analysis.events[j].category === "VERSION") {
118+
let result = {version: analysis.events[j].name,
119+
alert_status:"",
120+
bugs:"0", vulnerabilities:"0", code_smells:"0",
121+
reliability_rating:"",security_rating:"",sqale_rating:""
122+
};
123+
const numberOfMeasuresRetrieved = 7;
124+
125+
for (let k = 0; k < numberOfMeasuresRetrieved; k++) {
126+
for(let d = 0; d < responseMetrics.measures[k].history.length; d++) {
127+
if ( responseMetrics.measures[k].history[d].date === responseAnalyses.analyses[i].date ) {
128+
//console.log(responseMetrics.measures[k].metric);
129+
if (responseMetrics.measures[k].metric === "bugs") {
130+
result.bugs = responseMetrics.measures[k].history[d].value;
131+
} else if (responseMetrics.measures[k].metric === "vulnerabilities") {
132+
result.vulnerabilities = responseMetrics.measures[k].history[d].value;
133+
} else if (responseMetrics.measures[k].metric === "code_smells") {
134+
result.code_smells = responseMetrics.measures[k].history[d].value;
135+
} else if (responseMetrics.measures[k].metric === "alert_status") {
136+
result.alert_status = responseMetrics.measures[k].history[d].value;
137+
} else if (responseMetrics.measures[k].metric === "reliability_rating") {
138+
result.reliability_rating = responseMetrics.measures[k].history[d].value;
139+
} else if (responseMetrics.measures[k].metric === "security_rating") {
140+
result.security_rating = responseMetrics.measures[k].history[d].value;
141+
} else if (responseMetrics.measures[k].metric === "sqale_rating") {
142+
result.sqale_rating = responseMetrics.measures[k].history[d].value;
143+
}
144+
}
145+
}
146+
}
147+
148+
data[numberOfVersions] = result;
149+
numberOfVersions++;
150+
}
151+
}
152+
}
153+
//console.table(data);
154+
return data;
155+
});
156+
}
157+
});
158+
}
159+
160+

src/main/js/app-metrics_summary.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
*/
66
import React from 'react';
77
import { render, unmountComponentAtNode } from 'react-dom';
8-
import App from './App';
98
import './style.css';
9+
import MetricsSummaryTab from './components/MetricsSummaryTab';
1010

1111
window.registerExtension('icode/icode_metrics_summary', options => {
1212

1313
const { el } = options;
1414

1515
render(
16-
<App/>, el
16+
<MetricsSummaryTab project={options.component}/>, el
1717
);
1818

1919
return () => unmountComponentAtNode(el);

src/main/js/components/MetricsSummaryTab/MetricsSummaryTab.js

Lines changed: 122 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,143 @@
11
import React from "react";
22
import "./MetricsSummaryTab.css";
33
import MetricsSummaryPanel from '../MetricsSummaryPanel';
4+
import {findMetricsSummaryMeasures} from '../../api.js';
5+
import {getJSON} from 'sonar-request';
6+
7+
const NESTING = 0;
8+
const RATIO_COMMENT = 1;
9+
const COMPLEXITY_SIMPLIFIED = 2;
10+
const LOC = 3;
11+
12+
const metricKeysTabF77 = [
13+
'icode-f77-nesting-min',
14+
'icode-f77-nesting-mean',
15+
'icode-f77-nesting-max',
16+
'icode-f77-ratio-comment-min',
17+
'icode-f77-ratio-comment-mean',
18+
'icode-f77-ratio-comment-max',
19+
'icode-f77-cyclomatic-complexity-min',
20+
'icode-f77-cyclomatic-complexity-mean',
21+
'icode-f77-cyclomatic-complexity-max',
22+
'icode-f77-loc',
23+
'icode-f77-loc-min',
24+
'icode-f77-loc-mean',
25+
'icode-f77-loc-max'];
26+
27+
const metricKeysTabF90 = [
28+
'icode-f90-nesting-min',
29+
'icode-f90-nesting-mean',
30+
'icode-f90-nesting-max',
31+
'icode-f90-ratio-comment-min',
32+
'icode-f90-ratio-comment-mean',
33+
'icode-f90-ratio-comment-max',
34+
'icode-f90-cyclomatic-complexity-min',
35+
'icode-f90-cyclomatic-complexity-mean',
36+
'icode-f90-cyclomatic-complexity-max',
37+
'icode-f90-loc',
38+
'icode-f90-loc-min',
39+
'icode-f90-loc-mean',
40+
'icode-f90-loc-max'];
41+
42+
const metricKeysTabSH = [
43+
'icode-shell-nesting-min',
44+
'icode-shell-nesting-mean',
45+
'icode-shell-nesting-max',
46+
'icode-shell-ratio-comment-min',
47+
'icode-shell-ratio-comment-mean',
48+
'icode-shell-ratio-comment-max',
49+
'icode-shell-cyclomatic-complexity-min',
50+
'icode-shell-cyclomatic-complexity-mean',
51+
'icode-shell-cyclomatic-complexity-max',
52+
'icode-shell-loc',
53+
'icode-shell-loc-min',
54+
'icode-shell-loc-mean',
55+
'icode-shell-loc-max'];
56+
57+
function findMeasuresF77(componentName) {
58+
return new Promise(function(resolve, reject) {
59+
60+
let metricKeys = metricKeysTabF77.reduce((accumulator, currentValue) => accumulator + ',' + currentValue);
61+
62+
let measure_component = getJSON('/api/measures/component', {
63+
metricKeys:metricKeys,
64+
component:componentName
65+
}).then(function (measure_component) {
66+
console.log(measure_component);
67+
68+
// Build map 'metric:value'
69+
let allMetrics = new Map();
70+
measure_component.component.measures.forEach(element => {
71+
for (let index = 0; index < metricKeysTabF77.length; index++) {
72+
const key = metricKeysTabF77[index];
73+
if(element.metric === key){
74+
allMetrics.set(element.metric, element.value);
75+
}
76+
}
77+
});
78+
console.log(allMetrics);
79+
80+
// Build result table
81+
let res=[
82+
{name:'Nesting',
83+
total: '-',
84+
min: allMetrics.get('icode-f77-nesting-min'),
85+
mean: allMetrics.get('icode-f77-nesting-mean'),
86+
max: allMetrics.get('icode-f77-nesting-max')
87+
},
88+
{name:'Ratio Comment',
89+
total: '-',
90+
min: allMetrics.get('icode-f77-ratio-comment-min'),
91+
mean: allMetrics.get('icode-f77-ratio-comment-mean'),
92+
max: allMetrics.get('icode-f77-ratio-comment-max')
93+
},
94+
{name:'Complexity Simplified',
95+
total: '-',
96+
min: allMetrics.get('icode-f77-cyclomatic-complexity-min'),
97+
mean: allMetrics.get('icode-f77-cyclomatic-complexity-mean'),
98+
max: allMetrics.get('icode-f77-cyclomatic-complexity-max')
99+
},
100+
{name:'Line Of Code',
101+
total: allMetrics.get('icode-f77-loc'),
102+
min: allMetrics.get('icode-f77-loc-min'),
103+
mean: allMetrics.get('icode-f77-loc-mean'),
104+
max: allMetrics.get('icode-f77-loc-max')
105+
}
106+
];
107+
console.log(res);
108+
109+
resolve(res);
110+
}).catch(function(error) {
111+
console.log('No measures found: ' + error.message);
112+
reject(null);
113+
});
114+
});
115+
}
4116

5117
class MetricsSummaryTab extends React.Component {
6118

7119
state = {
8120
dataF77: [],
9121
dataF90: [],
10-
dataSH: []
122+
dataSH: [],
123+
data:[]
11124
};
12125

13126
componentDidMount() {
127+
128+
findMeasuresF77(this.props.project.key).then((item) => {
129+
this.setState({
130+
dataF77: item
131+
});
132+
});
14133

15134
this.setState({
16-
dataF77: [
135+
/* dataF77: [
17136
{ name: 'Nesting', total: '-', min: 5, mean: 5.2, max: 6 },
18137
{ name: 'Ratio Comment', total: '-', min: 18.6, mean: 29.8, max: 38.9 },
19138
{ name: 'Complexity Simplified', total: '-', min: 26, mean: 33.6, max: 58 },
20139
{ name: 'Line Of Code', total: 601, min: 101, mean: 110.2, max: 182 }
21-
],
140+
],*/
22141
dataF90: [
23142
{ name: 'Nesting', total: '-', min: '-', mean: '-', max: '-' },
24143
{ name: 'Ratio Comment', total: '-', min: '-', mean: '-', max: '-' },

0 commit comments

Comments
 (0)