Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function(scope) {
  2.     let record;
  3.     const request = p.getRequest();
  4.     if (!!request.embedded_to){
  5.         const model = await p.getModel(request.embedded_to.model);
  6.         record = await model.findOne({id: request.embedded_to.record_id});
  7.     }else{
  8.         const model = await p.getModel("test_iy");
  9.         record = await model.findOne({alias: "test1"});
  10.     }
  11.     if (!record) throw new Error('record is not found');
  12.  
  13.     const location_id = record.getValue("location_id");
  14.     if (!location_id) throw new Error('Location_id is not found');
  15.     const attachment = await record.getAttachmentByName(`${location_id}_charts.json`);
  16.        
  17.     const jsonString = await attachment.getContent();
  18.     const json_data = JSON.parse(jsonString);
  19.     const origin = json_data.origin;
  20.     const chart_data = [];
  21.     const event_data = [];
  22.     let dates = [];
  23.     for (let i in origin){
  24.             const name = i;
  25.             const branch = origin[i];
  26.             const values = branch.values;
  27.             if (branch.ts == 1){ // chart expected (only one)
  28.                 const graph = {};
  29.                 graph.name = name;
  30.                 graph.data = [];
  31.                 for(let j in values){
  32.                     var d = new Date(j);
  33.                     const v = values[j];
  34.                     graph.data.push({date: d, val: v[0], mval: v[1]});
  35.                     dates.push(j);
  36.                 }
  37.                 chart_data.push(graph);
  38.  
  39.             }else{          // 0 - vlines expected
  40.                 const event = {};
  41.                 event.name = name;
  42.                 event.data = [];
  43.                 for(let j in values){
  44.                     event.data.push({ date: j, from:-0.2, to: 1.2, val: values[j]});
  45.                     dates.push(j);
  46.                 }
  47.                 event_data.push(event);
  48.             }
  49.         }
  50.         dates = [...new Set(dates)];
  51.         dates = dates.map(a => new Date(a).getTime());
  52.  
  53.   return {
  54.     main: chart_data,
  55.     events: event_data,
  56.     dates: {min: moment(Math.min(...dates)).format("MMM DD, YY"), max: moment(Math.max(...dates)).format("MMM DD, YY")}
  57.   };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement