<s> import datetime <EOL> import webob . exc <EOL> from nova . api . openstack import extensions <EOL> from nova . api . openstack import wsgi <EOL> from nova import compute <EOL> import nova . conf <EOL> from nova . i18n import _ <EOL> from nova import utils <EOL> CONF = nova . conf . CONF <EOL> ALIAS = '<STR_LIT>' <EOL> authorize = extensions . os_compute_authorizer ( ALIAS ) <EOL> class InstanceUsageAuditLogController ( wsgi . Controller ) : <EOL> def __init__ ( self ) : <EOL> self . host_api = compute . HostAPI ( ) <EOL> @ extensions . expected_errors ( ( ) ) <EOL> def index ( self , req ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> task_log = self . _get_audit_task_logs ( context ) <EOL> return { '<STR_LIT>' : task_log } <EOL> @ extensions . expected_errors ( <NUM_LIT> ) <EOL> def show ( self , req , id ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> try : <EOL> if '<STR_LIT:.>' in id : <EOL> before_date = datetime . datetime . strptime ( str ( id ) , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> before_date = datetime . datetime . strptime ( str ( id ) , <EOL> "<STR_LIT>" ) <EOL> except ValueError : <EOL> msg = _ ( "<STR_LIT>" ) % id <EOL> raise webob . exc . HTTPBadRequest ( explanation = msg ) <EOL> task_log = self . _get_audit_task_logs ( context , <EOL> before = before_date ) <EOL> return { '<STR_LIT>' : task_log } <EOL> def _get_audit_task_logs ( self , context , begin = None , end = None , <EOL> before = None ) : <EOL> """<STR_LIT>""" <EOL> defbegin , defend = utils . last_completed_audit_period ( before = before ) <EOL> if begin is None : <EOL> begin = defbegin <EOL> if end is None : <EOL> end = defend <EOL> task_logs = self . host_api . task_log_get_all ( context , <EOL> "<STR_LIT>" , <EOL> begin , end ) <EOL> filters = { '<STR_LIT>' : CONF . compute_topic } <EOL> services = self . host_api . service_get_all ( context , filters = filters ) <EOL> hosts = set ( serv [ '<STR_LIT:host>' ] for serv in services ) <EOL> seen_hosts = set ( ) <EOL> done_hosts = set ( ) <EOL> running_hosts = set ( ) <EOL> total_errors = <NUM_LIT:0> <EOL> total_items = <NUM_LIT:0> <EOL> for tlog in task_logs : <EOL> seen_hosts . add ( tlog [ '<STR_LIT:host>' ] ) <EOL> if tlog [ '<STR_LIT:state>' ] == "<STR_LIT>" : <EOL> done_hosts . add ( tlog [ '<STR_LIT:host>' ] ) <EOL> if tlog [ '<STR_LIT:state>' ] == "<STR_LIT>" : <EOL> running_hosts . add ( tlog [ '<STR_LIT:host>' ] ) <EOL> total_errors += tlog [ '<STR_LIT>' ] <EOL> total_items += tlog [ '<STR_LIT>' ] <EOL> log = { tl [ '<STR_LIT:host>' ] : dict ( state = tl [ '<STR_LIT:state>' ] , <EOL> instances = tl [ '<STR_LIT>' ] , <EOL> errors = tl [ '<STR_LIT>' ] , <EOL> message = tl [ '<STR_LIT:message>' ] ) <EOL> for tl in task_logs } <EOL> missing_hosts = hosts - seen_hosts <EOL> overall_status = "<STR_LIT>" % ( <EOL> '<STR_LIT>' if len ( done_hosts ) == len ( hosts ) <EOL> else "<STR_LIT>" % ( len ( done_hosts ) , len ( hosts ) ) , <EOL> total_errors ) <EOL> return dict ( period_beginning = str ( begin ) , <EOL> period_ending = str ( end ) , <EOL> num_hosts = len ( hosts ) , <EOL> num_hosts_done = len ( done_hosts ) , <EOL> num_hosts_running = len ( running_hosts ) , <EOL> num_hosts_not_run = len ( missing_hosts ) , <EOL> hosts_not_run = list ( missing_hosts ) , <EOL> total_instances = total_items , <EOL> total_errors = total_errors , <EOL> overall_status = overall_status , <EOL> log = log ) <EOL> class InstanceUsageAuditLog ( extensions . V21APIExtensionBase ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> alias = ALIAS <EOL> version = <NUM_LIT:1> <EOL> def get_resources ( self ) : <EOL> ext = extensions . ResourceExtension ( '<STR_LIT>' , <EOL> InstanceUsageAuditLogController ( ) ) <EOL> return [ ext ] <EOL> def get_controller_extensions ( self ) : <EOL> return [ ] </s>
