Skip to content

Commit

Permalink
Add tests for fitbit polling
Browse files Browse the repository at this point in the history
  • Loading branch information
caarmen committed Jun 17, 2023
1 parent cbc634e commit 73a1e25
Show file tree
Hide file tree
Showing 3 changed files with 358 additions and 32 deletions.
79 changes: 49 additions & 30 deletions slackhealthbot/scheduler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dataclasses
import datetime
import logging
from threading import Timer
Expand All @@ -14,15 +15,19 @@
from slackhealthbot.services.models import SleepData, user_last_sleep_data
from slackhealthbot.settings import settings

_cache_success: dict[str, datetime.date] = {}
_cache_fail: dict[str, datetime.date] = {}

@dataclasses.dataclass
class Cache:
cache_success: dict[str, datetime.date] = dataclasses.field(default_factory=dict)
cache_fail: dict[str, datetime.date] = dataclasses.field(default_factory=dict)


def handle_success_poll(
db: Session,
fitbit_user: models.FitbitUser,
sleep_data: Optional[SleepData],
when: datetime.date,
cache: Cache,
):
if sleep_data:
last_sleep_data = user_last_sleep_data(fitbit_user)
Expand All @@ -32,53 +37,67 @@ def handle_success_poll(
new_sleep_data=sleep_data,
last_sleep_data=last_sleep_data,
)
_cache_success[fitbit_user.oauth_userid] = when
_cache_fail.pop(fitbit_user.oauth_userid, None)
cache.cache_success[fitbit_user.oauth_userid] = when
cache.cache_fail.pop(fitbit_user.oauth_userid, None)


def handle_fail_poll(
fitbit_user: models.FitbitUser,
when: datetime.date,
cache: Cache,
):
last_error_post = _cache_fail.get(fitbit_user.oauth_userid)
last_error_post = cache.cache_fail.get(fitbit_user.oauth_userid)
if not last_error_post or last_error_post < when:
slack.post_user_logged_out(
slack_alias=fitbit_user.user.slack_alias,
service="fitbit",
)
_cache_fail[fitbit_user.oauth_userid] = when
cache.cache_fail[fitbit_user.oauth_userid] = when


def fitbit_poll():
def fitbit_poll(cache: Cache):
logging.info("fitbit poll")
today = datetime.date.today()
try:
with SessionLocal() as db:
fitbit_users = db.query(models.FitbitUser).all()
today = datetime.date.today()
for fitbit_user in fitbit_users:
latest_successful_poll = _cache_success.get(fitbit_user.oauth_userid)
if not latest_successful_poll or latest_successful_poll < today:
try:
sleep_data = fitbit_api.get_sleep(
db,
user=fitbit_user.user,
when=today,
)
except UserLoggedOutException:
handle_fail_poll(fitbit_user=fitbit_user, when=today)
else:
handle_success_poll(
db=db,
fitbit_user=fitbit_user,
sleep_data=sleep_data,
when=today,
)
do_poll(db, cache, when=today)
except Exception:
logging.error("Error polling fitbit", exc_info=True)
schedule_fitbit_poll()
schedule_fitbit_poll(cache=cache)


def do_poll(db: Session, cache: Cache, when: datetime.date):
fitbit_users = db.query(models.FitbitUser).all()
for fitbit_user in fitbit_users:
latest_successful_poll = cache.cache_success.get(fitbit_user.oauth_userid)
if not latest_successful_poll or latest_successful_poll < when:
try:
sleep_data = fitbit_api.get_sleep(
db,
user=fitbit_user.user,
when=when,
)
except UserLoggedOutException:
handle_fail_poll(
fitbit_user=fitbit_user,
when=when,
cache=cache,
)
else:
handle_success_poll(
db=db,
fitbit_user=fitbit_user,
sleep_data=sleep_data,
when=when,
cache=cache,
)

def schedule_fitbit_poll(delay_s: int = settings.fitbit_poll_interval_s):
timer = Timer(delay_s, fitbit_poll)

def schedule_fitbit_poll(
delay_s: int = settings.fitbit_poll_interval_s, cache: Cache = None
):
if cache is None:
cache = Cache()
timer = Timer(delay_s, function=fitbit_poll, args=[cache])
timer.daemon = True
timer.start()
4 changes: 2 additions & 2 deletions slackhealthbot/services/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def get_datetime_change_icon(
return "➡️"

fake_last_datetime = last_datetime + datetime.timedelta(days=1)
time_diff_seconds = (new_datetime - fake_last_datetime).total_seconds()
return get_datetime_change_icon(time_diff_seconds)
time_diff_seconds = int((new_datetime - fake_last_datetime).total_seconds())
return get_seconds_change_icon(time_diff_seconds)


def post_sleep(
Expand Down
Loading

0 comments on commit 73a1e25

Please sign in to comment.