2017-02-11 20:39:38 -05:00
|
|
|
var ReportConstants = require('../constants/ReportConstants');
|
|
|
|
|
|
|
|
var ErrorActions = require('./ErrorActions');
|
|
|
|
|
|
|
|
var models = require('../models.js');
|
|
|
|
var Report = models.Report;
|
|
|
|
var Error = models.Error;
|
|
|
|
|
2017-02-17 10:01:31 -05:00
|
|
|
function fetchReport(reportName) {
|
2017-02-11 20:39:38 -05:00
|
|
|
return {
|
2017-02-17 10:01:31 -05:00
|
|
|
type: ReportConstants.FETCH_REPORT,
|
|
|
|
reportName: reportName
|
2017-02-11 20:39:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function reportFetched(report) {
|
|
|
|
return {
|
|
|
|
type: ReportConstants.REPORT_FETCHED,
|
|
|
|
report: report
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 10:01:31 -05:00
|
|
|
function selectReport(report, seriesTraversal) {
|
|
|
|
return {
|
|
|
|
type: ReportConstants.SELECT_REPORT,
|
|
|
|
report: report,
|
|
|
|
seriesTraversal: seriesTraversal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function reportSelected(flattenedReport, seriesTraversal) {
|
|
|
|
return {
|
|
|
|
type: ReportConstants.REPORT_SELECTED,
|
|
|
|
report: flattenedReport,
|
|
|
|
seriesTraversal: seriesTraversal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 20:39:38 -05:00
|
|
|
function fetch(report) {
|
|
|
|
return function (dispatch) {
|
2017-02-17 10:01:31 -05:00
|
|
|
dispatch(fetchReport(report));
|
2017-02-11 20:39:38 -05:00
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: "GET",
|
|
|
|
dataType: "json",
|
|
|
|
url: "report/"+report+"/",
|
|
|
|
success: function(data, status, jqXHR) {
|
|
|
|
var e = new Error();
|
|
|
|
e.fromJSON(data);
|
|
|
|
if (e.isError()) {
|
|
|
|
dispatch(ErrorActions.serverError(e));
|
|
|
|
} else {
|
|
|
|
var r = new Report();
|
|
|
|
r.fromJSON(data);
|
|
|
|
dispatch(reportFetched(r));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function(jqXHR, status, error) {
|
|
|
|
dispatch(ErrorActions.ajaxError(error));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-17 10:01:31 -05:00
|
|
|
function select(report, seriesTraversal) {
|
|
|
|
return function (dispatch) {
|
|
|
|
if (!seriesTraversal)
|
|
|
|
seriesTraversal = [];
|
|
|
|
dispatch(selectReport(report, seriesTraversal));
|
|
|
|
|
|
|
|
// Descend the tree to the right series to flatten
|
|
|
|
var series = report;
|
|
|
|
for (var i=0; i < seriesTraversal.length; i++) {
|
|
|
|
if (!series.Series.hasOwnProperty(seriesTraversal[i])) {
|
|
|
|
dispatch(ErrorActions.clientError("Invalid series"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
series = series.Series[seriesTraversal[i]];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actually flatten the data
|
|
|
|
var flattenedSeries = series.mapReduceChildren(null,
|
|
|
|
function(accumulator, currentValue, currentIndex, array) {
|
|
|
|
return accumulator + currentValue;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add back in any values from the current level
|
|
|
|
if (series.hasOwnProperty('Values'))
|
2017-06-07 19:12:53 -04:00
|
|
|
flattenedSeries[Report.topLevelAccountName()] = series.Values;
|
2017-02-17 10:01:31 -05:00
|
|
|
|
|
|
|
var flattenedReport = new Report();
|
|
|
|
|
|
|
|
flattenedReport.ReportId = report.ReportId;
|
|
|
|
flattenedReport.Title = report.Title;
|
|
|
|
flattenedReport.Subtitle = report.Subtitle;
|
|
|
|
flattenedReport.XAxisLabel = report.XAxisLabel;
|
|
|
|
flattenedReport.YAxisLabel = report.YAxisLabel;
|
|
|
|
flattenedReport.Labels = report.Labels.slice();
|
|
|
|
flattenedReport.FlattenedSeries = flattenedSeries;
|
|
|
|
|
|
|
|
dispatch(reportSelected(flattenedReport, seriesTraversal));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-11 20:39:38 -05:00
|
|
|
module.exports = {
|
2017-02-17 10:01:31 -05:00
|
|
|
fetch: fetch,
|
|
|
|
select: select
|
2017-02-11 20:39:38 -05:00
|
|
|
};
|