| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- {% extends 'layouts/base.html' %}
- {% from 'macros.html' import format_bandwidth %}
- {% set valid_users = {} %}
- {% for user, user_data in grouped_connections.items()%}
- {% if user and user != "-" and user != "Anónimo" %}
- {% set _ = valid_users.update({user: user_data}) %}
- {% endif %}
- {% endfor %}
- {% block header %}
- <!-- CONTAINER ALIGNED WITH THE REST OF THE CONTENT -->
- <div class="max-w-7xl mx-auto">
- <div class="flex flex-wrap items-center gap-3 mb-4 bg-white/70 dark:bg-gray-800/60 px-3 py-2 rounded-md border border-gray-200 shadow-sm text-sm w-full">
- <label for="refresh-interval" class="text-gray-700 dark:text-gray-200 font-medium select-none">⏱️</label>
- <div class="flex items-center gap-1">
- <input id="refresh-interval" type="number" min="10" class="w-20 px-2 py-1 border border-gray-300 dark:border-gray-600 rounded focus:ring-1 focus:ring-blue-500 focus:outline-none bg-white/80 dark:bg-gray-700/60 text-gray-700 dark:text-gray-100" placeholder="60" aria-label="{{ _("Intervalo de refresco (segundos)") }}">
- <span class="text-gray-500 dark:text-gray-400">s</span>
- </div>
- <div class="flex items-center gap-1 text-gray-600 dark:text-gray-300">
- <span class="hidden sm:inline">→</span>
- <span id="next-refresh" class="font-mono">--</span>
- </div>
- <button id="set-interval-btn" class="px-3 py-1 rounded bg-blue-600 hover:bg-blue-700 text-white font-medium transition text-xs">{{ _("Aplicar") }}</button>
- <span id="persist-status" class="text-[11px] text-gray-400 ml-auto pr-1 opacity-0 transition-opacity"></span>
- </div>
- </div>
- {% endblock header %}
- {% block content %}
- <div id="datos-conexiones" class="w-full">
- {% include 'partials/conexiones.html' %}
- </div>
- {% endblock %}
- {% block scripts %}
- <script src="{{ url_for('static', filename='js/conexiones.js') }}"></script>
- <script>
- let isDetailedView = true;
- const SUMMARY_LABEL = "{{ _('Ver Resumen') }}";
- const DETAILS_LABEL = "{{ _('Ver Detalles') }}";
- function applyViewState() {
- const contents = document.querySelectorAll("[data-accordion-content]");
- const icons = document.querySelectorAll(".user-accordion .fa-chevron-down");
- const toggleAllButton = document.getElementById("toggle-all-button");
- if (contents.length === 0 || !toggleAllButton) return;
- if (isDetailedView) {
- contents.forEach((content) => content.style.display = '');
- icons.forEach((icon) => icon.classList.add("rotate-180"));
- toggleAllButton.textContent = SUMMARY_LABEL;
- } else {
- contents.forEach((content) => content.style.display = 'none');
- icons.forEach((icon) => icon.classList.remove("rotate-180"));
- toggleAllButton.textContent = DETAILS_LABEL;
- }
- }
- function setupSummaryButton() {
- const toggleAllButton = document.getElementById("toggle-all-button");
- if (!toggleAllButton) return;
- const newButton = toggleAllButton.cloneNode(true);
- toggleAllButton.parentNode.replaceChild(newButton, toggleAllButton);
- newButton.addEventListener("click", () => {
- isDetailedView = !isDetailedView;
- applyViewState();
- });
- }
- async function refrescarConexiones() {
- try {
- // CAMBIO: Usar la ruta principal con parámetro partial en lugar de ruta separada
- const response = await fetch("/?partial=true");
- if (!response.ok) throw new Error("Respuesta no OK");
- const html = await response.text();
- const container = document.getElementById("datos-conexiones");
- container.innerHTML = html;
- setupSummaryButton();
- applyViewState();
- // Restaurar el estado individual de los acordeones después de aplicar la vista global
- if (window.restoreAccordionState) window.restoreAccordionState();
- } catch (error) {
- console.error("Error al actualizar conexiones:", error);
- }
- }
- const REFRESH_KEY = 'squidstats.refreshIntervalSec';
- const VIEW_KEY = 'squidstats.viewMode';
- let refreshInterval = 60000; // ms
- let refreshTimer = null;
- let countdownTimer = null;
- let secondsLeft = 0;
- function safeGet(key) {
- try { return localStorage.getItem(key); } catch { return null; }
- }
- function safeSet(key, val) {
- try { localStorage.setItem(key, val); persistStatus('Guardado'); } catch { persistStatus('No se pudo guardar'); }
- }
- function persistStatus(msg) {
- const el = document.getElementById('persist-status');
- if (!el) return;
- el.textContent = msg;
- el.classList.remove('opacity-0');
- setTimeout(() => { el.classList.add('opacity-0'); }, 1600);
- }
- function getRefreshIntervalFromStorage() {
- const stored = safeGet(REFRESH_KEY);
- const val = parseInt(stored);
- return (!isNaN(val) && val >= 5) ? val : 60;
- }
- function setRefreshIntervalToStorage(val) { safeSet(REFRESH_KEY, String(val)); }
- function restoreViewState() {
- const stored = safeGet(VIEW_KEY);
- if (stored === 'summary') isDetailedView = false;
- }
- function saveViewState() { safeSet(VIEW_KEY, isDetailedView ? 'detailed' : 'summary'); }
- const originalApplyViewState = applyViewState;
- applyViewState = function() {
- originalApplyViewState();
- saveViewState();
- };
- function startRefreshTimer() {
- if (refreshTimer) clearInterval(refreshTimer);
- if (countdownTimer) clearInterval(countdownTimer);
- secondsLeft = refreshInterval / 1000;
- updateCountdown();
- refreshTimer = setInterval(() => {
- refrescarConexiones();
- secondsLeft = refreshInterval / 1000; // reset
- }, refreshInterval);
- countdownTimer = setInterval(() => {
- secondsLeft -= 1;
- if (secondsLeft < 0) secondsLeft = refreshInterval / 1000;
- updateCountdown();
- }, 1000);
- }
- function updateCountdown() {
- const el = document.getElementById('next-refresh');
- if (!el) return;
- el.textContent = secondsLeft + 's';
- }
- function applyNewInterval(valSec) {
- refreshInterval = valSec * 1000;
- setRefreshIntervalToStorage(valSec);
- startRefreshTimer();
- }
- document.addEventListener('DOMContentLoaded', () => {
- const storedRaw = safeGet(REFRESH_KEY);
- const intervalValue = getRefreshIntervalFromStorage();
- document.getElementById('refresh-interval').value = intervalValue;
- refreshInterval = intervalValue * 1000;
- if (storedRaw === null) {
- setRefreshIntervalToStorage(intervalValue);
- console.debug('[SquidStats] Intervalo por defecto almacenado:', intervalValue, 's');
- }
- restoreViewState();
- setupSummaryButton();
- applyViewState();
- // Restaurar el estado individual de los acordeones después de aplicar la vista global
- if (window.restoreAccordionState) window.restoreAccordionState();
- startRefreshTimer();
- const intervalInput = document.getElementById('refresh-interval');
- document.getElementById('set-interval-btn').addEventListener('click', () => {
- const val = parseInt(intervalInput.value);
- if (!isNaN(val) && val >= 5) {
- applyNewInterval(val);
- } else {
- intervalInput.value = getRefreshIntervalFromStorage();
- }
- });
- intervalInput.addEventListener('change', () => {
- const val = parseInt(intervalInput.value);
- if (!isNaN(val) && val >= 5) applyNewInterval(val);
- });
- });
- </script>
- {% endblock scripts %}
|