cambios de api conexion
This commit is contained in:
@@ -248,24 +248,71 @@ async function get_commands_by_user(req, res, dbcentral) {
|
||||
|
||||
async function get_online_stores(req, res, dbcentral) {
|
||||
try {
|
||||
const filter = req.query.filter; // 'online', 'offline', 'all' (default: 'all')
|
||||
const id_cliente = req.query.id_cliente; // Filtro opcional por cliente
|
||||
|
||||
// Construir la consulta base
|
||||
let whereClause = "WHERE h.process_name = 'sql_inyector'";
|
||||
const replacements = [];
|
||||
|
||||
// Filtrar por cliente si se proporciona
|
||||
if (id_cliente) {
|
||||
whereClause += " AND h.id_cliente = ?";
|
||||
replacements.push(id_cliente);
|
||||
}
|
||||
|
||||
// Obtenemos los locales y su último ping
|
||||
// Consideramos "Online" si el ping fue hace menos de 2 minutos
|
||||
// Consideramos "ONLINE" si el ping fue hace menos de 2 minutos
|
||||
const [results] = await dbcentral.query(
|
||||
`SELECT
|
||||
h.store_id,
|
||||
h.store_id,
|
||||
h.id_cliente,
|
||||
h.instance_id,
|
||||
h.last_ping,
|
||||
h.status,
|
||||
IF(TIMESTAMPDIFF(SECOND, h.last_ping, CURRENT_TIMESTAMP) <= 120, 'ONLINE', 'OFFLINE') as connectivity
|
||||
h.extra_info,
|
||||
TIMESTAMPDIFF(SECOND, h.last_ping, CURRENT_TIMESTAMP) as seconds_since_ping,
|
||||
CASE
|
||||
WHEN TIMESTAMPDIFF(SECOND, h.last_ping, CURRENT_TIMESTAMP) <= 120 THEN 'ONLINE'
|
||||
WHEN TIMESTAMPDIFF(SECOND, h.last_ping, CURRENT_TIMESTAMP) <= 300 THEN 'DEGRADED'
|
||||
ELSE 'OFFLINE'
|
||||
END as connectivity
|
||||
FROM tb_store_process_heartbeat h
|
||||
WHERE h.process_name = 'sql_inyector'
|
||||
ORDER BY h.last_ping DESC`
|
||||
${whereClause}
|
||||
ORDER BY h.last_ping DESC`,
|
||||
{ replacements }
|
||||
);
|
||||
|
||||
res.status(200).json(results);
|
||||
// Aplicar filtro de conectividad si se especifica
|
||||
let filteredResults = results;
|
||||
if (filter === 'online') {
|
||||
filteredResults = results.filter(r => r.connectivity === 'ONLINE');
|
||||
} else if (filter === 'offline') {
|
||||
filteredResults = results.filter(r => r.connectivity === 'OFFLINE' || r.connectivity === 'DEGRADED');
|
||||
}
|
||||
|
||||
// Formatear la respuesta
|
||||
const formattedResults = filteredResults.map(store => ({
|
||||
store_id: store.store_id,
|
||||
id_cliente: store.id_cliente,
|
||||
instance_id: store.instance_id,
|
||||
connectivity: store.connectivity,
|
||||
status: store.status,
|
||||
last_ping: store.last_ping,
|
||||
seconds_since_ping: store.seconds_since_ping,
|
||||
extra_info: store.extra_info
|
||||
}));
|
||||
|
||||
res.status(200).json({
|
||||
total: formattedResults.length,
|
||||
filter_applied: filter || 'all',
|
||||
stores: formattedResults
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("❌ Error al obtener tiendas online:", error.message);
|
||||
res.status(500).json({
|
||||
error: "Error interno del servidor"
|
||||
error: "Error interno del servidor",
|
||||
message: process.env.NODE_ENV === 'development' ? error.message : 'Error interno'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user