-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
399 lines (359 loc) · 14.8 KB
/
index.php
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<!DOCTYPE html>
<html>
<head>
<title>Spiget Resource Stats</title>
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<style>
html, body {
height: 100%;
margin: 0;
}
.container {
width: 95vw !important;
max-width: 100% !important;
}
/* https://coderwall.com/p/hkgamw/creating-full-width-100-container-inside-fixed-width-container */
.row-full {
width: 90vw;
height: 90vh;
position: relative;
margin-left: -45vw;
margin-top: 50px;
left: 50%;
}
</style>
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<div class="container" style="height: 100%;">
<br/>
<h2>General Stats</h2>
<div class="row">
<div class="input-field col s12 m6">
<div id="review_stats_chart" style="height: 90%;">
Loading Stats...
</div>
</div>
</div>
<div class="divider"></div>
<br/>
<h2>Resource & Author Stats</h2>
<div class="row">
<div class="input-field col s12 m6">
<input placeholder="Resource IDs" id="resource_filter" type="text">
<label for="resource_filter">Resource Filter (comma-separated IDs)</label>
</div>
<div class="input-field col s12 m6">
<input placeholder="Author IDs" id="author_filter" type="text">
<label for="author_filter">Author Filter (comma-separated IDs)</label>
</div>
</div>
<div class="divider"></div>
<div class="row-full">
<div id="resource_stats_chart" style="height: 90%;">
Loading Stats...
</div>
<br/>
<div style="margin: 0 auto; text-align: center; ">
<button class="btn" id="prev-page-resource" onclick="loadPrevPage('resource')" disabled><</button>
<span id="page-info-resource">Page #1</span>
<button class="btn" id="next-page-resource" onclick="loadNextPage('resource')">></button>
</div>
</div>
<div class="divider"></div>
<div class="row-full">
<div id="resource_growth_stats_chart" style="height: 90%;">
Loading Stats...
</div>
<br/>
<div style="margin: 0 auto; text-align: center; ">
<button class="btn" id="prev-page-resource_growth" onclick="loadPrevPage('resource_growth')" disabled><</button>
<span id="page-info-resource_growth">Page #1</span>
<button class="btn" id="next-page-resource_growth" onclick="loadNextPage('resource_growth')">></button>
</div>
</div>
<br/>
<div class="divider"></div>
<br/>
<div class="row-full">
<div id="author_total_stats_chart" style="height: 90%;">
Loading Stats...
</div>
<br/>
<div style="margin: 0 auto; text-align: center; ">
<button class="btn" id="prev-page-author_total" onclick="loadPrevPage('author_total')" disabled><</button>
<span id="page-info-author_total">Page #1</span>
<button class="btn" id="next-page-author_total" onclick="loadNextPage('author_total')">></button>
</div>
</div>
<div class="divider"></div>
<div class="row-full">
<div id="author_average_stats_chart" style="height: 90%;">
Loading Stats...
</div>
<br/>
<div style="margin: 0 auto; text-align: center; ">
<button class="btn" id="prev-page-author_average" onclick="loadPrevPage('author_average')" disabled><</button>
<span id="page-info-author_average">Page #1</span>
<button class="btn" id="next-page-author_average" onclick="loadNextPage('author_average')">></button>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.0.min.js" integrity="sha256-xNzN2a4ltkB44Mc/Jz3pT4iU1cmeR0FkXs4pru/JxaQ=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script>
let currentPage = {};
let resourceFilter = "";
let authorFilter = "";
$("#resource_filter").on("keyup",()=>{
resourceFilter = $("#resource_filter").val();
}).on("change",()=>{
reloadCurrentPage("resource");
reloadCurrentPage("resource_growth");
});
$("#author_filter").on("keyup",()=>{
authorFilter = $("#author_filter").val();
}).on("change",()=>{
reloadCurrentPage("resource");
reloadCurrentPage("resource_growth");
reloadCurrentPage("author_total");
reloadCurrentPage("author_average");
});
function loadPage(type, p = 1) {
p = Math.max(1, p);
currentPage[type] = p;
$("#prev-page-" + type).attr("disabled", p <= 1)
$("#page-info-" + type).text("Page #" + p);
$("#" + type + "_stats_chart").text("Loading Stats...");
fetch("get.php?type=" + type + "&page=" + p + "&resource_filter=" + resourceFilter + "&author_filter=" + authorFilter).then(res => res.json()).then(data => {
console.log(data)
$("#next-page-" + type).attr("disabled", data.length < 10);
if (type === "resource") {
makeResourceChart(data);
}
if (type === "resource_growth") {
makeResourceGrowthChart(data);
}
if (type === "author_total") {
makeAuthorTotalChart(data);
}
if (type === "author_average") {
makeAuthorAverageChart(data);
}
})
}
function loadReviewStats() {
fetch("https://api.spiget.org/v2/reviews/trends").then(res=>res.json()).then(data=>{
Highcharts.chart('review_stats_chart', {
chart: {
type: 'pie'
},
title: {
text: 'Reviews by Rating'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
accessibility: {
point: {
valueSuffix: '%'
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %'
}
}
},
series: [{
name: 'Ratings',
colorByPoint: true,
data: [{
name: '5⭐',
y: data["5"] / data["total"]
}, {
name: '4⭐',
y: data["4"] / data["total"]
}, {
name: '3⭐',
y: data["3"] / data["total"]
}, {
name: '2⭐',
y: data["2"] / data["total"]
}, {
name: '1⭐',
y: data["1"] / data["total"]
}]
}]
});
})
}
Highcharts.setOptions({
global: {
useUTC: false
}
});
function makeResourceChart(data) {
Highcharts.chart("resource_stats_chart", {
chart: {
type: "spline"
},
title: {
text: "Resource Download Stats"
},
subtitle: {
text: "Total Download Counts over Time"
},
xAxis: {
type: "datetime",
title: {
text: "Date"
}
},
yAxis: {
title: {
text: "Downloads"
}
},
series: data
})
}
function makeResourceGrowthChart(data) {
Highcharts.chart("resource_growth_stats_chart", {
chart: {
type: "spline"
},
title: {
text: "Resource Download Growth Stats"
},
subtitle: {
text: "Fastest Growing Download Counts over Time"
},
xAxis: {
type: "datetime",
title: {
text: "Date"
}
},
yAxis: {
title: {
text: "Downloads Increase"
}
},
series: data
})
}
function makeAuthorTotalChart(data) {
resolveAuthorNamesIn(data).then(()=>{
Highcharts.chart("author_total_stats_chart", {
chart: {
type: "spline"
},
title: {
text: "Author Download Stats"
},
subtitle: {
text: "Cumulative Download Counts per Author over Time"
},
xAxis: {
type: "datetime",
title: {
text: "Date"
}
},
yAxis: {
title: {
text: "Downloads"
}
},
series: data
})})
}
function makeAuthorAverageChart(data) {
resolveAuthorNamesIn(data).then(()=>{
Highcharts.chart("author_average_stats_chart", {
chart: {
type: "spline"
},
title: {
text: "Author Download Stats"
},
subtitle: {
text: "Average Download Counts per Author per Resource (downloads divided by resource count) over Time"
},
xAxis: {
type: "datetime",
title: {
text: "Date"
}
},
yAxis: {
title: {
text: "Downloads"
}
},
series: data
})
})
}
function loadNextPage(type) {
loadPage(type, (currentPage[type] || 1) + 1);
}
function loadPrevPage(type) {
loadPage(type, (currentPage[type] || 1) - 1);
}
function loadFirstPage(type) {
loadPage(type, 1);
}
function reloadCurrentPage(type) {
loadPage(type, currentPage[type]);
}
function resolveAuthorNamesIn(series) {
return resolveAuthorNames(series).then(nameMap=>{
for (let ser of series) {
ser.authorName = nameMap["" + ser.author];
ser.name = "[#"+ ser.author+"] "+ser.authorName;
}
})
}
function resolveAuthorNames(series) {
return new Promise((resolve, reject) => {
let promises = [];
let ids = [];
for (let ser of series) {
ids.push(ser.author);
promises.push(resolveAuthorName(ser.author));
}
Promise.all(promises).then(names => {
let map = {};
for (let i = 0; i < ids.length; i++) {
map["" + ids[i]] = names[i];
}
resolve(map);
}).catch(reject)
})
}
function resolveAuthorName(id) {
return new Promise((resolve, reject) => {
fetch("https://api.spiget.org/v2/authors/" + id + "?fields=name").then(res => res.json()).then(data => {
resolve(data.name);
}).catch(reject);
})
}
loadFirstPage('resource');
loadFirstPage('resource_growth');
loadFirstPage('author_total');
loadFirstPage('author_average');
loadReviewStats();
</script>
</body>
</html>