Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async function(scope) {
- let record;
- const request = p.getRequest();
- if (!!request.embedded_to){
- const model = await p.getModel(request.embedded_to.model);
- record = await model.findOne({id: request.embedded_to.record_id});
- }else{
- const model = await p.getModel("test_iy");
- record = await model.findOne({alias: "test1"});
- }
- if (!record) throw new Error('record is not found');
- const location_id = record.getValue("location_id");
- if (!location_id) throw new Error('Location_id is not found');
- const attachment = await record.getAttachmentByName(`${location_id}_charts.json`);
- const jsonString = await attachment.getContent();
- const json_data = JSON.parse(jsonString);
- const origin = json_data.origin;
- const chart_data = [];
- const event_data = [];
- let dates = [];
- for (let i in origin){
- const name = i;
- const branch = origin[i];
- const values = branch.values;
- if (branch.ts == 1){ // chart expected (only one)
- const graph = {};
- graph.name = name;
- graph.data = [];
- for(let j in values){
- var d = new Date(j);
- const v = values[j];
- graph.data.push({date: d, val: v[0], mval: v[1]});
- dates.push(j);
- }
- chart_data.push(graph);
- }else{ // 0 - vlines expected
- const event = {};
- event.name = name;
- event.data = [];
- for(let j in values){
- event.data.push({ date: j, from:-0.2, to: 1.2, val: values[j]});
- dates.push(j);
- }
- event_data.push(event);
- }
- }
- dates = [...new Set(dates)];
- dates = dates.map(a => new Date(a).getTime());
- return {
- main: chart_data,
- events: event_data,
- dates: {min: moment(Math.min(...dates)).format("MMM DD, YY"), max: moment(Math.max(...dates)).format("MMM DD, YY")}
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement