Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
name: Lint Files
name: ESLint

on:
push:
branches: "main"
pull_request:
branches: "**"
on: [push, pull_request]

jobs:
run-eslint:
Expand All @@ -16,7 +12,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 22
node-version: 24
- name: Install dependencies
run: npm install
- name: Run ESLint
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/data

# compiled output
dist
**/dist
**/tsconfig.tsbuildinfo
tmp
out-tsc

Expand Down
1 change: 1 addition & 0 deletions apps/api/data/fed/nodes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"127.0.0.1":{"name":"localhost"}}
5 changes: 0 additions & 5 deletions apps/api/eslint.config.ts

This file was deleted.

16 changes: 16 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"dependencies": {
"@fastify/static": "^8.3.0",
"fastify": "^5.6.1"
},
"devDependencies": {
"@types/ws": "^8.18.1"
},
"name": "@labstat/api",
"license": "MIT",
"scripts": {
"build": "tsc -b",
"dev": "tsc-watch --compileCommand tsgo --onSuccess 'node ../../dist/api/main.js'"
},
"version": "0.0.1"
}
41 changes: 0 additions & 41 deletions apps/api/project.json

This file was deleted.

50 changes: 0 additions & 50 deletions apps/api/rspack.config.ts

This file was deleted.

76 changes: 39 additions & 37 deletions apps/api/src/data.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
import { CPUFreq, LoadAvg } from "./collectors/cpu";
import Memory from "./collectors/memory";
import OS from "./collectors/os";
import type Collector from "./collectors/base";

import {
Disks, Graphics, Hostname, Network, Uptime
} from "./collectors/core";

// eslint-disable-next-line @typescript-eslint/prefer-function-type
const all_collectors: { new(): Collector }[] = [
CPUFreq, Disks, Graphics, Hostname, LoadAvg, Memory, Network, OS, Uptime
];

const active_collectors: Record<string, Collector | null> = {};

all_collectors.forEach(collector => {
const instance = new collector();

if (!instance.inactive) {
active_collectors[instance.id] = instance;
}
});

function activeCollectors(): string[] {
return Object.keys(active_collectors);
}

async function getData(id: string): Promise<string> {
const collector = active_collectors[id];

if (collector) {
return JSON.stringify(await collector.getData());
}

return "";
import type { FastifyInstance, FastifyRequest } from "fastify";
import WebSocket from "ws";

function registerWebsocket(ws_server: FastifyInstance) {
ws_server.get(
"/api/:node/data",
{ websocket: true },
(socket, request: FastifyRequest<{ Params: { node: string } }>) => {
const { node } = request.params;
const host = `ws://${node}:17220/api/data`;

const node_socket = (() => {
try {
return new WebSocket(host);
} catch (error: unknown) {
const err_str = JSON.stringify(error);
ws_server.log.warn(`[data] Failed to connect to ${host}: ${err_str}`);
return;
}
})();

if (!node_socket) return;

const node_ready = new Promise(resolve => {
node_socket.addEventListener("open", resolve);
});

node_socket.addEventListener("message", message => {
socket.send(message.data);
});

socket.on("message", message => {
void node_ready.then(() => {
node_socket.send(message);
});
});
}
);
}

export { activeCollectors, getData };
export default registerWebsocket;
84 changes: 67 additions & 17 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import Fastify from "fastify";
import * as Fastify from "fastify";
import fastifyStatic from "@fastify/static";
import fastifyWebsocket from "@fastify/websocket";
import { join } from "path";

import { activeCollectors, getData } from "./data";
import { getNodes, initDataFile, writeNode, type NodeOptions } from "./nodes";
import registerWebsocket from "./data";

interface Collectors {
collectors: string[];
}

const server = Fastify.fastify({
logger: {
Expand All @@ -12,33 +19,76 @@ const server = Fastify.fastify({
}
});

initDataFile();

server.register(fastifyWebsocket);
server.register(ws_server => {
ws_server.get("/api/data", { websocket: true }, socket => {
socket.on("message", (message: Buffer) => {
const id = message.toString();

void getData(id).then(data => {
socket.send(`${id} ${data}`);
});
});
});
registerWebsocket(ws_server);
});

server.register(fastifyStatic, {
prefix: "/",
root: join(__dirname, "public")
});

server.get("/api/collectors", (_, reply) => {
reply.send({
collectors: activeCollectors()
});
server.get("/api/nodes", (_, reply) => {
reply.send(getNodes());
});

server.post("/api/register/:ip", {
schema: {
body: {
additionalProperties: false,
type: "object",
properties: {
name: {
type: "string"
}
},
required: ["name"]
}
}
}, (
request: Fastify.FastifyRequest<{ Params: { ip: string } }>,
reply
) => {
const { ip } = request.params;

writeNode(ip, request.body as NodeOptions);
reply.send("");
});

server.get("/api/:node/collectors", async (
request: Fastify.FastifyRequest<{ Params: { node: string } }>,
reply
) => {
const { node } = request.params;
const url = `http://${node}:17220/api/collectors`;

const api_request = await (async () => {
try {
return await fetch(url);
} catch (error: unknown) {
const err_str = JSON.stringify(error);
server.log.warn(`[server] failed to get ${node} collectors: ${err_str}`);
return;
}
})();

if (!api_request) return;
const collectors = await api_request.json() as Collectors;

reply.send(collectors);
});

server.listen({
host: "0.0.0.0",
port: 17220
port: 17221
}, err => {
if (err) {
server.log.error(err);
process.exit(1);
} else {
server.log.info("[server] http://0.0.0.0:17220");
server.log.info("[server] http://0.0.0.0:17221");
}
});
File renamed without changes.
Loading