| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- {% extends 'layouts/base.html' %}
- {% block header_actions %}
- <!-- Date filter in the header -->
- <div class="flex flex-col xs:flex-row items-stretch xs:items-center gap-2 w-full xs:w-auto">
- <div class="bg-white p-3 rounded-lg shadow-sm border border-gray-200 hover:shadow-lg transition-shadow duration-200 flex-shrink-0">
- <div class="reports-date-picker flex items-center gap-2">
- <div class="flex items-center justify-center w-6 h-6 bg-indigo-50 rounded-md">
- <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">
- <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"/>
- </svg>
- </div>
- <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"/>
- <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">
- <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">
- <path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
- </svg>
- Ver
- </button>
- </div>
- </div>
- </div>
- {% endblock %}
- {% block styles %}
- <script src="{{ url_for('static', filename='js/tailwindcss.js') }}"></script>
- <script src="{{ url_for('static', filename='js/chart.js') }}"></script>
- <script src="{{ url_for('static', filename='js/chartjs-plugin-datalabels.js') }}"></script>
- {% endblock styles %}
- {% block content %}
- {% include 'partials/graph_reports.html' %}
- {% endblock %}
- {% block scripts %}
- <script type="application/json" id="chart-data">
- {
- "usersActivity": {
- "labels": {{ metrics.top_users_by_activity | map(attribute='username') | list | tojson | safe }},
- "data": {{ metrics.top_users_by_activity | map(attribute='total_visits') | list | tojson | safe }}
- },
- "usersData": {
- "labels": {{ metrics.top_users_by_data_transferred | map(attribute='username') | list | tojson | safe }},
- "data": {{ metrics.top_users_by_data_transferred | map(attribute='total_data_bytes') | map('divide', 1048576) | list | tojson | safe }}
- },
- "httpCodes": {
- "labels": {{ metrics.http_response_distribution_chart.labels | tojson | safe }},
- "data": {{ metrics.http_response_distribution_chart.data | tojson | safe }},
- "colors": {{ metrics.http_response_distribution_chart.colors | tojson | safe }}
- }
- }
- </script>
- <script>
- // Chart.js configuration
- document.addEventListener("DOMContentLoaded", function () {
- // Load data from JSON script tag
- const chartDataElement = document.getElementById("chart-data");
- const chartData = JSON.parse(chartDataElement.textContent);
- const chartColors = [
- "#3B82F6",
- "#10B981",
- "#F59E0B",
- "#EF4444",
- "#8B5CF6",
- "#EC4899",
- "#14B8A6",
- "#F97316",
- "#64748B",
- "#84CC16",
- "#06B6D4",
- "#D946EF",
- "#F43F5E",
- "#0EA5E9",
- "#22C55E",
- "#EAB308",
- "#F43F5E",
- "#8B5CF6",
- "#EC4899",
- "#14B8A6",
- ];
- // Helper function to get text color based on theme
- function getTextColor() {
- return document.documentElement.classList.contains("dark")
- ? "#d1d5db"
- : "#374151";
- }
- // Helper function to get border color based on theme
- function getBorderColor() {
- return document.documentElement.classList.contains("dark")
- ? "#374151"
- : "#fff";
- }
- // Users Activity Chart
- const usersActivityCtx = document.getElementById("usersActivityChart");
- if (usersActivityCtx && chartData.usersActivity) {
- new Chart(usersActivityCtx.getContext("2d"), {
- type: "bar",
- data: {
- labels: chartData.usersActivity.labels,
- datasets: [
- {
- label: "Visitas",
- data: chartData.usersActivity.data,
- backgroundColor: chartColors.slice(
- 0,
- chartData.usersActivity.data.length
- ),
- borderColor: chartColors
- .slice(0, chartData.usersActivity.data.length)
- .map((color) => color + "DD"),
- borderWidth: 1,
- },
- ],
- },
- options: {
- responsive: true,
- maintainAspectRatio: false,
- plugins: {
- legend: { display: false },
- tooltip: { mode: "index", intersect: false },
- datalabels: { display: false },
- },
- scales: {
- y: {
- beginAtZero: true,
- title: { display: true, text: "Número de Visitas" },
- ticks: { color: getTextColor() },
- },
- x: {
- ticks: {
- autoSkip: false,
- color: getTextColor(),
- },
- },
- },
- },
- });
- }
- // Users Data Chart
- const usersDataCtx = document.getElementById("usersDataChart");
- if (usersDataCtx && chartData.usersData) {
- new Chart(usersDataCtx.getContext("2d"), {
- type: "bar",
- data: {
- labels: chartData.usersData.labels,
- datasets: [
- {
- label: "Datos (MB)",
- data: chartData.usersData.data,
- backgroundColor: chartColors.slice(
- 0,
- chartData.usersData.data.length
- ),
- borderColor: chartColors
- .slice(0, chartData.usersData.data.length)
- .map((color) => color + "DD"),
- borderWidth: 1,
- },
- ],
- },
- options: {
- responsive: true,
- maintainAspectRatio: false,
- plugins: {
- legend: { display: false },
- tooltip: {
- mode: "index",
- intersect: false,
- callbacks: {
- label: function (context) {
- return (
- context.dataset.label +
- ": " +
- context.raw.toFixed(2) +
- " MB"
- );
- },
- },
- },
- datalabels: { display: false },
- },
- scales: {
- y: {
- beginAtZero: true,
- title: { display: true, text: "Datos Transmitidos (MB)" },
- ticks: { color: getTextColor() },
- },
- x: {
- ticks: {
- autoSkip: false,
- color: getTextColor(),
- },
- },
- },
- },
- });
- }
- // HTTP Codes Chart
- const httpCodesCtx = document.getElementById("httpCodesChart");
- if (httpCodesCtx && chartData.httpCodes) {
- new Chart(httpCodesCtx.getContext("2d"), {
- type: "pie",
- data: {
- labels: chartData.httpCodes.labels,
- datasets: [
- {
- data: chartData.httpCodes.data,
- backgroundColor: chartData.httpCodes.colors,
- borderColor: getBorderColor(),
- borderWidth: 2,
- },
- ],
- },
- options: {
- responsive: true,
- maintainAspectRatio: false,
- plugins: {
- legend: {
- position: "right",
- labels: {
- font: { size: 12 },
- color: getTextColor(),
- },
- },
- tooltip: {
- callbacks: {
- label: function (context) {
- const total = context.dataset.data.reduce(function (
- a,
- b
- ) {
- return a + b;
- },
- 0);
- const percentage = ((context.raw / total) * 100).toFixed(
- 1
- );
- return (
- context.label +
- ": " +
- context.raw +
- " (" +
- percentage +
- "%)"
- );
- },
- },
- },
- datalabels: {
- formatter: function (value, ctx) {
- const total = ctx.chart.data.datasets[0].data.reduce(
- function (a, b) {
- return a + b;
- },
- 0
- );
- const percentage = ((value / total) * 100).toFixed(1);
- return percentage > 3 ? percentage + "%" : "";
- },
- color: "#fff",
- font: {
- weight: "bold",
- size: 12,
- },
- },
- },
- },
- plugins: [ChartDataLabels],
- });
- }
- // Update chart colors when dark mode changes
- const observer = new MutationObserver(function (mutations) {
- mutations.forEach(function (mutation) {
- if (
- mutation.type === "attributes" &&
- mutation.attributeName === "class"
- ) {
- // Re-render charts with new colors when dark mode changes
- setTimeout(function () {
- Chart.helpers.each(Chart.instances, function (instance) {
- // Update scale colors
- if (
- instance.config.options &&
- instance.config.options.scales
- ) {
- const textColor = getTextColor();
- if (
- instance.config.options.scales.y &&
- instance.config.options.scales.y.ticks
- ) {
- instance.config.options.scales.y.ticks.color = textColor;
- }
- if (
- instance.config.options.scales.x &&
- instance.config.options.scales.x.ticks
- ) {
- instance.config.options.scales.x.ticks.color = textColor;
- }
- }
- // Update legend colors
- if (
- instance.config.options &&
- instance.config.options.plugins &&
- instance.config.options.plugins.legend &&
- instance.config.options.plugins.legend.labels
- ) {
- instance.config.options.plugins.legend.labels.color =
- getTextColor();
- }
- instance.update();
- });
- }, 100);
- }
- });
- });
- observer.observe(document.documentElement, {
- attributes: true,
- attributeFilter: ["class"],
- });
- });
- </script>
- <script src="{{ url_for('static', filename='js/reports-datepicker.js') }}"></script>
- {% endblock scripts %}
|