This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.js
155 lines (134 loc) · 4.88 KB
/
processor.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'use strict';
var xml2js = require("xml2js");
var parser = xml2js.parseString;
var params = getProcessParameters();
exitOnSignal('SIGTERM');
if (Object.keys(params).length > 0) {
// custom metadata keys are prefixed with C_
console.error(' LOCATION: ' + params.C_AGM_LOCATION);
console.error(' DOMAIN: ' + params.C_DOMAIN);
console.error(' PROJECT: ' + params.C_PROJECT);
} else {
console.log('[]');
process.exit(0);
}
//For debugging read a file from disk instead of stdin
/*var fs = require('fs')
fs.readFile('data.xml', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
console.log('data is:' + data);
parseXml(data);
}); */
readInputStream(parseXml);
function parseXml (data){
console.error(' Start parsing agm-issue-change XML');
parser(data, function(err, result){
var issue_change = [];
var elements = result.Audits.Audit.length;
for(var i = 0; i< elements; i++) {
var auditEvent = {};
auditEvent.event = "issue_change";
//data timestamp is in UTC already, just should be adjusted with ISO-8601
auditEvent.time = new Date(result.Audits.Audit[i].Time[0]).toISOString();
//set ID section
var id = {};
id.uid = result.Audits.Audit[i].ParentId[0];
id.auditId = result.Audits.Audit[i].Id[0];
auditEvent.id = id;
//set tags, which are not from metadata
var tags = {};
tags.User = result.Audits.Audit[i].User[0];
tags.Action = result.Audits.Audit[i].Action[0];
auditEvent.tags = tags;
var source = {};
source.Location = params.C_AGM_LOCATION;
source.Domain = params.C_DOMAIN;
source.Project = params.C_PROJECT;
auditEvent.source = source;
var fi = [];
//the entire array of Properties, expecting to have length = 1 always
for(var j = 0; j < result.Audits.Audit[i].Properties.length ; j++){
var props = result.Audits.Audit[i].Properties[j];
if (props.Property) //Avoid failure in case of empty Properties section
{
fi.push(createFieldFromProperty(props.Property));
}
}
auditEvent.fields = fi[0];
issue_change.push(auditEvent);
}
console.error(' agm issue change payload to be sent to metrics-gateway-service: ' + JSON.stringify(issue_change));
//use process stdout via console.log to send the result to result-processing (parent process)
console.log(JSON.stringify(issue_change));
process.exit(0);
});
}
/**
* Create json array from properties object generated by xml parser
* All properties handled in 1 call of this function
* Also property names are set according to https://github.com/gaia-adm/api-data-format
*/
var createFieldFromProperty = function createFieldFromProperty(props){
var fields = [];
for(var p = 0; p < props.length; p++){
var field = {};
field.label = setIfNotEmpty(props[p].$.Label);
field.name = setIfNotEmpty(props[p].$.Name);
//oldValue can be empty in case of new entity
if(props[p].OldValue) {
field.from = setIfNotEmpty(props[p].OldValue[0]);
}
field.to = setIfNotEmpty(props[p].NewValue[0]);
//newValue can be empty in some cases; we'll set it to none for further handling (as it is mandatory field in our API)
if(field.to === undefined) {
field.to = 'none';
}
fields.push(field);
}
return fields;
};
var setIfNotEmpty = function setIfNotEmpty(val) {
if(val) {
return val;
}
};
function exitOnSignal(signal) {
process.on(signal, function() {
console.error(' Caught ' + signal + ', exiting');
process.exit(1);
});
}
function getProcessParameters() {
var params = {};
var keys = Object.keys(process.env);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (key.lastIndexOf('P_', 0) === 0) {
var value = process.env[key];
params[key.substr(2)] = value;
}
}
return params;
}
function readInputStream(callback) {
console.error(' Input stream received, start reading');
process.stdin.setEncoding('utf8');
var fullInput = '';
process.stdin.on('readable', function () {
var chunk = process.stdin.read();
if (chunk !== null) {
console.error('Next chunk size in characters is: ' + chunk.length);
fullInput = fullInput + chunk;
}
});
process.stdin.on('end', function(){
if(fullInput.length > 0){
console.error('XML created from the input stream; size in characters: ' + fullInput.length);
callback(fullInput);
}
});
}
module.exports.setIfNotEmpty = setIfNotEmpty;
module.exports.createFieldFromProperty = createFieldFromProperty;