""" Custom Accounting Statement Export workflow =========================================== Generate actual reports from Avro files Example call: garcon exec custom_export -c '{"period_ids":"199", "user_id":"10303", "user_type":"label","locale":"es_ES", "file_format":"txt", "transaction_types":"all","client_email":"paulo@theorchard.com"}' """ import os from garcon import runner from garcon.param import StaticParam from processing_accounting.flows.base import FlowBase from processing_accounting.flows.custom_export import setting from processing_accounting.flows.custom_export import tasks class Flow(FlowBase): required_params = [ 'period_ids', 'user_type', 'user_id', 'transaction_types'] available_params = [ 'period_ids', 'user_type', 'user_id', 'transaction_types'] def __init__(self): self.timeout = setting.START_TO_CLOSE_TIMEOUT super().__init__('custom_export', '2.0') self.env = os.getenv('Environment') if os.getenv('PROCESSING_ACCOUNTING_SNS_TOPIC'): self.sns_topic = os.environ.get('PROCESSING_ACCOUNTING_SNS_TOPIC') def workflow_id(self, initial_context=None): """Get workflow id """ report_version = initial_context.get('report_version') if report_version is None: report_version = setting.DEFAULT_REPORT_VERSION return ( '{flow_name}-{user_id}-{user_type}-{period_ids}-' '{transaction_types}-{locale}-{file_format}-' '{report_version}').format( flow_name=self.name, user_id=initial_context.get('user_id'), user_type=initial_context.get('user_type'), period_ids=initial_context.get('period_ids'), transaction_types=initial_context.get('transaction_types'), locale=initial_context.get('locale'), file_format=initial_context.get('file_format'), report_version=report_version) def decider(self, schedule, context): """Activity decider. Args: schedule (callable): the scheduler method. """ bootstrap = schedule('bootstrap', self.bootstrap) if bootstrap.result.get('bootstrap.stop'): return generate_report = schedule( 'generate_report', self.generate_report, requires=[bootstrap]) update_status = schedule( 'update_status', self.update_status, requires=[generate_report]) schedule( 'clean_unused_avro_file', self.clean_unused_avro_file, requires=[update_status]) if 'client_email' in context: schedule( 'send_email', self.send_email, requires=[update_status]) @property def bootstrap(self): """Bootstrap initial configuration. """ return self.create( name='bootstrap_config', schedule_to_start=3600, tasks=runner.Sync( tasks.bootstrap.fill( namespace='bootstrap', user_id='user_id', user_type='user_type', period_ids='period_ids', transaction_types='transaction_types', locale='locale', file_format='file_format', report_version='report_version'))) @property def generate_report(self): """Generate report activity """ return self.create( name='generate_report', retry=4, schedule_to_start=3600, tasks=runner.Sync( tasks.generate_report.fill( namespace='generate_report', user_id='user_id', user_type='user_type', avro_file_s3_path='bootstrap.avro_file_s3_path', transaction_types='bootstrap.transaction_types', locale='locale', file_format='bootstrap.file_format', period_ids='bootstrap.period_ids', redownload='redownload', report_version='bootstrap.report_version'), tasks.zip_up_files.fill( namespace='zip_up_files', source_path='generate_report.final_text_report_path', local_zip_path='generate_report.final_zipped_report_path'), tasks.upload_to_s3.fill( namespace='upload_to_s3', local_zip_path='generate_report.final_zipped_report_path', period_ids='bootstrap.period_ids', user_type='user_type', user_id='user_id', transaction_types='bootstrap.transaction_types', locale='locale', file_format='bootstrap.file_format', key_name='zip_up_files.zip_file_name'))) @property def update_status(self): """Update status of the custom report generation process to generated """ return self.create( name='update_status', schedule_to_start=3600, tasks=runner.Sync( tasks.update_status.fill( namespace='update_status', period_ids='bootstrap.period_ids', user_id='user_id', user_type='user_type', status=StaticParam('GENERATED'), file_format='bootstrap.file_format', locale='locale', s3_path='upload_to_s3.final_zipped_report_s3_path', transaction_types='bootstrap.transaction_types', start_time='generate_report.start_time', end_time='generate_report.end_time'))) @property def clean_unused_avro_file(self): """Clean unused avro local file """ return self.create( name='clean_shared_avro_file', schedule_to_start=3600, tasks=runner.Sync( tasks.clean_shared_avro_file.fill( namespace='clean_shared_avro_file', user_id='user_id', user_type='user_type', period_ids='bootstrap.period_ids'))) @property def send_email(self): """Send report is ready email to client """ return self.create( name='send_email', schedule_to_start=3600, tasks=runner.Sync( tasks.send_email.fill( namespace='update_status', client_email='client_email', period_ids='bootstrap.period_ids', user_id='user_id', user_type='user_type', locale='locale', transaction_types='bootstrap.transaction_types', file_format='bootstrap.file_format')))