reports.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. {% extends 'layouts/base.html' %}
  2. {% block header_actions %}
  3. <!-- Date filter in the header -->
  4. <div class="flex flex-col xs:flex-row items-stretch xs:items-center gap-2 w-full xs:w-auto">
  5. <div class="bg-white p-3 rounded-lg shadow-sm border border-gray-200 hover:shadow-lg transition-shadow duration-200 flex-shrink-0">
  6. <div class="reports-date-picker flex items-center gap-2">
  7. <div class="flex items-center justify-center w-6 h-6 bg-indigo-50 rounded-md">
  8. <svg xmlns="http://www.w3.org/2000/svg" class="h-3 w-3 text-indigo-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
  9. <path stroke-linecap="round" stroke-linejoin="round" d="M8 7V3m8 4V3M3 11h18M5 21h14a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2z"/>
  10. </svg>
  11. </div>
  12. <input id="reports-date" type="text" datepicker datepicker-orientation="bottom" datepicker-autohide datepicker-container="body" class="w-28 xs:w-32 rounded-md border border-gray-300 bg-white px-2 py-1 text-sm text-gray-900 placeholder-gray-500 focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500 transition-colors duration-200"/>
  13. <button id="reports-go" class="inline-flex items-center justify-center px-2 py-1 border border-transparent text-xs xs:text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 transition-colors duration-200 disabled:opacity-50 disabled:cursor-not-allowed">
  14. <svg xmlns="http://www.w3.org/2000/svg" class="h-3 w-3 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
  15. <path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
  16. </svg>
  17. Ver
  18. </button>
  19. </div>
  20. </div>
  21. </div>
  22. {% endblock %}
  23. {% block styles %}
  24. <script src="{{ url_for('static', filename='js/tailwindcss.js') }}"></script>
  25. <script src="{{ url_for('static', filename='js/chart.js') }}"></script>
  26. <script src="{{ url_for('static', filename='js/chartjs-plugin-datalabels.js') }}"></script>
  27. {% endblock styles %}
  28. {% block content %}
  29. {% include 'partials/graph_reports.html' %}
  30. {% endblock %}
  31. {% block scripts %}
  32. <script type="application/json" id="chart-data">
  33. {
  34. "usersActivity": {
  35. "labels": {{ metrics.top_users_by_activity | map(attribute='username') | list | tojson | safe }},
  36. "data": {{ metrics.top_users_by_activity | map(attribute='total_visits') | list | tojson | safe }}
  37. },
  38. "usersData": {
  39. "labels": {{ metrics.top_users_by_data_transferred | map(attribute='username') | list | tojson | safe }},
  40. "data": {{ metrics.top_users_by_data_transferred | map(attribute='total_data_bytes') | map('divide', 1048576) | list | tojson | safe }}
  41. },
  42. "httpCodes": {
  43. "labels": {{ metrics.http_response_distribution_chart.labels | tojson | safe }},
  44. "data": {{ metrics.http_response_distribution_chart.data | tojson | safe }},
  45. "colors": {{ metrics.http_response_distribution_chart.colors | tojson | safe }}
  46. }
  47. }
  48. </script>
  49. <script>
  50. // Chart.js configuration
  51. document.addEventListener("DOMContentLoaded", function () {
  52. // Load data from JSON script tag
  53. const chartDataElement = document.getElementById("chart-data");
  54. const chartData = JSON.parse(chartDataElement.textContent);
  55. const chartColors = [
  56. "#3B82F6",
  57. "#10B981",
  58. "#F59E0B",
  59. "#EF4444",
  60. "#8B5CF6",
  61. "#EC4899",
  62. "#14B8A6",
  63. "#F97316",
  64. "#64748B",
  65. "#84CC16",
  66. "#06B6D4",
  67. "#D946EF",
  68. "#F43F5E",
  69. "#0EA5E9",
  70. "#22C55E",
  71. "#EAB308",
  72. "#F43F5E",
  73. "#8B5CF6",
  74. "#EC4899",
  75. "#14B8A6",
  76. ];
  77. // Helper function to get text color based on theme
  78. function getTextColor() {
  79. return document.documentElement.classList.contains("dark")
  80. ? "#d1d5db"
  81. : "#374151";
  82. }
  83. // Helper function to get border color based on theme
  84. function getBorderColor() {
  85. return document.documentElement.classList.contains("dark")
  86. ? "#374151"
  87. : "#fff";
  88. }
  89. // Users Activity Chart
  90. const usersActivityCtx = document.getElementById("usersActivityChart");
  91. if (usersActivityCtx && chartData.usersActivity) {
  92. new Chart(usersActivityCtx.getContext("2d"), {
  93. type: "bar",
  94. data: {
  95. labels: chartData.usersActivity.labels,
  96. datasets: [
  97. {
  98. label: "Visitas",
  99. data: chartData.usersActivity.data,
  100. backgroundColor: chartColors.slice(
  101. 0,
  102. chartData.usersActivity.data.length
  103. ),
  104. borderColor: chartColors
  105. .slice(0, chartData.usersActivity.data.length)
  106. .map((color) => color + "DD"),
  107. borderWidth: 1,
  108. },
  109. ],
  110. },
  111. options: {
  112. responsive: true,
  113. maintainAspectRatio: false,
  114. plugins: {
  115. legend: { display: false },
  116. tooltip: { mode: "index", intersect: false },
  117. datalabels: { display: false },
  118. },
  119. scales: {
  120. y: {
  121. beginAtZero: true,
  122. title: { display: true, text: "Número de Visitas" },
  123. ticks: { color: getTextColor() },
  124. },
  125. x: {
  126. ticks: {
  127. autoSkip: false,
  128. color: getTextColor(),
  129. },
  130. },
  131. },
  132. },
  133. });
  134. }
  135. // Users Data Chart
  136. const usersDataCtx = document.getElementById("usersDataChart");
  137. if (usersDataCtx && chartData.usersData) {
  138. new Chart(usersDataCtx.getContext("2d"), {
  139. type: "bar",
  140. data: {
  141. labels: chartData.usersData.labels,
  142. datasets: [
  143. {
  144. label: "Datos (MB)",
  145. data: chartData.usersData.data,
  146. backgroundColor: chartColors.slice(
  147. 0,
  148. chartData.usersData.data.length
  149. ),
  150. borderColor: chartColors
  151. .slice(0, chartData.usersData.data.length)
  152. .map((color) => color + "DD"),
  153. borderWidth: 1,
  154. },
  155. ],
  156. },
  157. options: {
  158. responsive: true,
  159. maintainAspectRatio: false,
  160. plugins: {
  161. legend: { display: false },
  162. tooltip: {
  163. mode: "index",
  164. intersect: false,
  165. callbacks: {
  166. label: function (context) {
  167. return (
  168. context.dataset.label +
  169. ": " +
  170. context.raw.toFixed(2) +
  171. " MB"
  172. );
  173. },
  174. },
  175. },
  176. datalabels: { display: false },
  177. },
  178. scales: {
  179. y: {
  180. beginAtZero: true,
  181. title: { display: true, text: "Datos Transmitidos (MB)" },
  182. ticks: { color: getTextColor() },
  183. },
  184. x: {
  185. ticks: {
  186. autoSkip: false,
  187. color: getTextColor(),
  188. },
  189. },
  190. },
  191. },
  192. });
  193. }
  194. // HTTP Codes Chart
  195. const httpCodesCtx = document.getElementById("httpCodesChart");
  196. if (httpCodesCtx && chartData.httpCodes) {
  197. new Chart(httpCodesCtx.getContext("2d"), {
  198. type: "pie",
  199. data: {
  200. labels: chartData.httpCodes.labels,
  201. datasets: [
  202. {
  203. data: chartData.httpCodes.data,
  204. backgroundColor: chartData.httpCodes.colors,
  205. borderColor: getBorderColor(),
  206. borderWidth: 2,
  207. },
  208. ],
  209. },
  210. options: {
  211. responsive: true,
  212. maintainAspectRatio: false,
  213. plugins: {
  214. legend: {
  215. position: "right",
  216. labels: {
  217. font: { size: 12 },
  218. color: getTextColor(),
  219. },
  220. },
  221. tooltip: {
  222. callbacks: {
  223. label: function (context) {
  224. const total = context.dataset.data.reduce(function (
  225. a,
  226. b
  227. ) {
  228. return a + b;
  229. },
  230. 0);
  231. const percentage = ((context.raw / total) * 100).toFixed(
  232. 1
  233. );
  234. return (
  235. context.label +
  236. ": " +
  237. context.raw +
  238. " (" +
  239. percentage +
  240. "%)"
  241. );
  242. },
  243. },
  244. },
  245. datalabels: {
  246. formatter: function (value, ctx) {
  247. const total = ctx.chart.data.datasets[0].data.reduce(
  248. function (a, b) {
  249. return a + b;
  250. },
  251. 0
  252. );
  253. const percentage = ((value / total) * 100).toFixed(1);
  254. return percentage > 3 ? percentage + "%" : "";
  255. },
  256. color: "#fff",
  257. font: {
  258. weight: "bold",
  259. size: 12,
  260. },
  261. },
  262. },
  263. },
  264. plugins: [ChartDataLabels],
  265. });
  266. }
  267. // Update chart colors when dark mode changes
  268. const observer = new MutationObserver(function (mutations) {
  269. mutations.forEach(function (mutation) {
  270. if (
  271. mutation.type === "attributes" &&
  272. mutation.attributeName === "class"
  273. ) {
  274. // Re-render charts with new colors when dark mode changes
  275. setTimeout(function () {
  276. Chart.helpers.each(Chart.instances, function (instance) {
  277. // Update scale colors
  278. if (
  279. instance.config.options &&
  280. instance.config.options.scales
  281. ) {
  282. const textColor = getTextColor();
  283. if (
  284. instance.config.options.scales.y &&
  285. instance.config.options.scales.y.ticks
  286. ) {
  287. instance.config.options.scales.y.ticks.color = textColor;
  288. }
  289. if (
  290. instance.config.options.scales.x &&
  291. instance.config.options.scales.x.ticks
  292. ) {
  293. instance.config.options.scales.x.ticks.color = textColor;
  294. }
  295. }
  296. // Update legend colors
  297. if (
  298. instance.config.options &&
  299. instance.config.options.plugins &&
  300. instance.config.options.plugins.legend &&
  301. instance.config.options.plugins.legend.labels
  302. ) {
  303. instance.config.options.plugins.legend.labels.color =
  304. getTextColor();
  305. }
  306. instance.update();
  307. });
  308. }, 100);
  309. }
  310. });
  311. });
  312. observer.observe(document.documentElement, {
  313. attributes: true,
  314. attributeFilter: ["class"],
  315. });
  316. });
  317. </script>
  318. <script src="{{ url_for('static', filename='js/reports-datepicker.js') }}"></script>
  319. {% endblock scripts %}