mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-08-09 09:04:04 +08:00
Uptime calculation improvement and 1-year uptime (#2750)
This commit is contained in:
79
server/utils/array-with-key.js
Normal file
79
server/utils/array-with-key.js
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* An object that can be used as an array with a key
|
||||
* Like PHP's array
|
||||
*/
|
||||
class ArrayWithKey {
|
||||
__stack = [];
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
constructor() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
push(key, value) {
|
||||
this[key] = value;
|
||||
this.__stack.push(key);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
pop() {
|
||||
let key = this.__stack.pop();
|
||||
let prop = this[key];
|
||||
delete this[key];
|
||||
return prop;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
getLastKey() {
|
||||
if (this.__stack.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return this.__stack[this.__stack.length - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
shift() {
|
||||
let key = this.__stack.shift();
|
||||
let value = this[key];
|
||||
delete this[key];
|
||||
return {
|
||||
key,
|
||||
value,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
length() {
|
||||
return this.__stack.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last element
|
||||
* @returns {*|null} The last element, or null if the array is empty
|
||||
*/
|
||||
last() {
|
||||
let key = this.getLastKey();
|
||||
if (key === null) {
|
||||
return null;
|
||||
}
|
||||
return this[key];
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ArrayWithKey
|
||||
};
|
37
server/utils/limit-queue.js
Normal file
37
server/utils/limit-queue.js
Normal file
@@ -0,0 +1,37 @@
|
||||
const { ArrayWithKey } = require("./array-with-key");
|
||||
|
||||
/**
|
||||
* Limit Queue
|
||||
* The first element will be removed when the length exceeds the limit
|
||||
*/
|
||||
class LimitQueue extends ArrayWithKey {
|
||||
|
||||
__limit;
|
||||
__onExceed = null;
|
||||
|
||||
/**
|
||||
* @param {number} limit
|
||||
*/
|
||||
constructor(limit) {
|
||||
super();
|
||||
this.__limit = limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
push(key, value) {
|
||||
super.push(key, value);
|
||||
if (this.length() > this.__limit) {
|
||||
let item = this.shift();
|
||||
if (this.__onExceed) {
|
||||
this.__onExceed(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
LimitQueue
|
||||
};
|
Reference in New Issue
Block a user