-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.js
153 lines (124 loc) · 4.08 KB
/
stats.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
var ajax = {};
ajax.x = function () {
if (typeof XMLHttpRequest !== 'undefined') {
return new XMLHttpRequest();
}
var versions = [
"MSXML2.XmlHttp.6.0",
"MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
"Microsoft.XmlHttp"
];
var xhr;
for (var i = 0; i < versions.length; i++) {
try {
xhr = new ActiveXObject(versions[i]);
break;
} catch (e) {
}
}
return xhr;
};
ajax.send = function (url, callback, method, data, async) {
if (async === undefined) {
async = true;
}
var x = ajax.x();
x.open(method, url, async);
x.onreadystatechange = function () {
if (x.readyState == 4) {
callback(x.responseText)
}
};
if (method == 'POST') {
x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
}
x.send(data)
};
ajax.get = function (url, data, callback, async) {
var query = [];
for (var key in data) {
query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
}
ajax.send(url + (query.length ? '?' + query.join('&') : ''), callback, 'GET', null, async)
};
var data = {};
// Iterate over all years
var yearOptionValues= [];
document.querySelectorAll("#timePeriodForm select option").forEach(yearOption =>{
var yearOptionValue = yearOption.value;
if(!yearOptionValue.includes("year-")){
return;
}
yearOptionValues.push(yearOptionValue);
});
function fetchYear(yearIndex){
var yearOptionValue = yearOptionValues[yearIndex];
var year = parseInt(yearOptionValue.replace("year-", ""));
console.debug("Fetching year " + year + " started");
ajax.get(window.location.origin + window.location.pathname , {orderFilter: yearOptionValue}, function(data) {
var el = document.createElement( 'html' );
el.innerHTML = data;
parsePage(yearIndex, year, el);
});
}
function parsePage(yearIndex, year, rootElement, orderData){
var orderData = orderData || [];
rootElement.querySelectorAll(".a-box-group").forEach(orderElement => {
var orderDetailElements = orderElement.querySelectorAll(".a-color-secondary.value");
var returned = orderElement.textContent.includes("Rücksendung abgeschlossen");
var sDate = orderDetailElements[0].textContent.trim();
var sPriceTotal = orderDetailElements[1].textContent.trim();
var priceTotal = parseFloat(sPriceTotal.replace("EUR ","").replace(",","."))
var originalPriceTotal = 0;
orderElement.querySelectorAll(".a-color-price").forEach(e => {
var origPrice = parseFloat(e.textContent.trim().replace("EUR ","").replace(",","."));
if(origPrice) {
originalPriceTotal += origPrice;
}
});
if(orderDetailElements.length == 3){
orderData.push({
"order_date" : sDate,
"total" : priceTotal,
"original_total" : originalPriceTotal,
"returned" : returned
});
}
});
var nextPageElement = rootElement.querySelector("li.a-last a");
if(nextPageElement){
console.debug("next page");
var nextPageUrl = nextPageElement.href;
ajax.get(nextPageUrl, {}, function(data) {
var el = document.createElement( 'html' );
el.innerHTML = data;
parsePage(yearIndex, year, el, orderData );
});
}else{
console.debug("Fetching year " + year + " finished");
printStats(year, orderData);
if((yearIndex+1) < yearOptionValues.length){
fetchYear(yearIndex+1);
}else{
console.log("Finished");
}
}
}
function printStats(year, orderData){
var total = 0;
var totalReturned = 0;
var originalTotal = 0;
orderData.forEach(order =>{
if(order.returned){
totalReturned += order.total;
}else{
total += order.total;
originalTotal += order.original_total;
}
});
console.log("Year " + year + " total:" + Math.round(total) + "; original total: " + Math.round(originalTotal) + "; orders: " + orderData.length + "; returned: " + Math.round(totalReturned));
}
fetchYear(0);