Skip to content

Commit

Permalink
Fix date conversion (#162)
Browse files Browse the repository at this point in the history
* cronvert only once

* fix date
  • Loading branch information
MateoLostanlen authored Jul 7, 2024
1 parent 2741c08 commit 02fe018
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
2 changes: 2 additions & 0 deletions app/callbacks/data_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import config as cfg
from services import api_client, call_api
from utils.data import (
convert_time,
past_ndays_api_events,
process_bbox,
read_stored_DataFrame,
Expand Down Expand Up @@ -120,6 +121,7 @@ def api_watcher(n_intervals, local_events, local_alerts, user_headers, user_cred
logger.info("Start Fetching the events")
# Fetch events
api_events = pd.DataFrame(call_api(api_client.get_unacknowledged_events, user_credentials)())
api_events["created_at"] = convert_time(api_events)
if len(api_events) == 0:
return [
json.dumps(
Expand Down
5 changes: 3 additions & 2 deletions app/callbacks/display_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,12 +497,13 @@ def update_map_and_alert_info(alert_data, local_events, event_id_on_display):
localization=row_with_localization["processed_loc"],
)

cam_name = local_events[local_events["id"] == event_id_on_display]["device_name"].values[0]
date_val, cam_name = local_events[local_events["id"] == event_id_on_display][
["created_at", "device_name"]
].values[0]

camera_info = f"Camera: {cam_name}"
location_info = f"Localisation: {row_with_localization['lat']:.4f}, {row_with_localization['lon']:.4f}"
angle_info = f"Azimuth de detection: {detection_azimuth}°"
date_val = row_with_localization["created_at"].strftime("%Y-%m-%d %H:%M:%S")
date_info = f"Date: {date_val}"

return (
Expand Down
26 changes: 26 additions & 0 deletions app/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,40 @@

import ast
import json
from datetime import datetime
from io import StringIO
from pathlib import Path
from typing import List

import pandas as pd
import pytz
from timezonefinder import TimezoneFinder

from utils.sites import get_sites

tf = TimezoneFinder()


def convert_time(df):
df_ts_local = []
for _, row in df.iterrows():
lat = round(row["lat"], 4)
lon = round(row["lon"], 4)

# Convert created_at to a timezone-aware datetime object assuming it's in UTC
alert_ts_utc = datetime.fromisoformat(str(row["created_at"])).replace(tzinfo=pytz.utc)

# Find the timezone for the alert location
timezone_str = tf.timezone_at(lat=lat, lng=lon)
if timezone_str is None: # If the timezone is not found, handle it appropriately
timezone_str = "UTC" # Fallback to UTC or some default
alert_timezone = pytz.timezone(timezone_str)

# Convert alert_ts_utc to the local timezone of the alert
df_ts_local.append(alert_ts_utc.astimezone(alert_timezone).strftime("%Y-%m-%dT%H:%M:%S"))

return df_ts_local


def read_stored_DataFrame(data):
"""
Expand Down
22 changes: 0 additions & 22 deletions app/utils/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.

from datetime import datetime

import dash_leaflet as dl
import pytz
import requests
from dash import html
from geopy import Point
from geopy.distance import geodesic
from timezonefinder import TimezoneFinder

import config as cfg
from services import api_client
Expand Down Expand Up @@ -180,25 +177,6 @@ def create_event_list_from_df(api_events):
"""
if api_events.empty:
return []
tf = TimezoneFinder()
alert_ts_local = []
for _, row in api_events.iterrows():
alert_lat = round(row["lat"], 4)
alert_lon = round(row["lon"], 4)

# Convert created_at to a timezone-aware datetime object assuming it's in UTC
alert_ts_utc = datetime.fromisoformat(str(row["created_at"])).replace(tzinfo=pytz.utc)

# Find the timezone for the alert location
timezone_str = tf.timezone_at(lat=alert_lat, lng=alert_lon)
if timezone_str is None: # If the timezone is not found, handle it appropriately
timezone_str = "UTC" # Fallback to UTC or some default
alert_timezone = pytz.timezone(timezone_str)

# Convert alert_ts_utc to the local timezone of the alert
alert_ts_local.append(alert_ts_utc.astimezone(alert_timezone))

api_events["created_at"] = alert_ts_local
filtered_events = api_events.sort_values("created_at").drop_duplicates("id", keep="last")[::-1]

return [
Expand Down

0 comments on commit 02fe018

Please sign in to comment.