Beispiel #1
0
 def get_context_data(self, **kwargs):
     ret = super(VenturesVenture, self).get_context_data(**kwargs)
     start = None
     end = None
     if self.venture is None or not self.form.is_valid():
         items = []
         cost_data = []
         count_data = []
         cores_data = []
         vcores_data = []
     else:
         if self.venture == '':
             query = HistoryCost.objects.filter(venture=None)
         elif self.venture == '*':
             query = HistoryCost.objects.exclude(venture=None)
         else:
             ventures = []
             _venture_children(self.venture, ventures)
             query = HistoryCost.objects.filter(
                 venture__in=ventures
             )
         start = self.form.cleaned_data['start']
         end = self.form.cleaned_data['end']
         query = query.exclude(device__deleted=True)
         items = _get_summaries(query.all(), start, end, True, self.venture)
         cost_data = []
         count_data = []
         cores_data = []
         vcores_data = []
         one_day = datetime.timedelta(days=1)
         datapoints = set(dp for dp, in
                          query.values_list('start').distinct())
         datapoints |= set(dp for dp, in
                           query.values_list('end').distinct())
         datapoints |= set([start, end])
         datapoints = set(min(max(start, date or start), end) for
                          date in datapoints)
         for date in sorted(datapoints):
             timestamp = calendar.timegm(date.timetuple()) * 1000
             total_cost = get_total_cost(query, date, date + one_day)
             total_count, now_count, devices = get_total_count(
                     query, date, date + one_day)
             total_cores = get_total_cores(query, date, date + one_day)
             total_vcores = get_total_virtual_cores(query, date, date + one_day)
             cost_data.append([timestamp, total_cost])
             count_data.append([timestamp, total_count])
             cores_data.append([timestamp, total_cores])
             vcores_data.append([timestamp, total_vcores])
     ret.update({
         'items': items,
         'venture': self.venture,
         'cost_data': json.dumps(cost_data),
         'count_data': json.dumps(count_data),
         'cores_data': json.dumps(cores_data),
         'vcores_data': json.dumps(vcores_data),
         'form': self.form,
         'start_date': start,
         'end_date': end,
     })
     return ret
Beispiel #2
0
 def get(self, *args, **kwargs):
     profile = self.request.user.get_profile()
     has_perm = profile.has_perm
     if not has_perm(Perm.read_device_info_reports):
         return HttpResponseForbidden(
             "You don't have permission to see reports."
         )
     if 'start' in self.request.GET:
         self.form = DateRangeForm(self.request.GET)
     else:
         self.form = DateRangeForm(initial={
             'start': datetime.date.today() - datetime.timedelta(days=30),
             'end': datetime.date.today(),
         })
     if self.form.is_valid():
         self.ventures = profile.perm_ventures(
             Perm.read_device_info_reports
         ).filter(
             db.Q(parent=None) |
             db.Q(parent__parent=None),
             show_in_ralph=True
         ).order_by('path')
         start = self.form.cleaned_data['start']
         end = self.form.cleaned_data['end']
         total_cloud_cost = get_total_cost(
             HistoryCost.objects.filter(
                 device__model__type=DeviceType.cloud_server.id
             ), start, end
         )
         for venture in self.ventures:
             query = HistoryCost.objects.filter(
                 db.Q(venture=venture) |
                 db.Q(venture__parent=venture) |
                 db.Q(venture__parent__parent=venture) |
                 db.Q(venture__parent__parent__parent=venture) |
                 db.Q(venture__parent__parent__parent__parent=venture)
             ).exclude(device__deleted=True)
             venture.total = get_total_cost(query, start, end)
             (venture.count, venture.count_now,
              devices) = get_total_count(query, start, end)
             venture.core_count = get_total_cores(devices, start, end)
             venture.virtual_core_count = get_total_virtual_cores(
                 devices, start, end
             )
             cloud_cost = get_total_cost(
                 query.filter(
                     device__model__type=DeviceType.cloud_server.id
                 ), start, end
             )
             venture.cloud_use = (cloud_cost or 0) / total_cloud_cost * 100
     else:
         self.ventures = Venture.objects.none()
     if self.request.GET.get('export') == 'csv':
         return self.export_csv()
     return super(ReportVentures, self).get(*args, **kwargs)
Beispiel #3
0
 def get(self, *args, **kwargs):
     profile = self.request.user.get_profile()
     has_perm = profile.has_perm
     if not has_perm(Perm.read_device_info_reports):
         return HttpResponseForbidden(
             "You don't have permission to see reports.")
     if 'start' in self.request.GET:
         self.form = DateRangeForm(self.request.GET)
     else:
         self.form = DateRangeForm(
             initial={
                 'start': datetime.date.today() -
                 datetime.timedelta(days=30),
                 'end': datetime.date.today(),
             })
     if self.form.is_valid():
         self.ventures = profile.perm_ventures(
             Perm.read_device_info_reports).filter(
                 db.Q(parent=None) | db.Q(parent__parent=None),
                 show_in_ralph=True).order_by('path')
         start = self.form.cleaned_data['start']
         end = self.form.cleaned_data['end']
         total_cloud_cost = get_total_cost(
             HistoryCost.objects.filter(
                 device__model__type=DeviceType.cloud_server.id), start,
             end)
         for venture in self.ventures:
             query = HistoryCost.objects.filter(
                 db.Q(venture=venture) | db.Q(venture__parent=venture)
                 | db.Q(venture__parent__parent=venture)
                 | db.Q(venture__parent__parent__parent=venture)
                 | db.Q(venture__parent__parent__parent__parent=venture)
             ).exclude(device__deleted=True)
             venture.total = get_total_cost(query, start, end)
             (venture.count, venture.count_now,
              devices) = get_total_count(query, start, end)
             venture.core_count = get_total_cores(devices, start, end)
             venture.virtual_core_count = get_total_virtual_cores(
                 devices, start, end)
             cloud_cost = get_total_cost(
                 query.filter(
                     device__model__type=DeviceType.cloud_server.id), start,
                 end)
             venture.cloud_use = (cloud_cost or 0) / total_cloud_cost * 100
     else:
         self.ventures = Venture.objects.none()
     if self.request.GET.get('export') == 'csv':
         return self.export_csv()
     return super(ReportVentures, self).get(*args, **kwargs)
Beispiel #4
0
def _report_ventures_get_totals(start, end, query, extra_types):
    venture_total = get_total_cost(query, start, end)
    venture_count, venture_count_now, devices = get_total_count(
        # Exclude all non-physical devices.
        query.exclude(
            device__model__type__in=(
                DeviceType.cloud_server,
                DeviceType.virtual_server,
                DeviceType.unknown,
                DeviceType.data_center,
                DeviceType.rack,
                DeviceType.management,
                DeviceType.switch_stack,
            ),
        ).exclude(
            device_id__isnull=True
        ),
        start,
        end,
    )
    venture_core_count = get_total_cores(devices, start, end)
    venture_virtual_core_count = get_total_virtual_cores(
        devices, start, end
    )
    q = query.filter(extra=None)
    venture_hardware_cost = get_total_cost(q, start, end)
    cloud_cost = get_total_cost(
        query.filter(
            device__model__type=DeviceType.cloud_server.id
        ), start, end
    )
    venture_extras = []
    for extra_type in extra_types:
        cost = None
        for extra_cost in extra_type.ventureextracost_set.all():
            q = query.filter(extra=extra_cost)
            c = get_total_cost(q, start, end)
            cost = cost + (c or 0) if cost else c
        venture_extras.append(cost)
    return {
        'count': venture_count,
        'count_now': venture_count_now,
        'core_count': venture_core_count,
        'virtual_core_count': venture_virtual_core_count,
        'hardware_cost': venture_hardware_cost,
        'cloud_cost': cloud_cost,
        'extras': venture_extras,
        'total': venture_total,
    }
Beispiel #5
0
def _report_ventures_get_totals(start, end, query, extra_types):
    venture_total = get_total_cost(query, start, end)
    venture_count, venture_count_now, devices = get_total_count(
        # Exclude all non-physical devices.
        query.exclude(device__model__type__in=(
            DeviceType.cloud_server,
            DeviceType.virtual_server,
            DeviceType.unknown,
            DeviceType.data_center,
            DeviceType.rack,
            DeviceType.management,
            DeviceType.switch_stack,
        ), ).exclude(device_id__isnull=True),
        start,
        end,
    )
    venture_core_count = get_total_cores(devices, start, end)
    venture_virtual_core_count = get_total_virtual_cores(devices, start, end)
    q = query.filter(extra=None)
    venture_hardware_cost = get_total_cost(q, start, end)
    cloud_cost = get_total_cost(
        query.filter(device__model__type=DeviceType.cloud_server.id), start,
        end)
    venture_extras = []
    for extra_type in extra_types:
        cost = None
        for extra_cost in extra_type.ventureextracost_set.all():
            q = query.filter(extra=extra_cost)
            c = get_total_cost(q, start, end)
            cost = cost + (c or 0) if cost else c
        venture_extras.append(cost)
    return {
        'count': venture_count,
        'count_now': venture_count_now,
        'core_count': venture_core_count,
        'virtual_core_count': venture_virtual_core_count,
        'hardware_cost': venture_hardware_cost,
        'cloud_cost': cloud_cost,
        'extras': venture_extras,
        'total': venture_total,
    }
Beispiel #6
0
 def _get_totals(self, start, end, query, extra_types):
     venture_total = get_total_cost(query, start, end)
     venture_count, venture_count_now, devices = get_total_count(
         # Exclude all non-physical devices.
         query.exclude(
             device__model__type__in=(
                 DeviceType.cloud_server,
                 DeviceType.virtual_server,
                 DeviceType.unknown,
                 DeviceType.data_center,
                 DeviceType.rack,
                 DeviceType.management,
             )
         ),
         start,
         end,
     )
     venture_core_count = get_total_cores(devices, start, end)
     venture_virtual_core_count = get_total_virtual_cores(devices, start, end)
     q = query.filter(extra=None)
     venture_hardware_cost = get_total_cost(q, start, end)
     cloud_cost = get_total_cost(query.filter(device__model__type=DeviceType.cloud_server.id), start, end)
     venture_extras = []
     for extra_type in extra_types:
         cost = None
         for extra_cost in extra_type.ventureextracost_set.all():
             q = query.filter(extra=extra_cost)
             c = get_total_cost(q, start, end)
             cost = cost + (c or 0) if cost else c
         venture_extras.append(cost)
     return {
         "count": venture_count,
         "count_now": venture_count_now,
         "core_count": venture_core_count,
         "virtual_core_count": venture_virtual_core_count,
         "hardware_cost": venture_hardware_cost,
         "cloud_cost": cloud_cost,
         "extras": venture_extras,
         "total": venture_total,
     }