-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbwh_monitor_mini.js
100 lines (73 loc) · 2.61 KB
/
bwh_monitor_mini.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
let params = getArgs();
const url = `https://api.64clouds.com/v1/getLiveServiceInfo?veid=${params.veid}&api_key=${params.apikey}`;
function getArgs() {
return Object.fromEntries(
args.widgetParameter
.split("&")
.map((item) => item.split("="))
.map(([k, v]) => [k, decodeURIComponent(v)])
);
}
function formatTime(time) {
if (time < 1000000000000) time *= 1000;
let dateObj = new Date(time);
let year = dateObj.getFullYear();
let month = dateObj.getMonth() + 1;
let day = dateObj.getDate();
return `${year}/${String(month).padStart(2, '0')}/${String(day).padStart(2, '0')}`;
}
function bytesToSize(bytes) {
if (bytes === 0) return "0B";
let k = 1024;
let sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
let i = Math.floor(Math.log(bytes) / Math.log(k));
return Math.round((bytes / Math.pow(k, i))) + sizes[i];
}
function getResetDaysLeft(reset) {
if (!reset) return;
let now = new Date().getTime();
let resetTime;
if (/^[\d.]+$/.test(reset)) {
resetTime = parseInt(reset) * 1000;
} else {
resetTime = new Date(reset).getTime();
}
let daysLeft = Math.ceil((resetTime - now) / (1000 * 60 * 60 * 24));
return daysLeft > 0 ? daysLeft : null;
}
let widget = new ListWidget();
let req = new Request(url);
try {
let jsonData = await req.loadJSON();
let used = jsonData.data_counter;
let total = jsonData.plan_monthly_data;
let resetday = jsonData.data_next_reset;
let useddisk = jsonData.ve_used_disk_space_b;
let totaldisk = jsonData.plan_disk;
let usedram = jsonData.plan_ram - jsonData.mem_available_kb * 1024
let totalram = jsonData.plan_ram
let node_location_id = jsonData.node_location_id
let hostname = jsonData.live_hostname
let ip_addr = jsonData.ip_addresses[0]
let title = widget.addText(params.title ? `${params.title}` : `📦 ${hostname}`)
title.font = Font.boldRoundedSystemFont(16)
widget.addSpacer()
let content = [`📌 ${node_location_id}`];
content.push(`🖥 ${ip_addr}`);
content.push(`🪫 ${bytesToSize(usedram)}/${bytesToSize(totalram)}`);
content.push(`💿 ${bytesToSize(useddisk)}/${bytesToSize(totaldisk)}`);
content.push(`⏳ ${bytesToSize(used)}/${bytesToSize(total)}`);
content.push(`⏱️ ${formatTime(resetday)}(${getResetDaysLeft(resetday)}d)`);
let description = widget.addText(content.join("\n"));
description.font = Font.mediumSystemFont(12)
widget.presentSmall()
} catch (error) {
console.error("Error fetching data:", error);
widget.addText("Error fetching data");
}
if (config.runsInWidget) {
Script.setWidget(widget);
} else {
widget.presentMedium();
}
Script.complete();