-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpku.js
53 lines (46 loc) · 1.43 KB
/
pku.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
const cname = require("./utils").cname;
const MAP = {
done: "S",
sync: "Y",
error: "F",
};
const statusConverter = function(item) {
const c = MAP[item.state] ?? "U";
const last = Math.round(new Date(item.lastSyncTime).getTime()/1000).toString();
const next = Math.round(new Date(item.nextSyncTime).getTime()/1000).toString();
if (c == "S")
return c + last + "X" + next;
else
return c + "O" + last + "X" + next;
};
const human = function(size) {
const scale = ["MiB", "GiB", "TiB", "PiB"];
let i = 0;
while (size > 1024) {
size /= 1024;
i += 1;
}
return size.toFixed(2) + scale[i];
}
module.exports = async function (siteUrl) {
const name_func = await cname();
const site = await (await fetch(siteUrl)).json();
const disk = await (await fetch("https://mirrors.pku.edu.cn/monitor_device_status/disk_space.json")).json();
site.disk = human(disk.result.data.slice(-1)[0][1]) + "/" + human(disk.result.data.slice(-1)[0][1] + disk.result.data.slice(-1)[0][2]);
const stat = await (await fetch("https://mirrors.pku.edu.cn/monitor/status")).json();
const mirrors = stat.map((item) => {
const mirror = {
cname: name_func(item.id),
url: "/" + item.id,
status: statusConverter(item),
}
if ("diskUsage" in item && item.diskUsage !== "")
mirror["size"] = item.diskUsage;
return mirror;
}).filter((e) => e !== null);
return {
site,
info: [],
mirrors,
}
};