Пример #1
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)
Пример #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)
Пример #3
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,
    }
Пример #4
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
Пример #5
0
 def get_context_data(self, **kwargs):
     context = super(ReportMargins, self).get_context_data(**kwargs)
     if self.form.is_valid():
         venture = Venture.objects.get(
                 id=self.form.cleaned_data['margin_venture'])
         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)
         )
         total_cost = 0
         total_sim = 0
         total_count = 0
         start = self.form.cleaned_data['start']
         end = self.form.cleaned_data['end']
         for mk in self.margin_kinds:
             q = query.filter(db.Q(device__margin_kind=mk) |
                  db.Q(
                     db.Q(device__margin_kind=None) &
                     db.Q(
                         db.Q(device__venture__margin_kind=mk) |
                         db.Q(device__venture__margin_kind=None,
                              device__venture__parent__margin_kind=mk) |
                         db.Q(device__venture__margin_kind=None,
                              device__venture__parent__margin_kind=None,
                              device__venture__parent__parent__margin_kind=mk) |
                         db.Q(device__venture__margin_kind=None,
                              device__venture__parent__margin_kind=None,
                              device__venture__parent__parent__margin_kind=None,
                     device__venture__parent__parent__parent__margin_kind=mk)
                     )
                 )
             )
             mk.total = get_total_cost(q, start, end)
             mk.count, mk.count_now, devices = get_total_count(q, start, end)
             mk.sim_margin = self.form.get('m_%d' % mk.id, 0) or 0
             mk.sim_cost = ((mk.total or 0) /
                            (1 + mk.margin/100) *
                            (1 + mk.sim_margin/100))
             total_sim += mk.sim_cost
             total_cost += mk.total or 0
             total_count += mk.count or 0
         context.update({
             'venture': venture,
             'total_cost': total_cost,
             'total_sim': total_sim,
             'total_count': total_count,
         })
     context.update({
         'form': self.form,
         'margin_kinds': self.margin_kinds,
         'zip_margin_kinds_form': zip([f for f in self.form if
                                       not f.label],
                                       self.margin_kinds),
     })
     return context
Пример #6
0
 def get_context_data(self, **kwargs):
     context = super(ReportMargins, self).get_context_data(**kwargs)
     if self.form.is_valid():
         venture = Venture.objects.get(
             id=self.form.cleaned_data['margin_venture'])
         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)
         total_cost = 0
         total_sim = 0
         total_count = 0
         start = self.form.cleaned_data['start']
         end = self.form.cleaned_data['end']
         for mk in self.margin_kinds:
             q = query.filter(
                 db.Q(device__margin_kind=mk) | db.Q(
                     db.Q(device__margin_kind=None) & db.Q(
                         db.Q(device__venture__margin_kind=mk)
                         | db.Q(device__venture__margin_kind=None,
                                device__venture__parent__margin_kind=mk)
                         | db.
                         Q(device__venture__margin_kind=None,
                           device__venture__parent__margin_kind=None,
                           device__venture__parent__parent__margin_kind=mk)
                         | db.
                         Q(device__venture__margin_kind=None,
                           device__venture__parent__margin_kind=None,
                           device__venture__parent__parent__margin_kind=None,
                           device__venture__parent__parent__parent__margin_kind
                           =mk))))
             mk.total = get_total_cost(q, start, end)
             mk.count, mk.count_now, devices = get_total_count(
                 q, start, end)
             mk.sim_margin = self.form.get('m_%d' % mk.id) or 0
             mk.sim_cost = ((mk.total or 0) / (1 + mk.margin / 100) *
                            (1 + mk.sim_margin / 100))
             total_sim += mk.sim_cost
             total_cost += mk.total or 0
             total_count += mk.count or 0
         context.update({
             'venture': venture,
             'total_cost': total_cost,
             'total_sim': total_sim,
             'total_count': total_count,
         })
     context.update({
         'form':
         self.form,
         'margin_kinds':
         self.margin_kinds,
         'zip_margin_kinds_form':
         zip([f for f in self.form if not f.label], self.margin_kinds),
     })
     return context
Пример #7
0
 def get_context_data(self, **kwargs):
     context = super(ReportMargins, self).get_context_data(**kwargs)
     if self.form.is_valid():
         venture = Venture.objects.get(id=self.form.cleaned_data["margin_venture"])
         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)
         total_cost = 0
         total_sim = 0
         total_count = 0
         start = self.form.cleaned_data["start"]
         end = self.form.cleaned_data["end"]
         for mk in self.margin_kinds:
             q = query.filter(
                 db.Q(device__margin_kind=mk)
                 | db.Q(
                     db.Q(device__margin_kind=None)
                     & db.Q(
                         db.Q(device__venture__margin_kind=mk)
                         | db.Q(device__venture__margin_kind=None, device__venture__parent__margin_kind=mk)
                         | db.Q(
                             device__venture__margin_kind=None,
                             device__venture__parent__margin_kind=None,
                             device__venture__parent__parent__margin_kind=mk,
                         )
                         | db.Q(
                             device__venture__margin_kind=None,
                             device__venture__parent__margin_kind=None,
                             device__venture__parent__parent__margin_kind=None,
                             device__venture__parent__parent__parent__margin_kind=mk,
                         )
                     )
                 )
             )
             mk.total = get_total_cost(q, start, end)
             mk.count, mk.count_now, devices = get_total_count(q, start, end)
             mk.sim_margin = self.form.get("m_%d" % mk.id) or 0
             mk.sim_cost = (mk.total or 0) / (1 + mk.margin / 100) * (1 + mk.sim_margin / 100)
             total_sim += mk.sim_cost
             total_cost += mk.total or 0
             total_count += mk.count or 0
         context.update(
             {"venture": venture, "total_cost": total_cost, "total_sim": total_sim, "total_count": total_count}
         )
     context.update(
         {
             "form": self.form,
             "margin_kinds": self.margin_kinds,
             "zip_margin_kinds_form": zip([f for f in self.form if not f.label], self.margin_kinds),
         }
     )
     return context
Пример #8
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,
    }
Пример #9
0
def _total_dict(name, query, start, end, url=None):
    cost = get_total_cost(query, start, end)
    count, count_now, devices = get_total_count(query, start, end)
    if not count and not count_now:
        return None
    return {
        'name': name,
        'count': count,
        'cost': cost,
        'count_now': count_now,
        'url': url,
    }
Пример #10
0
def _total_dict(name, query, start, end, url=None):
    cost = get_total_cost(query, start, end)
    count, count_now, devices = get_total_count(query, start, end)
    if not count and not count_now:
        return None
    return {
        'name': name,
        'count': count,
        'cost': cost,
        'count_now': count_now,
        'url': url,
    }
Пример #11
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,
     }
Пример #12
0
 def _get_venture_data(self, start, end, ventures, extra_types):
     total_cloud_cost = get_total_cost(
         HistoryCost.objects.filter(device__model__type=DeviceType.cloud_server.id), start, end
     )
     for venture in 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)
         data = self._get_totals(start, end, query, extra_types)
         data.update(
             {
                 "id": venture.id,
                 "name": venture.name,
                 "symbol": venture.symbol,
                 "path": venture.path,
                 "department": unicode(venture.department or ""),
                 "margin": venture.get_margin(),
                 "top_level": venture.parent is None,
                 "venture": venture,
                 "cloud_use": ((data["cloud_cost"] or 0) / total_cloud_cost) if total_cloud_cost else 0,
             }
         )
         yield data
         if venture.parent is not None:
             continue
         if not venture.child_set.exists():
             continue
         query = HistoryCost.objects.filter(venture=venture)
         data = self._get_totals(start, end, query, extra_types)
         data.update(
             {
                 "id": venture.id,
                 "name": "-",
                 "symbol": venture.symbol,
                 "path": venture.path,
                 "department": unicode(venture.department or ""),
                 "margin": venture.get_margin(),
                 "top_level": False,
                 "venture": venture,
                 "cloud_use": ((data["cloud_cost"] or 0) / total_cloud_cost) if total_cloud_cost else 0,
             }
         )
         yield data
Пример #13
0
def _report_ventures_data_provider(start, end, ventures_ids, extra_types):
    ventures = Venture.objects.filter(id__in=ventures_ids).order_by('path')
    total_cloud_cost = get_total_cost(
        HistoryCost.objects.filter(
            device__model__type=DeviceType.cloud_server.id, ),
        start,
        end,
    )
    result = []
    for venture in 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)
        data = _report_ventures_get_totals(start, end, query, extra_types)
        (
            splunk_cost,
            splunk_count,
            splunk_count_now,
            splunk_size,
        ) = SplunkUsage.get_cost(venture, start, end)
        data.update({
            'id':
            venture.id,
            'name':
            venture.name,
            'symbol':
            venture.symbol,
            'path':
            venture.path,
            'department':
            unicode(venture.department or ''),
            'margin':
            venture.get_margin(),
            'top_level':
            venture.parent is None,
            'venture_icon':
            get_venture_icon(venture),
            'cloud_use': ((data['cloud_cost'] or 0) /
                          total_cloud_cost) if total_cloud_cost else 0,
            'splunk_cost':
            splunk_cost,
        })
        result.append(data)
        if venture.parent is not None:
            continue
        if not venture.child_set.exists():
            continue
        query = HistoryCost.objects.filter(venture=venture)
        data = _report_ventures_get_totals(start, end, query, extra_types)
        (
            splunk_cost,
            splunk_count,
            splunk_count_now,
            splunk_size,
        ) = SplunkUsage.get_cost(venture, start, end, shallow=True)
        data.update({
            'id':
            venture.id,
            'name':
            '-',
            'symbol':
            venture.symbol,
            'path':
            venture.path,
            'department':
            unicode(venture.department or ''),
            'margin':
            venture.get_margin(),
            'top_level':
            False,
            'venture_icon':
            get_venture_icon(venture),
            'cloud_use': ((data['cloud_cost'] or 0) /
                          total_cloud_cost) if total_cloud_cost else 0,
            'splunk_cost':
            splunk_cost,
        })
        result.append(data)
    return result
Пример #14
0
def _report_ventures_data_provider(start, end, ventures_ids, extra_types):
    ventures = Venture.objects.filter(id__in=ventures_ids).order_by('path')
    total_cloud_cost = get_total_cost(
        HistoryCost.objects.filter(
            device__model__type=DeviceType.cloud_server.id,
        ),
        start,
        end,
    )
    result = []
    for venture in 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)
        data = _report_ventures_get_totals(start, end, query, extra_types)
        (
            splunk_cost,
            splunk_count,
            splunk_count_now,
            splunk_size,
        ) = SplunkUsage.get_cost(venture, start, end)
        data.update({
            'id': venture.id,
            'name': venture.name,
            'symbol': venture.symbol,
            'path': venture.path,
            'department': unicode(venture.department or ''),
            'margin': venture.get_margin(),
            'top_level': venture.parent is None,
            'venture_icon': get_venture_icon(venture),
            'cloud_use': (
                (data['cloud_cost'] or 0) / total_cloud_cost
            ) if total_cloud_cost else 0,
            'splunk_cost': splunk_cost,
        })
        result.append(data)
        if venture.parent is not None:
            continue
        if not venture.child_set.exists():
            continue
        query = HistoryCost.objects.filter(venture=venture)
        data = _report_ventures_get_totals(start, end, query, extra_types)
        (
            splunk_cost,
            splunk_count,
            splunk_count_now,
            splunk_size,
        ) = SplunkUsage.get_cost(venture, start, end, shallow=True)
        data.update({
            'id': venture.id,
            'name': '-',
            'symbol': venture.symbol,
            'path': venture.path,
            'department': unicode(venture.department or ''),
            'margin': venture.get_margin(),
            'top_level': False,
            'venture_icon': get_venture_icon(venture),
            'cloud_use': (
                (data['cloud_cost'] or 0) / total_cloud_cost
            ) if total_cloud_cost else 0,
            'splunk_cost': splunk_cost,
        })
        result.append(data)
    return result
Пример #15
0
def _get_summaries(query, start, end, overlap=True, venture=None):
    if overlap:
        yield _total_dict(
            'Servers',
            query.filter(
                device__model__type__in=(DeviceType.rack_server.id,
                                         DeviceType.blade_server.id,
                                         DeviceType.virtual_server.id)), start,
            end, _get_search_url(venture, type=(201, 202, 203)))
    for dc in DataCenter.objects.all():
        yield _total_dict(
            '  • Servers in %s' % dc.name,
            query.filter(
                device__model__type__in=(DeviceType.rack_server.id,
                                         DeviceType.blade_server.id,
                                         DeviceType.virtual_server.id)).filter(
                                             device__dc__iexact=dc.name),
            start, end, _get_search_url(venture, dc=dc, type=(201, 202, 203)))
        if overlap:
            yield _total_dict(
                '    ∙ Rack servers in %s' % dc.name,
                query.filter(
                    device__model__type=DeviceType.rack_server.id, ).filter(
                        device__dc__iexact=dc.name), start, end,
                _get_search_url(venture, dc=dc, type=(201, )))
            for mg in DeviceModelGroup.objects.filter(
                    type=DeviceType.rack_server.id).order_by('name'):
                yield _total_dict(
                    '        %s in %s' % (mg, dc.name),
                    query.filter(device__model__group=mg, ).filter(
                        device__dc__iexact=dc.name), start, end,
                    _get_search_url(venture,
                                    dc=dc,
                                    type=(201, ),
                                    model_group=mg.id))
            yield _total_dict(
                '    ∙ Blade servers in %s' % dc.name,
                query.filter(
                    device__model__type=DeviceType.blade_server.id, ).filter(
                        device__dc__iexact=dc.name), start, end,
                _get_search_url(venture, dc=dc, type=(202, )))
            for mg in DeviceModelGroup.objects.filter(
                    type=DeviceType.blade_server.id).order_by('name'):
                yield _total_dict(
                    '        %s in %s' % (mg, dc.name),
                    query.filter(device__model__group=mg, ).filter(
                        device__dc__iexact=dc.name), start, end,
                    _get_search_url(venture,
                                    dc=dc,
                                    type=(202, ),
                                    model_group=mg.id))
            yield _total_dict(
                '    ∙ Virtual servers in %s' % dc.name,
                query.filter(
                    device__model__type=DeviceType.virtual_server.id, ).filter(
                        device__dc__iexact=dc.name), start, end,
                _get_search_url(venture, dc=dc, type=(203, )))
    if overlap:
        yield _total_dict(
            'Loadbalancers',
            query.filter(
                device__model__type__in=(DeviceType.load_balancer.id, )),
            start, end, _get_search_url(venture, type=(103, )))
    for dc in DataCenter.objects.all():
        yield _total_dict(
            ' • Loadbalancers in %s' % dc.name,
            query.filter(device__model__type__in=(
                DeviceType.load_balancer.id, )).filter(
                    device__dc__iexact=dc.name), start, end,
            _get_search_url(venture, dc=dc, type=(103, )))
    if overlap:
        yield _total_dict(
            'Storage',
            query.filter(device__model__type__in=(
                DeviceType.storage.id,
                DeviceType.fibre_channel_switch.id,
            )), start, end, _get_search_url(venture, type=(301, )))
    for dc in DataCenter.objects.all():
        yield _total_dict(
            ' • Storage in %s' % dc.name,
            query.filter(device__model__type__in=(
                DeviceType.storage.id,
                DeviceType.fibre_channel_switch.id,
            )).filter(device__dc__iexact=dc.name), start, end,
            _get_search_url(venture, dc=dc, type=(301, )))
    if overlap:
        yield _total_dict(
            'Network',
            query.filter(device__model__type__in=(
                DeviceType.switch.id,
                DeviceType.router.id,
                DeviceType.firewall.id,
                DeviceType.smtp_gateway.id,
                DeviceType.appliance.id,
            )), start, end,
            _get_search_url(venture,
                            type=(
                                DeviceType.switch.id,
                                DeviceType.router.id,
                                DeviceType.firewall.id,
                                DeviceType.smtp_gateway.id,
                                DeviceType.appliance.id,
                            )))
    for dc in DataCenter.objects.all():
        yield _total_dict(
            ' • Network in %s' % dc.name,
            query.filter(device__model__type__in=(
                DeviceType.switch.id,
                DeviceType.router.id,
                DeviceType.firewall.id,
                DeviceType.smtp_gateway.id,
                DeviceType.appliance.id,
            )).filter(device__dc__iexact=dc.name), start, end,
            _get_search_url(venture,
                            dc=dc,
                            type=(
                                DeviceType.switch.id,
                                DeviceType.router.id,
                                DeviceType.firewall.id,
                                DeviceType.smtp_gateway.id,
                                DeviceType.appliance.id,
                            )))
    yield _total_dict(
        'Cloud',
        query.filter(device__model__type__in=(DeviceType.cloud_server.id, )),
        start, end,
        _get_search_url(venture, type=(DeviceType.cloud_server.id, )))
    if overlap:
        yield _total_dict(
            'Unknown',
            query.filter(device__model__type__in=(DeviceType.unknown.id, )),
            start, end, _get_search_url(venture,
                                        type=(DeviceType.unknown.id, )))
    for dc in DataCenter.objects.all():
        yield _total_dict(
            ' • Unknown in %s' % dc.name,
            query.filter(
                device__model__type__in=(DeviceType.unknown.id, )).filter(
                    device__dc__iexact=dc.name), start, end,
            _get_search_url(venture, dc=dc, type=(DeviceType.unknown.id, )))
    splunk_usage = SplunkUsage.objects.filter(day__gte=start, day__lte=end)
    if venture and venture != '*':
        splunk_usage = splunk_usage.filter(
            db.Q(device__venture=venture)
            | db.Q(device__venture__parent=venture)
            | db.Q(device__venture__parent__parent=venture)
            | db.Q(device__venture__parent__parent__parent=venture)
            | db.Q(device__venture__parent__parent__parent__parent=venture))
    elif not venture:  # specifically "devices with no venture set"
        splunk_usage = splunk_usage.filter(device__venture=None)
    if splunk_usage.count():
        splunk_size = splunk_usage.aggregate(db.Sum('size'))['size__sum'] or 0
        splunk_count = splunk_usage.values('device').distinct().count()
        yesterday = datetime.date.today() - datetime.timedelta(days=1)
        splunk_count_now = SplunkUsage.objects.filter(
            day=yesterday).values('device').distinct().count()
        url = None
        try:
            splunk_model = ComponentModel.objects.get(family='splunkvolume')
        except ComponentModel.DoesNotExist:
            pass
        else:
            if splunk_model.group_id:
                url = ('/ui/search/components/'
                       '?component_group=%d' % splunk_model.group_id)
        yield {
            'name':
            'Splunk usage ({:,.0f} MB)'.format(splunk_size).replace(',', ' '),
            'cost':
            splunk_usage[0].get_price(size=splunk_size),
            'count':
            splunk_count,
            'count_now':
            splunk_count_now,
            'url':
            url,
        }
    for extra_id, in query.values_list('extra_id').distinct():
        if extra_id is None:
            continue
        extra = VentureExtraCost.objects.get(id=extra_id)
        q = query.filter(extra=extra)
        cost = get_total_cost(q, start, end)
        count, count_now, devices = get_total_count(q, start, end)
        yield {
            'name':
            extra.name + ' (from %s)' % extra.venture.name,
            'count':
            'expires %s' %
            extra.expire.strftime('%Y-%m-%d') if extra.expire else '',
            'cost':
            cost,
            'count_now':
            count_now,
        }
    if overlap:
        yield _total_dict('Total', query, start, end,
                          _get_search_url(venture, type=()))
Пример #16
0
def _get_summaries(query, start, end, overlap=True, venture=None):
    if overlap:
        yield _total_dict('Servers', query.filter(
            device__model__type__in=(DeviceType.rack_server.id,
                                     DeviceType.blade_server.id,
                                     DeviceType.virtual_server.id)), start, end,
                          _get_search_url(venture, type=(201, 202, 203)))
    for dc in DataCenter.objects.all():
        yield _total_dict('  • Servers in %s' % dc.name, query.filter(
            device__model__type__in=(DeviceType.rack_server.id,
                                     DeviceType.blade_server.id,
                                     DeviceType.virtual_server.id)
        ).filter(device__dc__iexact=dc.name), start, end,
            _get_search_url(venture, dc=dc, type=(201, 202, 203))
        )
        if overlap:
            yield _total_dict(
                '    ∙ Rack servers in %s' % dc.name, query.filter(
                    device__model__type=DeviceType.rack_server.id,
                ).filter(device__dc__iexact=dc.name), start, end,
                _get_search_url(venture, dc=dc, type=(201,))
            )
            for mg in DeviceModelGroup.objects.filter(
                    type=DeviceType.rack_server.id).order_by('name'):
                yield _total_dict(
                    '        %s in %s' % (mg, dc.name), query.filter(
                        device__model__group=mg,
                    ).filter(device__dc__iexact=dc.name), start, end,
                    _get_search_url(venture, dc=dc, type=(201,),
                                    model_group=mg.id)
                )
            yield _total_dict(
                '    ∙ Blade servers in %s' % dc.name, query.filter(
                    device__model__type=DeviceType.blade_server.id,
                ).filter(device__dc__iexact=dc.name), start, end,
                _get_search_url(venture, dc=dc, type=(202,))
            )
            for mg in DeviceModelGroup.objects.filter(
                    type=DeviceType.blade_server.id).order_by('name'):
                yield _total_dict(
                    '        %s in %s' % (mg, dc.name), query.filter(
                        device__model__group=mg,
                    ).filter(device__dc__iexact=dc.name), start, end,
                    _get_search_url(venture, dc=dc, type=(202,),
                                    model_group=mg.id)
                )
            yield _total_dict(
                '    ∙ Virtual servers in %s' % dc.name, query.filter(
                    device__model__type=DeviceType.virtual_server.id,
                ).filter(device__dc__iexact=dc.name), start, end,
                _get_search_url(venture, dc=dc, type=(203,))
            )
    if overlap:
        yield _total_dict('Loadbalancers', query.filter(
            device__model__type__in=(DeviceType.load_balancer.id,)
        ), start, end, _get_search_url(venture, type=(103,)))
    for dc in DataCenter.objects.all():
        yield _total_dict(' • Loadbalancers in %s' % dc.name, query.filter(
            device__model__type__in=(DeviceType.load_balancer.id,)
        ).filter(device__dc__iexact=dc.name), start, end,
            _get_search_url(venture, dc=dc, type=(103,))
        )
    if overlap:
        yield _total_dict('Storage', query.filter(
            device__model__type__in=(
                DeviceType.storage.id,
                DeviceType.fibre_channel_switch.id,
            )), start, end,
            _get_search_url(venture, type=(301,))
        )
    for dc in DataCenter.objects.all():
        yield _total_dict(' • Storage in %s' % dc.name, query.filter(
            device__model__type__in=(
                DeviceType.storage.id,
                DeviceType.fibre_channel_switch.id,
            )
        ).filter(device__dc__iexact=dc.name), start, end,
            _get_search_url(venture, dc=dc, type=(301,))
        )
    if overlap:
        yield _total_dict('Network', query.filter(
            device__model__type__in=(
                DeviceType.switch.id,
                DeviceType.router.id,
                DeviceType.firewall.id,
                DeviceType.smtp_gateway.id,
                DeviceType.appliance.id,
            )
        ), start, end,
            _get_search_url(venture, type=(
                DeviceType.switch.id,
                DeviceType.router.id,
                DeviceType.firewall.id,
                DeviceType.smtp_gateway.id,
                DeviceType.appliance.id,
            ))
        )
    for dc in DataCenter.objects.all():
        yield _total_dict(' • Network in %s' % dc.name, query.filter(
            device__model__type__in=(
                DeviceType.switch.id,
                DeviceType.router.id,
                DeviceType.firewall.id,
                DeviceType.smtp_gateway.id,
                DeviceType.appliance.id,
            )
        ).filter(device__dc__iexact=dc.name), start, end,
            _get_search_url(venture, dc=dc, type=(
                DeviceType.switch.id,
                DeviceType.router.id,
                DeviceType.firewall.id,
                DeviceType.smtp_gateway.id,
                DeviceType.appliance.id,
            ))
        )
    yield _total_dict('Cloud', query.filter(
        device__model__type__in=(DeviceType.cloud_server.id,)
    ), start, end,
        _get_search_url(venture, type=(DeviceType.cloud_server.id,))
    )
    if overlap:
        yield _total_dict('Unknown', query.filter(
            device__model__type__in=(DeviceType.unknown.id,)), start, end,
            _get_search_url(venture, type=(DeviceType.unknown.id,))
        )
    for dc in DataCenter.objects.all():
        yield _total_dict(' • Unknown in %s' % dc.name, query.filter(
            device__model__type__in=(DeviceType.unknown.id,)
        ).filter(device__dc__iexact=dc.name), start, end,
            _get_search_url(venture, dc=dc, type=(DeviceType.unknown.id,))
        )
    (
        splunk_cost,
        splunk_count,
        splunk_count_now,
        splunk_size,
    ) = SplunkUsage.get_cost(venture, start, end)
    if splunk_cost:
        url = None
        try:
            splunk_model = ComponentModel.objects.get(family='splunkvolume')
        except ComponentModel.DoesNotExist:
            pass
        else:
            if splunk_model.group_id:
                url = ('/ui/search/components/'
                       '?component_group=%d' % splunk_model.group_id)
        yield {
            'name': 'Splunk usage ({:,.0f} MB)'.format(
                splunk_size).replace(',', ' '),
            'cost': splunk_cost,
            'count': splunk_count,
            'count_now': splunk_count_now,
            'url': url,
        }
    for extra_id, in query.values_list('extra_id').distinct():
        if extra_id is None:
            continue
        extra = VentureExtraCost.objects.get(id=extra_id)
        q = query.filter(extra=extra)
        cost = get_total_cost(q, start, end)
        count, count_now, devices = get_total_count(q, start, end)
        if count:
            yield {
                'name': extra.name + ' (from %s)' % extra.venture.name,
                'count': 'expires %s' % extra.expire.strftime(
                    '%Y-%m-%d') if extra.expire else '',
                'cost': cost,
                'count_now': count_now,
            }
    if overlap:
        yield _total_dict(
            'Total',
            query,
            start,
            end,
            _get_search_url(venture, type=()),
        )
        yield _total_dict(
            'Total physical',
            query.exclude(
                device__model__type__in=(
                    DeviceType.cloud_server,
                    DeviceType.virtual_server,
                    DeviceType.unknown,
                    DeviceType.data_center,
                    DeviceType.rack,
                    DeviceType.management,
                ),
            ).exclude(
                device=None,
            ),
            start,
            end,
            _get_search_url(venture, type=()),
        )
Пример #17
0
def _get_summaries(query, start, end, overlap=True, venture=None):
    if overlap:
        yield _total_dict('Servers', query.filter(
            device__model__type__in=(DeviceType.rack_server.id,
                             DeviceType.blade_server.id,
                             DeviceType.virtual_server.id)), start, end,
            _get_search_url(venture, type=(201, 202, 203)))
    for dc in DataCenter.objects.all():
        yield _total_dict('  • Servers in %s' % dc.name, query.filter(
            device__model__type__in=(DeviceType.rack_server.id,
                             DeviceType.blade_server.id,
                             DeviceType.virtual_server.id)
            ).filter(device__dc__iexact=dc.name), start, end,
            _get_search_url(venture, dc=dc, type=(201, 202, 203))
            )
        if overlap:
            yield _total_dict(
                '    ∙ Rack servers in %s' % dc.name, query.filter(
                        device__model__type=DeviceType.rack_server.id,
                    ).filter(device__dc__iexact=dc.name), start, end,
                        _get_search_url(venture, dc=dc, type=(201,))
                    )
            for mg in DeviceModelGroup.objects.filter(
                    type=DeviceType.rack_server.id).order_by('name'):
                yield _total_dict(
                    '        %s in %s' % (mg, dc.name), query.filter(
                            device__model__group=mg,
                        ).filter(device__dc__iexact=dc.name), start, end,
                        _get_search_url(venture, dc=dc, type=(201,),
                                        model_group=mg.id)
                        )
            yield _total_dict(
                '    ∙ Blade servers in %s' % dc.name, query.filter(
                        device__model__type=DeviceType.blade_server.id,
                    ).filter(device__dc__iexact=dc.name), start, end,
                        _get_search_url(venture, dc=dc, type=(202,))
                    )
            for mg in DeviceModelGroup.objects.filter(
                    type=DeviceType.blade_server.id).order_by('name'):
                yield _total_dict(
                    '        %s in %s' % (mg, dc.name), query.filter(
                            device__model__group=mg,
                        ).filter(device__dc__iexact=dc.name), start, end,
                            _get_search_url(venture, dc=dc, type=(202,),
                                            model_group=mg.id)
                        )
            yield _total_dict(
                '    ∙ Virtual servers in %s' % dc.name, query.filter(
                        device__model__type=DeviceType.virtual_server.id,
                    ).filter(device__dc__iexact=dc.name), start, end,
                        _get_search_url(venture, dc=dc, type=(203,))
                    )
    if overlap:
        yield _total_dict('Loadbalancers', query.filter(
                device__model__type__in=(DeviceType.load_balancer.id,)
            ), start, end, _get_search_url(venture, type=(103,)))
    for dc in DataCenter.objects.all():
        yield _total_dict(' • Loadbalancers in %s' % dc.name, query.filter(
                device__model__type__in=(DeviceType.load_balancer.id,)
            ).filter(device__dc__iexact=dc.name), start, end,
                _get_search_url(venture, dc=dc, type=(103,))
            )
    if overlap:
        yield _total_dict('Storage', query.filter(
            device__model__type__in=(
                DeviceType.storage.id,
                DeviceType.fibre_channel_switch.id,
            )), start, end,
                _get_search_url(venture, type=(301,))
            )
    for dc in DataCenter.objects.all():
        yield _total_dict(' • Storage in %s' % dc.name, query.filter(
            device__model__type__in=(
                    DeviceType.storage.id,
                    DeviceType.fibre_channel_switch.id,
                )
            ).filter(device__dc__iexact=dc.name), start, end,
                _get_search_url(venture, dc=dc, type=(301,))
            )
    if overlap:
        yield _total_dict('Network', query.filter(
            device__model__type__in=(
                DeviceType.switch.id,
                DeviceType.router.id,
                DeviceType.firewall.id,
                DeviceType.smtp_gateway.id,
                DeviceType.appliance.id,
            )
            ), start, end,
                _get_search_url(venture, type=(
                    DeviceType.switch.id,
                    DeviceType.router.id,
                    DeviceType.firewall.id,
                    DeviceType.smtp_gateway.id,
                    DeviceType.appliance.id,
                ))
            )
    for dc in DataCenter.objects.all():
        yield _total_dict(' • Network in %s' % dc.name, query.filter(
            device__model__type__in=(
                DeviceType.switch.id,
                DeviceType.router.id,
                DeviceType.firewall.id,
                DeviceType.smtp_gateway.id,
                DeviceType.appliance.id,
            )
            ).filter(device__dc__iexact=dc.name), start, end,
                _get_search_url(venture, dc=dc, type=(
                    DeviceType.switch.id,
                    DeviceType.router.id,
                    DeviceType.firewall.id,
                    DeviceType.smtp_gateway.id,
                    DeviceType.appliance.id,
                ))
            )
    yield _total_dict('Cloud', query.filter(
            device__model__type__in=(DeviceType.cloud_server.id,)
        ), start, end,
            _get_search_url(venture, type=(DeviceType.cloud_server.id,))
        )
    if overlap:
        yield _total_dict('Unknown', query.filter(
            device__model__type__in=(DeviceType.unknown.id,)), start, end,
                _get_search_url(venture, type=(DeviceType.unknown.id,))
            )
    for dc in DataCenter.objects.all():
        yield _total_dict(' • Unknown in %s' % dc.name, query.filter(
            device__model__type__in=(DeviceType.unknown.id,)
            ).filter(device__dc__iexact=dc.name), start, end,
                _get_search_url(venture, dc=dc, type=(DeviceType.unknown.id,))
            )
    splunk_usage = SplunkUsage.objects.filter(day__gte=start, day__lte=end)
    if venture and venture != '*':
        splunk_usage = splunk_usage.filter(db.Q(device__venture=venture) |
            db.Q(device__venture__parent=venture) |
            db.Q(device__venture__parent__parent=venture) |
            db.Q(device__venture__parent__parent__parent=venture) |
            db.Q(device__venture__parent__parent__parent__parent=venture))
    elif not venture: # specifically "devices with no venture set"
        splunk_usage = splunk_usage.filter(device__venture=None)
    if splunk_usage.count():
        splunk_size = splunk_usage.aggregate(db.Sum('size'))['size__sum'] or 0
        splunk_count = splunk_usage.values('device').distinct().count()
        yesterday = datetime.date.today() - datetime.timedelta(days=1)
        splunk_count_now = SplunkUsage.objects.filter(
                day=yesterday).values('device').distinct().count()
        url = None
        try:
            splunk_model = ComponentModel.objects.get(family='splunkvolume')
        except ComponentModel.DoesNotExist:
            pass
        else:
            if splunk_model.group_id:
                url = ('/ui/search/components/'
                       '?component_group=%d' % splunk_model.group_id)
        yield {
            'name': 'Splunk usage ({:,.0f} MB)'.format(
                                    splunk_size).replace(',', ' '),
            'cost': splunk_usage[0].get_price(size=splunk_size),
            'count': splunk_count,
            'count_now': splunk_count_now,
            'url': url,
        }
    for extra_id, in query.values_list('extra_id').distinct():
        if extra_id is None:
            continue
        extra = VentureExtraCost.objects.get(id=extra_id)
        q = query.filter(extra=extra)
        cost = get_total_cost(q, start, end)
        count, count_now, devices = get_total_count(q, start, end)
        yield {
            'name': extra.name + ' (from %s)' % extra.venture.name,
            'count': 'expires %s' % extra.expire.strftime(
                '%Y-%m-%d') if extra.expire else '',
            'cost': cost,
            'count_now': count_now,
        }
    if overlap:
        yield _total_dict('Total', query, start, end,
                _get_search_url(venture, type=()))
Пример #18
0
def _get_summaries(query, start, end, overlap=True, venture=None):
    if overlap:
        yield _total_dict(
            'Servers',
            query.filter(
                device__model__type__in=(DeviceType.rack_server.id,
                                         DeviceType.blade_server.id,
                                         DeviceType.virtual_server.id)), start,
            end, _get_search_url(venture, type=(201, 202, 203)))
    for dc in DataCenter.objects.all():
        yield _total_dict(
            '  • Servers in %s' % dc.name,
            query.filter(
                device__model__type__in=(DeviceType.rack_server.id,
                                         DeviceType.blade_server.id,
                                         DeviceType.virtual_server.id)).filter(
                                             device__dc__iexact=dc.name),
            start, end, _get_search_url(venture, dc=dc, type=(201, 202, 203)))
        if overlap:
            yield _total_dict(
                '    ∙ Rack servers in %s' % dc.name,
                query.filter(
                    device__model__type=DeviceType.rack_server.id, ).filter(
                        device__dc__iexact=dc.name), start, end,
                _get_search_url(venture, dc=dc, type=(201, )))
            for mg in DeviceModelGroup.objects.filter(
                    type=DeviceType.rack_server.id).order_by('name'):
                yield _total_dict(
                    '        %s in %s' % (mg, dc.name),
                    query.filter(device__model__group=mg, ).filter(
                        device__dc__iexact=dc.name), start, end,
                    _get_search_url(venture,
                                    dc=dc,
                                    type=(201, ),
                                    model_group=mg.id))
            yield _total_dict(
                '    ∙ Blade servers in %s' % dc.name,
                query.filter(
                    device__model__type=DeviceType.blade_server.id, ).filter(
                        device__dc__iexact=dc.name), start, end,
                _get_search_url(venture, dc=dc, type=(202, )))
            for mg in DeviceModelGroup.objects.filter(
                    type=DeviceType.blade_server.id).order_by('name'):
                yield _total_dict(
                    '        %s in %s' % (mg, dc.name),
                    query.filter(device__model__group=mg, ).filter(
                        device__dc__iexact=dc.name), start, end,
                    _get_search_url(venture,
                                    dc=dc,
                                    type=(202, ),
                                    model_group=mg.id))
            yield _total_dict(
                '    ∙ Virtual servers in %s' % dc.name,
                query.filter(
                    device__model__type=DeviceType.virtual_server.id, ).filter(
                        device__dc__iexact=dc.name), start, end,
                _get_search_url(venture, dc=dc, type=(203, )))
    if overlap:
        yield _total_dict(
            'Loadbalancers',
            query.filter(
                device__model__type__in=(DeviceType.load_balancer.id, )),
            start, end, _get_search_url(venture, type=(103, )))
    for dc in DataCenter.objects.all():
        yield _total_dict(
            ' • Loadbalancers in %s' % dc.name,
            query.filter(device__model__type__in=(
                DeviceType.load_balancer.id, )).filter(
                    device__dc__iexact=dc.name), start, end,
            _get_search_url(venture, dc=dc, type=(103, )))
    if overlap:
        yield _total_dict(
            'Storage',
            query.filter(device__model__type__in=(
                DeviceType.storage.id,
                DeviceType.fibre_channel_switch.id,
            )), start, end, _get_search_url(venture, type=(301, )))
    for dc in DataCenter.objects.all():
        yield _total_dict(
            ' • Storage in %s' % dc.name,
            query.filter(device__model__type__in=(
                DeviceType.storage.id,
                DeviceType.fibre_channel_switch.id,
            )).filter(device__dc__iexact=dc.name), start, end,
            _get_search_url(venture, dc=dc, type=(301, )))
    if overlap:
        yield _total_dict(
            'Network',
            query.filter(device__model__type__in=(
                DeviceType.switch.id,
                DeviceType.router.id,
                DeviceType.firewall.id,
                DeviceType.smtp_gateway.id,
                DeviceType.appliance.id,
            )), start, end,
            _get_search_url(venture,
                            type=(
                                DeviceType.switch.id,
                                DeviceType.router.id,
                                DeviceType.firewall.id,
                                DeviceType.smtp_gateway.id,
                                DeviceType.appliance.id,
                            )))
    for dc in DataCenter.objects.all():
        yield _total_dict(
            ' • Network in %s' % dc.name,
            query.filter(device__model__type__in=(
                DeviceType.switch.id,
                DeviceType.router.id,
                DeviceType.firewall.id,
                DeviceType.smtp_gateway.id,
                DeviceType.appliance.id,
            )).filter(device__dc__iexact=dc.name), start, end,
            _get_search_url(venture,
                            dc=dc,
                            type=(
                                DeviceType.switch.id,
                                DeviceType.router.id,
                                DeviceType.firewall.id,
                                DeviceType.smtp_gateway.id,
                                DeviceType.appliance.id,
                            )))
    yield _total_dict(
        'Cloud',
        query.filter(device__model__type__in=(DeviceType.cloud_server.id, )),
        start, end,
        _get_search_url(venture, type=(DeviceType.cloud_server.id, )))
    if overlap:
        yield _total_dict(
            'Unknown',
            query.filter(device__model__type__in=(DeviceType.unknown.id, )),
            start, end, _get_search_url(venture,
                                        type=(DeviceType.unknown.id, )))
    for dc in DataCenter.objects.all():
        yield _total_dict(
            ' • Unknown in %s' % dc.name,
            query.filter(
                device__model__type__in=(DeviceType.unknown.id, )).filter(
                    device__dc__iexact=dc.name), start, end,
            _get_search_url(venture, dc=dc, type=(DeviceType.unknown.id, )))
    (
        splunk_cost,
        splunk_count,
        splunk_count_now,
        splunk_size,
    ) = SplunkUsage.get_cost(venture, start, end)
    if splunk_cost:
        url = None
        try:
            splunk_model = ComponentModel.objects.get(family='splunkvolume')
        except ComponentModel.DoesNotExist:
            pass
        else:
            if splunk_model.group_id:
                url = ('/ui/search/components/'
                       '?component_group=%d' % splunk_model.group_id)
        yield {
            'name':
            'Splunk usage ({:,.0f} MB)'.format(splunk_size).replace(',', ' '),
            'cost':
            splunk_cost,
            'count':
            splunk_count,
            'count_now':
            splunk_count_now,
            'url':
            url,
        }
    for extra_id, in query.values_list('extra_id').distinct():
        if extra_id is None:
            continue
        extra = VentureExtraCost.objects.get(id=extra_id)
        q = query.filter(extra=extra)
        cost = get_total_cost(q, start, end)
        count, count_now, devices = get_total_count(q, start, end)
        if count:
            yield {
                'name':
                extra.name + ' (from %s)' % extra.venture.name,
                'count':
                'expires %s' %
                extra.expire.strftime('%Y-%m-%d') if extra.expire else '',
                'cost':
                cost,
                'count_now':
                count_now,
            }
    if overlap:
        yield _total_dict(
            'Total',
            query,
            start,
            end,
            _get_search_url(venture, type=()),
        )
        yield _total_dict(
            'Total physical',
            query.exclude(device__model__type__in=(
                DeviceType.cloud_server,
                DeviceType.virtual_server,
                DeviceType.unknown,
                DeviceType.data_center,
                DeviceType.rack,
                DeviceType.management,
            ), ).exclude(device=None, ),
            start,
            end,
            _get_search_url(venture, type=()),
        )
Пример #19
0
def _total_dict(name, query, start, end, url=None):
    cost = get_total_cost(query, start, end)
    count, count_now, devices = get_total_count(query, start, end)
    if not count and not count_now:
        return None
    return {"name": name, "count": count, "cost": cost, "count_now": count_now, "url": url}
Пример #20
0
def _get_summaries(query, start, end, overlap=True, venture=None):
    if overlap:
        yield _total_dict(
            "Servers",
            query.filter(
                device__model__type__in=(
                    DeviceType.rack_server.id,
                    DeviceType.blade_server.id,
                    DeviceType.virtual_server.id,
                )
            ),
            start,
            end,
            _get_search_url(venture, type=(201, 202, 203)),
        )
    for dc in DataCenter.objects.all():
        yield _total_dict(
            "  • Servers in %s" % dc.name,
            query.filter(
                device__model__type__in=(
                    DeviceType.rack_server.id,
                    DeviceType.blade_server.id,
                    DeviceType.virtual_server.id,
                )
            ).filter(device__dc__iexact=dc.name),
            start,
            end,
            _get_search_url(venture, dc=dc, type=(201, 202, 203)),
        )
        if overlap:
            yield _total_dict(
                "    ∙ Rack servers in %s" % dc.name,
                query.filter(device__model__type=DeviceType.rack_server.id).filter(device__dc__iexact=dc.name),
                start,
                end,
                _get_search_url(venture, dc=dc, type=(201,)),
            )
            for mg in DeviceModelGroup.objects.filter(type=DeviceType.rack_server.id).order_by("name"):
                yield _total_dict(
                    "        %s in %s" % (mg, dc.name),
                    query.filter(device__model__group=mg).filter(device__dc__iexact=dc.name),
                    start,
                    end,
                    _get_search_url(venture, dc=dc, type=(201,), model_group=mg.id),
                )
            yield _total_dict(
                "    ∙ Blade servers in %s" % dc.name,
                query.filter(device__model__type=DeviceType.blade_server.id).filter(device__dc__iexact=dc.name),
                start,
                end,
                _get_search_url(venture, dc=dc, type=(202,)),
            )
            for mg in DeviceModelGroup.objects.filter(type=DeviceType.blade_server.id).order_by("name"):
                yield _total_dict(
                    "        %s in %s" % (mg, dc.name),
                    query.filter(device__model__group=mg).filter(device__dc__iexact=dc.name),
                    start,
                    end,
                    _get_search_url(venture, dc=dc, type=(202,), model_group=mg.id),
                )
            yield _total_dict(
                "    ∙ Virtual servers in %s" % dc.name,
                query.filter(device__model__type=DeviceType.virtual_server.id).filter(device__dc__iexact=dc.name),
                start,
                end,
                _get_search_url(venture, dc=dc, type=(203,)),
            )
    if overlap:
        yield _total_dict(
            "Loadbalancers",
            query.filter(device__model__type__in=(DeviceType.load_balancer.id,)),
            start,
            end,
            _get_search_url(venture, type=(103,)),
        )
    for dc in DataCenter.objects.all():
        yield _total_dict(
            " • Loadbalancers in %s" % dc.name,
            query.filter(device__model__type__in=(DeviceType.load_balancer.id,)).filter(device__dc__iexact=dc.name),
            start,
            end,
            _get_search_url(venture, dc=dc, type=(103,)),
        )
    if overlap:
        yield _total_dict(
            "Storage",
            query.filter(device__model__type__in=(DeviceType.storage.id, DeviceType.fibre_channel_switch.id)),
            start,
            end,
            _get_search_url(venture, type=(301,)),
        )
    for dc in DataCenter.objects.all():
        yield _total_dict(
            " • Storage in %s" % dc.name,
            query.filter(device__model__type__in=(DeviceType.storage.id, DeviceType.fibre_channel_switch.id)).filter(
                device__dc__iexact=dc.name
            ),
            start,
            end,
            _get_search_url(venture, dc=dc, type=(301,)),
        )
    if overlap:
        yield _total_dict(
            "Network",
            query.filter(
                device__model__type__in=(
                    DeviceType.switch.id,
                    DeviceType.router.id,
                    DeviceType.firewall.id,
                    DeviceType.smtp_gateway.id,
                    DeviceType.appliance.id,
                )
            ),
            start,
            end,
            _get_search_url(
                venture,
                type=(
                    DeviceType.switch.id,
                    DeviceType.router.id,
                    DeviceType.firewall.id,
                    DeviceType.smtp_gateway.id,
                    DeviceType.appliance.id,
                ),
            ),
        )
    for dc in DataCenter.objects.all():
        yield _total_dict(
            " • Network in %s" % dc.name,
            query.filter(
                device__model__type__in=(
                    DeviceType.switch.id,
                    DeviceType.router.id,
                    DeviceType.firewall.id,
                    DeviceType.smtp_gateway.id,
                    DeviceType.appliance.id,
                )
            ).filter(device__dc__iexact=dc.name),
            start,
            end,
            _get_search_url(
                venture,
                dc=dc,
                type=(
                    DeviceType.switch.id,
                    DeviceType.router.id,
                    DeviceType.firewall.id,
                    DeviceType.smtp_gateway.id,
                    DeviceType.appliance.id,
                ),
            ),
        )
    yield _total_dict(
        "Cloud",
        query.filter(device__model__type__in=(DeviceType.cloud_server.id,)),
        start,
        end,
        _get_search_url(venture, type=(DeviceType.cloud_server.id,)),
    )
    if overlap:
        yield _total_dict(
            "Unknown",
            query.filter(device__model__type__in=(DeviceType.unknown.id,)),
            start,
            end,
            _get_search_url(venture, type=(DeviceType.unknown.id,)),
        )
    for dc in DataCenter.objects.all():
        yield _total_dict(
            " • Unknown in %s" % dc.name,
            query.filter(device__model__type__in=(DeviceType.unknown.id,)).filter(device__dc__iexact=dc.name),
            start,
            end,
            _get_search_url(venture, dc=dc, type=(DeviceType.unknown.id,)),
        )
    splunk_usage = SplunkUsage.objects.filter(day__gte=start, day__lte=end)
    if venture and venture != "*":
        splunk_usage = splunk_usage.filter(
            db.Q(device__venture=venture)
            | db.Q(device__venture__parent=venture)
            | db.Q(device__venture__parent__parent=venture)
            | db.Q(device__venture__parent__parent__parent=venture)
            | db.Q(device__venture__parent__parent__parent__parent=venture)
        )
    elif not venture:  # specifically "devices with no venture set"
        splunk_usage = splunk_usage.filter(device__venture=None)
    if splunk_usage.count():
        splunk_size = splunk_usage.aggregate(db.Sum("size"))["size__sum"] or 0
        splunk_count = splunk_usage.values("device").distinct().count()
        yesterday = datetime.date.today() - datetime.timedelta(days=1)
        splunk_count_now = SplunkUsage.objects.filter(day=yesterday).values("device").distinct().count()
        url = None
        try:
            splunk_model = ComponentModel.objects.get(family="splunkvolume")
        except ComponentModel.DoesNotExist:
            pass
        else:
            if splunk_model.group_id:
                url = "/ui/search/components/" "?component_group=%d" % splunk_model.group_id
        yield {
            "name": "Splunk usage ({:,.0f} MB)".format(splunk_size).replace(",", " "),
            "cost": splunk_usage[0].get_price(size=splunk_size),
            "count": splunk_count,
            "count_now": splunk_count_now,
            "url": url,
        }
    for (extra_id,) in query.values_list("extra_id").distinct():
        if extra_id is None:
            continue
        extra = VentureExtraCost.objects.get(id=extra_id)
        q = query.filter(extra=extra)
        cost = get_total_cost(q, start, end)
        count, count_now, devices = get_total_count(q, start, end)
        yield {
            "name": extra.name + " (from %s)" % extra.venture.name,
            "count": "expires %s" % extra.expire.strftime("%Y-%m-%d") if extra.expire else "",
            "cost": cost,
            "count_now": count_now,
        }
    if overlap:
        yield _total_dict("Total", query, start, end, _get_search_url(venture, type=()))
        yield _total_dict(
            "Total physical",
            query.exclude(
                device__model__type__in=(
                    DeviceType.cloud_server,
                    DeviceType.virtual_server,
                    DeviceType.unknown,
                    DeviceType.data_center,
                    DeviceType.rack,
                    DeviceType.management,
                )
            ),
            start,
            end,
            _get_search_url(venture, type=()),
        )