Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
opening_hours: measure call durations
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-dupre committed Feb 4, 2021
1 parent d5b69ab commit 97bf357
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
8 changes: 4 additions & 4 deletions idunn/blocks/opening_hour.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,9 @@ def from_es_with_oh(cls, place, _lang, raw_oh):
logger.info("No timezone found for poi %s", place.get("id"))
return None

oh = OpeningHours(raw_oh, poi_tz, poi_country_code)
curr_dt = utc.localize(datetime.utcnow())

if not oh.validate():
try:
oh = OpeningHours(raw_oh, poi_tz, poi_country_code)
except SyntaxError:
logger.info(
"Failed to validate opening_hours field, id:'%s' raw:'%s'",
place.get_id(),
Expand All @@ -183,6 +182,7 @@ def from_es_with_oh(cls, place, _lang, raw_oh):
)
return None

curr_dt = utc.localize(datetime.utcnow())
next_transition = oh.next_change(curr_dt)

if oh.is_open(curr_dt):
Expand Down
35 changes: 34 additions & 1 deletion idunn/utils/opening_hours.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,74 @@
from datetime import date, datetime, time, timedelta

import opening_hours
import logging


def timelog(f, method=True):
def wrapped(*args, **kwargs):
import time

start = time.time()
res = f(*args, **kwargs)
end = time.time()

if method:
args = args[1:]

args = ", ".join(
[str(a) for a in args] + [f"{key}={val}" for key, val in kwargs.items()]
)
print(f"{f.__name__}({args}) in {end - start:.06f}s")

return res

return wrapped


logger = logging.getLogger(__name__)


class OpeningHours:
@timelog
def __init__(self, oh, tz, _country_code):
oh = oh.replace(" ; PH off", "").replace(" ; ", "; ")
self.raw = oh
self.tz = tz
self.oh = opening_hours.OpeningHours(oh)

@timelog
def is_24_7(self, dt):
"""Check if this is always open starting from a given date"""
assert isinstance(dt, datetime)
# TODO: add is_24_7
return self.oh.next_change(dt) is None

@timelog
def is_open(self, dt):
"""Check if open at a given time"""
assert isinstance(dt, datetime)
return self.oh.is_open(dt.astimezone(self.tz))

@timelog
def is_open_at_date(self, d):
"""Check if this is open at some point in a given date"""
assert isinstance(d, date)
start = datetime.combine(d, time(0, 0))
end = datetime.combine(d + timedelta(days=1), time(0, 0))
return bool(self.get_open_intervals(start, end))

@timelog
def next_change(self, dt):
"""Get datetime of next change of state"""
assert isinstance(dt, datetime)
date = self.oh.next_change(dt.astimezone(self.tz))

if naive_date is None:
if date is None:
return None

return self.tz.localize(date)

@timelog
def get_open_intervals(self, start, end):
"""Get opened intervals for a period of time"""
assert isinstance(start, datetime)
Expand All @@ -51,6 +83,7 @@ def get_open_intervals(self, start, end):
]
return res

@timelog
def get_open_intervals_at_date(self, d, overlap_next_day=False):
"""
Get opening intervals at given date. By default the intervals will be
Expand Down

0 comments on commit 97bf357

Please sign in to comment.