clean_data.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. {% extends "admin/base.html" %}
  2. {% block title %}{{ _("Limpiar Datos - Administrador Squid Proxy") }}{% endblock %}
  3. {% block content %}
  4. <div class="max-w-4xl mx-auto">
  5. <!-- Header -->
  6. <div class="mb-8">
  7. <h2 class="text-3xl font-bold text-gray-800 mb-2">
  8. <i class="fas fa-database mr-3"></i>{{ _("Limpiar Datos") }}
  9. </h2>
  10. <p class="text-gray-600">{{ _("Selecciona una tabla y elimina todos sus datos") }}</p>
  11. </div>
  12. <!-- Warning Alert -->
  13. <div class="bg-red-50 border-l-4 border-red-500 p-4 mb-6">
  14. <div class="flex items-center">
  15. <i class="fas fa-exclamation-triangle text-red-500 text-xl mr-3"></i>
  16. <div>
  17. <p class="text-red-800 font-medium">{{ _("¡Advertencia!") }}</p>
  18. <p class="text-red-700 text-sm">{{ _("Esta acción es irreversible. Una vez eliminados los datos, no se pueden recuperar.") }}</p>
  19. </div>
  20. </div>
  21. </div>
  22. <!-- Clean Data Form -->
  23. <div class="bg-white rounded-lg shadow-md overflow-hidden">
  24. <div class="px-6 py-4 bg-gradient-to-r from-blue-600 to-blue-700 border-b border-blue-700">
  25. <h3 class="text-xl font-semibold text-white flex items-center">
  26. <i class="fas fa-trash-alt mr-2"></i>
  27. {{ _("Eliminar Datos de Tabla") }}
  28. </h3>
  29. </div>
  30. <div class="p-6">
  31. <form id="cleanDataForm" class="space-y-6">
  32. <!-- Table Selection -->
  33. <div>
  34. <label for="tableSelect" class="block text-sm font-medium text-gray-700 mb-2">
  35. <i class="fas fa-table mr-1"></i>{{ _("Seleccionar Tabla") }}
  36. </label>
  37. <select id="tableSelect" class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500">
  38. <option value="">{{ _("Cargando tablas...") }}</option>
  39. </select>
  40. </div>
  41. <!-- Table Info Display -->
  42. <div id="tableInfo" class="hidden bg-gray-50 p-4 rounded-lg border">
  43. <div class="flex items-center justify-between">
  44. <div>
  45. <h4 class="text-sm font-medium text-gray-900">{{ _("Información de la Tabla") }}</h4>
  46. <p id="tableDetails" class="text-sm text-gray-600 mt-1"></p>
  47. </div>
  48. <div id="actionButtons" class="hidden">
  49. <button type="button" id="cleanButton" class="bg-gradient-to-r from-red-500 to-red-600 hover:from-red-600 hover:to-red-700 text-white font-medium px-6 py-2 rounded-lg shadow-md hover:shadow-lg transition-all duration-200 flex items-center">
  50. <i class="fas fa-trash-alt mr-2"></i>
  51. {{ _("Limpiar Datos") }}
  52. </button>
  53. </div>
  54. </div>
  55. </div>
  56. </form>
  57. </div>
  58. </div>
  59. <!-- Info Card -->
  60. <div class="mt-6 bg-blue-50 border-l-4 border-blue-500 p-4">
  61. <div class="flex items-start">
  62. <i class="fas fa-info-circle text-blue-500 text-xl mr-3 mt-1"></i>
  63. <div>
  64. <p class="text-blue-800 font-medium mb-2">{{ _("Información Importante") }}</p>
  65. <ul class="text-blue-700 text-sm space-y-1 list-disc list-inside">
  66. <li>{{ _("Solo se eliminan los datos de la tabla, no la estructura") }}</li>
  67. <li>{{ _("Las tablas con sufijo de fecha (ej: log_20240121) son tablas diarias") }}</li>
  68. <li>{{ _("Algunas tablas son críticas para el funcionamiento del sistema") }}</li>
  69. <li>{{ _("Se recomienda hacer un respaldo antes de eliminar datos importantes") }}</li>
  70. </ul>
  71. </div>
  72. </div>
  73. </div>
  74. </div>
  75. <script>
  76. let tablesData = [];
  77. // Get CSRF token from meta tag
  78. function getCsrfToken() {
  79. return document.querySelector('meta[name="csrf-token"]').getAttribute('content');
  80. }
  81. function formatSize(bytes) {
  82. if (bytes === 0 || bytes === null || bytes === undefined) return '0 B';
  83. const k = 1024;
  84. const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
  85. const i = Math.floor(Math.log(bytes) / Math.log(k));
  86. return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
  87. }
  88. // Load tables on page load
  89. document.addEventListener('DOMContentLoaded', function() {
  90. loadTables();
  91. });
  92. async function loadTables() {
  93. try {
  94. const response = await fetch('/admin/api/get-tables', {
  95. method: 'GET',
  96. headers: {
  97. 'X-CSRFToken': getCsrfToken()
  98. }
  99. });
  100. const data = await response.json();
  101. if (data.status === 'success') {
  102. tablesData = data.tables;
  103. populateTableSelect();
  104. } else {
  105. showNotification('Error al cargar las tablas', 'error');
  106. }
  107. } catch (error) {
  108. console.error('Error:', error);
  109. showNotification('Error al cargar las tablas', 'error');
  110. }
  111. }
  112. function populateTableSelect() {
  113. const select = document.getElementById('tableSelect');
  114. select.innerHTML = '<option value="">{{ _('Selecciona una tabla...') }}</option>';
  115. tablesData.forEach(table => {
  116. const option = document.createElement('option');
  117. option.value = table.name;
  118. option.textContent = `${table.name} (${table.rows.toLocaleString()} registros, ${formatSize(table.size)})`;
  119. select.appendChild(option);
  120. });
  121. }
  122. // Handle table selection change
  123. document.getElementById('tableSelect').addEventListener('change', function() {
  124. const selectedTable = this.value;
  125. const tableInfo = document.getElementById('tableInfo');
  126. const tableDetails = document.getElementById('tableDetails');
  127. const actionButtons = document.getElementById('actionButtons');
  128. const cleanButton = document.getElementById('cleanButton');
  129. if (selectedTable) {
  130. const table = tablesData.find(t => t.name === selectedTable);
  131. if (table) {
  132. tableDetails.textContent = `Tabla: ${table.name} | Registros: ${table.rows.toLocaleString()} | Tamaño: ${formatSize(table.size)}`;
  133. tableInfo.classList.remove('hidden');
  134. if (table.has_data) {
  135. actionButtons.classList.remove('hidden');
  136. cleanButton.onclick = () => confirmDeleteTable(table.name, table.rows, table.size);
  137. } else {
  138. actionButtons.classList.add('hidden');
  139. tableDetails.textContent += ` | Tamaño: ${formatSize(table.size)} | Esta tabla está vacía`;
  140. }
  141. }
  142. } else {
  143. tableInfo.classList.add('hidden');
  144. }
  145. });
  146. function confirmDeleteTable(tableName, rowCount, size) {
  147. const message = `¿Está seguro de que desea eliminar TODOS los datos de la tabla "${tableName}"?\n\nSe eliminarán ${rowCount.toLocaleString()} registros.\nTamaño aproximado: ${formatSize(size)}\n\nEsta acción NO se puede deshacer.`;
  148. showCustomConfirm(message, async () => {
  149. await deleteTableData(tableName);
  150. });
  151. }
  152. async function deleteTableData(tableName) {
  153. try {
  154. showNotification('Eliminando datos...', 'info');
  155. const response = await fetch('/admin/api/delete-table-data', {
  156. method: 'POST',
  157. headers: {
  158. 'Content-Type': 'application/json',
  159. 'X-CSRFToken': getCsrfToken()
  160. },
  161. body: JSON.stringify({ table_name: tableName })
  162. });
  163. const data = await response.json();
  164. if (data.status === 'success') {
  165. showNotification(data.message, 'success');
  166. // Reload tables after 1.5 seconds
  167. setTimeout(() => {
  168. loadTables();
  169. // Reset form
  170. document.getElementById('tableSelect').value = '';
  171. document.getElementById('tableInfo').classList.add('hidden');
  172. }, 1500);
  173. } else {
  174. showNotification(data.message || 'Error al eliminar los datos', 'error');
  175. }
  176. } catch (error) {
  177. console.error('Error:', error);
  178. showNotification('Error al eliminar los datos de la tabla', 'error');
  179. }
  180. }
  181. function showNotification(message, type) {
  182. if (window.toastr) {
  183. if (type === 'success') {
  184. toastr.success(message);
  185. } else if (type === 'error') {
  186. toastr.error(message);
  187. } else if (type === 'warning') {
  188. toastr.warning(message);
  189. } else {
  190. toastr.info(message);
  191. }
  192. }
  193. }
  194. </script>
  195. {% endblock %}