"""DelphiFeed class and related status, finder methods.""" import csv import os from feed_status.models.orm import feed_delphi_status INGESTION_STATUS_INGESTED = 'ingested' class DelphiFeed(object): """DelphiFeed class to store configuration about each feed.""" def __init__(self, feed_id, feed_name, feed_type, status_source_db, data_source_name, report_name, licensor_name, update_freq, section, status=None, metrics=None, only_business_days=False): """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 = feed_type self.section = section self.status = status self.metrics = metrics if self.metrics is not None: self.only_business_days = only_business_days else: self.only_business_days = False self.data_source_name = data_source_name self.report_name = report_name self.licensor_name = licensor_name 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()) delphi_filename = os.path.dirname(__file__) + '/delphi_feeds.csv' def get_delphi_feeds_config(): """Get delphi feeds from configuration.""" with open(delphi_filename, mode='r') as csv_file: csv_reader = csv.DictReader(csv_file) for line in csv_reader: yield line active_feeds = [] for line in get_delphi_feeds_config(): feed_id = (f'delphi-{line["data_source_name"]}-' f'{line["licensor_name"]}-{line["report_name"]}') feed = DelphiFeed( feed_id=feed_id, feed_name=line['full_name'], feed_type=line['feed_type'], status_source_db='delphi_slz', data_source_name=line['data_source_name'], report_name=line['report_name'], licensor_name=line['licensor_name'], update_freq=int(line['data_gap']), section=line['section'], only_business_days=bool(line['only_business_days'])) active_feeds.append(feed) def get_active_feeds(section): """Retrieve active feeds. Args: section (str): for example, main or other. Returns: list: array of DelphiFeed configuration instances. """ return [f for f in active_feeds if f.section == section] def get_all_delphi_feed_history(from_date, to_date): """Retrieve outgoing history for feed within a date range.""" if feed.status_source_db == 'delphi_slz': feed_history_list = feed_delphi_status.get_all_ingestion_history( from_date.isoformat(), to_date.isoformat()) return feed_history_list return None