mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-09 23:48:36 +01:00
rewrite consumer + testmode utils
This commit is contained in:
@@ -2,7 +2,6 @@ import { ProjectModel } from "./shared/schema/project/ProjectSchema";
|
||||
import { UserModel } from "./shared/schema/UserSchema";
|
||||
import { LimitNotifyModel } from "./shared/schema/broker/LimitNotifySchema";
|
||||
import { EmailService } from './shared/services/EmailService';
|
||||
import { requireEnv } from "./shared/utils/requireEnv";
|
||||
import { TProjectLimit } from "./shared/schema/project/ProjectsLimits";
|
||||
import { EmailServiceHelper } from "./EmailServiceHelper";
|
||||
|
||||
|
||||
@@ -5,26 +5,6 @@ import { requireEnv } from './shared/utils/requireEnv';
|
||||
|
||||
const stream_name = requireEnv('STREAM_NAME');
|
||||
|
||||
export class MetricsManager {
|
||||
|
||||
private static processTime = new Map<string, number[]>();
|
||||
|
||||
static onProcess(id: string, time: number) {
|
||||
const target = this.processTime.get(id);
|
||||
if (!target) {
|
||||
this.processTime.set(id, [time]);
|
||||
} else {
|
||||
target.push(time);
|
||||
if (target.length > 1000) target.splice(0, target.length - 1000);
|
||||
}
|
||||
}
|
||||
|
||||
static get() {
|
||||
return Array.from(this.processTime.entries());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const metricsRouter = Router();
|
||||
|
||||
metricsRouter.get('/queue', async (req, res) => {
|
||||
@@ -39,7 +19,7 @@ metricsRouter.get('/queue', async (req, res) => {
|
||||
|
||||
metricsRouter.get('/durations', async (req, res) => {
|
||||
try {
|
||||
const durations = MetricsManager.get();
|
||||
const durations = RedisStreamService.METRICS_get()
|
||||
res.json({ durations });
|
||||
} catch (ex) {
|
||||
console.error(ex);
|
||||
|
||||
@@ -13,14 +13,14 @@ import express from 'express';
|
||||
|
||||
import { ProjectLimitModel } from './shared/schema/project/ProjectsLimits';
|
||||
import { ProjectCountModel } from './shared/schema/project/ProjectsCounts';
|
||||
import { MetricsManager, metricsRouter } from './Metrics';
|
||||
import { metricsRouter } from './Metrics';
|
||||
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use('/metrics', metricsRouter);
|
||||
|
||||
app.listen(process.env.PORT);
|
||||
app.listen(process.env.PORT, () => console.log(`Listening on port ${process.env.PORT}`));
|
||||
|
||||
connectDatabase(requireEnv('MONGO_CONNECTION_STRING'));
|
||||
main();
|
||||
@@ -47,15 +47,15 @@ async function processStreamEntry(data: Record<string, string>) {
|
||||
try {
|
||||
|
||||
const eventType = data._type;
|
||||
if (!eventType) return;
|
||||
if (!eventType) return console.log('No type');
|
||||
|
||||
const { pid, sessionHash } = data;
|
||||
|
||||
const project = await ProjectModel.exists({ _id: pid });
|
||||
if (!project) return;
|
||||
if (!project) return console.log('No project');
|
||||
|
||||
const canLog = await checkLimits(pid);
|
||||
if (!canLog) return;
|
||||
if (!canLog) return console.log('No limits');
|
||||
|
||||
if (eventType === 'event') {
|
||||
await process_event(data, sessionHash);
|
||||
@@ -71,7 +71,7 @@ async function processStreamEntry(data: Record<string, string>) {
|
||||
|
||||
const duration = Date.now() - start;
|
||||
|
||||
MetricsManager.onProcess(CONSUMER_NAME, duration);
|
||||
RedisStreamService.METRICS_onProcess(CONSUMER_NAME, duration);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user