From b420b346944257cf0430ad8e48137041b1160027 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 22 Aug 2024 23:31:24 +0300 Subject: [PATCH] Start implementing methods of dashboard api --- README.md | 28 +++++++++++++++++++- package.json | 5 ++-- src/index.d.ts | 16 ++++++++++-- src/index.js | 69 +++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 106 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ef748a4..68ced14 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Supports UptimeKuma versions from 1.13.1 to 1.23.13 npm install uptimekuma-api ``` -## Usage +## Pushing usage ### Start pushing ```js @@ -19,9 +19,23 @@ kuma.startPushing("push code",60); ### Stop pushing ```js +kuma.cancelPushing("push code"); +``` +or to stop all pushes +```js kuma.cancelPushing(); ``` +### Push custom data +```js +kuma.on("prePush", (url, params) => { + params.msg = "test"; + params.status = "down"; +}); +``` + +## Status pages usage + ### Get statuses ```js for (let x of (await kuma.status())) { @@ -29,4 +43,16 @@ for (let x of (await kuma.status())) { console.log(monitor.name + " " + monitor.heartbeats[1].status+ " - " + (monitor.uptime*100) + "%"); } } +``` + +## Dashboard usage + +### Login to dashboard +```js +await kuma.login("username", "password"); +``` + +### Get database size +```js +console.log(await kuma.getDatabaseSize()); ``` \ No newline at end of file diff --git a/package.json b/package.json index d3e840e..f337e41 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uptimekuma-api", - "version": "1.0.6", + "version": "1.1.0", "description": "", "main": "src/index.js", "types": "src/index.d.ts", @@ -18,6 +18,7 @@ "url": "https://github.com/RedGuys/uptimekuma-api" }, "dependencies": { - "axios": "^0.21.1" + "axios": "1.6.0", + "socket.io-client": "^4.7.5" } } diff --git a/src/index.d.ts b/src/index.d.ts index 62a87c6..d3c17af 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -10,13 +10,13 @@ export default class UptimeKumaApi { * @param code The monitor code * @param interval The interval in seconds */ - startPushing(code: string, interval?: number); + startPushing(code: string, interval?: number): void; /** * Stops pushing heartbeats to the server * @param code The monitor code, if not specified, all monitors will be stopped */ - cancelPushing(code?: string); + cancelPushing(code?: string): void; on(event: "pushSuccessful", handle: (url: string) => void); on(event: "pushFailed", handle: (url: string, err: Error) => void); @@ -41,4 +41,16 @@ export default class UptimeKumaApi { }] }] }]>; + + /** + * Initiates websocket connection and logs in + * @param login Login used to authenticate + * @param password Password used to authenticate + */ + login(login: string, password: string): Promise; + + /** + * Gets the size of the database + */ + getDatabaseSize(): Promise; } diff --git a/src/index.js b/src/index.js index e15de42..b4fdc55 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ const client = require("axios").default; const EventEmitter = require("events"); +const {io} = require("socket.io-client"); module.exports = class UptimeKumaApi extends EventEmitter { @@ -12,9 +13,9 @@ module.exports = class UptimeKumaApi extends EventEmitter { } push(url) { - let params = {status:"up",msg:"OK",ping:undefined}; + let params = {status: "up", msg: "OK", ping: undefined}; this.emit("prePush", url, params); - client.get(url,{params}).then(res => { + client.get(url, {params}).then(res => { this.emit("pushSuccessful", url); }).catch(err => { this.emit("pushFailed", url, err); @@ -25,13 +26,13 @@ module.exports = class UptimeKumaApi extends EventEmitter { if (this._pushTimers[code]) this._pushTimers[code].cancel(); this._pushTimers[code] = setInterval(() => { - this.push(this._baseURL+"api/push/"+code); + this.push(this._baseURL + "api/push/" + code); }, interval * 1000); - this.push(this._baseURL+"api/push/"+code); + this.push(this._baseURL + "api/push/" + code); } cancelPushing(code = undefined) { - if(code) { + if (code) { if (this._pushTimers[code]) this._pushTimers[code].cancel(); } else { @@ -44,8 +45,8 @@ module.exports = class UptimeKumaApi extends EventEmitter { } async status(name = "default") { - let resp = await client.get(this._baseURL + "api/status-page/"+name); - let heartBeats = (await client.get(this._baseURL + "api/status-page/heartbeat/"+name)).data; + let resp = await client.get(this._baseURL + "api/status-page/" + name); + let heartBeats = (await client.get(this._baseURL + "api/status-page/heartbeat/" + name)).data; let result = []; for (let srcCategory of resp.data.publicGroupList) { let targetCategory = {id: srcCategory.id, name: srcCategory.name, weight: srcCategory.weight, monitors: []}; @@ -61,4 +62,58 @@ module.exports = class UptimeKumaApi extends EventEmitter { } return result; } + + async connect() { + this._socket = io(this._baseURL, { + transports: ["websocket"], + upgrade: true + }); + this._socket.onAny((event, ...args) => { + this.processMessage(event, ...args); + }); + return new Promise((resolve, reject) => { + this._socket.on("connect", () => { + this._socket.emit("login", { + username: this._username, + password: this._password, + token: "" + }, () => { + resolve(); + }); + }); + this._socket.on("connect_error", (err) => { + reject(err); + }); + }); + } + + async processMessage(event, ...args) { + switch (event) { + case "430": { + this.emit("login", data[0]); + break; + } + } + } + + async handleReconnect() { + this.connect(); + } + + async login(username, password) { + this._username = username; + this._password = password; + await this.connect(); + } + + getDatabaseSize() { + return new Promise((resolve, reject) => { + this._socket.emit("getDatabaseSize", (args) => { + if (args.ok) + resolve(args.size); + else + reject(args.error); + }); + }); + } } \ No newline at end of file