forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result_store.js
231 lines (196 loc) · 6.74 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// results.js - programmatic handling of plugin results
'use strict';
var config = require('./config');
var util = require('util');
// 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, list, search) {
var name = this.resolve_plugin_name(plugin);
var result = this.store[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];
switch (typeof search) {
case 'string':
case 'number':
case 'boolean':
if (search === item) return true;
break;
case 'object':
if (item.match(search)) return true;
break;
}
}
}
return false;
};
ResultStore.prototype.redis_publish = function (name, obj) {
if (!this.conn.server || !this.conn.server.notes) return;
if (!this.conn.server.notes.redis) return;
var channel = 'result-' +
(this.conn.transaction ?
this.conn.transaction.uuid :
this.conn.uuid);
this.conn.server.notes.redis.publish(channel,
JSON.stringify({ plugin: name, result: obj }));
};
ResultStore.prototype.add = function (plugin, obj) {
var name = this.resolve_plugin_name(plugin);
var result = this.store[name];
if (!result) {
result = default_result();
this.store[name] = result;
}
this.redis_publish(name, obj);
// 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;
if (Array.isArray(obj[key])) {
result[key] = result[key].concat(obj[key]);
}
else {
result[key].push(obj[key]);
}
}
// these arrays are overwritten when passed
for (var j=0; j < overwrite_lists.length; j++) {
key = overwrite_lists[j];
if (!obj[key]) continue;
result[key] = obj[key];
}
// anything else is an arbitrary key/val to store
for (key in obj) {
if (all_opts.indexOf(key) !== -1) continue; // weed out our keys
result[key] = obj[key]; // save the rest
}
return this._log(plugin, result, obj);
};
ResultStore.prototype.incr = function (plugin, obj) {
var name = this.resolve_plugin_name(plugin);
var result = this.store[name];
if (!result) {
result = default_result();
this.store[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 name = this.resolve_plugin_name(plugin);
var result = this.store[name];
if (!result) {
result = default_result();
this.store[name] = result;
}
this.redis_publish(name, obj);
for (var key in obj) {
if (!result[key]) result[key] = [];
if (Array.isArray(obj[key])) {
result[key] = result[key].concat(obj[key]);
}
else {
result[key].push(obj[key]);
}
}
return this._log(plugin, result, obj);
};
ResultStore.prototype.collate = function (plugin) {
var name = this.resolve_plugin_name(plugin);
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.resolve_plugin_name = function (thing) {
if (!thing) { return; }
if (typeof thing === 'string') { return thing; }
return thing.name;
};
ResultStore.prototype.get_all = function () {
return this.store;
};
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 (key[0] === '_') continue; // ignore 'private' keys
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;
if (typeof result[key] === 'object') 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;
};
ResultStore.prototype._log = function (plugin, result, obj) {
var name = plugin.name;
// 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) {
// Handle error objects by logging the message
if (util.isError(obj.err)) {
this.conn.logerror(plugin, obj.err.message);
}
else {
this.conn.logerror(plugin, obj.err);
}
}
if (!obj.emit && !obj.err) { // by config
var pic = cfg[name];
if (pic && pic.debug) this.conn.logdebug(plugin, result.human);
}
return this.human;
};
module.exports = ResultStore;