alphascript-server
Version:
CRUD operations for mongo and other functionalities to get started quickly in any CMS project
123 lines (99 loc) • 3.71 kB
JavaScript
var api = require('../../../');
var moment = require('moment');
module.exports = {
get: function (req, res) {
var db = req.params.namespace === 'common' ? api.common : api.db[req.params.namespace];
if (typeof db === 'function') db = db(req);
getGraph(db[req.params.collection], req.body, function (err, graph) {
if (err) return res.status(500).send(err);
res.json(graph);
});
},
custom: function (graphKey) {
return function (req, res) {
api.graphMap[graphKey].get(req, function (err, data) {
if (err) return res.status(500).send(err);
res.json(data);
});
};
}
};
//var ['line', 'radar', 'doughnut', 'pie', 'polarArea' 'bar', 'horizontal-bar']; //'bubble'
function getGraph(targetDb, params, callback) {
var query = params.query || {};
var select = params.select || '';
var obj = params.obj || undefined;
var seriesKey = params.seriesKey || undefined;
var dataKey = params.dataKey || undefined;
var labelsKey = params.labelsKey || undefined;
targetDb.find(query).select(select).lean().exec(function (err, items) {
if (err) {
api.error.log(err);
return callback("Impossível estabelecer conexão à base de dados ao gerar o gráfico");
}
var graph = {
labels: [],
data: [],
series: [],
dict: {}
};
items.forEach(function (item) {
if (typeof seriesKey !== 'undefined' && item[seriesKey] === 'undefined') return;
if (typeof obj === 'undefined') return setMetadata(graph, item, labelsKey, dataKey);
if (!(item[obj] instanceof Array)) return setMetadata(graph, item[obj], labelsKey, dataKey);
item[obj].forEach(function (atom) {
setMetadata(graph, atom, labelsKey, dataKey);
});
});
graph.labels.sort();
graph.labels.forEach(function (label, i) {
graph.dict[label].index = i;
});
items.forEach(function (item, i) {
if (typeof seriesKey !== 'undefined') {
if (typeof item[seriesKey] === 'undefined') return;
graph.series.push(item[seriesKey]);
}
graph.data.push(new Array(graph.labels.length));
var index = seriesKey ? i : undefined;
if (typeof obj === 'undefined') return setData(graph, item, index, labelsKey, dataKey);
if (!(item[obj] instanceof Array)) return setData(graph, item[obj], index, labelsKey, dataKey);
item[obj].forEach(function (atom) {
setData(graph, atom, index, labelsKey, dataKey);
});
});
callback(null, {
labels: graph.labels,
series: graph.series,
data: [graph.data]
});
});
}
function setData(graph, item, index, labelsKey, dataKey) {
if (typeof item[labelsKey] === 'undefined') return;
var label = getLabel(item[labelsKey]);
var graphData = typeof index === 'undefined' ? graph.data : graph.data[index];
if (typeof dataKey !== 'undefined') {
if (typeof item[dataKey] === 'undefined') return;
graphData[graph.dict[label].index] = item[dataKey];
} else graphData[graph.dict[label].index] = graph.dict[label].count;
}
function getLabel(rawLabel) {
if (rawLabel instanceof Date) return moment(rawLabel).format('YYYY-MM-DD');
else if (rawLabel instanceof Array) return rawLabel.length;
return rawLabel;
}
function setMetadata(graph, item, labelsKey, dataKey) {
if (typeof item[labelsKey] === 'undefined') return;
if (typeof dataKey !== 'undefined' && typeof item[dataKey] === 'undefined') return;
var label = getLabel(item[labelsKey]);
if (typeof graph.dict[label] === 'undefined') {
graph.labels.push(label);
graph.dict[label] = {
index: -1,
count: 0,
sum: 0
};
}
graph.dict[label].count++;
}