Просмотр исходного кода

feat: update PDF export to include generation timestamp and improve metrics display

Manuel 8 месяцев назад
Родитель
Сommit
2cf48075fd
2 измененных файлов с 10 добавлено и 17 удалено
  1. 1 7
      routes/reports_routes.py
  2. 9 10
      templates/reports_pdf.html

+ 1 - 7
routes/reports_routes.py

@@ -72,13 +72,6 @@ def reports():
 @reports_bp.route("/reports/download/pdf")
 def reports_download_pdf():
     """Pdf export endpoint for the same data shown in /reports."""
-    if HTML is None:
-        logger.error("WeasyPrint is not installed, PDF export unavailable.")
-        return render_template(
-            "error.html",
-            message="La generación de PDF requiere weasyprint. Instale 'weasyprint'.",
-        ), 500
-
     date_str = request.args.get("date")
     if date_str:
         try:
@@ -129,6 +122,7 @@ def reports_download_pdf():
             "reports_pdf.html",
             metrics=metrics,
             selected_date=selected,
+            generated_at=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
         )
 
         pdf_bytes = HTML(string=html, base_url=request.url_root).write_pdf(

+ 9 - 10
templates/reports_pdf.html

@@ -17,27 +17,26 @@
 </head>
 <body>
     <h1>Reportes SquidStats</h1>
-    <div class="meta">Fecha del reporte: {{ selected_date.isoformat() }} - Generado: {{ now().strftime('%Y-%m-%d %H:%M:%S') }}</div>
+    <div class="meta">Fecha del reporte: {{ selected_date.isoformat() }} - Generado: {{ generated_at }}</div>
 
     <div class="box">
         <h2>Resumen General</h2>
-        <p>Total usuarios: {{ metrics.total_users | default(0) }}</p>
-        <p>Total solicitudes: {{ metrics.total_requests | default(0) }}</p>
-        <p>Datos consumidos (MB): {{ metrics.total_data_consumed_mb | default(0) | round(2) }}</p>
+        <p>Total usuarios: {{ metrics.total_stats.total_users | default(0) }}</p>
+        <p>Total solicitudes: {{ metrics.total_stats.total_requests | default(0) }}</p>
+        <p>Datos consumidos (MB): {{ ((metrics.total_stats.total_data_transmitted | default(0)) / 1048576) | round(2) }}</p>
     </div>
 
     <div class="box">
         <h2>Top usuarios por actividad</h2>
         <table class="table">
             <thead>
-                <tr><th>Usuario</th><th>Visitas</th><th>Datos (MB)</th></tr>
+                <tr><th>Usuario</th><th>Visitas</th></tr>
             </thead>
             <tbody>
             {% for item in metrics.top_users_by_activity %}
                 <tr>
                     <td>{{ item.username }}</td>
                     <td>{{ item.total_visits }}</td>
-                    <td>{{ (item.total_data_bytes / 1048576) | round(2) }}</td>
                 </tr>
             {% endfor %}
             </tbody>
@@ -51,10 +50,10 @@
                 <tr><th>URL</th><th>Datos (MB)</th></tr>
             </thead>
             <tbody>
-            {% for item in metrics.top_urls_by_data %}
+            {% for item in metrics.top_pages_by_data %}
                 <tr>
                     <td>{{ item.url }}</td>
-                    <td>{{ (item.total_data_bytes / 1048576) | round(2) }}</td>
+                    <td>{{ ((item.total_data_bytes | default(0)) / 1048576) | round(2) }}</td>
                 </tr>
             {% endfor %}
             </tbody>
@@ -68,10 +67,10 @@
                 <tr><th>Código</th><th>Conteo</th></tr>
             </thead>
             <tbody>
-            {% for i, code in enumerate(metrics.http_response_distribution_chart.labels) %}
+            {% for code in metrics.http_response_distribution_chart.labels %}
                 <tr>
                     <td>{{ code }}</td>
-                    <td>{{ metrics.http_response_distribution_chart.data[i] }}</td>
+                    <td>{{ metrics.http_response_distribution_chart.data[loop.index0] }}</td>
                 </tr>
             {% endfor %}
             </tbody>