-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert_to_virtual.js
120 lines (105 loc) · 3.41 KB
/
convert_to_virtual.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
downloadJson({
dataSourceXids: ['internal_mango_monitoring_ds'],
changeXids: true
});
/**
* You dont need to edit below here!
*/
function downloadJson(options) {
const result = convertToVirtual(options);
const filename = options.dataSourceXids.join('_') + '.json';
response.setContentType('application/json');
response.setHeader('Content-Disposition', `attachment; filename="${filename}"`);
print(result);
}
function convertToVirtual(options) {
const {dataSourceXids, changeXids} = options;
const DATA_TYPE_CODES = {
1: 'BINARY',
2: 'MULTISTATE',
3: 'NUMERIC',
4: 'ALPHANUMERIC',
5: 'IMAGE'
};
const POINT_LOCATORS = {
'BINARY': {
dataType: 'BINARY',
changeType: {
type: 'ALTERNATE_BOOLEAN',
startValue: "false"
},
settable: true
},
'MULTISTATE': {
dataType: 'MULTISTATE',
changeType: {
type: 'INCREMENT_MULTISTATE',
roll: true,
values: [0, 1, 2, 3, 4],
startValue: '0'
},
settable: true
},
'NUMERIC': {
dataType : 'NUMERIC',
changeType: {
type: 'BROWNIAN',
max: 100.0,
maxChange: 0.5,
min: 0.0,
startValue: '50'
},
settable:true
},
'ALPHANUMERIC': {
dataType: 'ALPHANUMERIC',
changeType: {
type: 'NO_CHANGE',
startValue: "abcd"
},
settable: true
}
};
const dataSources = services.dataSourceService.buildQuery()
.in('xid', ...dataSourceXids)
.query();
const dataSourceIds = Array.from(dataSources).map(ds => ds.getId());
const dataPoints = services.dataPointService.buildQuery()
.in('dataSourceId', ...dataSourceIds)
.query();
const dataTypes = {};
for (const dp of dataPoints) {
dataTypes[dp.getXid()] = DATA_TYPE_CODES[dp.getPointLocator().getDataType().getId()];
}
const exported = services.emportService.export({
dataSources,
dataPoints
}, 0);
const data = JSON.parse(exported);
const dsXidMap = {};
for (const ds of data.dataSources) {
if (changeXids) {
const newXid = services.dataSourceService.generateUniqueXid();
ds.xid = dsXidMap[ds.xid] = newXid;
}
ds.type = 'VIRTUAL';
ds.updatePeriods = ds.updatePeriods || 1;
ds.updatePeriodType = ds.updatePeriodType || 'MINUTES';
ds.polling = true;
}
for (const dp of data.dataPoints) {
const dataType = dataTypes[dp.xid];
if (dataType === 'IMAGE') {
throw new Error(`Datapoint ${dp.xid} has data type IMAGE which is unsupported by virtual data sources`);
}
if (changeXids) {
dp.xid = services.dataPointService.generateUniqueXid();
dp.dataSourceXid = dsXidMap[dp.dataSourceXid];
}
dp.pointLocator = POINT_LOCATORS[dataType];
if (dataType === 'MULTISTATE' && dp.textRenderer.type === 'MULTISTATE') {
dp.pointLocator.changeType.values = dp.textRenderer.multistateValues.map(msv => msv.key);
}
}
return JSON.stringify(data, null, 2);
}