"""Util functions for Projections ETL.""" from datetime import datetime from datetime import timedelta from flows import datastore from flows import s3 from flows.theatrical_cuts import queries def get_upc_theatrical_cuts_anchor_date(upc): """Get the anchor date of a UPC from the datastore records. Args: upc (str): UPC to lookup. Returns: date: UPC's anchor date. """ results = datastore.query( queries.THEATRICAL_CUTS_ANCHOR_DATE, {'upc': upc}) row = results.fetchone() return row[0] def get_cuts_upc(drop_url): """Get UPC value from cuts file. Args: drop_url (str): S3 cuts file url. Returns: str: UPC value. """ data = s3.download_csv(drop_url) return data[0][0] def transpose_weekly_percentage(row, anchor_date, upc): """Transform theatrical cuts csv data to db table format. Args: row (list): row with percentage from csv. anchor_date (str): anchor date in ISO format: YYYY-MM-DD. upc (str): release UPC. Yields: dict: {percentage, upc, start_date, end_date}. """ start_date = datetime.strptime(anchor_date, '%Y-%m-%d').date() dates = [{ 'start_date': (start_date + timedelta(days=i * 7)).isoformat(), 'end_date': (start_date + timedelta(days=(i + 1) * 7 - 1)).isoformat()} for i in range(len(row)-1)] for cell, date in zip(row[1:], dates): yield dict(percentage=100 - float(cell), upc=upc, **date) def transpose_percentage_data(row, anchor_date, upc): """Transform theatrical cuts csv data to db table format. Args: row (list): row with percentage from csv. anchor_date (str): anchor date in ISO format: YYYY-MM-DD. upc (str): release UPC. Yields: dict: {upc, date, percent}. """ start_date = datetime.strptime(anchor_date, '%Y-%m-%d').date() dates = [ (start_date + timedelta(days=i * 7)).isoformat() for i in range(len(row)-1)] for percentage, date in zip(row[1:], dates): yield dict(percentage=float(percentage), upc=upc, date=date)