Exemplo n.º 1
0
def usage(request, tenant_id=None):
    tenant_id = tenant_id or request.user.tenant_id
    today = time.today()
    date_start = datetime.date(today.year, today.month, 1)
    datetime_start = datetime.datetime.combine(date_start, time.time())
    datetime_end = time.utcnow()

    show_terminated = request.GET.get('show_terminated', False)

    try:
        usage = api.usage_get(request, tenant_id, datetime_start, datetime_end)
    except:
        usage = api.nova.Usage(None)
        redirect = reverse("horizon:nova:overview:index")
        exceptions.handle(request,
                          _('Unable to retrieve usage information.'))

    total_ram = 0
    ram_unit = "MB"

    instances = []
    terminated = []
    if hasattr(usage, 'server_usages'):
        total_ram = usage.total_active_memory_mb
        now = datetime.datetime.now()
        for i in usage.server_usages:
            i['uptime_at'] = now - datetime.timedelta(seconds=i['uptime'])
            if i['ended_at'] and not show_terminated:
                terminated.append(i)
            else:
                instances.append(i)

    if total_ram >= 1024:
        ram_unit = "GB"
        total_ram /= 1024

    if request.GET.get('format', 'html') == 'csv':
        template = 'nova/overview/usage.csv'
        mimetype = "text/csv"
    else:
        template = 'nova/overview/usage.html'
        mimetype = "text/html"

    dash_url = horizon.get_dashboard('nova').get_absolute_url()

    return shortcuts.render(request, template, {
                                'usage': usage,
                                'ram_unit': ram_unit,
                                'total_ram': total_ram,
                                'csv_link': '?format=csv',
                                'show_terminated': show_terminated,
                                'datetime_start': datetime_start,
                                'datetime_end': datetime_end,
                                'instances': instances,
                                'dash_url': dash_url},
                            content_type=mimetype)
Exemplo n.º 2
0
    def get_context_data(self, request):
        submitted = self.request.GET.get('meter', None) is not None

        host = client_proxy.get_host(self.request, self.tab_group.kwargs['host_id'])
        if not host:
            raise exceptions.Http302(reverse('horizon:giraffe_dashboard'
                                             ':hosts:index'))

        # @[fbahr] - TODO: make /hosts/hid/meters return only host meters?!
        meters = [meter \
                  for meter in client_proxy.get_host_meters( \
                                       self.request, \
                                       self.tab_group.kwargs['host_id']) \
                  if meter.name.startswith('host')]

        today = time.today()
        month = self.request.GET.get('month', today.month)
        day = self.request.GET.get('day', 0)
        year = self.request.GET.get('year', today.year)

        meter_id = self.request.GET.get('meter', meters[0].id if meters else None)
        meter = next(m for m in meters if int(m.id) == int(meter_id)) \
                    if meter_id \
                    else None

        form = forms.DateMeterForm(initial={'month': month,
                                            'year': year,
                                            'day': 0,
                                            'meter': meter_id},
                                   meters=meters)

        context = {'submitted': submitted}
        if submitted and meter_id:
            avgs = client_proxy.get_host_meter_records_avg(request=self.request,
                                                           host_id=host.id,
                                                           meter_id=meter_id,
                                                           year=year,
                                                           month=month,
                                                           day=int(day))

            ticks = 25 \
                    if   day \
                    else (calendar.monthrange(*map(int, (year, month)))[1] + 1)

            context['graph'] = {'y_data': avgs, 'x_data': range(1, ticks)}

        context['form'] = form
        context['meters'] = meters
        context['host'] = host
        context['meter'] = meter

        return context
Exemplo n.º 3
0
 def get_context_data(self, **kwargs):
     context = super(IndexView, self).get_context_data(**kwargs)
     month = self._get_month()
     year = self._get_year()
     if year and month:
         data = self._get_billing_data(year, month)
         total = float(data[4]['meter_cpu'])\
                 + float(data[4]['meter_disk_io'])\
                 + float(data[4]['meter_net_io'])
         context['total_costs'] = '%0.2f' % total
     self.today = time.today()
     initial = {'month': month if month else self.today.month,
                'year': year if year else self.today.year}
     self.form = forms.DateForm(initial=initial)
     context['form'] = self.form
     return context
Exemplo n.º 4
0
    def summarize(self, start, end):
        if start <= end <= time.today():
            # Convert to datetime.datetime just for API call.
            start = BaseUsage.get_datetime(start)
            end = BaseUsage.get_datetime(end, now=True)
            try:
                self.usage_list = self.get_usage_list(start, end)
            except:
                exceptions.handle(self.request, _("Unable to retrieve usage information."))
        else:
            messages.info(self.request, _("You are viewing data for the future, " "which may or may not exist."))

        for tenant_usage in self.usage_list:
            tenant_summary = tenant_usage.get_summary()
            for key, value in tenant_summary.items():
                self.summary.setdefault(key, 0)
                self.summary[key] += value
Exemplo n.º 5
0
 def get_context_data(self, **kwargs):
     context = super(IndexView, self).get_context_data(**kwargs)
     month = self._get_month()
     year = self._get_year()
     if year and month:
         data = self._get_billing_data(year, month)
         total = float(data[4]['meter_cpu'])\
                 + float(data[4]['meter_disk_io'])\
                 + float(data[4]['meter_net_io'])
         context['total_costs'] = '%0.2f' % total
     self.today = time.today()
     initial = {
         'month': month if month else self.today.month,
         'year': year if year else self.today.year
     }
     self.form = forms.DateForm(initial=initial)
     context['form'] = self.form
     return context
Exemplo n.º 6
0
    def summarize(self, start, end):
        if start <= end <= time.today():
            # Convert to datetime.datetime just for API call.
            start = BaseUsage.get_datetime(start)
            end = BaseUsage.get_datetime(end, now=True)
            try:
                self.usage_list = self.get_usage_list(start, end)
            except:
                exceptions.handle(self.request,
                                  _('Unable to retrieve usage information.'))
        else:
            messages.info(
                self.request,
                _("You are viewing data for the future, "
                  "which may or may not exist."))

        for tenant_usage in self.usage_list:
            tenant_summary = tenant_usage.get_summary()
            for key, value in tenant_summary.items():
                self.summary.setdefault(key, 0)
                self.summary[key] += value
 def get_end(year, month, day=1):
     period = relativedelta(months=1)
     date_end = BaseUsage.get_start(year, month, day) + period
     if date_end > time.today():
         date_end = time.today()
     return date_end
 def today(self):
     return time.today()
Exemplo n.º 9
0
 def get_end(year, month, day=1):
     period = relativedelta(months=1)
     date_end = BaseUsage.get_start(year, month, day) + period
     if date_end > time.today():
         date_end = time.today()
     return date_end
Exemplo n.º 10
0
 def today(self):
     return time.today()