47 lines
1.9 KiB
TypeScript
47 lines
1.9 KiB
TypeScript
import { RabbitManagementClient } from "packages/sim-shared/infrastructure/RabbitManagementClient.js";
|
|
import { OrderRepository } from "packages/sim-shared/infrastructure/OrderRepository.js";
|
|
import { DashboardData, QueueSummary } from "#domain/Dashboard.js";
|
|
import { Queue } from "packages/sim-shared/domain/Queue.js";
|
|
import { env } from "#config/env/index.js";
|
|
|
|
const EMPTY_QUEUE: Queue = { name: "", messages: 0, ready: 0, unacked: 0, consumers: 0}
|
|
|
|
export class DashboardUseCases {
|
|
constructor(
|
|
private readonly rabbitClient: RabbitManagementClient,
|
|
private readonly orderRepo: OrderRepository,
|
|
) {}
|
|
|
|
public async getDashboardData(): Promise<DashboardData> {
|
|
//si uno peta, no rompe al otro
|
|
const [queuesResult, pendingResult] = await Promise.allSettled([
|
|
this.rabbitClient.getQueues(),
|
|
this.orderRepo.getPendingOrders({ limit: 100 }), //por poner un límite
|
|
])
|
|
|
|
let queues: QueueSummary = { main: EMPTY_QUEUE, retry: EMPTY_QUEUE, dlx: EMPTY_QUEUE}
|
|
|
|
if (queuesResult.status === 'fulfilled') {
|
|
const all = queuesResult.value
|
|
const find = (name: string) => all.find(q => q.name === name) ?? {...EMPTY_QUEUE, name}
|
|
queues = {
|
|
main: find(env.QUEUE_MAIN!),
|
|
retry: find(env.QUEUE_RETRY!),
|
|
dlx: find(env.QUEUE_DLX!)
|
|
}
|
|
} else {
|
|
console.error('[Dasboard] Error obteniendo colas: ', queuesResult.reason)
|
|
}
|
|
const pendingOrders = (pendingResult.status === "fulfilled" && !pendingResult.value.error)
|
|
? pendingResult.value.data ?? []
|
|
: [];
|
|
if (pendingResult.status === 'rejected') {
|
|
console.error('[Dashboard]Error obteniendo tareas: ', pendingResult.reason)
|
|
}
|
|
return {
|
|
queues,
|
|
pendingOrders,
|
|
generatedAt: new Date().toISOString(),
|
|
}
|
|
}
|
|
} |