Merge main -> migracion alai
This commit is contained in:
@@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user