| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183 |
- <script src="{{ url_for('static', filename='js/chart.js') }}"></script>
- <link
- rel="stylesheet"
- href="{{ url_for('static', filename='css/toastr.min.css') }}"
- />
- <script src="{{ url_for('static', filename='js/jquery-3.7.1.min.js') }}"></script>
- <script src="{{ url_for('static', filename='js/toastr.min.js') }}"></script>
- <style>
- /* Estilo para los "chips" de redes sociales */
- .social-chip {
- padding: 8px 16px;
- border-radius: 9999px;
- font-size: 0.875rem;
- font-weight: 600;
- cursor: pointer;
- transition: all 0.2s ease-in-out;
- border: 1px solid #e5e7eb; /* gray-200 */
- background-color: #f9fafb; /* gray-50 */
- color: #374151; /* gray-700 */
- box-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05);
- }
- .social-chip:hover {
- border-color: #93c5fd; /* blue-300 */
- background-color: #eff6ff; /* blue-50 */
- transform: translateY(-1px);
- box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
- }
- .social-chip.selected {
- background-color: #2563eb; /* blue-600 */
- color: white;
- border-color: #1d4ed8; /* blue-800 */
- box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
- }
- .social-chip.selected:hover {
- background-color: #1d4ed8;
- }
- /* Estilo para el tooltip de ayuda sobre los chips */
- .tooltip {
- position: absolute;
- bottom: 100%;
- left: 50%;
- transform: translateX(-50%);
- margin-bottom: 8px;
- background-color: #111827; /* gray-900 */
- color: white;
- padding: 4px 8px;
- border-radius: 4px;
- font-size: 0.75rem;
- font-weight: 500;
- white-space: nowrap;
- opacity: 0;
- transition: opacity 0.2s ease-in-out, transform 0.2s ease-in-out;
- pointer-events: none; /* Para que no interfiera con el click */
- transform-origin: bottom center;
- transform: translateX(-50%) scale(0.95);
- }
- .group:hover .tooltip {
- opacity: 1;
- transform: translateX(-50%) scale(1);
- }
- </style>
- <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
- <div class="flex items-center mb-6 border-b pb-4">
- <i class="fas fa-magnifying-glass text-3xl text-blue-600 mr-4"></i>
- <div>
- <h1 class="text-3xl font-bold text-gray-800">Centro de Auditoría</h1>
- <p class="text-gray-500 mt-1">
- Herramienta para el análisis de actividad y seguridad.
- </p>
- </div>
- </div>
- <div class="bg-white p-6 rounded-lg shadow-lg mb-8">
- <form
- id="audit-form"
- class="grid grid-cols-1 lg:grid-cols-3 gap-x-8 gap-y-6"
- >
- <div class="lg:col-span-2 grid grid-cols-1 md:grid-cols-2 gap-6">
- <div class="md:col-span-2">
- <label
- for="audit-type"
- class="block text-sm font-semibold text-gray-700 mb-1"
- >Tipo de Auditoría</label
- >
- <select
- id="audit-type"
- name="audit_type"
- class="w-full border-gray-300 rounded-md shadow-sm focus:border-blue-500 focus:ring-blue-500 bg-gray-50"
- >
- <optgroup label="Actividad General">
- <option value="user_summary" data-requires="user">
- Resumen por Usuario
- </option>
- <option value="top_users_data">Top 10 Usuarios (Datos)</option>
- <!-- <option value="daily_activity" data-requires="user">
- Actividad Diaria
- </option> -->
- </optgroup>
- <optgroup label="Seguridad y Políticas">
- <option
- value="keyword_search"
- data-requires="user_optional,keyword"
- >
- Búsqueda por Palabra Clave
- </option>
- <option
- value="social_media_activity"
- data-requires="user_optional,social_media"
- >
- Actividad en Redes Sociales
- </option>
- </optgroup>
- <optgroup label="Auditoría Técnica">
- <option value="ip_activity" data-requires="ip">
- Actividad por Dirección IP
- </option>
- <option
- value="response_code_search"
- data-requires="user_optional,response_code"
- >
- Análisis por Código de Respuesta
- </option>
- </optgroup>
- </select>
- </div>
- <div id="conditional-inputs" class="contents">
- <div id="user-filter-container" class="conditional-input">
- <label
- for="username-filter"
- class="block text-sm font-semibold text-gray-700 mb-1"
- >Usuario</label
- >
- <div class="relative">
- <input
- type="text"
- id="user-search-input"
- placeholder="Buscar usuario..."
- class="w-full border-gray-300 rounded-md shadow-sm focus:border-blue-500 focus:ring-blue-500 bg-gray-50 pr-8"
- autocomplete="off"
- />
- <i
- class="fas fa-search absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 text-sm"
- ></i>
- <div
- id="user-dropdown"
- class="absolute z-50 w-full mt-1 bg-white border border-gray-300 rounded-md shadow-lg max-h-48 overflow-y-auto hidden"
- >
- <div id="user-options-container">
- <!-- Las opciones se llenarán dinámicamente -->
- </div>
- </div>
- </div>
- <select id="username-filter" name="username" class="hidden">
- <option value="">-- Todos --</option>
- </select>
- </div>
- <div id="keyword-input-container" class="conditional-input">
- <label
- for="keyword-input"
- class="block text-sm font-semibold text-gray-700 mb-1"
- >Palabra Clave</label
- >
- <input
- type="text"
- id="keyword-input"
- name="keyword"
- class="w-full border-gray-300 rounded-md shadow-sm bg-gray-50"
- placeholder="Ej: juegos, descargas..."
- />
- </div>
- <div id="ip-input-container" class="conditional-input">
- <label
- for="ip-input"
- class="block text-sm font-semibold text-gray-700 mb-1"
- >Dirección IP</label
- >
- <input
- type="text"
- id="ip-input"
- name="ip_address"
- class="w-full border-gray-300 rounded-md shadow-sm bg-gray-50"
- placeholder="Ej: 192.168.1.100"
- />
- </div>
- <div
- id="social-media-input-container"
- class="conditional-input md:col-span-2"
- >
- <label class="block text-sm font-semibold text-gray-700 mb-2"
- >Redes Sociales</label
- >
- <div id="social-media-chips" class="flex flex-wrap gap-3">
- <div class="relative group">
- <button type="button" class="social-chip" data-value="Facebook">
- Facebook
- </button>
- <span class="tooltip">Selección múltiple</span>
- </div>
- <div class="relative group">
- <button
- type="button"
- class="social-chip"
- data-value="Instagram"
- >
- Instagram
- </button>
- <span class="tooltip">Selección múltiple</span>
- </div>
- <div class="relative group">
- <button type="button" class="social-chip" data-value="YouTube">
- YouTube
- </button>
- <span class="tooltip">Selección múltiple</span>
- </div>
- <div class="relative group">
- <button
- type="button"
- class="social-chip"
- data-value="Twitter/X"
- >
- Twitter/X
- </button>
- <span class="tooltip">Selección múltiple</span>
- </div>
- <div class="relative group">
- <button type="button" class="social-chip" data-value="WhatsApp">
- WhatsApp
- </button>
- <span class="tooltip">Selección múltiple</span>
- </div>
- <div class="relative group">
- <button type="button" class="social-chip" data-value="Telegram">
- Telegram
- </button>
- <span class="tooltip">Selección múltiple</span>
- </div>
- <div class="relative group">
- <button
- type="button"
- class="social-chip"
- data-value="Pinterest"
- >
- Pinterest
- </button>
- <span class="tooltip">Selección múltiple</span>
- </div>
- </div>
- </div>
- <div id="response-code-input-container" class="conditional-input">
- <label
- for="response-code-input"
- class="block text-sm font-semibold text-gray-700 mb-1"
- >Código HTTP</label
- >
- <select
- id="response-code-input"
- name="response_code"
- class="w-full border-gray-300 rounded-md shadow-sm focus:border-blue-500 focus:ring-blue-500 bg-gray-50"
- >
- <option value="">-- Seleccione un código --</option>
- <optgroup label="Respuestas Exitosas (2xx)">
- <option value="200">200 OK</option>
- </optgroup>
- <optgroup label="Redirecciones (3xx)">
- <option value="301">301 Moved Permanently</option>
- <option value="302">302 Found</option>
- <option value="304">304 Not Modified</option>
- </optgroup>
- <optgroup label="Errores de Cliente (4xx)">
- <option value="400">400 Bad Request</option>
- <option value="401">401 Unauthorized</option>
- <option value="403">403 Forbidden</option>
- <option value="404">404 Not Found</option>
- </optgroup>
- <optgroup label="Errores de Servidor (5xx)">
- <option value="500">500 Internal Server Error</option>
- <option value="502">502 Bad Gateway</option>
- <option value="503">503 Service Unavailable</option>
- <option value="504">504 Gateway Timeout</option>
- </optgroup>
- </select>
- </div>
- </div>
- </div>
- <div
- class="lg:col-span-1 bg-slate-50 border border-slate-200 rounded-lg p-4 h-fit"
- >
- <h3 class="text-md font-semibold text-slate-800 mb-4 border-b pb-2">
- Fecha del Reporte
- </h3>
- <div class="space-y-4">
- <div id="start-date-container">
- <label
- for="start-date"
- class="block text-sm font-semibold text-gray-700 mb-1"
- >Fecha</label
- >
- <input
- type="date"
- id="start-date"
- name="start_date"
- class="w-full border-gray-300 rounded-md shadow-sm focus:border-blue-500 focus:ring-blue-500 bg-gray-50"
- />
- </div>
- <div id="end-date-container">
- <label
- for="end-date"
- class="block text-sm font-semibold text-gray-700 mb-1"
- >Fecha de Fin</label
- >
- <input
- type="date"
- id="end-date"
- name="end_date"
- class="w-full border-gray-300 rounded-md shadow-sm focus:border-blue-500 focus:ring-blue-500 bg-gray-50"
- />
- </div>
- </div>
- </div>
- <div class="lg:col-span-3">
- <button
- type="submit"
- class="w-full bg-blue-600 text-white font-bold py-2.5 px-4 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 transition-all duration-300 flex items-center justify-center shadow-lg hover:shadow-xl"
- >
- <i class="fas fa-play mr-2"></i>
- Generar Reporte
- </button>
- </div>
- </form>
- </div>
- <div id="audit-results-container" class="space-y-4">
- <div
- class="bg-white p-6 rounded-lg shadow-lg min-h-[300px] flex items-center justify-center"
- >
- <div class="text-center text-gray-500 py-10">
- <i class="fas fa-clipboard-list text-5xl mb-4 text-gray-300"></i>
- <h3 class="text-xl font-semibold">
- Seleccione los filtros y genere un reporte
- </h3>
- <p class="mt-2">Los resultados de su auditoría aparecerán aquí.</p>
- </div>
- </div>
- </div>
- </div>
- <script>
- toastr.options = {
- closeButton: false,
- debug: false,
- newestOnTop: true,
- progressBar: false,
- positionClass: "toast-top-right",
- preventDuplicates: true,
- onclick: null,
- showDuration: "300",
- hideDuration: "1000",
- timeOut: "5000",
- extendedTimeOut: "1000",
- showEasing: "swing",
- hideEasing: "linear",
- showMethod: "fadeIn",
- hideMethod: "fadeOut",
- };
- function toggleDetails(header) {
- const details = header.nextElementSibling;
- const icon = header.querySelector(".toggle-icon");
- if (details.style.display === "block") {
- details.style.display = "none";
- icon.classList.remove("rotate-180");
- } else {
- details.style.display = "block";
- icon.classList.add("rotate-180");
- }
- }
- document.addEventListener("DOMContentLoaded", function () {
- const form = document.getElementById("audit-form");
- const resultsContainer = document.getElementById("audit-results-container");
- const auditTypeSelect = document.getElementById("audit-type");
- const usernameSelect = document.getElementById("username-filter");
- const startDateInput = document.getElementById("start-date");
- const endDateInput = document.getElementById("end-date");
- const startDateContainer = document.getElementById("start-date-container");
- const endDateContainer = document.getElementById("end-date-container");
- const defaultResultsHTML = `<div class="bg-white p-6 rounded-lg shadow-lg min-h-[300px] flex items-center justify-center">
- <div class="text-center text-gray-500 py-10">
- <i class="fas fa-clipboard-list text-5xl mb-4 text-gray-300"></i>
- <h3 class="text-xl font-semibold">Seleccione los filtros y genere un reporte</h3>
- <p class="mt-2">Los resultados de su auditoría aparecerán aquí.</p>
- </div>
- </div>`;
- resultsContainer.innerHTML = defaultResultsHTML;
- const conditionalInputs = {
- user: document.getElementById("user-filter-container"),
- user_optional: document.getElementById("user-filter-container"),
- keyword: document.getElementById("keyword-input-container"),
- ip: document.getElementById("ip-input-container"),
- response_code: document.getElementById("response-code-input-container"),
- social_media: document.getElementById("social-media-input-container"),
- };
- const getLocalDateString = () => {
- const today = new Date();
- const year = today.getFullYear();
- const month = String(today.getMonth() + 1).padStart(2, "0");
- const day = String(today.getDate()).padStart(2, "0");
- return `${year}-${month}-${day}`;
- };
- const todayString = getLocalDateString();
- startDateInput.value = todayString;
- endDateInput.value = todayString;
- // Variables para el buscador de usuarios
- let allUsers = [];
- let filteredUsers = [];
- let selectedUserIndex = -1;
- const userSearchInput = document.getElementById("user-search-input");
- const userDropdown = document.getElementById("user-dropdown");
- const userOptionsContainer = document.getElementById(
- "user-options-container"
- );
- const usernameFilterSelect = document.getElementById("username-filter");
- // Función para renderizar las opciones de usuarios
- function renderUserOptions(users, searchTerm = "") {
- userOptionsContainer.innerHTML = "";
- // Agregar opción "Todos" si no hay término de búsqueda
- if (!searchTerm) {
- const allOption = document.createElement("div");
- allOption.className =
- "px-3 py-2 hover:bg-blue-50 cursor-pointer text-gray-700 border-b border-gray-100";
- allOption.textContent = "-- Todos --";
- allOption.dataset.value = "";
- allOption.addEventListener("click", () =>
- selectUser("", "-- Todos --")
- );
- userOptionsContainer.appendChild(allOption);
- }
- users.forEach((user, index) => {
- const option = document.createElement("div");
- option.className =
- "px-3 py-2 hover:bg-blue-50 cursor-pointer text-gray-700";
- option.textContent = user;
- option.dataset.value = user;
- option.dataset.index = index;
- option.addEventListener("click", () => selectUser(user, user));
- // Resaltar texto coincidente
- if (searchTerm) {
- const regex = new RegExp(`(${searchTerm})`, "gi");
- option.innerHTML = user.replace(
- regex,
- '<mark class="bg-yellow-200">$1</mark>'
- );
- }
- userOptionsContainer.appendChild(option);
- });
- if (users.length === 0 && searchTerm) {
- const noResults = document.createElement("div");
- noResults.className = "px-3 py-2 text-gray-500 italic";
- noResults.textContent = "No se encontraron usuarios";
- userOptionsContainer.appendChild(noResults);
- }
- }
- // Función para seleccionar un usuario
- function selectUser(value, displayText) {
- userSearchInput.value = displayText;
- usernameFilterSelect.value = value;
- userDropdown.classList.add("hidden");
- selectedUserIndex = -1;
- }
- // Función para filtrar usuarios
- function filterUsers(searchTerm) {
- if (!searchTerm) {
- filteredUsers = [...allUsers];
- } else {
- filteredUsers = allUsers.filter((user) =>
- user.toLowerCase().includes(searchTerm.toLowerCase())
- );
- }
- renderUserOptions(filteredUsers, searchTerm);
- }
- // Event listeners para el buscador de usuarios
- userSearchInput.addEventListener("input", function (e) {
- const searchTerm = e.target.value;
- filterUsers(searchTerm);
- userDropdown.classList.remove("hidden");
- selectedUserIndex = -1;
- });
- userSearchInput.addEventListener("focus", function () {
- filterUsers(userSearchInput.value);
- userDropdown.classList.remove("hidden");
- });
- userSearchInput.addEventListener("keydown", function (e) {
- const options = userOptionsContainer.querySelectorAll("[data-value]");
- if (e.key === "ArrowDown") {
- e.preventDefault();
- selectedUserIndex = Math.min(selectedUserIndex + 1, options.length - 1);
- updateSelectedOption(options);
- } else if (e.key === "ArrowUp") {
- e.preventDefault();
- selectedUserIndex = Math.max(selectedUserIndex - 1, -1);
- updateSelectedOption(options);
- } else if (e.key === "Enter") {
- e.preventDefault();
- if (selectedUserIndex >= 0 && options[selectedUserIndex]) {
- const selectedOption = options[selectedUserIndex];
- selectUser(selectedOption.dataset.value, selectedOption.textContent);
- }
- } else if (e.key === "Escape") {
- userDropdown.classList.add("hidden");
- selectedUserIndex = -1;
- }
- });
- function updateSelectedOption(options) {
- options.forEach((option, index) => {
- option.classList.toggle("bg-blue-100", index === selectedUserIndex);
- });
- }
- // Cerrar dropdown al hacer clic fuera
- document.addEventListener("click", function (e) {
- if (
- !userSearchInput.contains(e.target) &&
- !userDropdown.contains(e.target)
- ) {
- userDropdown.classList.add("hidden");
- selectedUserIndex = -1;
- }
- });
- fetch("/api/all-users")
- .then((response) => response.json())
- .then((users) => {
- allUsers = users;
- filteredUsers = [...users];
- // Llenar el select oculto para compatibilidad
- users.forEach((user) => {
- const option = document.createElement("option");
- option.value = user;
- option.textContent = user;
- usernameFilterSelect.appendChild(option);
- });
- renderUserOptions(filteredUsers);
- /* toastr.success(`${users.length} usuarios cargados`, "Usuarios disponibles", {
- "timeOut": 2000 */
- })
- .catch((error) => {
- toastr.error("Error al cargar la lista de usuarios", "Error de carga");
- });
- function toggleConditionalInputs() {
- resultsContainer.innerHTML = defaultResultsHTML;
- Object.values(conditionalInputs).forEach((el) => {
- if (el) el.style.display = "none";
- });
- const selectedOption =
- auditTypeSelect.options[auditTypeSelect.selectedIndex];
- const auditType = selectedOption.value;
- const required = selectedOption.dataset.requires?.split(",") || [];
- required.forEach((inputId) => {
- if (conditionalInputs[inputId]) {
- conditionalInputs[inputId].style.display = "block";
- }
- });
- const userSelectContainer = conditionalInputs.user;
- const defaultUserOption = userSelectContainer.querySelector(
- 'select option[value=""]'
- );
- if (auditType === "daily_activity") {
- endDateContainer.style.display = "none";
- startDateContainer.querySelector("label").textContent = "Fecha";
- } else {
- endDateContainer.style.display = "block";
- startDateContainer.querySelector("label").textContent =
- "Fecha de Inicio";
- }
- if (auditType === "user_summary" || auditType === "daily_activity") {
- userSearchInput.placeholder = "Seleccionar usuario...";
- userSearchInput.value = "";
- usernameFilterSelect.value = "";
- userSearchInput.disabled = false;
- } else {
- userSearchInput.placeholder = "Buscar usuario... (Opcional)";
- userSearchInput.value = "-- Todos --";
- usernameFilterSelect.value = "";
- userSearchInput.disabled = false;
- }
- }
- auditTypeSelect.addEventListener("change", toggleConditionalInputs);
- toggleConditionalInputs();
- const socialMediaChipsContainer =
- document.getElementById("social-media-chips");
- socialMediaChipsContainer.addEventListener("click", function (event) {
- if (event.target.classList.contains("social-chip")) {
- event.target.classList.toggle("selected");
- }
- });
- // Función para restaurar el botón de envío a su estado original
- function restoreSubmitButton() {
- const submitButton = form.querySelector('button[type="submit"]');
- const buttonIcon = submitButton.querySelector("i");
- submitButton.disabled = false;
- submitButton.classList.remove("opacity-50", "cursor-not-allowed");
- submitButton.classList.add("hover:bg-blue-700", "hover:shadow-xl");
- // Restaurar el icono y texto original
- buttonIcon.className = "fas fa-play mr-2";
- submitButton.innerHTML =
- '<i class="fas fa-play mr-2"></i>Generar Reporte';
- }
- form.addEventListener("submit", function (event) {
- event.preventDefault();
- const formData = new FormData(form);
- const data = Object.fromEntries(formData.entries());
- const selectedChips = socialMediaChipsContainer.querySelectorAll(
- ".social-chip.selected"
- );
- data.social_media_sites = Array.from(selectedChips).map(
- (chip) => chip.dataset.value
- );
- const selectedOption =
- auditTypeSelect.options[auditTypeSelect.selectedIndex];
- const requiredInputs = selectedOption.dataset.requires?.split(",") || [];
- if (requiredInputs.includes("user") && !usernameFilterSelect.value) {
- toastr.error("Por favor, seleccione un usuario.", "Campo requerido");
- return;
- }
- if (requiredInputs.includes("keyword") && !data.keyword) {
- toastr.error(
- "Por favor, ingrese una palabra clave.",
- "Campo requerido"
- );
- return;
- }
- if (requiredInputs.includes("ip") && !data.ip_address) {
- toastr.error("Por favor, ingrese una dirección IP.", "Campo requerido");
- return;
- }
- if (requiredInputs.includes("response_code") && !data.response_code) {
- toastr.error(
- "Por favor, seleccione un código de respuesta.",
- "Campo requerido"
- );
- return;
- }
- if (
- requiredInputs.includes("social_media") &&
- data.social_media_sites.length === 0
- ) {
- toastr.error(
- "Por favor, seleccione al menos una red social.",
- "Campo requerido"
- );
- return;
- }
- /* toastr.info("Generando reporte, por favor espere...", "Procesando", {
- "timeOut": 0,
- "extendedTimeOut": 0,
- "tapToDismiss": false,
- "closeButton": false
- }); */
- // Deshabilitar el botón y cambiar su apariencia
- const submitButton = form.querySelector('button[type="submit"]');
- const buttonIcon = submitButton.querySelector("i");
- const buttonText =
- submitButton.querySelector("span") || submitButton.lastChild;
- submitButton.disabled = true;
- submitButton.classList.add("opacity-50", "cursor-not-allowed");
- submitButton.classList.remove("hover:bg-blue-700", "hover:shadow-xl");
- // Cambiar el icono a spinner
- buttonIcon.className = "fas fa-spinner fa-spin mr-2";
- // Cambiar el texto del botón
- if (buttonText.nodeType === Node.TEXT_NODE) {
- buttonText.textContent = "Generando Reporte...";
- } else {
- submitButton.innerHTML =
- '<i class="fas fa-spinner fa-spin mr-2"></i>Generando Reporte...';
- }
- resultsContainer.innerHTML = `
- <div class="bg-white p-6 rounded-lg shadow-lg min-h-[300px] flex items-center justify-center">
- <div class="text-center text-gray-500 py-10">
- <i class="fas fa-spinner fa-spin text-5xl mb-4 text-blue-500"></i>
- <h3 class="text-xl font-semibold">Generando reporte...</h3>
- </div>
- </div>`;
- fetch("/api/run-audit", {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify(data),
- })
- .then((response) => response.json())
- .then((result) => {
- toastr.clear();
- restoreSubmitButton();
- if (result.error) {
- toastr.error(result.error, "Error en la auditoría");
- } else {
- toastr.success(
- "Reporte generado exitosamente",
- "Auditoría completada"
- );
- }
- renderResults(data.audit_type, result, data);
- })
- .catch((error) => {
- toastr.clear();
- restoreSubmitButton();
- toastr.error("Error de conexión con el servidor", "Error de red");
- resultsContainer.innerHTML = `<div class="bg-white p-6 rounded-lg shadow-lg"><div class="text-center text-red-500 py-10"><strong>Error de red o del servidor:</strong> ${error}</div></div>`;
- });
- });
- function renderResults(auditType, data, formData) {
- let html = "";
- if (data.error) {
- html = `<div class="bg-white p-6 rounded-lg shadow-lg"><div class="text-center text-red-500 py-10"><strong>Error:</strong> ${data.error}</div></div>`;
- } else {
- if (auditType === "user_summary") {
- html = renderUserSummary(data);
- } else if (auditType === "top_users_data") {
- html = renderTopUsers(data);
- } else if (auditType === "daily_activity") {
- const user = formData.username;
- const date = formData.start_date;
- let title = `Distribución de Peticiones para '${user}' el ${date}`;
- if (data.total_requests > 0) {
- html = `<div class="bg-white p-6 rounded-lg shadow-lg">
- <h2 class="text-2xl font-bold mb-2">${title}</h2>
- <p class="text-gray-600 mb-4">Total de Peticiones en el día: <span class="font-bold text-blue-600">${data.total_requests.toLocaleString()}</span></p>
- <canvas id="daily-activity-chart"></canvas>
- </div>`;
- } else {
- html = `<div class="bg-white p-6 rounded-lg shadow-lg"><h2 class="text-2xl font-bold mb-4">${title}</h2><p class="text-gray-500">No se encontraron peticiones para la fecha y usuario seleccionado.</p></div>`;
- }
- } else if (
- [
- "keyword_search",
- "ip_activity",
- "response_code_search",
- "social_media_activity",
- ].includes(auditType)
- ) {
- let title = "";
- if (auditType === "keyword_search") {
- const user = formData.username || "Todos";
- title = `Resultados para la palabra clave "${formData.keyword}" para: ${user}`;
- } else if (auditType === "ip_activity") {
- title = `Resultados de Búsqueda para la IP: ${formData.ip_address}`;
- } else if (auditType === "response_code_search") {
- const user = formData.username || "Todos";
- title = `Resultados por Código de Respuesta ${formData.response_code} para: ${user}`;
- } else if (auditType === "social_media_activity") {
- const user = formData.username || "Todos";
- const sites = formData.social_media_sites.join(", ");
- title = `Actividad en Redes Sociales (${sites}) para: ${user}`;
- }
- html = renderNestedAccordionResults(title, data.results);
- } else {
- html =
- '<div class="bg-white p-6 rounded-lg shadow-lg"><p>Tipo de reporte no reconocido.</p></div>';
- }
- }
- resultsContainer.innerHTML = html;
- const dailyActivityChartCanvas = document.getElementById(
- "daily-activity-chart"
- );
- if (dailyActivityChartCanvas && data.hourly_activity) {
- renderDailyActivityChart(
- dailyActivityChartCanvas,
- data.hourly_activity
- );
- }
- const domainChartCanvas = document.getElementById("domain-chart");
- if (domainChartCanvas && data.top_domains) {
- renderDomainChart(domainChartCanvas, data.top_domains);
- }
- const responseChartCanvas = document.getElementById("response-chart");
- if (responseChartCanvas && data.response_summary) {
- renderResponseChart(responseChartCanvas, data.response_summary);
- }
- }
- function formatBytes(bytes, decimals = 2) {
- if (!+bytes) return "0 Bytes";
- const k = 1024;
- const dm = decimals < 0 ? 0 : decimals;
- const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
- const i = Math.floor(Math.log(bytes) / Math.log(k));
- return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
- }
- function formatTime(datetimeString) {
- if (!datetimeString || typeof datetimeString !== "string") {
- return "--";
- }
- try {
- const parts = datetimeString.split(" ");
- if (parts.length < 5) return "--";
- const timePart = parts[4];
- const [hoursStr, minutesStr, secondsStr] = timePart.split(":");
- let hour = parseInt(hoursStr, 10);
- if (isNaN(hour)) return "--";
- const ampm = hour >= 12 ? "PM" : "AM";
- hour = hour % 12;
- hour = hour || 12;
- const formattedHour = String(hour).padStart(2, "0");
- return `${formattedHour}:${minutesStr}:${secondsStr} ${ampm}`;
- } catch (error) {
- console.error(
- "Error crítico al formatear la hora:",
- error,
- "Input was:",
- datetimeString
- );
- return "--";
- }
- }
- function renderNestedAccordionResults(title, results) {
- if (!results || results.length === 0) {
- return `<div class="bg-white p-6 rounded-lg shadow-lg"><h2 class="text-2xl font-bold mb-4">${title}</h2><p class="text-gray-500">No se encontraron registros.</p></div>`;
- }
- const usersData = {};
- results.forEach((item) => {
- if (!usersData[item.username]) usersData[item.username] = {};
- const dateStr = `${item.log_date.substring(
- 0,
- 4
- )}-${item.log_date.substring(4, 6)}-${item.log_date.substring(6, 8)}`;
- if (!usersData[item.username][dateStr]) {
- usersData[item.username][dateStr] = {
- daily_requests: 0,
- daily_data: 0,
- activities: [],
- };
- }
- const dateGroup = usersData[item.username][dateStr];
- dateGroup.activities.push(item);
- dateGroup.daily_requests += Number(item.access_count) || 0;
- dateGroup.daily_data += Number(item.total_data) || 0;
- });
- let html = `<h2 class="text-2xl font-bold mb-4">${title}</h2>`;
- for (const username in usersData) {
- const userDataByDate = usersData[username];
- html += `
- <div class="border rounded-lg mb-3 shadow-md bg-white">
- <div class="bg-gray-100 p-3 flex justify-between items-center cursor-pointer hover:bg-gray-200" onclick="toggleDetails(this)">
- <h3 class="font-bold text-gray-800 flex items-center"><i class="fas fa-user mr-3 text-blue-600"></i>${username}</h3>
- <i class="fas fa-chevron-down toggle-icon transition-transform"></i>
- </div>
- <div class="user-card-details border-t border-gray-200" style="display: none;">`;
- const sortedDates = Object.keys(userDataByDate).sort().reverse();
- for (const date of sortedDates) {
- const dateData = userDataByDate[date];
- html += `<div class="border-b last:border-b-0">
- <div class="bg-blue-50 p-2 pl-6 flex justify-between items-center cursor-pointer hover:bg-blue-100" onclick="toggleDetails(this)">
- <h4 class="font-semibold text-blue-800"><i class="fas fa-calendar-alt mr-2"></i>${date}</h4>
- <div class="flex items-center space-x-4 text-sm">
- <span class="font-medium text-blue-700"><i class="fas fa-hashtag mr-1"></i>${dateData.daily_requests.toLocaleString()} Peticiones</span>
- <span class="font-medium text-green-700"><i class="fas fa-database mr-1"></i>${formatBytes(
- dateData.daily_data
- )}</span>
- <i class="fas fa-chevron-down toggle-icon transition-transform text-xs"></i>
- </div>
- </div>
- <div class="date-details p-3" style="display: none;">
- <table class="w-full text-left text-sm mt-2">
- <thead class="border-b-2 border-gray-300">
- <tr>
- <th class="py-1 px-2 text-center">Hora (Últ. Acceso)</th>
- <th class="py-1 px-2">URL</th>
- <th class="py-1 px-2 text-center">Intentos</th>
- <th class="py-1 px-2">Datos</th>
- </tr>
- </thead>
- <tbody class="divide-y divide-gray-100">`;
- const ensureProtocol = (url) =>
- url.startsWith("http") ? url : `http://${url}`;
- dateData.activities.forEach((activity) => {
- html += `<tr class="hover:bg-gray-50">
- <td class="py-1 px-2 text-center font-mono text-xs">${formatTime(
- activity.last_seen
- )}</td>
- <td class="py-1 px-2 truncate">
- <a href="${ensureProtocol(
- activity.url
- )}" target="_blank" class="text-blue-600 hover:underline" title="${
- activity.url
- }">${activity.url}</a>
- </td>
- <td class="py-1 px-2 text-center">${
- activity.access_count
- }</td>
- <td class="py-1 px-2">${formatBytes(
- activity.total_data
- )}</td>
- </tr>`;
- });
- html += `</tbody></table></div></div>`;
- }
- html += `</div></div>`;
- }
- return html;
- }
- function renderUserSummary(data) {
- const topDomainsHtml = data.top_domains
- .map(
- (d) =>
- `<li>${d.domain} <span class="font-bold">(${d.count} reqs)</span></li>`
- )
- .join("");
- const responseSummaryHtml = data.response_summary
- .slice(0, 5)
- .map(
- (r) =>
- `<li>${
- r.code
- }: <span class="font-bold">${r.count.toLocaleString()}</span></li>`
- )
- .join("");
- return `<div class="bg-white p-6 rounded-lg shadow-lg">
- <h2 class="text-2xl font-bold mb-4">Resumen de Actividad</h2>
- <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
- <div class="bg-blue-100 p-4 rounded-lg text-center"><div class="text-3xl font-bold">${data.total_requests.toLocaleString()}</div><div class="text-sm">Peticiones Totales</div></div>
- <div class="bg-green-100 p-4 rounded-lg text-center"><div class="text-3xl font-bold">${
- data.total_data_gb
- }</div><div class="text-sm">GB Consumidos</div></div>
- <div class="bg-gray-100 p-4 rounded-lg">
- <h4 class="font-semibold text-sm mb-1 text-center">Resumen de Respuestas</h4>
- <ul class="text-xs space-y-1">${responseSummaryHtml}</ul>
- </div>
- </div>
- <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
- <div>
- <h3 class="text-xl font-semibold mb-2">Top 15 Dominios Visitados</h3>
- <canvas id="domain-chart"></canvas>
- </div>
- <div>
- <h3 class="text-xl font-semibold mb-2">Distribución de Respuestas HTTP</h3>
- <canvas id="response-chart"></canvas>
- </div>
- </div></div>`;
- }
- function renderTopUsers(data) {
- const rows = data.top_users
- .map(
- (user, index) => `
- <tr class="hover:bg-gray-50">
- <td class="p-3">${index + 1}</td>
- <td class="p-3 font-medium">${user.username}</td>
- <td class="p-3 font-mono">${user.total_data_gb.toFixed(
- 2
- )} GB</td>
- </tr>`
- )
- .join("");
- return `<div class="bg-white p-6 rounded-lg shadow-lg"><h2 class="text-2xl font-bold mb-4">Top 10 Usuarios por Consumo de Datos</h2>
- <table class="w-full text-left">
- <thead class="bg-gray-100"><tr><th class="p-3">#</th><th class="p-3">Usuario</th><th class="p-3">Datos Consumidos</th></tr></thead>
- <tbody class="divide-y">${rows}</tbody>
- </table></div>`;
- }
- function renderDailyActivityChart(canvas, hourlyData) {
- const labels = Array.from({ length: 24 }, (_, i) => {
- const hour = i % 12 === 0 ? 12 : i % 12;
- const ampm = i < 12 ? "AM" : "PM";
- return `${hour} ${ampm}`;
- });
- new Chart(canvas, {
- type: "bar",
- data: {
- labels: labels,
- datasets: [
- {
- label: "Peticiones por Hora",
- data: hourlyData,
- backgroundColor: "rgba(59, 130, 246, 0.5)",
- borderColor: "rgba(37, 99, 235, 1)",
- borderWidth: 1,
- },
- ],
- },
- options: {
- responsive: true,
- scales: {
- x: {
- grid: { display: false },
- title: { display: true, text: "Hora del Día" },
- },
- y: {
- beginAtZero: true,
- title: { display: true, text: "Número de Peticiones" },
- },
- },
- plugins: {
- legend: { display: false },
- },
- },
- });
- }
- function renderDomainChart(canvas, domains) {
- // Genera colores equidistantes en el espectro HSL
- const generateColors = (count) =>
- Array.from(
- { length: count },
- (_, i) => `hsl(${((i * 360) / count) % 360}, 70%, 50%)`
- );
- // Configuración del gráfico
- const config = {
- type: "pie",
- data: {
- labels: domains.map((d) => d.domain),
- datasets: [
- {
- label: "Requests",
- data: domains.map((d) => d.count),
- backgroundColor: generateColors(domains.length),
- },
- ],
- },
- options: {
- responsive: true,
- plugins: {
- legend: {
- position: "right",
- labels: {
- boxWidth: 12,
- font: { size: 10 },
- generateLabels: (chart) => {
- const { labels, datasets } = chart.data;
- if (!labels.length || !datasets.length) return [];
- return labels.map((label, i) => ({
- text: `${label} (${datasets[0].data[i]})`,
- fillStyle: datasets[0].backgroundColor[i],
- strokeStyle: datasets[0].backgroundColor[i],
- lineWidth: 0,
- index: i,
- }));
- },
- },
- },
- tooltip: {
- callbacks: {
- label: (context) => {
- const label = context.label || "";
- const value = context.raw || 0;
- const total = context.dataset.data.reduce((a, b) => a + b, 0);
- const percentage = Math.round((value / total) * 100);
- return `${label}: ${value} (${percentage}%)`;
- },
- },
- },
- },
- },
- };
- return new Chart(canvas, config);
- }
- function renderResponseChart(canvas, responses) {
- // Mapeo de códigos de respuesta a colores
- const getResponseColor = (code) => {
- if (code >= 500) return "#DC2626"; // Error de servidor
- if (code >= 400) return "#F59E0B"; // Error de cliente
- if (code >= 300) return "#3B82F6"; // Redirección
- if (code >= 200) return "#10B981"; // Éxito
- return "#6B7280"; // Otros
- };
- // Configuración del gráfico
- const config = {
- type: "pie",
- data: {
- labels: responses.map((r) => `HTTP ${r.code}`),
- datasets: [
- {
- label: "Respuestas",
- data: responses.map((r) => r.count),
- backgroundColor: responses.map((r) => getResponseColor(r.code)),
- borderWidth: 1,
- },
- ],
- },
- options: {
- responsive: true,
- maintainAspectRatio: true,
- plugins: {
- legend: {
- position: "right",
- labels: {
- font: {
- size: 12,
- },
- padding: 10,
- usePointStyle: true,
- pointStyle: "circle",
- },
- },
- tooltip: {
- callbacks: {
- label: (context) => {
- const label = context.label.replace("HTTP ", "") || "";
- const value = context.raw || 0;
- const total = context.dataset.data.reduce((a, b) => a + b, 0);
- const percentage = Math.round((value / total) * 100);
- return `${label}: ${value} (${percentage}%)`;
- },
- },
- },
- },
- },
- };
- return new Chart(canvas, config);
- }
- });
- </script>
|