-
Notifications
You must be signed in to change notification settings - Fork 16
/
saveMetrics.sjs
48 lines (46 loc) · 1.71 KB
/
saveMetrics.sjs
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
declareUpdate();
var dbName;
var metricsDocumentStr;
var uriRoot;
var collections;
const insertDoc = function(metricsDocumentStr, collection, uriRoot){
return function(){
const json = JSON.parse(metricsDocumentStr);
let jobName = json["job"]["name"];
if (!jobName) { jobName = json["job"]["runLocation"]; }
if (fn.startsWith(jobName, "/")) {
jobName=fn.substring(jobName, 2);
}
const coll = [];
if (collection != "NA") {
coll.push( fn.tokenize(collection , ",").toArray());
}
coll.push(jobName);
const dateTime = fn.currentDateTime();
let uri_root = uriRoot;
if (uriRoot == "NA") { uri_root = "/ServiceMetrics/"; }
if (!fn.startsWith(uri_root, "/")) { uri_root = "/" + $uri_root; }
if (fn.endsWith(uri_root, "/")) { uri_root = uri_root + "/"; }
let orig_uri = json["job"]["metricsDocUri"];
let uri = orig_uri;
if (!uri) {
uri = uri_root + "CORB/" +
jobName + "/" +
fn.yearFromDateTime(dateTime) + "/" +
fn.monthFromDateTime(dateTime) + "/" +
fn.dayFromDateTime(dateTime) + "/" +
fn.hoursFromDateTime(dateTime) + "/" +
fn.minutesFromDateTime(dateTime) + "/" +
xdmp.random() + ".json";
orig_uri = uri;
}
else if (!json["job"]["endTime"]) {//Job finished so update the root document
uri = uri + "/" + xdmp.random();
}
xdmp.documentInsert(uri, json, xdmp.defaultPermissions(), coll);
return orig_uri;
}
};
const database = xdmp.database(dbName);
xdmp.invokeFunction(insertDoc(metricsDocumentStr, collections, uriRoot),
{transactionMode:"update-auto-commit", database:database});