feat(lstv2 move): moved lstv2 into this app to keep them combined and easier to maintain
This commit is contained in:
22
lstV2/server/services/rfid/utils/monitorTags.ts
Normal file
22
lstV2/server/services/rfid/utils/monitorTags.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { and, lt, ne, sql } from "drizzle-orm";
|
||||
import { db } from "../../../../database/dbclient.js";
|
||||
import { rfidTags } from "../../../../database/schema/rfidTags.js";
|
||||
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||
|
||||
/**
|
||||
* This will monitor tags that are older than 6hours and are still linked to a line.
|
||||
* it will then remove the line from the last area in as we will asume it dose not exist.
|
||||
*/
|
||||
export const monitorRfidTags = async () => {
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.update(rfidTags)
|
||||
.set({ lastareaIn: "miss scanned" })
|
||||
.where(
|
||||
and(
|
||||
ne(rfidTags.lastareaIn, "wrapper1"), // not equal to 'wrapper1'
|
||||
lt(rfidTags.lastRead, sql`NOW() - INTERVAL '6 hours'`) // older than 6 hours)
|
||||
)
|
||||
)
|
||||
);
|
||||
};
|
||||
19
lstV2/server/services/rfid/utils/rateLimit.ts
Normal file
19
lstV2/server/services/rfid/utils/rateLimit.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
const lastRunMap = new Map();
|
||||
|
||||
/**
|
||||
* Returns true if the function with a given key was called within the last `seconds`.
|
||||
* @param {string} key - Unique key to track cooldown per function/context.
|
||||
* @param {number} seconds - Cooldown in seconds.
|
||||
* @returns {boolean} - true if should skip (still in cooldown), false if allowed.
|
||||
*/
|
||||
export function shouldSkipByCooldown(key: any, seconds: any) {
|
||||
const now = Date.now();
|
||||
const cooldown = seconds * 1000;
|
||||
|
||||
if (lastRunMap.has(key) && now - lastRunMap.get(key) < cooldown) {
|
||||
return true;
|
||||
}
|
||||
|
||||
lastRunMap.set(key, now);
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user