* 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 { getJSON } from '../helpers/request';
-export function getTimeMachineData (componentKey, metrics) {
- const url = '/api/timemachine/index';
- const data = { resource: componentKey, metrics };
- return getJSON(url, data);
-}
+type Response = {
+ measures: Array<{
+ metric: string,
+ history: Array<{
+ date: string,
+ value: string
+ }>
+ }>,
+ paging: {
+ pageIndex: number,
+ pageSize: number,
+ total: number
+ }
+};
+
+export const getTimeMachineData = (component: string, metrics: Array<string>, other?: {}): Promise<Response> => (
+ getJSON('/api/measures/search_history', {
+ component,
+ metrics: metrics.join(),
+ ps: 1000,
+ ...other
+ })
+);
const HEIGHT = 500;
-function parseValue (value, type) {
- return type === 'RATING' && typeof value === 'string' ? value.charCodeAt(0) - 'A'.charCodeAt(0) + 1 : value;
-}
-
export default class MeasureHistory extends React.Component {
state = {
components: [],
metricsToRequest.push(comparisonMetric);
}
- return getTimeMachineData(this.props.component.key, metricsToRequest.join()).then(r => {
- return r[0].cells.map(cell => {
- return {
- date: moment(cell.d).toDate(),
- values: cell.v
- };
- });
+ return getTimeMachineData(this.props.component.key, metricsToRequest).then(r => {
+ if (r.measures.length === 0) {
+ return [];
+ }
+ return r.measures[0].history.map(analysis => ({
+ date: moment(analysis.date).toDate(),
+ value: analysis.value
+ }));
});
}
});
}
- renderLineChart (snapshots, metric, index) {
+ renderLineChart (snapshots, metric) {
if (!metric) {
return null;
}
const data = snapshots.map(snapshot => {
return {
x: snapshot.date,
- y: parseValue(snapshot.values[index], metric.type)
+ y: Number(snapshot.value)
};
});
);
}
- renderLineCharts () {
- const { metric } = this.props;
-
- return (
- <div>
- {this.renderLineChart(this.state.snapshots, metric, 0)}
- {this.renderLineChart(this.state.snapshots, this.state.comparisonMetric, 1)}
- </div>
- );
- }
-
render () {
const { fetching, snapshots } = this.state;
return (
<div className="measure-details-history">
- {this.renderLineCharts()}
+ {this.renderLineChart(this.state.snapshots, this.props.metric)}
</div>
);
}
}
loadHistory (component) {
- const metrics = HISTORY_METRICS_LIST.join(',');
- return getTimeMachineData(component.key, metrics).then(r => {
+ return getTimeMachineData(component.key, HISTORY_METRICS_LIST).then(r => {
if (this.mounted) {
const history = {};
- r[0].cols.forEach((col, index) => {
- history[col.metric] = r[0].cells.map(cell => {
- const date = moment(cell.d).toDate();
- const value = cell.v[index] || 0;
- return { date, value };
- });
+ r.measures.forEach(measure => {
+ const measureHistory = measure.history.map(analysis => ({
+ date: moment(analysis.date).toDate(),
+ value: analysis.value
+ }));
+ history[measure.metric] = measureHistory;
});
const historyStartDate = history[HISTORY_METRICS_LIST[0]][0].date;
this.setState({ history, historyStartDate });