forked from BuidlGuidl/buidlguidl-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
117 lines (100 loc) · 3.27 KB
/
helpers.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import fs from "fs";
import os from "os";
import path from "path";
const installDir = os.homedir();
export function setupDebugLogging(debugLogPath) {
if (fs.existsSync(debugLogPath)) {
fs.unlinkSync(debugLogPath);
}
function logDebug(message) {
if (typeof message === "object") {
message = JSON.stringify(message, null, 2);
}
fs.appendFileSync(
debugLogPath,
`[${new Date().toISOString()}] ${message}\n`
);
}
console.log = function (message, ...optionalParams) {
if (optionalParams.length > 0) {
message +=
" " +
optionalParams
.map((param) =>
typeof param === "object" ? JSON.stringify(param, null, 2) : param
)
.join(" ");
}
logDebug(message);
};
}
export function debugToFile(data, callback) {
const filePath = path.join(installDir, "bgnode", "debug.log");
const now = new Date();
const timestamp = `${now.toLocaleDateString()} ${now.toLocaleTimeString()}`;
const content =
typeof data === "object"
? `${timestamp} - ${JSON.stringify(data, null, 2)}\n`
: `${timestamp} - ${data}\n`;
fs.writeFile(filePath, content, { flag: "a" }, (err) => {
if (err) {
console.error("Failed to write to file:", err);
} else {
if (callback) callback();
}
});
}
// function getFormattedDateTime() {
// const now = new Date();
// const year = now.getFullYear();
// const month = (now.getMonth() + 1).toString().padStart(2, "0");
// const day = now.getDate().toString().padStart(2, "0");
// const hour = now.getHours().toString().padStart(2, "0");
// const minute = now.getMinutes().toString().padStart(2, "0");
// const second = now.getSeconds().toString().padStart(2, "0");
// return `${year}_${month}_${day}_${hour}_${minute}_${second}`;
// }
// let lastStats = {
// totalSent: 0,
// totalReceived: 0,
// timestamp: Date.now(),
// };
// function getNetworkStats() {
// return new Promise((resolve, reject) => {
// si.networkStats()
// .then((interfaces) => {
// let currentTotalSent = 0;
// let currentTotalReceived = 0;
// interfaces.forEach((iface) => {
// currentTotalSent += iface.tx_bytes;
// currentTotalReceived += iface.rx_bytes;
// });
// // Calculate time difference in seconds
// const currentTime = Date.now();
// const timeDiff = (currentTime - lastStats.timestamp) / 1000;
// // Calculate bytes per second
// const sentPerSecond =
// (currentTotalSent - lastStats.totalSent) / timeDiff;
// const receivedPerSecond =
// (currentTotalReceived - lastStats.totalReceived) / timeDiff;
// // Update last stats for next calculation
// lastStats = {
// totalSent: currentTotalSent,
// totalReceived: currentTotalReceived,
// timestamp: currentTime,
// };
// resolve({
// sentPerSecond: sentPerSecond / 1000000,
// receivedPerSecond: receivedPerSecond / 1000000,
// });
// })
// .catch((error) => {
// debugToFile(
// `getNetworkStats() Error fetching network stats: ${error}`,
// () => {}
// );
// reject(error);
// });
// });
// }
// getNetworkStats();