forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_store.js
164 lines (137 loc) · 4.93 KB
/
result_store.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
156
157
158
159
160
161
162
163
164
// results.js - programmatic handling of plugin results
'use strict';
var config = require('./config');
// see docs in docs/Results.md
var append_lists = ['msg','pass','fail','skip','err'];
var overwrite_lists = ['hide','order'];
var log_opts = ['emit','human','human_html'];
var all_opts = append_lists.concat(overwrite_lists, log_opts);
var cfg;
function ResultStore(conn) {
this.conn = conn;
this.store = {};
cfg = config.get('results.ini');
}
function default_result () {
return { pass: [], fail: [], msg: [], err: [], skip: [] };
}
ResultStore.prototype.has = function (plugin_name, list, search) {
var result = this.store[plugin_name];
if (!result) return false;
if (!result[list]) return false;
if (typeof result[list] === 'string') {
if (typeof search === 'string' && search === result[list]) return true;
if (typeof search === 'object' && result[list].match(search)) return true;
return false;
}
if (Array.isArray(result[list])) {
for (var i=0; i<result[list].length; i++) {
var item = result[list][i];
if (typeof search === 'string' && search === item) return true;
if (typeof search === 'object' && item.match(search)) return true;
}
}
return false;
};
ResultStore.prototype.add = function (plugin, obj) {
var name = plugin.name;
var result = this.store[name];
if (!result) {
result = default_result();
this.store[name] = result;
}
// these are arrays each invocation appends to
for (var i=0; i < append_lists.length; i++) {
var key = append_lists[i];
if (!obj[key]) continue;
result[key].push(obj[key]);
}
// these arrays are overwritten when passed
for (var j=0; j < overwrite_lists.length; j++) {
var key = overwrite_lists[j];
if (!obj[key]) continue;
result[key] = obj[key];
}
// anything else is an arbitrary key/val to store
for (var key in obj) {
if (all_opts.indexOf(key) !== -1) continue; // weed out our keys
result[key] = obj[key]; // save the rest
}
// collate results
result.human = obj.human;
if (!result.human) {
var r = this.private_collate(result, name);
result.human = r.join(', ');
result.human_html = r.join(', \t ');
}
// logging results
if (obj.emit) this.conn.loginfo(plugin, result.human); // by request
if (obj.err) this.conn.logerror(plugin, obj.err); // by default
if (!obj.emit && !obj.err) { // by config
var pic = cfg[name];
if (pic && pic.debug) this.conn.logdebug(plugin, result.human);
}
return this.human;
};
ResultStore.prototype.incr = function (plugin, obj) {
var result = this.store[plugin.name];
if (!result) {
result = default_result();
this.store[plugin.name] = result;
}
for (var key in obj) {
var val = parseFloat(obj[key]) || 0;
if (isNaN(val)) val = 0;
if (isNaN(result[key])) result[key] = 0;
result[key] = parseFloat(result[key]) + parseFloat(val);
}
};
ResultStore.prototype.push = function (plugin, obj) {
var result = this.store[plugin.name];
if (!result) {
result = default_result();
this.store[plugin.name] = result;
}
for (var key in obj) {
if (!result[key]) result[key] = [];
result[key].push( obj[key] );
}
};
ResultStore.prototype.collate = function (plugin) {
var name = plugin.name;
var result = this.store[name];
if (!result) return;
return this.private_collate(result, name).join(', ');
};
ResultStore.prototype.get = function (plugin_name) {
var result = this.store[plugin_name];
if (!result) return;
return result;
};
ResultStore.prototype.private_collate = function (result, name) {
var r = [], order = [], hide = [];
if (cfg[name]) {
if (cfg[name].hide) hide = cfg[name].hide.trim().split(/[,; ]+/);
if (cfg[name].order) order = cfg[name].order.trim().split(/[,; ]+/);
}
// anything not predefined in the result was purposeful, show it first
for (var key in result) {
if (all_opts.indexOf(key) !== -1) continue;
if (hide.length && hide.indexOf(key) !== -1) continue;
if (Array.isArray(result[key]) && result[key].length === 0) continue;
r.push(key + ': ' + result[key]);
}
// and then supporting information
var array = append_lists; // default
if (order && order.length) array = order; // config file
if (result.order && result.order.length) array = result.order; // caller
for (var i=0; i < array.length; i++) {
key = array[i];
if (!result[key]) continue;
if (!result[key].length) continue;
if (hide && hide.length && hide.indexOf(key) !== -1) continue;
r.push( key + ':' + result[key].join(', '));
}
return r;
};
module.exports = ResultStore;