UNPKG

2.16 kBJavaScriptView Raw
1/*jshint node:true, laxcomma:true */
2
3/**
4 * delete_stats - delete all matching statistics
5 *
6 * Side effect notes: this function works by altering stats_type in place,
7 * and calls stream.write(str) to display user feedback.
8 *
9 * @param stats_type array of all statistics of this type (eg~ timers) to delete from
10 * @param cmdline array of all requested deletions, which can be fully qualified,
11 * or end in a .* to delete a folder, like stats.temp.*
12 * @param stream buffer output for for all outgoing user feedback
13 */
14exports.delete_stats = function(stats_type, cmdline, stream) {
15
16 //for each metric requested on the command line
17 for (const index in cmdline) {
18
19 //get a list of deletable metrics that match the request
20 deletable = existing_stats(stats_type, cmdline[index]);
21
22 //warn if no matches
23 if (deletable.length === 0) {
24 stream.write("metric " + cmdline[index] + " not found\n");
25 }
26
27 //delete all requested metrics
28 for (const del_idx in deletable) {
29 delete stats_type[deletable[del_idx]];
30 stream.write("deleted: " + deletable[del_idx] + "\n");
31 }
32 }
33 stream.write("END\n\n");
34};
35
36/**
37 * existing_stats - find fully qualified matches for the requested stats bucket
38 *
39 * @param stats_type array of all statistics of this type (eg~ timers) to match
40 * @param bucket string to search on, which can be fully qualified,
41 * or end in a .* to search for a folder, like stats.temp.*
42 *
43 * @return array of fully qualified stats that match the specified bucket. if
44 * no matches, an empty array is a valid response
45 */
46function existing_stats(stats_type, bucket){
47 matches = [];
48
49 //typical case: one-off, fully qualified
50 if (bucket in stats_type) {
51 matches.push(bucket);
52 }
53
54 //special case: match a whole 'folder' (and subfolders) of stats
55 if (bucket.slice(-2) == ".*") {
56 const folder = bucket.slice(0,-1);
57
58 for (const name in stats_type) {
59 //check if stat is in bucket, ie~ name starts with folder
60 if (name.substring(0, folder.length) == folder) {
61 matches.push(name);
62 }
63 }
64 }
65
66 return matches;
67}
68
69exports.existing_stats = existing_stats;