|
| 1 | +# Copyright (C) 2023-2025, Pyronear. |
| 2 | + |
| 3 | +# This program is licensed under the Apache License 2.0. |
| 4 | +# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details. |
| 5 | + |
| 6 | +from io import StringIO |
| 7 | + |
| 8 | +import logging_config |
| 9 | +import pandas as pd |
| 10 | +from dash import Input, Output, State, dcc |
| 11 | +from dash.dependencies import Input, Output |
| 12 | +from dash.exceptions import PreventUpdate |
| 13 | +from main import app |
| 14 | +from translations import translate |
| 15 | + |
| 16 | +import config as cfg |
| 17 | +from services import get_client |
| 18 | + |
| 19 | +logger = logging_config.configure_logging(cfg.DEBUG, cfg.SENTRY_DSN) |
| 20 | + |
| 21 | + |
| 22 | +@app.callback( |
| 23 | + Output("export-status-text", "children"), |
| 24 | + Output("export-trigger", "data"), |
| 25 | + Input("export-button", "n_clicks"), |
| 26 | + State("language", "data"), |
| 27 | + prevent_initial_call=True, |
| 28 | +) |
| 29 | +def trigger_export(n_clicks, lang): |
| 30 | + return translate("downloading_in_progress", lang), True |
| 31 | + |
| 32 | + |
| 33 | +@app.callback( |
| 34 | + Output("export-download", "data"), |
| 35 | + Output("export-status-text-done", "children"), |
| 36 | + Input("export-trigger", "data"), |
| 37 | + State("export-start-date", "date"), |
| 38 | + State("export-end-date", "date"), |
| 39 | + State("user_token", "data"), |
| 40 | + State("language", "data"), |
| 41 | + State("api_cameras", "data"), |
| 42 | + prevent_initial_call=True, |
| 43 | +) |
| 44 | +def handle_export(trigger, start_date, end_date, user_token, lang, api_cameras): |
| 45 | + if not trigger or not user_token or not start_date or not end_date: |
| 46 | + raise PreventUpdate |
| 47 | + |
| 48 | + try: |
| 49 | + cameras_df = pd.read_json(StringIO(api_cameras), orient="split") |
| 50 | + cameras_df = cameras_df.rename(columns={"id": "camera_id_metadata"}) |
| 51 | + cameras_df = cameras_df[["camera_id_metadata", "name", "angle_of_view", "lat", "lon"]] |
| 52 | + except Exception as e: |
| 53 | + return None, f"Erreur lecture des caméras : {e}" |
| 54 | + |
| 55 | + client = get_client(user_token) |
| 56 | + |
| 57 | + try: |
| 58 | + all_sequences = [] |
| 59 | + current = pd.to_datetime(start_date) |
| 60 | + final = pd.to_datetime(end_date) |
| 61 | + |
| 62 | + while current <= final: |
| 63 | + response = client.fetch_sequences_from_date(current.strftime("%Y-%m-%d"), limit=100) |
| 64 | + daily_df = pd.DataFrame(response.json()) |
| 65 | + if not daily_df.empty: |
| 66 | + all_sequences.append(daily_df) |
| 67 | + current += pd.Timedelta(days=1) |
| 68 | + |
| 69 | + if not all_sequences: |
| 70 | + return None, translate("no_data_found", lang) |
| 71 | + |
| 72 | + export_df = pd.concat(all_sequences, ignore_index=True) |
| 73 | + |
| 74 | + if "camera_id" in export_df.columns: |
| 75 | + export_df = export_df.merge(cameras_df, how="left", left_on="camera_id", right_on="camera_id_metadata") |
| 76 | + export_df = export_df.drop(columns=["camera_id", "camera_id_metadata"]) |
| 77 | + else: |
| 78 | + return None, "camera_id column missing", False |
| 79 | + |
| 80 | + # Reorder columns |
| 81 | + ordered_columns = [ |
| 82 | + "id", |
| 83 | + "name", |
| 84 | + "azimuth", |
| 85 | + "cone_angle", |
| 86 | + "started_at", |
| 87 | + "last_seen_at", |
| 88 | + "is_wildfire", |
| 89 | + "angle_of_view", |
| 90 | + "lat", |
| 91 | + "lon", |
| 92 | + ] |
| 93 | + # Keep only those columns that exist |
| 94 | + export_df = export_df[[col for col in ordered_columns if col in export_df.columns]] |
| 95 | + |
| 96 | + return dcc.send_data_frame(export_df.to_csv, "export.csv", index=False), translate("download_ready", lang) |
| 97 | + |
| 98 | + except Exception as e: |
| 99 | + return None, str(e) |
| 100 | + |
| 101 | + |
| 102 | +@app.callback(Output("export-title", "children"), Input("language", "data")) |
| 103 | +def update_export_title(lang): |
| 104 | + return translate("export_title", lang) |
| 105 | + |
| 106 | + |
| 107 | +@app.callback(Output("export-start-date-label", "children"), Input("language", "data")) |
| 108 | +def update_export_start_label(lang): |
| 109 | + return translate("start_date", lang) |
| 110 | + |
| 111 | + |
| 112 | +@app.callback(Output("export-end-date-label", "children"), Input("language", "data")) |
| 113 | +def update_export_end_label(lang): |
| 114 | + return translate("end_date", lang) |
| 115 | + |
| 116 | + |
| 117 | +@app.callback(Output("export-button", "children"), Input("language", "data")) |
| 118 | +def update_export_button_text(lang): |
| 119 | + return translate("prepare_archive", lang) |
0 commit comments