Calculo del tiempo en suspension

This commit is contained in:
2026-04-27 12:12:12 +02:00
parent e1450c6e97
commit 4517796ef3
5 changed files with 156 additions and 8 deletions

View File

@@ -6,6 +6,7 @@ export interface IOperationsRepository {
createOperation(data: ObjeniousOperation): Promise<Result<string, ObjeniousOperation>>
updateOperation(data: ObjeniousOperationChange): Promise<Result<string, ObjeniousOperation>>
getPendingOperations(): Promise<Result<string, ObjeniousOperation[]>>
getSuspendedTime(iccid: string): Promise<Result<string, { total_milliseconds: number, total_days: number }>>
}
export type ObjeniousOperation = {

View File

@@ -1,4 +1,4 @@
import { before, describe, it } from "node:test";
import { before, after, describe, it } from "node:test";
import { ObjeniousOperationsRepository } from "./ObjeniousOperationRepository.js";
import { httpObjClient, postgresClient } from "../config/config.test.js";
import { ObjeniousOperation } from "../domain/operationsRepository.port.js";
@@ -21,6 +21,7 @@ describe("[Integration] Test API requests", () => {
httpObjClient,
postgresClient
)
const suspend_iccid = "test_suspended_time_iccid";
before(async () => {
await repository.createOperation(correctOperation)
@@ -36,4 +37,40 @@ describe("[Integration] Test API requests", () => {
* - Se ignoran las erroneas
*/
})
it("Calculates suspended time accurately", async () => {
// Se crean registros con un iccid concocido
await postgresClient.query(`
INSERT INTO objenious_operation (operation, iccids, status, error, start_date, end_date) VALUES
('suspend', $1, 'finished', NULL, NOW() - INTERVAL '3 days', NOW() - INTERVAL '3 days'),
('reactivate', $1, 'finished', NULL, NOW() - INTERVAL '2 days', NOW() - INTERVAL '2 days'),
('suspend', $1, 'finished', NULL, NOW() - INTERVAL '1 day', NOW() - INTERVAL '1 day');
`, [suspend_iccid]);
const result = await repository.getSuspendedTime(suspend_iccid);
if (result.error) {
throw new Error("Query returned an error: " + result.error);
}
// Se esperan mas o menos 2 dias para cada periodo, total 4 (Puede cambiar un poco por zona horaria)
// 2 dias en ms
const expectedApproxMs = 2 * 24 * 60 * 60 * 1000;
const msDiff = Math.abs(result.data!.total_milliseconds - expectedApproxMs);
// Margen de 5s
if (msDiff > 5000) {
throw new Error(`Expected approx ${expectedApproxMs} ms, got ${result.data!.total_milliseconds}`);
}
// como se incluye el dia de sespension los dias van a variar de 2 a 3
if (result.data!.total_days < 2) {
throw new Error("total_days should be at least 2");
}
});
after(async () => {
// Eliminacion de los iccid de periodo de suspensiones
await postgresClient.query(`DELETE FROM objenious_operation WHERE iccids = '${suspend_iccid}'`);
});
})

View File

@@ -231,4 +231,66 @@ export class ObjeniousOperationsRepository implements IOperationsRepository {
}
}
}
/**
* Obtiene el tiempo en suspensión de una linea en miliseguntos y dias efetivos
* Todo el calculo se hace en postgres. Puede que haga falta traer las transiciones
* que normalmente son pocas, para hacer filtros personalizados.
*/
async getSuspendedTime(iccid: string): Promise<Result<string, { total_milliseconds: number, total_days: number }>> {
const query = `
WITH ordered_events AS (
-- 1. Selecciona y normaliza los eventos relevantes del historial
-- Se define el 'estado' final (suspendido vs activo) basado en la operación
SELECT operation, end_date,
CASE WHEN operation = 'suspend' THEN 'suspended' ELSE 'active' END as state
FROM objenious_operation
WHERE iccids = $1 AND status = 'finished' AND error IS NULL
AND operation IN ('suspend', 'activate', 'reactivate', 'terminate')
ORDER BY end_date
),
state_transitions AS (
-- 2. Detecta cambios de estado comparando con la fila anterior (LAG)
SELECT state, end_date,
LAG(state) OVER (ORDER BY end_date) as prev_state
FROM ordered_events
),
filtered_transitions AS (
-- 3. Filtra solo las filas donde el estado realmente ha cambaido
-- Se obtiene la fecha de inicio del siguiente intervalo (LEAD)
SELECT state, end_date,
LEAD(end_date) OVER (ORDER BY end_date) as next_end_date
FROM state_transitions
WHERE state IS DISTINCT FROM prev_state
),
intervals AS (
-- 4. Calcula la duración de los periodos en los que el estado fue 'suspended'
-- Se usa NOW() para el intervalo abierto (último estado hasta hoy)
SELECT EXTRACT(EPOCH FROM (COALESCE(next_end_date, NOW() at time zone 'utc') - end_date)) * 1000 as ms_duration,
(COALESCE(next_end_date, NOW() at time zone 'utc')::date - end_date::date) + 1 as days_duration
FROM filtered_transitions
WHERE state = 'suspended'
)
-- 5. Suma total de tiempo en estado de suspensión
SELECT COALESCE(SUM(ms_duration)::bigint, 0) as total_milliseconds,
COALESCE(SUM(days_duration), 0) as total_days
FROM intervals;
`;
try {
const { rows } = await this.pgClient.query<{ total_milliseconds: string, total_days: string }>(query, [iccid]);
return {
data: {
total_milliseconds: parseFloat(rows[0].total_milliseconds),
total_days: parseInt(rows[0].total_days)
}
};
} catch (e) {
console.error("Error calculating suspended time", e);
return {
error: String(e),
data: undefined
};
}
}
}