This repository has been archived by the owner on Mar 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 355
/
queue.js
287 lines (234 loc) · 7.75 KB
/
queue.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Simplecrawler - queue module
// Christopher Giffard, 2011
//
// http://www.github.com/cgiffard/node-simplecrawler
var fs = require("fs");
var allowedStatistics = [
"requestTime",
"requestLatency",
"downloadTime",
"contentLength",
"actualDataSize"
];
var FetchQueue = function(){
this.oldestUnfetchedIndex = 0;
this.completeCache = 0;
this.scanIndex = {};
};
module.exports = FetchQueue;
FetchQueue.prototype = [];
FetchQueue.prototype.add = function(protocol,domain,port,path,callback) {
callback = callback && callback instanceof Function ? callback : function(){};
var self = this;
// Ensure all variables conform to reasonable defaults
protocol = protocol === "https" ? "https" : "http";
if (isNaN(port) || !port) {
return callback(new Error("Port must be numeric!"));
}
var url = protocol + "://" + domain + (port !== 80 ? ":" + port : "") + path;
this.exists(protocol,domain,port,path,
function(err,exists) {
if (err) return callback(err);
if (!exists) {
var queueItem = {
"url": url,
"protocol": protocol,
"host": domain,
"port": port,
"path": path,
"fetched": false,
"status": "queued",
"stateData": {}
};
self.push(queueItem);
callback(null,queueItem);
} else {
var error = new Error("Resource already exists in queue!");
error.code = "DUP";
callback(error);
}
});
};
// Check if an item already exists in the queue...
FetchQueue.prototype.exists = function(protocol,domain,port,path,callback) {
callback = callback && callback instanceof Function ? callback : function(){};
port = (port !== 80 ? ":" + port : "");
var url =
(protocol + "://" + domain + port + path)
.toLowerCase();
if (!!this.scanIndex[url]) {
callback(null,1);
return 1;
} else {
this.scanIndex[url] = true;
callback(null,0);
return 1;
}
};
// Get last item in queue...
FetchQueue.prototype.last = function(callback) {
callback = callback && callback instanceof Function ? callback : function(){};
var item, self = this;
item = self[self.length-1];
callback(null, item);
return item;
};
// Get item from queue
FetchQueue.prototype.get = function(id, callback) {
callback = callback && callback instanceof Function ? callback : function(){};
var item, self = this;
if (!isNaN(id) && self.length > id) {
item = self[id];
callback(null, item);
return item;
}
};
// Get first unfetched item in the queue (and return its index)
FetchQueue.prototype.oldestUnfetchedItem = function(callback) {
callback = callback && callback instanceof Function ? callback : function(){};
var item, self = this;
for (var itemIndex = self.oldestUnfetchedIndex; itemIndex < self.length; itemIndex ++) {
if (self[itemIndex].status === "queued") {
self.oldestUnfetchedIndex = itemIndex;
item = self[itemIndex];
callback(null, item);
return item;
}
}
callback(new Error("No unfetched items remain."));
};
// Gets the maximum total request time, request latency, or download time
FetchQueue.prototype.max = function(statisticName,callback) {
callback = callback && callback instanceof Function ? callback : function(){};
var maxStatisticValue = 0, self = this;
if (allowedStatistics.join().indexOf(statisticName) === -1) {
// Not a recognised statistic!
return callback(new Error("Invalid statistic."));
}
self.forEach(function(item) {
if (item.fetched && item.stateData[statisticName] !== null && item.stateData[statisticName] > maxStatisticValue) {
maxStatisticValue = item.stateData[statisticName];
}
});
callback(null,maxStatisticValue);
return maxStatisticValue;
};
// Gets the minimum total request time, request latency, or download time
FetchQueue.prototype.min = function(statisticName,callback) {
callback = callback && callback instanceof Function ? callback : function(){};
var minimum, minStatisticValue = Infinity, self = this;
if (allowedStatistics.join().indexOf(statisticName) === -1) {
// Not a recognised statistic!
return callback(new Error("Invalid statistic."));
}
self.forEach(function(item) {
if (item.fetched && item.stateData[statisticName] !== null && item.stateData[statisticName] < minStatisticValue) {
minStatisticValue = item.stateData[statisticName];
}
});
minimum = minStatisticValue === Infinity? 0 : minStatisticValue;
callback(null, minimum);
return minimum;
};
// Gets the minimum total request time, request latency, or download time
FetchQueue.prototype.avg = function(statisticName,callback) {
callback = callback && callback instanceof Function ? callback : function(){};
var average, NumberSum = 0, NumberCount = 0, self = this;
if (allowedStatistics.join().indexOf(statisticName) === -1) {
// Not a recognised statistic!
return callback(new Error("Invalid statistic."));
}
self.forEach(function(item) {
if (item.fetched && item.stateData[statisticName] !== null && !isNaN(item.stateData[statisticName])) {
NumberSum += item.stateData[statisticName];
NumberCount ++;
}
});
average = NumberSum / NumberCount;
callback(null, average);
return average;
};
// Gets the number of requests which have been completed.
FetchQueue.prototype.complete = function(callback) {
callback = callback && callback instanceof Function ? callback : function(){};
var NumberComplete = 0, self = this;
self.forEach(function(item) {
if (item.fetched) {
NumberComplete ++;
}
});
callback(null,NumberComplete);
return NumberComplete;
};
// Gets the number of queue items with the given status
FetchQueue.prototype.countWithStatus = function(status,callback) {
callback = callback && callback instanceof Function ? callback : function(){};
var queueItemsMatched = 0, self = this;
self.forEach(function(item) {
if (item.status === status) {
queueItemsMatched ++;
}
});
callback(null,queueItemsMatched);
return queueItemsMatched;
};
// Gets the number of queue items with the given status
FetchQueue.prototype.getWithStatus = function(status,callback) {
callback = callback && callback instanceof Function ? callback : function(){};
var subqueue = [], self = this;
self.forEach(function(item,index) {
if (item.status === status) {
subqueue.push(item);
subqueue[subqueue.length-1].queueIndex = index;
}
});
callback(null,subqueue);
return subqueue;
};
// Gets the number of requests which have failed for some reason
FetchQueue.prototype.errors = function(callback) {
callback = callback && callback instanceof Function ? callback : function(){};
var total, failedCount, notFoundCount, self = this;
failedCount = self.countWithStatus("failed");
notFoundCount = self.countWithStatus("notfound");
total = failedCount + notFoundCount;
callback(null, total);
return total;
};
// Writes the queue to disk
FetchQueue.prototype.freeze = function(filename,callback) {
callback = callback && callback instanceof Function ? callback : function(){};
var self = this;
// Re-queue in-progress items before freezing...
self.forEach(function(item) {
if (item.fetched !== true) {
item.status = "queued";
}
});
fs.writeFile(filename,JSON.stringify(self),function(err) {
callback(err,self);
});
};
// Reads the queue from disk
FetchQueue.prototype.defrost = function(filename,callback) {
callback = callback && callback instanceof Function ? callback : function(){};
var fileData, self = this, defrostedQueue = [];
fs.readFile(filename,function(err,fileData) {
if (err) return callback(err);
if (!fileData.toString("utf8").length) {
return callback(new Error("Failed to defrost queue from zero-length JSON."));
}
try {
defrostedQueue = JSON.parse(fileData.toString("utf8"));
} catch(error) {
return callback(error);
}
for (var index in defrostedQueue) {
if (defrostedQueue.hasOwnProperty(index) && !isNaN(index)) {
var queueItem = defrostedQueue[index];
self.push(queueItem);
}
}
callback(null,self);
});
};