index.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 src="{{ url_for('static', filename='js/conexiones.js') }}"></script>
  34. <script>
  35. let isDetailedView = true;
  36. const SUMMARY_LABEL = "{{ _('Ver Resumen') }}";
  37. const DETAILS_LABEL = "{{ _('Ver Detalles') }}";
  38. function applyViewState() {
  39. const contents = document.querySelectorAll("[data-accordion-content]");
  40. const icons = document.querySelectorAll(".user-accordion .fa-chevron-down");
  41. const toggleAllButton = document.getElementById("toggle-all-button");
  42. if (contents.length === 0 || !toggleAllButton) return;
  43. if (isDetailedView) {
  44. contents.forEach((content) => content.style.display = '');
  45. icons.forEach((icon) => icon.classList.add("rotate-180"));
  46. toggleAllButton.textContent = SUMMARY_LABEL;
  47. } else {
  48. contents.forEach((content) => content.style.display = 'none');
  49. icons.forEach((icon) => icon.classList.remove("rotate-180"));
  50. toggleAllButton.textContent = DETAILS_LABEL;
  51. }
  52. }
  53. function setupSummaryButton() {
  54. const toggleAllButton = document.getElementById("toggle-all-button");
  55. if (!toggleAllButton) return;
  56. const newButton = toggleAllButton.cloneNode(true);
  57. toggleAllButton.parentNode.replaceChild(newButton, toggleAllButton);
  58. newButton.addEventListener("click", () => {
  59. isDetailedView = !isDetailedView;
  60. applyViewState();
  61. });
  62. }
  63. async function refrescarConexiones() {
  64. try {
  65. // CAMBIO: Usar la ruta principal con parámetro partial en lugar de ruta separada
  66. const response = await fetch("/?partial=true");
  67. if (!response.ok) throw new Error("Respuesta no OK");
  68. const html = await response.text();
  69. const container = document.getElementById("datos-conexiones");
  70. container.innerHTML = html;
  71. setupSummaryButton();
  72. applyViewState();
  73. // Restaurar el estado individual de los acordeones después de aplicar la vista global
  74. if (window.restoreAccordionState) window.restoreAccordionState();
  75. } catch (error) {
  76. console.error("Error al actualizar conexiones:", error);
  77. }
  78. }
  79. const REFRESH_KEY = 'squidstats.refreshIntervalSec';
  80. const VIEW_KEY = 'squidstats.viewMode';
  81. let refreshInterval = 60000; // ms
  82. let refreshTimer = null;
  83. let countdownTimer = null;
  84. let secondsLeft = 0;
  85. function safeGet(key) {
  86. try { return localStorage.getItem(key); } catch { return null; }
  87. }
  88. function safeSet(key, val) {
  89. try { localStorage.setItem(key, val); persistStatus('Guardado'); } catch { persistStatus('No se pudo guardar'); }
  90. }
  91. function persistStatus(msg) {
  92. const el = document.getElementById('persist-status');
  93. if (!el) return;
  94. el.textContent = msg;
  95. el.classList.remove('opacity-0');
  96. setTimeout(() => { el.classList.add('opacity-0'); }, 1600);
  97. }
  98. function getRefreshIntervalFromStorage() {
  99. const stored = safeGet(REFRESH_KEY);
  100. const val = parseInt(stored);
  101. return (!isNaN(val) && val >= 5) ? val : 60;
  102. }
  103. function setRefreshIntervalToStorage(val) { safeSet(REFRESH_KEY, String(val)); }
  104. function restoreViewState() {
  105. const stored = safeGet(VIEW_KEY);
  106. if (stored === 'summary') isDetailedView = false;
  107. }
  108. function saveViewState() { safeSet(VIEW_KEY, isDetailedView ? 'detailed' : 'summary'); }
  109. const originalApplyViewState = applyViewState;
  110. applyViewState = function() {
  111. originalApplyViewState();
  112. saveViewState();
  113. };
  114. function startRefreshTimer() {
  115. if (refreshTimer) clearInterval(refreshTimer);
  116. if (countdownTimer) clearInterval(countdownTimer);
  117. secondsLeft = refreshInterval / 1000;
  118. updateCountdown();
  119. refreshTimer = setInterval(() => {
  120. refrescarConexiones();
  121. secondsLeft = refreshInterval / 1000; // reset
  122. }, refreshInterval);
  123. countdownTimer = setInterval(() => {
  124. secondsLeft -= 1;
  125. if (secondsLeft < 0) secondsLeft = refreshInterval / 1000;
  126. updateCountdown();
  127. }, 1000);
  128. }
  129. function updateCountdown() {
  130. const el = document.getElementById('next-refresh');
  131. if (!el) return;
  132. el.textContent = secondsLeft + 's';
  133. }
  134. function applyNewInterval(valSec) {
  135. refreshInterval = valSec * 1000;
  136. setRefreshIntervalToStorage(valSec);
  137. startRefreshTimer();
  138. }
  139. document.addEventListener('DOMContentLoaded', () => {
  140. const storedRaw = safeGet(REFRESH_KEY);
  141. const intervalValue = getRefreshIntervalFromStorage();
  142. document.getElementById('refresh-interval').value = intervalValue;
  143. refreshInterval = intervalValue * 1000;
  144. if (storedRaw === null) {
  145. setRefreshIntervalToStorage(intervalValue);
  146. console.debug('[SquidStats] Intervalo por defecto almacenado:', intervalValue, 's');
  147. }
  148. restoreViewState();
  149. setupSummaryButton();
  150. applyViewState();
  151. // Restaurar el estado individual de los acordeones después de aplicar la vista global
  152. if (window.restoreAccordionState) window.restoreAccordionState();
  153. startRefreshTimer();
  154. const intervalInput = document.getElementById('refresh-interval');
  155. document.getElementById('set-interval-btn').addEventListener('click', () => {
  156. const val = parseInt(intervalInput.value);
  157. if (!isNaN(val) && val >= 5) {
  158. applyNewInterval(val);
  159. } else {
  160. intervalInput.value = getRefreshIntervalFromStorage();
  161. }
  162. });
  163. intervalInput.addEventListener('change', () => {
  164. const val = parseInt(intervalInput.value);
  165. if (!isNaN(val) && val >= 5) applyNewInterval(val);
  166. });
  167. });
  168. </script>
  169. {% endblock scripts %}