<s> from datetime import datetime <EOL> from datetime import timedelta <EOL> import json <EOL> from django . http import HttpResponse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . views . generic import TemplateView <EOL> from horizon import exceptions <EOL> from horizon import tabs <EOL> from openstack_dashboard import api <EOL> from openstack_dashboard . api import ceilometer <EOL> from openstack_dashboard . dashboards . admin . metering import tabs as metering_tabs <EOL> class IndexView ( tabs . TabbedTableView ) : <EOL> tab_group_class = metering_tabs . CeilometerOverviewTabs <EOL> template_name = '<STR_LIT>' <EOL> class SamplesView ( TemplateView ) : <EOL> template_name = "<STR_LIT>" <EOL> @ staticmethod <EOL> def _series_for_meter ( aggregates , <EOL> resource_name , <EOL> meter_name , <EOL> stats_name , <EOL> unit ) : <EOL> """<STR_LIT>""" <EOL> series = [ ] <EOL> for resource in aggregates : <EOL> if getattr ( resource , meter_name ) : <EOL> point = { '<STR_LIT>' : unit , <EOL> '<STR_LIT:name>' : getattr ( resource , resource_name ) , <EOL> '<STR_LIT:data>' : [ ] } <EOL> for statistic in getattr ( resource , meter_name ) : <EOL> date = statistic . duration_end [ : <NUM_LIT> ] <EOL> value = float ( getattr ( statistic , stats_name ) ) <EOL> point [ '<STR_LIT:data>' ] . append ( { '<STR_LIT:x>' : date , '<STR_LIT:y>' : value } ) <EOL> series . append ( point ) <EOL> return series <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> meter = request . GET . get ( '<STR_LIT>' , None ) <EOL> meter_name = meter . replace ( "<STR_LIT:.>" , "<STR_LIT:_>" ) <EOL> date_options = request . GET . get ( '<STR_LIT>' , None ) <EOL> date_from = request . GET . get ( '<STR_LIT>' , None ) <EOL> date_to = request . GET . get ( '<STR_LIT>' , None ) <EOL> stats_attr = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if ( date_options == "<STR_LIT>" ) : <EOL> try : <EOL> if date_from : <EOL> date_from = datetime . strptime ( date_from , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> pass <EOL> if date_to : <EOL> date_to = datetime . strptime ( date_to , <EOL> "<STR_LIT>" ) <EOL> date_to = ( date_to + timedelta ( days = <NUM_LIT:1> ) - <EOL> timedelta ( seconds = <NUM_LIT:1> ) ) <EOL> else : <EOL> date_to = datetime . now ( ) <EOL> except Exception : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> try : <EOL> date_from = datetime . now ( ) - timedelta ( days = int ( date_options ) ) <EOL> date_to = datetime . now ( ) <EOL> except Exception : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if date_from and date_to : <EOL> if date_to < date_from : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> delta = date_to - date_from <EOL> if delta . days <= <NUM_LIT:0> : <EOL> delta_in_seconds = <NUM_LIT> * <NUM_LIT> <EOL> else : <EOL> delta_in_seconds = delta . days * <NUM_LIT> * <NUM_LIT> + delta . seconds <EOL> number_of_samples = <NUM_LIT> <EOL> period = delta_in_seconds / number_of_samples <EOL> else : <EOL> period = <NUM_LIT> * <NUM_LIT> <EOL> additional_query = [ ] <EOL> if date_from : <EOL> additional_query += [ { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:value>' : date_from } ] <EOL> if date_to : <EOL> additional_query += [ { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:value>' : date_to } ] <EOL> try : <EOL> meter_list = [ m for m in ceilometer . meter_list ( request ) <EOL> if m . name == meter ] <EOL> unit = meter_list [ <NUM_LIT:0> ] . unit <EOL> except Exception : <EOL> unit = "<STR_LIT>" <EOL> if request . GET . get ( '<STR_LIT>' , None ) == "<STR_LIT>" : <EOL> try : <EOL> tenants , more = api . keystone . tenant_list ( <EOL> request , <EOL> domain = None , <EOL> paginate = True , <EOL> marker = "<STR_LIT>" ) <EOL> except Exception : <EOL> tenants = [ ] <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' ) ) <EOL> queries = { } <EOL> for tenant in tenants : <EOL> tenant_query = [ { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:value>" : tenant . id } ] <EOL> queries [ tenant . name ] = tenant_query <EOL> ceilometer_usage = ceilometer . CeilometerUsage ( request ) <EOL> resources = ceilometer_usage . resource_aggregates_with_statistics ( <EOL> queries , [ meter ] , period = period , stats_attr = None , <EOL> additional_query = additional_query ) <EOL> series = self . _series_for_meter ( resources , <EOL> '<STR_LIT:id>' , <EOL> meter_name , <EOL> stats_attr , <EOL> unit ) <EOL> else : <EOL> query = [ ] <EOL> def filter_by_meter_name ( resource ) : <EOL> """<STR_LIT>""" <EOL> for link in resource . links : <EOL> if link [ '<STR_LIT>' ] == meter : <EOL> return True <EOL> return False <EOL> ceilometer_usage = ceilometer . CeilometerUsage ( request ) <EOL> try : <EOL> resources = ceilometer_usage . resources_with_statistics ( <EOL> query , [ meter ] , period = period , stats_attr = None , <EOL> additional_query = additional_query , <EOL> filter_func = filter_by_meter_name ) <EOL> except Exception : <EOL> resources = [ ] <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' ) ) <EOL> series = self . _series_for_meter ( resources , <EOL> '<STR_LIT>' , <EOL> meter_name , <EOL> stats_attr , <EOL> unit ) <EOL> ret = { } <EOL> ret [ '<STR_LIT>' ] = series <EOL> ret [ '<STR_LIT>' ] = { } <EOL> return HttpResponse ( json . dumps ( ret ) , <EOL> mimetype = '<STR_LIT:application/json>' ) </s>
