This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.js
136 lines (107 loc) · 3.07 KB
/
config.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
var _ = require("underscore");
function Config() {
}
Config.parse = function(text) {
var config = JSON.parse(text);
config.__proto__ = Config.prototype;
config.normalize();
return config;
};
Config.prototype.normalize = function() {
var _this = this;
if (!_this.variables)
_this.variables = {};
if (!_this.groups)
_this.groups = [];
_this.groups.forEach(function (group) {
if (!group.counters)
group.counters = [];
group.counters.forEach(function (counter, index) {
if (_.isString(counter)) {
counter = {
id: counter
};
group.counters[index] = counter;
}
if (!counter.name)
counter.name = /\\([^\\]+)\\(.+)/.exec(counter.id)[2];
if (!counter.format)
counter.format = "0,0";
if (counter.threshold && !_.isArray(counter.threshold))
counter.threshold = _.map(counter.threshold, function(v, k) {
return {
level: (k === "*" ? Number.MAX_VALUE : k),
name: v
};
});
if (counter.stats) {
counter.stats.forEach(function(stat, index) {
var match = /(\w+)\((\d+)\)/.exec(stat);
if (match)
counter.stats[index] = {
func: match[1],
count: match[2]
};
});
}
});
if (!group.lists)
group.lists = [];
group.lists.forEach(function (list, index) {
if (_.isString(group.list)) {
list = {
id: group.list
};
group.lists[index] = list;
}
if (!list.count)
list.count = 5;
if (!list.sort)
list.sort = "desc";
if (!list.format)
list.format = "0,0";
if (list.threshold && !_.isArray(list.threshold))
list.threshold = _.map(list.threshold, function(v, k) {
return {
level: (k === "*" ? Number.MAX_VALUE : k),
name: v
};
});
if (list.stats) {
list.stats.forEach(function(stat, index) {
var match = /(\w+)\((\d+)\)/.exec(stat);
if (match)
list.stats[index] = {
func: match[1],
count: match[2]
};
});
}
});
});
}
Config.prototype.instantiate = function(variables) {
var _this = this;
var instance = Config.parse(_this.toString());
if (variables)
variables.forEach(function(variable) {
instance.variables[variable.name] = variable.value;
});
instance.groups.forEach(function (group) {
group.counters.forEach(function (counter, index) {
counter.id = counter.id.replace(/%\w+%/gi, function(match) {
return instance.variables ? instance.variables[match.replace(/%/g, "")] || "" : "";
});
});
group.lists.forEach(function (list, index) {
list.id = list.id.replace(/%\w+%/gi, function(match) {
return instance.variables ? instance.variables[match.replace(/%/g, "")] || "" : "";
});
});
});
return instance;
};
Config.prototype.toString = function() {
return JSON.stringify(this);
};
module.exports = Config;