<s> import logging <EOL> from dbaas . celery import app <EOL> from datetime import datetime <EOL> from account . models import User <EOL> from util import get_worker_name <EOL> from django . db import transaction <EOL> from logical . models import Database <EOL> from util . decorators import only_one <EOL> from simple_audit . models import AuditRequest <EOL> from notification . models import TaskHistory <EOL> from dbaas_services . analyzing . models import ExecutionPlan <EOL> from dbaas_services . analyzing . models import AnalyzeRepository <EOL> from dbaas_services . analyzing . integration import AnalyzeService <EOL> from dbaas_services . analyzing . actions import database_can_not_be_resized <EOL> LOG = logging . getLogger ( __name__ ) <EOL> @ app . task ( bind = True ) <EOL> @ only_one ( key = "<STR_LIT>" , timeout = <NUM_LIT> ) <EOL> def analyze_databases ( self , task_history = None ) : <EOL> endpoint , healh_check_route , healh_check_string = get_analyzing_credentials ( ) <EOL> user = User . objects . get ( username = '<STR_LIT>' ) <EOL> worker_name = get_worker_name ( ) <EOL> task_history = TaskHistory . register ( task_history = task_history , request = self . request , user = user , <EOL> worker_name = worker_name ) <EOL> task_history . update_details ( persist = True , details = "<STR_LIT>" ) <EOL> AuditRequest . new_request ( "<STR_LIT>" , user , "<STR_LIT:localhost>" ) <EOL> try : <EOL> analyze_service = AnalyzeService ( endpoint , healh_check_route , <EOL> healh_check_string ) <EOL> with transaction . atomic ( ) : <EOL> databases = Database . objects . filter ( is_in_quarantine = False ) <EOL> today = datetime . now ( ) <EOL> for database in databases : <EOL> database_name , engine , instances , environment_name , databaseinfra_name = setup_database_info ( database ) <EOL> for execution_plan in ExecutionPlan . objects . all ( ) : <EOL> if database_can_not_be_resized ( database , execution_plan ) : <EOL> continue <EOL> params = execution_plan . setup_execution_params ( ) <EOL> result = analyze_service . run ( engine = engine , database = database_name , <EOL> instances = instances , ** params ) <EOL> if result [ '<STR_LIT:status>' ] == '<STR_LIT:success>' : <EOL> task_history . update_details ( persist = True , details = "<STR_LIT>" . format ( database , execution_plan . plan_name ) ) <EOL> if result [ '<STR_LIT>' ] != instances : <EOL> continue <EOL> for instance in result [ '<STR_LIT>' ] : <EOL> insert_analyze_repository_record ( today , database_name , instance , <EOL> engine , databaseinfra_name , <EOL> environment_name , <EOL> execution_plan ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> task_history . update_status_for ( TaskHistory . STATUS_SUCCESS , <EOL> details = '<STR_LIT>' ) <EOL> except Exception : <EOL> try : <EOL> task_history . update_details ( persist = True , <EOL> details = "<STR_LIT>" . format ( database , <EOL> execution_plan . plan_name ) ) <EOL> task_history . update_status_for ( TaskHistory . STATUS_ERROR , <EOL> details = '<STR_LIT>' . format ( result [ '<STR_LIT>' ] ) ) <EOL> except UnboundLocalError : <EOL> task_history . update_details ( persist = True , details = "<STR_LIT>" ) <EOL> task_history . update_status_for ( TaskHistory . STATUS_ERROR , details = '<STR_LIT>' ) <EOL> finally : <EOL> AuditRequest . cleanup_request ( ) <EOL> def get_analyzing_credentials ( ) : <EOL> from dbaas_credentials . models import CredentialType <EOL> from dbaas_credentials . models import Credential <EOL> credential = Credential . objects . get ( integration_type__type = CredentialType . DBAAS_SERVICES_ANALYZING ) <EOL> return credential . endpoint , credential . get_parameter_by_name ( '<STR_LIT>' ) , credential . get_parameter_by_name ( '<STR_LIT>' ) <EOL> def insert_analyze_repository_record ( date_time , database_name , instance , <EOL> engine , databaseinfra_name , environment_name , <EOL> execution_plan ) : <EOL> try : <EOL> get_analyzing_objects = AnalyzeRepository . objects . get <EOL> LOG . info ( date_time ) <EOL> repo_instance = get_analyzing_objects ( analyzed_at = date_time , <EOL> database_name = database_name , <EOL> instance_name = instance , <EOL> engine_name = engine , <EOL> databaseinfra_name = databaseinfra_name , <EOL> environment_name = environment_name , ) <EOL> except AnalyzeRepository . DoesNotExist as e : <EOL> LOG . info ( e ) <EOL> repo_instance = AnalyzeRepository ( analyzed_at = date_time , <EOL> database_name = database_name , <EOL> instance_name = instance , <EOL> engine_name = engine , <EOL> databaseinfra_name = databaseinfra_name , <EOL> environment_name = environment_name ) <EOL> setattr ( repo_instance , execution_plan . alarm_repository_attr , True ) <EOL> setattr ( repo_instance , execution_plan . threshold_repository_attr , <EOL> execution_plan . threshold ) <EOL> repo_instance . save ( ) <EOL> def setup_database_info ( database ) : <EOL> databaseinfra = database . databaseinfra <EOL> driver = databaseinfra . get_driver ( ) <EOL> database_instances = driver . get_database_instances ( ) <EOL> instances = [ db_instance . dns . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] for db_instance in database_instances ] <EOL> return database . name , database . engine_type , instances , database . environment . name , database . databaseinfra </s>
