misp-topcontrib/demo/js/misp_stats.js

87 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2024-11-27 18:00:49 -03:00
function crearConfiguracion(tipo_grafico, etiquetas, datos, eje_y) {
return config = {
type: tipo_grafico,
data: {
labels: etiquetas,
datasets: datos
},
options: {
responsive: true,
interaction: {
intersect: false,
},
scales: {
x: {
display: true,
title: {
display: true
}
},
y: {
display: true,
title: {
display: true,
text: eje_y
},
min: 0,
}
}
},
};
}
function procesarDatosDiarios(datos) {
let resultado = {
instituciones: {},
fechas: [],
};
let conjunto_fechas = new Set();
datos.forEach(stat => {
if (!(stat.organizacion in resultado.instituciones)) {
resultado.instituciones[stat.organizacion] = {};
}
resultado.instituciones[stat.organizacion][stat.fecha_creado] = stat.cantidad_ioc;
conjunto_fechas.add(stat.fecha_creado);
});
resultado.fechas = Array.from(conjunto_fechas).sort();
return resultado;
}
function procesarDatosSemanales(datos) {
return {
etiquetas: datos.map(dato => dato.organizacion),
datos: [
{
label: "Número de IOC",
data: datos.map(dato => dato.cantidad_total_ioc),
}
]
}
}
(function () {
fetch("https://mispstats.csirt.gob.cl/api/stats/")
.then(response => response.json())
.then(procesarDatosDiarios)
.then(resultado => {
const datos = Object.keys(resultado.instituciones).map(nombre => {
let datos = resultado.instituciones[nombre];
let data_array = resultado.fechas.map(fecha => fecha in datos ? datos[fecha] : null);
return {
label: nombre,
data: data_array,
tension: 0.5
}
})
const config = crearConfiguracion("line", resultado.fechas, datos, "Número de IoC reportados por institución cada día");
const ioc_dia = new Chart(document.querySelector('#ioc_dia'), config);
});
fetch("https://mispstats.csirt.gob.cl/api/stats/cantidad_por_organizacion/")
.then(respuesta => respuesta.json())
.then(procesarDatosSemanales)
.then(resultado => {
const config = crearConfiguracion("bar", resultado.etiquetas, resultado.datos, "Número de IoC reportados por institución este año");
const ioc_anio = new Chart(document.querySelector('#ioc_anio'), config);
});
})();