Skip to content

Commit

Permalink
adaptative time (#125)
Browse files Browse the repository at this point in the history
* adaptative time

* prevent timezone not found
  • Loading branch information
MateoLostanlen authored Nov 13, 2023
1 parent 09eaa94 commit 1156e5f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
22 changes: 18 additions & 4 deletions app/utils/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@

import ast
import json
from datetime import datetime, timedelta
from datetime import datetime
from typing import List

import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
import dash_leaflet as dl
import pandas as pd
import pytz
import requests
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 @@ -511,14 +513,26 @@ def build_individual_alert_components(live_alerts, alert_frame_urls, site_device
vision_polygons_children = []
alert_modals_children = []

tf = TimezoneFinder()

for _, row in all_events.iterrows():
alert_id = str(row["event_id"])
alert_lat = round(row["lat"], 4)
alert_lon = round(row["lon"], 4)
alert_azimuth = round(row["azimuth"], 1)
alert_ts = datetime.fromisoformat(str(row["created_at"]))
alert_date = alert_ts.date()
alert_time = (alert_ts + timedelta(hours=2)).time()
# 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 = alert_ts_utc.astimezone(alert_timezone)
alert_date = alert_ts_local.date()
alert_time = alert_ts_local.time()

device_id = row["device_id"]

Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ geopy = "^2.1.0"
requests = "^2.31.0"
sentry-sdk = { version = "^1.5.12", extras = ["flask"] }
gunicorn = ">=20.0.4"
timezonefinder = ">=6.2.0"
pytz = ">=2023.3.post1"
# Indirect deps
# cf. https://github.com/plotly/dash/issues/1992
werkzeug = ">=0.16.0,<2.1.0"
Expand Down

0 comments on commit 1156e5f

Please sign in to comment.