Skip to content

Commit

Permalink
Removed feature: memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
kwaitsing committed Oct 27, 2024
1 parent e0fa7c1 commit 1719a6a
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 10 deletions.
1 change: 1 addition & 0 deletions client/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ web_modules/
# dotenv environment variable files

.env
bun.lockb
.env.development.local
.env.test.local
.env.production.local
Expand Down
Binary file modified client/bun.lockb
Binary file not shown.
5 changes: 3 additions & 2 deletions client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { GlobalConf, oneTimeDataType } from "./src/type"
import { startTimer } from "./src/dataUpdater"
import { transmitter } from "./src/socket"

const env = Bun.env
const args = require('minimist')(Bun.argv)
const env = process.env
const args = require('minimist')(process.argv)

export const GlobalConfiguration: GlobalConf = {
remote: env.REMOTE || args.r || args.remote || 'ws://127.0.0.0:9702',
Expand All @@ -20,5 +20,6 @@ export const oneTimeData: oneTimeDataType = {
countryCode: IPandLoc.country
}


await startTimer()
transmitter()
3 changes: 1 addition & 2 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
},
"dependencies": {
"ashes-urn": "^100.0.8",
"minimist": "^1.2.8",
"systeminformation": "^5.23.5"
"minimist": "^1.2.8"
}
}
10 changes: 6 additions & 4 deletions client/src/dataUpdater.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Real Time Data Monitor
import { networkInterfaceDefault, currentLoad, networkStats, fsSize, mem } from "systeminformation";
import { networkInterfaceDefault, currentLoad, networkStats, fsSize, mem } from "./systeminformation";
import type { uploadDataType } from "./type";
import { GlobalConfiguration } from "..";

Expand Down Expand Up @@ -83,9 +83,11 @@ export const startTimer = async () => {
total: 0
};
(await fsSize()).forEach((fsObj) => {
if (doRecordFs.includes(fsObj.type))
singleRTStorage.used += fsObj.used;
singleRTStorage.total += fsObj.size;
if (doRecordFs.includes(fsObj.type)) {
singleRTStorage.used += fsObj.used
singleRTStorage.total += fsObj.size;

}
});
RTstorage = {
used: singleRTStorage.used,
Expand Down
1 change: 0 additions & 1 deletion client/src/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const transmitter = () => {
let isWSBlocked = false

// Create WebSocket channel

const wsSocket = new WebSocket(GlobalConfiguration.remote + '/api/v1/upload', {
//@ts-ignore
headers: {
Expand Down
102 changes: 102 additions & 0 deletions client/src/systeminformation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { $ } from "bun";
import os from "node:os"
import { GlobalConfiguration } from "..";

const store: {
cpu: any,
nw: any
} = {
cpu: null,
nw: null
}

export const networkInterfaceDefault = async () => {
const ifaces = os.networkInterfaces();
let ifacename = ''
const cmdReturn = (await $`LC_ALL=C ip route 2> /dev/null | grep default`.text()).split('\n')[0].split(/\s+/)
if (cmdReturn[0] === 'none' && cmdReturn[5]) {
ifacename = cmdReturn[5];
} else if (cmdReturn[4]) {
ifacename = cmdReturn[4];
}
if (ifacename.indexOf(':') > -1) {
ifacename = ifacename.split(':')[1].trim();
}
return ifacename
}

export const currentLoad = async () => {
const cmdReturn = (await $`cat /proc/stat 2>/dev/null | grep cpu`.text()).split('\n')[0];
const cpuStats = cmdReturn.split(/\s+/).slice(1, 5).map(Number); // Get user, nice, system, idle
if (!store.cpu) {
store.cpu = cpuStats
return { currentLoad: 0 };
}
const [prevUser, prevNice, prevSystem, prevIdle] = store.cpu;
const [currUser, currNice, currSystem, currIdle] = cpuStats;

const deltaUser = currUser - prevUser;
const deltaNice = currNice - prevNice;
const deltaSystem = currSystem - prevSystem;
const deltaIdle = currIdle - prevIdle;

const totalTime = deltaUser + deltaNice + deltaSystem + deltaIdle;

store.cpu = cpuStats;

return {
currentLoad: (totalTime - deltaIdle) / totalTime * 100
};
}

export const networkStats = async () => {
const mainInterface = await networkInterfaceDefault()
const tx = Number(await $`cat /sys/class/net/${mainInterface}/statistics/tx_bytes`.text())
const rx = Number(await $`cat /sys/class/net/${mainInterface}/statistics/rx_bytes`.text())
const returnData = [
{
iface: mainInterface,
tx_bytes: tx,
rx_bytes: rx,
tx_sec: store.nw ? (tx - store.nw.tx) / GlobalConfiguration.updInterval : 0,
rx_sec: store.nw ? (rx - store.nw.rx) / GlobalConfiguration.updInterval : 0
}
]

store.nw = {
rx: rx,
tx: tx
}
return returnData
}

export const fsSize = async () => {
let returnData: {
used: number,
size: number,
type: string
}[] = []
const cmdReturn = (await $`LC_ALL=C df -T -B1 -x squashfs`.text()).split('\n').filter(line => line.startsWith('/')).forEach((str) => {
const parts = str.split(/\s+/);
returnData.push({
used: parseInt(parts[3], 10),
size: parseInt(parts[2], 10),
type: parts[1]
})
})

return returnData
}

export const mem = async () => {
const cmdReturn = (await $`LC_ALL=C free -b`.text()).split('\n')
const memLine = cmdReturn[1].split(/\s+/);
const swapLine = cmdReturn[2].split(/\s+/);

return {
total: parseInt(memLine[1]),
active: parseInt(memLine[1]) - parseInt(memLine[6]),
swaptotal: parseInt(swapLine[1]),
swapused: parseInt(swapLine[2]),
};
}
Binary file modified frontend/bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ashes-wreath": "^0.1.6",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"toolbx": "^1.1.3"
"toolbx": "^1.1.4"
},
"devDependencies": {
"@eslint/js": "^9.13.0",
Expand Down

0 comments on commit 1719a6a

Please sign in to comment.