<s> """<STR_LIT>""" <EOL> import datetime <EOL> import sys <EOL> import six <EOL> from openstackclient . common import command <EOL> from openstackclient . common import utils <EOL> class ListUsage ( command . Lister ) : <EOL> """<STR_LIT>""" <EOL> def get_parser ( self , prog_name ) : <EOL> parser = super ( ListUsage , self ) . get_parser ( prog_name ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , <EOL> metavar = "<STR_LIT>" , <EOL> default = None , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , <EOL> metavar = "<STR_LIT>" , <EOL> default = None , <EOL> help = "<STR_LIT>" <EOL> ) <EOL> return parser <EOL> def take_action ( self , parsed_args ) : <EOL> def _format_project ( project ) : <EOL> if not project : <EOL> return "<STR_LIT>" <EOL> if project in project_cache . keys ( ) : <EOL> return project_cache [ project ] . name <EOL> else : <EOL> return project <EOL> compute_client = self . app . client_manager . compute <EOL> columns = ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ) <EOL> column_headers = ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ) <EOL> dateformat = "<STR_LIT>" <EOL> now = datetime . datetime . utcnow ( ) <EOL> if parsed_args . start : <EOL> start = datetime . datetime . strptime ( parsed_args . start , dateformat ) <EOL> else : <EOL> start = now - datetime . timedelta ( weeks = <NUM_LIT:4> ) <EOL> if parsed_args . end : <EOL> end = datetime . datetime . strptime ( parsed_args . end , dateformat ) <EOL> else : <EOL> end = now + datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> usage_list = compute_client . usage . list ( start , end , detailed = True ) <EOL> project_cache = { } <EOL> try : <EOL> for p in self . app . client_manager . identity . tenants . list ( ) : <EOL> project_cache [ p . id ] = p <EOL> except Exception : <EOL> pass <EOL> if parsed_args . formatter == '<STR_LIT>' and len ( usage_list ) > <NUM_LIT:0> : <EOL> sys . stdout . write ( "<STR_LIT>" % ( <EOL> start . strftime ( dateformat ) , <EOL> end . strftime ( dateformat ) , <EOL> ) ) <EOL> return ( column_headers , <EOL> ( utils . get_item_properties ( <EOL> s , columns , <EOL> formatters = { <EOL> '<STR_LIT>' : _format_project , <EOL> '<STR_LIT>' : lambda x : len ( x ) , <EOL> '<STR_LIT>' : lambda x : float ( "<STR_LIT>" % x ) , <EOL> '<STR_LIT>' : lambda x : float ( "<STR_LIT>" % x ) , <EOL> '<STR_LIT>' : lambda x : float ( "<STR_LIT>" % x ) , <EOL> } , <EOL> ) for s in usage_list ) ) <EOL> class ShowUsage ( command . ShowOne ) : <EOL> """<STR_LIT>""" <EOL> def get_parser ( self , prog_name ) : <EOL> parser = super ( ShowUsage , self ) . get_parser ( prog_name ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , <EOL> metavar = "<STR_LIT>" , <EOL> default = None , <EOL> help = "<STR_LIT>" <EOL> ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , <EOL> metavar = "<STR_LIT>" , <EOL> default = None , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , <EOL> metavar = "<STR_LIT>" , <EOL> default = None , <EOL> help = "<STR_LIT>" <EOL> ) <EOL> return parser <EOL> def take_action ( self , parsed_args ) : <EOL> identity_client = self . app . client_manager . identity <EOL> compute_client = self . app . client_manager . compute <EOL> dateformat = "<STR_LIT>" <EOL> now = datetime . datetime . utcnow ( ) <EOL> if parsed_args . start : <EOL> start = datetime . datetime . strptime ( parsed_args . start , dateformat ) <EOL> else : <EOL> start = now - datetime . timedelta ( weeks = <NUM_LIT:4> ) <EOL> if parsed_args . end : <EOL> end = datetime . datetime . strptime ( parsed_args . end , dateformat ) <EOL> else : <EOL> end = now + datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> if parsed_args . project : <EOL> project = utils . find_resource ( <EOL> identity_client . projects , <EOL> parsed_args . project , <EOL> ) . id <EOL> else : <EOL> project = self . app . client_manager . auth_ref . project_id <EOL> usage = compute_client . usage . get ( project , start , end ) <EOL> if parsed_args . formatter == '<STR_LIT>' : <EOL> sys . stdout . write ( "<STR_LIT>" % ( <EOL> start . strftime ( dateformat ) , <EOL> end . strftime ( dateformat ) , <EOL> project <EOL> ) ) <EOL> info = { } <EOL> info [ '<STR_LIT>' ] = ( <EOL> len ( usage . server_usages ) <EOL> if hasattr ( usage , "<STR_LIT>" ) else None ) <EOL> info [ '<STR_LIT>' ] = ( <EOL> float ( "<STR_LIT>" % usage . total_memory_mb_usage ) <EOL> if hasattr ( usage , "<STR_LIT>" ) else None ) <EOL> info [ '<STR_LIT>' ] = ( <EOL> float ( "<STR_LIT>" % usage . total_vcpus_usage ) <EOL> if hasattr ( usage , "<STR_LIT>" ) else None ) <EOL> info [ '<STR_LIT>' ] = ( <EOL> float ( "<STR_LIT>" % usage . total_local_gb_usage ) <EOL> if hasattr ( usage , "<STR_LIT>" ) else None ) <EOL> return zip ( * sorted ( six . iteritems ( info ) ) ) </s>
