-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker.js
More file actions
198 lines (161 loc) · 6.92 KB
/
Copy pathworker.js
File metadata and controls
198 lines (161 loc) · 6.92 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
188
189
190
191
192
193
194
195
196
197
198
var readline = require('readline');
var spawn = require('child_process').spawn;
const days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var Worker = {
// Spawn a jq proccess that will filter the stream by product name and type
// and pipe the output stream to an awk filter to get the first occurence
// of every device launch for this particular product. Count the lines.
countFirstLaunches:function(filePath,product,callback)
{
console.log("calculating first time launches of product...");
console.time("EXEC TIME (first time launches)");
var launches = 0;
var jq = spawn('jq',['{type,source,device_id: .device.device_id}| \
select(.type == "launch")|select(.source=="'+product+'")|tostring',
filePath,'--raw-output']);
var awk = spawn("awk",["!x[$0]++"]);
var stream = readline.createInterface({input : awk.stdout});
jq.stdout.pipe(awk.stdin);
stream.on('line', function(line) {
launches++;
});
stream.on('close',function() {
console.timeEnd("EXEC TIME (first time launches)")
callback(launches);
});
},
// Spawn a jq proccess that will filter the file by product name and type
// and pipe the output stream to an awk filter to get the first occurence
// of every event_id thus filtering out duplicate events. Count the lines.
//
// NOTE: I have asumed that duplicates should not be included while
// counting the launches. If this is not an issue skip the awk filter.
countLanches:function(filePath,product,callback)
{
var launches = 0;
console.log("calculating total launches of product...");
console.time("EXEC TIME (total launches)");
var jq = spawn('jq',['{event_id,type,source}| \
select(.type == "launch")|select(.source=="'+product+'")|tostring',
filePath,'--raw-output']);
var awk = spawn("awk",["!x[$0]++"]);
jq.stdout.pipe(awk.stdin);
var stream = readline.createInterface({input : awk.stdout});
stream.on('line', function(line) {
launches++;
});
stream.on('close',function() {
console.timeEnd("EXEC TIME (total launches)")
callback(launches);
});
},
// Spawn a jq proccess that will stream the event ids only and apply
// awk filter that counts the will outputs only the repeated ids.
countDuplicates:function(filePath,callback)
{
var duplicates = 0;
console.log("calculating total duplicates...");
console.time("EXEC TIME (total duplicates)");
var jq = spawn('jq',['{event_id}|tostring',filePath,'--raw-output']);
var awk = spawn("awk",["x[$0]++>=1"]);
jq.stdout.pipe(awk.stdin);
var stream = readline.createInterface({input : awk.stdout});
stream.on('line', function(line) {
duplicates++;
});
stream.on('close',function() {
console.timeEnd("EXEC TIME (total duplicates)")
callback(duplicates);
});
},
// Spawn jq proccess that will parse only the time feild. For every
// timestamp, transform it to a local day of week and hour recording the
// occurence of an event in that hour in that day of the week. Returns the
// least hour/day with events count in a 3 days window.
getBestMaintenanceTime:function(filePath,callback)
{
console.log("finding best time for maintenance...");
console.time("EXEC TIME (time)");
var data = [];
for(var i=0;i<7;i++){
data[i] = new Array(24)
for(var j= 0;j<24;j++)
data[i][j]=0;
}
var jq = spawn('jq',['{time}|tostring',filePath,'--raw-output']);
var stream = readline.createInterface({ input: jq.stdout });
stream.on('line', function(line) {
var time = new Date(JSON.parse(line).time.create_timestamp);
data[time.getDay()][time.getHours()]++;
});
stream.on('close',function() {
console.timeEnd("EXEC TIME (time)")
var hr = 0;
var day = null;
var mini = Infinity;
for(i in data){
for(j in data[i]){
if(data[i][j]+data[i][j+1]+data[i][j-1] < mini){
hr = j;
day = i;
mini = (data[i][j]+data[i][j+1]+data[i][j-1]);
}
}
}
callback({day:days[day],hour:hr,mini:mini});
});
},
// Stream the file from begining to the end saving the timestamp of the
// first occurence of every device. Also, stream the file from end to
// begining saving the timestamp of the first occurence (which is the
// last event) of every device. Subtract the start time and end time
// of every device and return the device id with tha biggest diffrence
// which is the longest active time of all devices.
findLongestActivityDevice:function(filePath,callback)
{
var devices = [];
var done = false;
var jqQuery = '{device_id:.device.device_id,time:\
.time.create_timestamp}|tostring';
var awkQuery = '!x[substr($0,14,44)]++';
var cat = spawn('cat',[filePath])
var tac = spawn('tac',[filePath])
console.log("finding longest activity device...");
console.time("EXEC TIME (longest activity device)");
process(cat,devices,'startTime');
process(tac,devices,'endTime');
function process(proc,devices,att) {
var jq = spawn('jq',[jqQuery,'--raw-output']);
var awk = spawn("awk",[awkQuery]);
proc.stdout.pipe(jq.stdin);
jq.stdout.pipe(awk.stdin);
var stream = readline.createInterface({ input : awk.stdout });
stream.on('line', function(line) {
var json = JSON.parse(line);
if(json.device_id in devices)
devices[json.device_id][att] = parseInt(json.time);
else
devices[json.device_id] = {[att]:parseInt(json.time)}
});
stream.on('close',function() {
if(done==true)
callback(calculate());
else
done = true;
});
}
function calculate(){
var max = 0;
var device;
for(var i in devices){
if(devices[i].endTime - devices[i].startTime > max) {
device = i;
max = devices[i].endTime - devices[i].startTime;
}
}
console.timeEnd("EXEC TIME (longest activity device)")
return {device:device,time:max};
}
}
}
module.exports = Worker;