unpkg/scripts/show-stats.js

59 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-02-18 02:00:56 +00:00
const subDays = require("date-fns/sub_days");
const prettyBytes = require("pretty-bytes");
const table = require("text-table");
2017-05-25 22:27:42 +00:00
2018-02-18 02:00:56 +00:00
const StatsAPI = require("../server/StatsAPI");
const now = new Date();
2017-05-25 22:27:42 +00:00
2017-11-08 19:07:48 +00:00
function createRange(start, end) {
2018-02-18 02:00:56 +00:00
const range = [];
while (start < end) range.push(start++);
return range;
2017-05-25 22:27:42 +00:00
}
2017-11-08 19:07:48 +00:00
function createPastDays(n) {
return createRange(1, n + 1)
.map(days => subDays(now, days))
2018-02-18 02:00:56 +00:00
.reverse();
2017-11-08 19:07:48 +00:00
}
2017-05-25 22:27:42 +00:00
2018-02-18 02:00:56 +00:00
const pastSevenDays = createPastDays(7);
const pastThirtyDays = createPastDays(30);
2017-05-25 22:27:42 +00:00
Promise.all([
2018-02-18 02:00:56 +00:00
StatsAPI.sumKeys(
pastSevenDays.map(date => `stats-requests-${StatsAPI.createDayKey(date)}`)
),
StatsAPI.sumKeys(
pastSevenDays.map(date => `stats-bandwidth-${StatsAPI.createDayKey(date)}`)
),
StatsAPI.sumKeys(
pastThirtyDays.map(date => `stats-requests-${StatsAPI.createDayKey(date)}`)
),
StatsAPI.sumKeys(
pastThirtyDays.map(date => `stats-bandwidth-${StatsAPI.createDayKey(date)}`)
)
]).then(results => {
2018-02-18 02:00:56 +00:00
console.log("\n## Summary");
console.log("Requests this week: %s", results[0].toLocaleString());
console.log("Bandwidth this week: %s", prettyBytes(results[1]));
console.log("Requests this month: %s", results[2].toLocaleString());
console.log("Bandwidth this month: %s", prettyBytes(results[3]));
2017-11-08 19:07:48 +00:00
StatsAPI.sumTopScores(
2018-02-18 02:00:56 +00:00
pastSevenDays.map(
date => `stats-packageRequests-${StatsAPI.createDayKey(date)}`
)
2017-11-08 19:07:48 +00:00
).then(topPackages => {
2018-02-18 02:00:56 +00:00
console.log("\n## Top Packages This Week");
2017-05-30 16:20:45 +00:00
topPackages.forEach(result => {
2018-02-18 02:00:56 +00:00
result[1] = result[1].toLocaleString();
});
2018-02-18 02:00:56 +00:00
console.log(table(topPackages));
2018-02-18 02:00:56 +00:00
process.exit();
});
});