index.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. {% extends 'layouts/base.html' %}
  2. {% from 'macros.html' import format_bandwidth %}
  3. {% set valid_users = {} %}
  4. {% for user, user_data in grouped_connections.items()%}
  5. {% if user and user != "-" and user != "Anónimo" %}
  6. {% set _ = valid_users.update({user: user_data}) %}
  7. {% endif %}
  8. {% endfor %}
  9. {% block header %}
  10. <!-- CONTAINER ALIGNED WITH THE REST OF THE CONTENT -->
  11. <div class="max-w-7xl mx-auto">
  12. <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">
  13. <label for="refresh-interval" class="text-gray-700 dark:text-gray-200 font-medium select-none">⏱️</label>
  14. <div class="flex items-center gap-1">
  15. <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)">
  16. <span class="text-gray-500 dark:text-gray-400">s</span>
  17. </div>
  18. <div class="flex items-center gap-1 text-gray-600 dark:text-gray-300">
  19. <span class="hidden sm:inline">→</span>
  20. <span id="next-refresh" class="font-mono">--</span>
  21. </div>
  22. <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>
  23. <span id="persist-status" class="text-[11px] text-gray-400 ml-auto pr-1 opacity-0 transition-opacity"></span>
  24. </div>
  25. </div>
  26. {% endblock header %}
  27. {% block content %}
  28. <div id="datos-conexiones" class="w-full">
  29. {% include 'partials/conexiones.html' %}
  30. </div>
  31. {% endblock %}
  32. {% block scripts %}
  33. <script>
  34. let isDetailedView = true;
  35. function applyViewState() {
  36. const contents = document.querySelectorAll("[data-accordion-content]");
  37. const icons = document.querySelectorAll(".user-accordion .fa-chevron-down");
  38. const toggleAllButton = document.getElementById("toggle-all-button");
  39. if (contents.length === 0 || !toggleAllButton) return;
  40. if (isDetailedView) {
  41. contents.forEach((content) => content.style.display = '');
  42. icons.forEach((icon) => icon.classList.add("rotate-180"));
  43. toggleAllButton.textContent = "Ver Resumen";
  44. } else {
  45. contents.forEach((content) => content.style.display = 'none');
  46. icons.forEach((icon) => icon.classList.remove("rotate-180"));
  47. toggleAllButton.textContent = "Ver Detalles";
  48. }
  49. }
  50. function setupSummaryButton() {
  51. const toggleAllButton = document.getElementById("toggle-all-button");
  52. if (!toggleAllButton) return;
  53. const newButton = toggleAllButton.cloneNode(true);
  54. toggleAllButton.parentNode.replaceChild(newButton, toggleAllButton);
  55. newButton.addEventListener("click", () => {
  56. isDetailedView = !isDetailedView;
  57. applyViewState();
  58. });
  59. }
  60. async function refrescarConexiones() {
  61. try {
  62. // CAMBIO: Usar la ruta principal con parámetro partial en lugar de ruta separada
  63. const response = await fetch("/?partial=true");
  64. if (!response.ok) throw new Error("Respuesta no OK");
  65. const html = await response.text();
  66. const container = document.getElementById("datos-conexiones");
  67. container.innerHTML = html;
  68. setupSummaryButton();
  69. applyViewState();
  70. // Restaurar el estado individual de los acordeones después de aplicar la vista global
  71. if (window.restoreAccordionState) window.restoreAccordionState();
  72. } catch (error) {
  73. console.error("Error al actualizar conexiones:", error);
  74. }
  75. }
  76. const REFRESH_KEY = 'squidstats.refreshIntervalSec';
  77. const VIEW_KEY = 'squidstats.viewMode';
  78. let refreshInterval = 60000; // ms
  79. let refreshTimer = null;
  80. let countdownTimer = null;
  81. let secondsLeft = 0;
  82. function safeGet(key) {
  83. try { return localStorage.getItem(key); } catch { return null; }
  84. }
  85. function safeSet(key, val) {
  86. try { localStorage.setItem(key, val); persistStatus('Guardado'); } catch { persistStatus('No se pudo guardar'); }
  87. }
  88. function persistStatus(msg) {
  89. const el = document.getElementById('persist-status');
  90. if (!el) return;
  91. el.textContent = msg;
  92. el.classList.remove('opacity-0');
  93. setTimeout(() => { el.classList.add('opacity-0'); }, 1600);
  94. }
  95. function getRefreshIntervalFromStorage() {
  96. const stored = safeGet(REFRESH_KEY);
  97. const val = parseInt(stored);
  98. return (!isNaN(val) && val >= 5) ? val : 60;
  99. }
  100. function setRefreshIntervalToStorage(val) { safeSet(REFRESH_KEY, String(val)); }
  101. function restoreViewState() {
  102. const stored = safeGet(VIEW_KEY);
  103. if (stored === 'summary') isDetailedView = false;
  104. }
  105. function saveViewState() { safeSet(VIEW_KEY, isDetailedView ? 'detailed' : 'summary'); }
  106. const originalApplyViewState = applyViewState;
  107. applyViewState = function() {
  108. originalApplyViewState();
  109. saveViewState();
  110. };
  111. function startRefreshTimer() {
  112. if (refreshTimer) clearInterval(refreshTimer);
  113. if (countdownTimer) clearInterval(countdownTimer);
  114. secondsLeft = refreshInterval / 1000;
  115. updateCountdown();
  116. refreshTimer = setInterval(() => {
  117. refrescarConexiones();
  118. secondsLeft = refreshInterval / 1000; // reset
  119. }, refreshInterval);
  120. countdownTimer = setInterval(() => {
  121. secondsLeft -= 1;
  122. if (secondsLeft < 0) secondsLeft = refreshInterval / 1000;
  123. updateCountdown();
  124. }, 1000);
  125. }
  126. function updateCountdown() {
  127. const el = document.getElementById('next-refresh');
  128. if (!el) return;
  129. el.textContent = secondsLeft + 's';
  130. }
  131. function applyNewInterval(valSec) {
  132. refreshInterval = valSec * 1000;
  133. setRefreshIntervalToStorage(valSec);
  134. startRefreshTimer();
  135. }
  136. document.addEventListener('DOMContentLoaded', () => {
  137. const storedRaw = safeGet(REFRESH_KEY);
  138. const intervalValue = getRefreshIntervalFromStorage();
  139. document.getElementById('refresh-interval').value = intervalValue;
  140. refreshInterval = intervalValue * 1000;
  141. if (storedRaw === null) {
  142. setRefreshIntervalToStorage(intervalValue);
  143. console.debug('[SquidStats] Intervalo por defecto almacenado:', intervalValue, 's');
  144. }
  145. restoreViewState();
  146. setupSummaryButton();
  147. applyViewState();
  148. // Restaurar el estado individual de los acordeones después de aplicar la vista global
  149. if (window.restoreAccordionState) window.restoreAccordionState();
  150. startRefreshTimer();
  151. const intervalInput = document.getElementById('refresh-interval');
  152. document.getElementById('set-interval-btn').addEventListener('click', () => {
  153. const val = parseInt(intervalInput.value);
  154. if (!isNaN(val) && val >= 5) {
  155. applyNewInterval(val);
  156. } else {
  157. intervalInput.value = getRefreshIntervalFromStorage();
  158. }
  159. });
  160. intervalInput.addEventListener('change', () => {
  161. const val = parseInt(intervalInput.value);
  162. if (!isNaN(val) && val >= 5) applyNewInterval(val);
  163. });
  164. });
  165. </script>
  166. {% endblock scripts %}