Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix past n days #146

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/callbacks/data_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def api_watcher(n_intervals, local_events, local_alerts, user_headers, user_cred

# Fetch events
api_events = pd.DataFrame(call_api(api_client.get_unacknowledged_events, user_credentials)())
api_events = past_ndays_api_events(api_events, n_days=1) # keep only events from today
api_events = past_ndays_api_events(api_events, n_days=0) # keep only events from today
if api_events.empty:
return dash.no_update, dash.no_update, True
api_events = api_events[::-1] # Display the last alert first
Expand Down
23 changes: 16 additions & 7 deletions app/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,36 @@ def process_bbox(input_str):
return new_boxes


def past_ndays_api_events(api_events, n_days=1):
import pandas as pd


def past_ndays_api_events(api_events, n_days=0):
"""
Filters the given live events to retain only those within the past n days.

Args:
api_events (pd.Dataframe): DataFrame containing live events data. It must have a "created_at" column
indicating the datetime of the event.
n_days (int, optional): Specifies the number of days into the past to retain events. Defaults to 1.
n_days (int, optional): Specifies the number of days into the past to retain events. Defaults to 0.

Returns:
pd.DataFrame: A filtered DataFrame containing only events from the past n_days.
"""
# Ensure the column is in datetime format
api_events["created_at"] = pd.to_datetime(api_events["created_at"])

# Define the start and end dates for the filter
end_date = pd.Timestamp.utcnow().replace(tzinfo=None).normalize()
start_date = end_date - pd.Timedelta(days=n_days - 1)
# Define the end date (now) for the filter
end_date = pd.Timestamp.now()

if n_days == 0:
# When n_days is 0, adjust start_date to the beginning of today to include today's events
start_date = end_date.normalize()
else:
# For n_days > 0
start_date = end_date - pd.Timedelta(days=n_days)

# Filter events from the past n days
api_events = api_events[api_events["created_at"] > start_date]
# Filter events from the past n days, including all events from today when n_days is 0
api_events = api_events[(api_events["created_at"] > start_date) | (api_events["created_at"] == start_date)]

return api_events

Expand Down
Loading