Version 1.0.0 #1

Merged
AstroGD merged 8 commits from dev into main 2023-09-16 15:46:18 +02:00
5 changed files with 275 additions and 3931 deletions
Showing only changes of commit d94a303ee2 - Show all commits

View File

@ -1,3 +1,3 @@
console.log("Hello world");
import { PushMonitor } from "./src/pushMonitor";
export {};
export default PushMonitor.instance;

4038
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@astrogd/eu.astrogd.uptime-kuma-push-monitor",
"version": "1.0.0-dev.1",
"version": "1.0.0-dev.2",
"description": "Handles uptime kuma push monitor uptime requests",
"main": "dist/index.js",
"types": "dist/types/",
@ -34,5 +34,6 @@
"typescript": "^5.1.6"
},
"dependencies": {
"axios": "^1.5.0"
}
}

139
src/pushMonitor.ts Normal file
View File

@ -0,0 +1,139 @@
import sendHeartbeat from "./requestHandler";
/**
* Defines an uptime kuma monitor
*/
interface Monitor {
/**
* The url to push heartbeats to
*/
url: string;
/**
* The interval in which to push heartbeats in seconds
*/
heartbeatIntervall: number;
}
/**
* Defines a monitor store
*/
interface MonitorStore {
/**
* The monitor
*/
monitor: Monitor;
/**
* The last heartbeat
*/
lastHeartbeat: Date;
}
/**
* The handler managing all monitors
*/
export class PushMonitor {
/**
* The private constructor
* @private
*/
private constructor() {
this.initTimer();
}
/**
* The singleton instance of the PushMonitor
* @private
*/
private static _instance = new PushMonitor();
/**
* The monitors to manage
* @private
*/
private _monitors: MonitorStore[] = [];
/**
* The check interval
* @private
*/
private _checkInterval: NodeJS.Timer | undefined;
/**
* Whether to log debug information
* @private
*/
private _debug: boolean = false;
/**
* Returns the singleton instance of the PushMonitor
*/
public static get instance(): PushMonitor {
return PushMonitor._instance;
}
/**
* Starts the monitor
* @returns This instance
*/
public start(): this {
this.initTimer();
return this;
}
/**
* Stops the monitor
* @returns This instance
*/
public stop(): this {
if (this._checkInterval) {
clearInterval(this._checkInterval);
}
return this;
}
/**
* Registers a new monitor to be managed
* @param url {string} The url to push heartbeats to
* @param heartbeatIntervall {number} The interval in which to push heartbeats in seconds. Default is 58 seconds
* @returns This instance
*/
public register(url: string, heartbeatIntervall: number = 58): this {
this._monitors.push({
monitor: {
url,
heartbeatIntervall,
},
lastHeartbeat: new Date(0),
});
return this;
}
/**
* Sets whether to log debug information
* @param debug Whether to log debug information
* @returns This instance
*/
public setDebug(debug: boolean): this {
this._debug = debug;
return this;
}
/**
* Initializes the timer
* @private
*/
private initTimer() {
if (this._checkInterval) {
clearInterval(this._checkInterval);
}
this._checkInterval = setInterval(() => {
this._monitors.forEach((monitor) => {
if (new Date().getTime() - monitor.lastHeartbeat.getTime() > monitor.monitor.heartbeatIntervall * 1000) {
sendHeartbeat(monitor.monitor.url, this._debug);
monitor.lastHeartbeat = new Date();
}
});
}, 1000);
}
}

22
src/requestHandler.ts Normal file
View File

@ -0,0 +1,22 @@
import axios from "axios";
/**
* Sends a heartbeat to the provided URL
* @param url The URL to send the heartbeat to
* @param debug Whether to log debug information
* @private
*/
export default async function sendHeartbeat(url: string, debug: boolean = false) {
const useUrl = new URL(url);
try {
const response = await axios.get(`${useUrl.origin}/${useUrl.pathname}?status=up&msg=OK`);
if (debug)
console.log(
`[eu.astrogd.uptime-kuma-push-monitor] (${new Date().toISOString()}) <${url}>: ${response.status} ${response.statusText} - ${
response.data
}`
);
} catch (error) {
console.error(`[eu.astrogd.uptime-kuma-push-monitor] (${new Date().toISOString()}) <${url}>: ${error}`);
}
}