Skip to content

Instantly share code, notes, and snippets.

@arnaudbesnier
Last active September 16, 2020 12:23
Show Gist options
  • Save arnaudbesnier/2208ff8a592bba613870b6140e4ae35c to your computer and use it in GitHub Desktop.
Save arnaudbesnier/2208ff8a592bba613870b6140e4ae35c to your computer and use it in GitHub Desktop.
'use strict';
const _ = require('lodash');
const moment = require('moment');
const liana = require('forest-express-sequelize');
const models = require('../../../../models');
function values(request, response) {
// NOTICE: To access to the current recordId => request.body.record_id
const query = `RAW SQL QUERY HERE`;
return models.sequelize
.query(query)
.then((results) => {
let resultsFormated = { labels: [], values: [] };
const weeks = _.uniq(results[0].map((r) => r.week ));
resultsFormated.labels = weeks.map((m) => moment(m).format('[W]w-YYYY'));
var keys = _.uniq(results[0].map((r) => r.environmentName));
keys.forEach((key) => {
var current = _.filter(results[0], { environmentName: key });
var values = weeks.map((week) => {
var v = _.findWhere(current, { week: week });
if (v) {
return parseInt(v.count, 10);
} else {
return 0;
}
});
resultsFormated.values.push({
key: key,
values: values
});
});
// NOTICE: value format is like
// { labels: ['W01-2017', 'W02-2017'], values: [{ key: 'line1', values: ['2', '3'] }, { key: 'line2', values: ['0', '0'] }, ...] }
var json = new liana.StatSerializer({ value: resultsFormated }).perform();
response.send(json);
});
}
module.exports = (app) => {
app.post('smart-chart-url', liana.ensureAuthenticated, values);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment