Skip to content

Instantly share code, notes, and snippets.

@compwron
Created February 27, 2020 08:41
Show Gist options
  • Save compwron/ed952c5b440ebd336b8d1cc79f805790 to your computer and use it in GitHub Desktop.
Save compwron/ed952c5b440ebd336b8d1cc79f805790 to your computer and use it in GitHub Desktop.
code that thankfully we don't need to _populate_missing_weeks
def _populate_missing_weeks(session: Session,
result_dict: List[Dict[str, str]], start_date: str, end_date: str) -> List[Dict[str, str]]:
# result_dict_week_start_dates = list(map(lambda data: data['start'], result_dict))
result_dict_week_start_dates = set(data['start'] for data in result_dict)
for week_start, week_end in _week_starts_between(session, start_date, end_date):
if week_start not in result_dict_week_start_dates:
result_dict.append({
'messages_received_per_patient': Decimal(0),
'start': week_start,
'end': week_end
})
return sorted(result_dict, key=lambda data: data['start'])
def _week_starts_between(session, start_date: str, end_date: str) -> Set[Tuple[str, str]]:
params = {
'start_date': start_date,
'end_date': end_date,
}
result = run_raw_sql(session, week_start_ends, params)
return [list(x) for x in set(tuple(x) for x in result)] # not order preserving
# don't do this, this is terribad, use https://dateutil.readthedocs.io/en/stable/ instead which is great
week_start_ends = """
select STR_TO_DATE(concat(yearweek(db_date), "Sunday"), "%X%V %W") as week_start_date,
STR_TO_DATE(concat(yearweek(db_date), "Saturday"), "%X%V %W")
from calendar
where date(db_date) between :start_date and :end_date
and STR_TO_DATE(concat(yearweek(db_date), "Saturday"), "%X%V %W") <= :end_date
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment