forked from simplecrawler/simplecrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
183 lines (146 loc) · 4.3 KB
/
Copy pathqueue.js
File metadata and controls
183 lines (146 loc) · 4.3 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
// 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(){};
FetchQueue.prototype = [];
FetchQueue.prototype.add = function(protocol,domain,port,path) {
// Ensure all variables conform to reasonable defaults
protocol = protocol === "https" ? "https" : "http";
if (isNaN(port)) {
throw Error("Port must be numeric!");
}
var url = protocol + "://" + domain + (port !== 80 ? ":" + port : "") + path;
if (!this.reduce(function(prev, current, index, array) {
return prev || String(current.url).toLowerCase() === String(url).toLowerCase();
},false)) {
this.push({
"url": url,
"protocol": protocol,
"domain": domain,
"port": port,
"path": path,
"fetched": false,
"status": "queued",
"stateData": {}
});
return true;
} else {
return false;
}
};
// Gets the maximum total request time, request latency, or download time
FetchQueue.prototype.max = function(statisticName) {
var maxStatisticValue = 0;
if (allowedStatistics.join().indexOf(statisticName) === -1) {
// Not a recognised statistic!
return false;
}
this.forEach(function(item) {
if (item.fetched && item.stateData[statisticName] !== null && item.stateData[statisticName] > maxStatisticValue) {
maxStatisticValue = item.stateData[statisticName];
}
});
return maxStatisticValue;
};
// Gets the minimum total request time, request latency, or download time
FetchQueue.prototype.min = function(statisticName) {
var minStatisticValue = Infinity;
if (allowedStatistics.join().indexOf(statisticName) === -1) {
// Not a recognised statistic!
return false;
}
this.forEach(function(item) {
if (item.fetched && item.stateData[statisticName] !== null && item.stateData[statisticName] < minStatisticValue) {
minStatisticValue = item.stateData[statisticName];
}
});
return minStatisticValue === Infinity? 0 : minStatisticValue;
};
// Gets the minimum total request time, request latency, or download time
FetchQueue.prototype.avg = function(statisticName) {
var NumberSum = 0, NumberCount = 0;
if (allowedStatistics.join().indexOf(statisticName) === -1) {
// Not a recognised statistic!
return false;
}
this.forEach(function(item) {
if (item.fetched && item.stateData[statisticName] !== null && !isNaN(item.stateData[statisticName])) {
NumberSum += item.stateData[statisticName];
NumberCount ++;
}
});
return NumberSum / NumberCount;
};
// Gets the number of requests which have been completed.
FetchQueue.prototype.complete = function() {
var NumberComplete = 0;
this.forEach(function(item) {
if (item.fetched) {
NumberComplete ++;
}
});
return NumberComplete;
};
// Gets the number of queue items with the given status
FetchQueue.prototype.countWithStatus = function(status) {
var queueItemsMatched = 0;
this.forEach(function(item) {
if (item.status === status) {
queueItemsMatched ++;
}
});
return queueItemsMatched;
};
// Gets the number of queue items with the given status
FetchQueue.prototype.getWithStatus = function(status) {
var subqueue = [];
this.forEach(function(item,index) {
if (item.status === status) {
subqueue.push(item);
subqueue[subqueue.length-1].queueIndex = index;
}
});
return subqueue;
};
// Gets the number of requests which have failed for some reason
FetchQueue.prototype.errors = function() {
return this.countWithStatus("failed") + this.countWithStatus("notfound");
};
// Writes the queue to disk
FetchQueue.prototype.freeze = function(filename) {
// Re-queue items before freezing...
this.forEach(function(item) {
if (item.fetched !== true) {
item.status = "queued";
}
});
fs.writeFileSync(filename,JSON.stringify(this));
};
// Reads the queue from disk
FetchQueue.prototype.defrost = function(filename) {
var fileData;
try {
if ((fileData = fs.readFileSync(filename))) {
var defrostedQueue = JSON.parse(fileData.toString("utf8"));
for (var index in defrostedQueue) {
if (defrostedQueue.hasOwnProperty(index) && !isNaN(index)) {
var queueItem = defrostedQueue[index];
this.push(queueItem);
}
}
}
return true;
} catch(error) {
return false;
}
};
exports.queue = FetchQueue;