"""OutgoingFeed class and related status, finder methods.""" from feed_status import util from feed_status.models.orm import feed_sender_status OUTGOING_STATUS_NOT_AVAILABLE = 'not_available' OUTGOING_STATUS_PROCESSING = 'processing' OUTGOING_STATUS_UPLOADED_TO_FTP = 'uploaded_to_ftp' OUTGOING_STATUS_UPLOADED_TO_S3 = 'uploaded_to_s3' OUTGOING_STATUS_SENT = 'sent' class OutgoingFeed(object): """OutgoingFeed class to store configuration about each feed.""" def __init__(self, feed_id, feed_name, status_source_db, update_freq, status=None, metrics=None): """Initialize class with attributes.""" self.feed_id = feed_id self.feed_name = feed_name self.update_freq = update_freq self.status_source_db = status_source_db # todo: move feed_type to FeedMonitor class self.feed_type = None self.status = status self.metrics = metrics def match(self, **kwargs): """Whether instance of IngestionFeed contains provided attributes. Args: kwargs: keys/values to match on. Returns: boolean: True if contains all provided attributes, False otherwise. """ return all( getattr(self, key, None) == val for (key, val) in kwargs.items()) # todo: move to table active_feeds = [ OutgoingFeed(feed_id='proper_new_releases_tracks', feed_name='proper_new_releases_tracks', status_source_db='feed_status', update_freq=1), OutgoingFeed(feed_id='proper_changed_releases', feed_name='proper_changed_releases', status_source_db='feed_status', update_freq=1), ] def get_active_feeds(): """Retrieve active feeds. Returns: list: array of OutgoingFeed configuration instances. """ return active_feeds def get_outgoing_feed_history(feed, from_date, to_date): """Retrieve outgoing history for feed within a date range. Args: feed (OutgoingFeed): OutgoingFeed instance. from_date (date): start date of interest. to_date (date): end date of history. Returns: dict: map of dates to feed status. """ status = util.generate_default_date_dict_range(from_date, to_date) if feed.status_source_db == 'feed_status': feed_history_list = feed_sender_status.get_ingestion_history( feed.feed_name, from_date.isoformat(), to_date.isoformat()) for feed_history in feed_history_list: status[feed_history['date']] = feed_history['status'].lower() return status