forked from SkyCryptWebsite/SkyCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (72 loc) · 2.11 KB
/
index.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
import "./src/main.js";
import * as cluster from "cluster";
const developEnv = process.env?.NODE_ENV == "development";
// import * as io from '@pm2/io';
let requests = [];
let packSelect = {};
let packUsage = {};
import axiosDebugLog from "axios-debug-log";
axiosDebugLog({
request: (debug, config) => {
const requestURL = config.baseURL ? config.baseURL + config.url : config.url;
if (developEnv) {
console.log(`Sent request to ${requestURL} on ${cluster.isWorker ? "worker" + cluster.worker.id : "master"}.`);
}
if (requestURL.startsWith("https://api.hypixel.net/") && config?.params?.key) {
if (cluster.isPrimary) {
requests.push(getTime());
} else {
process.send({ type: "hypixel_request", time: getTime() });
}
}
},
});
function getTime() {
return `${Math.floor(Math.random() * 10000 + 1000)
.toString()
.substring(0, 4)}/${+new Date()}`;
}
async function init() {
setInterval(() => {
requests = requests.filter((a) => a.substring(5) > +new Date() - 60 * 1000);
// console.log(`Current API requests: ${requests.length} / min`);
}, 1000);
setInterval(() => {
// todo: do some actual output for these stats
packSelect = {};
packUsage = {};
}, 1000 * 60);
}
if (cluster.isPrimary) {
// const apiRequests = io.metric({
// name: "API Requests",
// unit: "reqs/min",
// value: () => {
// return requests.length;
// },
// });
// console.log(apiRequests);
const msgHandler = function (msg) {
if (msg?.type == null) return;
switch (msg.type) {
case "hypixel_request":
if (!msg.time) return;
requests.push(msg.time);
return;
case "selected_pack":
if (!msg.id) return;
if (packSelect[msg.id] == null) packSelect[msg.id] = 1;
else packSelect[msg.id]++;
return;
case "used_pack":
if (!msg.id) return;
if (packUsage[msg.id] == null) packUsage[msg.id] = 1;
else packUsage[msg.id]++;
return;
}
};
for (const id in cluster.workers) {
cluster.workers[id].on("message", msgHandler);
}
init();
}