"""Trending Tracks v2 flow.""" from datetime import datetime from datetime import timedelta from garcon import runner from activity_detector.flows.base_flow import BaseFlow from activity_detector.flows.trending_tracks_discrete import config from activity_detector.flows.trending_tracks_discrete import tasks class Flow(BaseFlow): """Class representing the workflow.""" def __init__(self): """Initiailze flow object.""" super(Flow, self).__init__( flow_name=config.SWF_FLOW_NAME, version=config.SWF_FLOW_VERSION ) def workflow_id(self, initial_context): """Override workflow id to match default date selection. Args: initial_context (dict): The initial context for the flow. Returns: str: A unique identifier for a workflow """ if 'context_date' not in initial_context: date = (datetime.today() - timedelta( days=config.DEFAULT_SHIFT_DAYS)).strftime('%Y-%m-%d') else: date = initial_context['context_date'] return '{flow_name}-{date}'.format(flow_name=self.name, date=date) def decider(self, schedule, context): """Activity decider. Args: schedule (callable): scheduler method context (dict): flow initial context """ bootstrap = schedule('bootstrap', self.bootstrap) check_dynamo_status = schedule( 'check_dynamo_status', self.check_dynamo_status, requires=[bootstrap]) if check_dynamo_status.result.get( 'check_dynamo_status.should_run') is False: return detect_spikes = schedule( 'detect_spikes', self.detect_spikes, requires=[check_dynamo_status] ) schedule( 'set_dynamo_status', self.set_dynamo_status, requires=[detect_spikes] ) @property def bootstrap(self): """Bootstrap initial configuration.""" return self.create( name='bootstrap', tasks=runner.Sync( tasks.bootstrap.fill( namespace='bootstrap', date='context_date', run_id='execution.run_id'))) @property def check_dynamo_status(self): """Check dynamo status.""" return self.create( name='check_dynamo_status', tasks=runner.Sync( tasks.check_dynamo_status.fill( namespace='check_dynamo_status', date='bootstrap.target_date'))) @property def detect_spikes(self): """Query for trending track spikes.""" return self.create( name='detect_spikes', tasks=runner.Sync( tasks.detect_spikes.fill( namespace='detect_spikes', date='bootstrap.target_date'))) @property def set_dynamo_status(self): """Set activity status.""" return self.create( name='set_dynamo_status', tasks=runner.Sync( tasks.set_dynamo_status.fill( namespace='set_dynamo_status', date='bootstrap.target_date', failures='detect_spikes.failures')))