"""Chartmetric spike detector flow.""" from garcon import runner from activity_detector.flows.base_flow import BaseFlow from activity_detector.flows.chartmetric_spike_detector import config from activity_detector.flows.chartmetric_spike_detector import tasks class Flow(BaseFlow): """Class representing the workflow.""" def __init__(self): """Initialize flow object.""" super(Flow, self).__init__( flow_name=config.SWF_FLOW_NAME, version=config.SWF_FLOW_VERSION ) 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 # load data into spike table load_data = schedule( 'load_data', self.load_data, requires=[check_dynamo_status] ) # get data from Snowflake showing spikes detect_spikes = schedule( 'detect_spikes', self.detect_spikes, requires=[load_data] ) 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 load_data(self): """Insert one day of social spike data.""" return self.create( name='load_data', tasks=runner.Async( tasks.load_instagram_data.fill( namespace='load_instagram_data', date='bootstrap.target_date' ), tasks.load_youtube_data.fill( namespace='load_youtube_data', date='bootstrap.target_date' ) ) ) @property def detect_spikes(self): """Query for social 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')))