forked from OpenNMS/backshift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackshift.Utilities.RrdGraphVisitor.js
More file actions
187 lines (166 loc) · 5.31 KB
/
Backshift.Utilities.RrdGraphVisitor.js
File metadata and controls
187 lines (166 loc) · 5.31 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
Backshift.namespace('Backshift.Utilities.RrdGraphConverter');
Backshift.Utilities.RrdGraphVisitor = Backshift.Class.create({
initialize: function (args) {
this.onInit(args);
},
onInit: function (args) {
// Defined by subclasses
},
_visit: function (graphDef) {
// Inspired from http://krasimirtsonev.com/blog/article/Simple-command-line-parser-in-JavaScript
var CommandLineParser = (function () {
var parse = function (str, lookForQuotes) {
var args = [];
var readingPart = false;
var part = '';
var n = str.length;
for (var i = 0; i < n; i++) {
if (str.charAt(i) === ' ' && !readingPart) {
args.push(part);
part = '';
} else {
if (str.charAt(i) === '\"' && lookForQuotes) {
readingPart = !readingPart;
part += str.charAt(i);
} else {
part += str.charAt(i);
}
}
}
args.push(part);
return args;
};
return {
parse: parse
}
})();
var i, args, command, name, path, dsName, consolFun, rpnExpression, subParts, width, srcName,
color, legend, aggregation, value, seriesName;
var parts = CommandLineParser.parse(graphDef.command, true);
var n = parts.length;
for (i = 0; i < n; i++) {
if (parts[i].indexOf("--") === 0) {
args = /--(.*)=(.*)/.exec(parts[i]);
if (args === null) {
continue;
}
if (args[1] === "title") {
this._onTitle(this.displayString(this._decodeString(args[2])));
} else if (args[1] === "vertical-label") {
this._onVerticalLabel(this.displayString(this._decodeString(args[2])));
}
}
args = parts[i].match(/(\\.|[^:])+/g);
if (args === null) {
continue;
}
command = args[0];
if (command === "DEF") {
subParts = args[1].split("=");
name = subParts[0];
path = subParts[1];
dsName = args[2];
consolFun = args[3];
this._onDEF(name, path, dsName, consolFun);
} else if (command === "CDEF") {
subParts = args[1].split("=");
name = subParts[0];
rpnExpression = subParts[1];
this._onCDEF(name, rpnExpression);
} else if (command === "VDEF") {
subParts = args[1].split("=");
name = subParts[0];
rpnExpression = subParts[1];
this._onVDEF(name, rpnExpression);
} else if (command.match(/LINE/)) {
width = parseInt(/LINE(\d+)/.exec(command));
subParts = args[1].split("#");
srcName = subParts[0];
color = this._getColor(subParts[1]);
legend = this._decodeString(args[2]);
this._onLine(srcName, color, legend, width);
} else if (command === "AREA") {
subParts = args[1].split("#");
srcName = subParts[0];
color = this._getColor(subParts[1]);
legend = this._decodeString(args[2]);
this._onArea(srcName, color, legend);
} else if (command === "STACK") {
subParts = args[1].split("#");
srcName = subParts[0];
color = this._getColor(subParts[1]);
legend = this._decodeString(args[2]);
this._onStack(srcName, color, legend);
} else if (command === "GPRINT") {
if (args.length === 3) {
srcName = args[1];
aggregation = undefined;
value = this._decodeString(args[2]);
} else {
srcName = args[1];
aggregation = args[2];
value = this._decodeString(args[3]);
}
this._onGPrint(srcName, aggregation, value);
} else if (command === "COMMENT") {
value = this._decodeString(args[1]);
this._onComment(value);
}
}
},
_getColor: function (color) {
if (color === undefined || color === "") {
return undefined;
}
// Normalization scope: only 8-digit hex with an explicit 00 alpha channel
// is treated as "transparent" (RRD convention, e.g. #00000000). 3-, 4-,
// and 6-digit values are passed through as-is; browsers parse 3-6-digit
// natively, 4-digit support varies and callers shouldn't rely on it.
if (color.length === 8 && color.slice(6).toLowerCase() === "00") {
return undefined;
}
return '#' + color;
},
_onTitle: function (title) {
},
_onVerticalLabel: function (label) {
},
_onDEF: function (name, path, dsName, consolFun) {
},
_onCDEF: function (name, rpnExpression) {
},
_onVDEF: function (name, rpnExpression) {
},
_onLine: function (srcName, color, legend, width) {
},
_onArea: function (srcName, color, legend) {
},
_onStack: function (srcName, color, legend) {
},
_onGPrint: function (srcName, aggregation, value) {
},
_onComment: function (value) {
},
_seriesName: function(string) {
},
_decodeString: function (string) {
if (string === undefined) {
return string;
}
// Remove any quotes
string = string.replace(/"/g, '');
// Replace escaped colons
string = string.replace("\\:", ':');
return string;
},
displayString: function (string) {
if (string === undefined) {
return string;
}
// Remove any newlines
string = string.replace("\\n", '');
// Remove any leading/trailing whitespace
string = string.trim();
return string;
}
});