Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. 'use strict';
  2. const _ = require('lodash');
  3. const moment = require('moment');
  4. const liana = require('forest-express-sequelize');
  5. const models = require('../../../../models');
  6.  
  7. function values(request, response) {
  8. // NOTICE: To access to the current recordId => request.body.record_id
  9. const query = `RAW SQL QUERY HERE`;
  10.  
  11. return models.sequelize
  12. .query(query)
  13. .then((results) => {
  14. let resultsFormated = { labels: [], values: [] };
  15.  
  16. const weeks = _.uniq(results[0].map((r) => r.week ));
  17. resultsFormated.labels = weeks.map((m) => moment(m).format('[W]w-YYYY'));
  18.  
  19. var keys = _.uniq(results[0].map((r) => r.environmentName));
  20.  
  21. keys.forEach((key) => {
  22. var current = _.filter(results[0], { environmentName: key });
  23.  
  24. var values = weeks.map((week) => {
  25. var v = _.findWhere(current, { week: week });
  26. if (v) {
  27. return parseInt(v.count, 10);
  28. } else {
  29. return 0;
  30. }
  31. });
  32.  
  33. resultsFormated.values.push({
  34. key: key,
  35. values: values
  36. });
  37. });
  38.  
  39. // NOTICE: value format is like
  40. // { labels: ['W01-2017', 'W02-2017'], values: [{ key: 'line1', values: ['2', '3'] }, { key: 'line2', values: ['0', '0'] }, ...] }
  41.  
  42. var json = new liana.StatSerializer({ value: resultsFormated }).perform();
  43. response.send(json);
  44. });
  45. }
  46.  
  47. module.exports = (app) => {
  48. app.post('smart-chart-url', liana.ensureAuthenticated, values);
  49. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement