forked from philipmulcahy/azad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_scheduler.js
251 lines (234 loc) · 10 KB
/
request_scheduler.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
/* Copyright(c) 2016 Philip Mulcahy. */
/* jshint strict: true, esversion: 6 */
// Uses code from http://eloquentjavascript.net/1st_edition/appendix2.html
const amazon_order_history_request_scheduler = (function() {
'use strict';
class BinaryHeap {
constructor(scoreFunction) {
this.content = [];
this.scoreFunction = scoreFunction;
}
push(element) {
// Add the new element to the end of the array.
this.content.push(element);
// Allow it to bubble up.
this.bubbleUp(this.content.length - 1);
}
pop() {
// Store the first element so we can return it later.
const result = this.content[0];
// Get the element at the end of the array.
const end = this.content.pop();
// If there are any elements left, put the end element at the
// start, and let it sink down.
if (this.content.length > 0) {
this.content[0] = end;
this.sinkDown(0);
}
return result;
}
remove(node) {
const length = this.content.length;
// To remove a value, we must search through the array to find
// it.
for (let i = 0; i < length; i++) {
if (this.content[i] != node) continue;
// When it is found, the process seen in 'pop' is repeated
// to fill up the hole.
const end = this.content.pop();
// If the element we popped was the one we needed to remove,
// we're done.
if (i == length - 1) break;
// Otherwise, we replace the removed element with the popped
// one, and allow it to float up or sink down as appropriate.
this.content[i] = end;
this.bubbleUp(i);
this.sinkDown(i);
break;
}
}
size() {
return this.content.length;
}
bubbleUp(n) {
// Fetch the element that has to be moved.
const element = this.content[n], score = this.scoreFunction(element);
// When at 0, an element can not go up any further.
while (n > 0) {
// Compute the parent element's index, and fetch it.
const parentN = Math.floor((n + 1) / 2) - 1,
parent = this.content[parentN];
// If the parent has a lesser score, things are in order and we
// are done.
if (score >= this.scoreFunction(parent))
break;
// Otherwise, swap the parent with the current element and
// continue.
this.content[parentN] = element;
this.content[n] = parent;
n = parentN;
}
}
sinkDown(n) {
// Look up the target element and its score.
const length = this.content.length;
const element = this.content[n];
const elemScore = this.scoreFunction(element);
while(true) {
// Compute the indices of the child elements.
const child2N = (n + 1) * 2;
const child1N = child2N - 1;
// This is used to store the new position of the element,
// if any.
let swap = null;
let child1Score = null;
// If the first child exists (is inside the array)...
if (child1N < length) {
// Look it up and compute its score.
const child1 = this.content[child1N];
child1Score = this.scoreFunction(child1);
// If the score is less than our element's, we need to swap.
if (child1Score < elemScore)
swap = child1N;
}
// Do the same checks for the other child.
if (child2N < length) {
const child2 = this.content[child2N];
const child2Score = this.scoreFunction(child2);
if (child2Score < (swap === null ? elemScore : child1Score))
swap = child2N;
}
// No need to swap further, we are done.
if (swap === null) break;
// Otherwise, swap and continue.
this.content[n] = this.content[swap];
this.content[swap] = element;
n = swap;
}
}
}
class RequestScheduler {
constructor() {
// chrome allows 6 requests per domain at the same time.
this.CONCURRENCY = 6; // Chrome allows 6 connections per server.
this.cache = cachestuff.createLocalCache('REQUESTSCHEDULER');
this.queue = new BinaryHeap( item => item.priority );
this.running_count = 0;
this.completed_count = 0;
this.error_count = 0;
this.signin_warned = false;
this.execute = function(query, event_converter, callback, priority) {
console.log(
'Executing ' + query +
' with queue size ' + this.queue.size() +
' and priority ' + priority
);
const req = new XMLHttpRequest();
req.open('GET', query, true);
req.onerror = function() {
this.running_count -= 1;
this.error_count += 1;
console.log(
'Unknown error fetching ' + query);
};
req.onload = function(evt) {
this.running_count -= 1;
if ( req.status != 200 ) {
this.error_count += 1;
console.log(
'Got HTTP' + req.status + ' fetching ' + query);
return;
}
if ( req.responseURL.includes('/ap/signin?') ) {
this.error_count += 1;
console.log('Got sign-in redirect from: ' + query);
if ( !this.signin_warned ) {
alert('Amazon Order History Reporter Chrome Extension\n\n' +
'It looks like you might have been logged out of Amazon.\n' +
'Sometimes this can be "partial" - some types of order info stay logged in and some do not.\n' +
'I will now attempt to open a new tab with a login prompt. Please use it to login,\n' +
'and then retry your chosen orange button.');
this.signin_warned = true;
chrome.runtime.sendMessage(
{
action: 'open_tab',
url: query
}
);
}
return;
}
this.completed_count += 1;
console.log(
'Finished ' + query +
' with queue size ' + this.queue.size());
while (this.running_count < this.CONCURRENCY &&
this.queue.size() > 0
) {
const task = this.queue.pop();
this.execute(
task.query,
task.event_converter,
task.callback,
task.priority
);
}
const converted = event_converter(evt);
this.cache.set(query, converted);
callback(converted);
}.bind(this);
this.running_count += 1;
req.send();
this.updateProgress();
};
this.statistics = function() {
return {
'queued' : this.queue.size(),
'running' : this.running_count,
'completed' : this.completed_count,
'errors' : this.error_count,
'cache_hits' : this.cache.hitCount(),
};
};
this.updateProgress = function() {
const target = document.getElementById('order_reporter_progress');
if (target !== null) {
target.textContent = Object.entries(this.statistics())
.map(([k,v]) => {return k + ':' + v;})
.join('; ');
}
setTimeout(function() { this.updateProgress(); }.bind(this), 2000);
};
this.updateProgress();
}
schedule(query, event_converter, callback, priority, nocache) {
const cached_response = nocache ?
undefined :
this.cache.get(query);
if (cached_response !== undefined) {
console.log('Already had ' + query + ' with ' + this.queue.size());
callback(cached_response);
} else {
console.log('Scheduling ' + query + ' with ' + this.queue.size());
if (this.running_count < this.CONCURRENCY) {
this.execute(query, event_converter, callback, priority);
} else {
this.queue.push({
'query': query,
'event_converter': event_converter,
'callback': callback,
'priority': priority,
});
}
}
}
clearCache() {
this.cache.clear();
}
}
return {
create: function() {
return new RequestScheduler();
}
};
})();