Exemplo n.º 1
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
         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, mk.count, mk.count_now = total_cost_count(
                     q,
                     self.form.cleaned_data['start'],
                     self.form.cleaned_data['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
Exemplo n.º 2
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
         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, mk.count, mk.count_now = total_cost_count(
                 q,
                 self.form.cleaned_data['start'],
                 self.form.cleaned_data['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
Exemplo n.º 3
0
def _total_dict(name, query, start, end, url=None):
    cost, count, count_now = total_cost_count(query, start, end)
    if not count:
        return None
    return {
        'name': name,
        'count': count,
        'cost': cost,
        'count_now': count_now,
        'url': url,
    }
Exemplo n.º 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 = []
     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 = HistoryCost.filter_span(start, end, query)
         items = _get_summaries(query.all(), start, end, True, self.venture)
         cost_data = []
         count_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, total_count, now_count  = total_cost_count(
                     query.all(), date, date+one_day)
             cost_data.append([timestamp, total_cost])
             count_data.append([timestamp, total_count])
     ret.update({
         'items': items,
         'venture': self.venture,
         'cost_data': json.dumps(cost_data),
         'count_data': json.dumps(count_data),
         'form': self.form,
         'start_date': start,
         'end_date': end,
     })
     return ret
Exemplo n.º 5
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')
         for venture in self.ventures:
             (venture.total, venture.count,
              venture.count_now) = total_cost_count(
                      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)
                      ).distinct(),
                      self.form.cleaned_data['start'],
                      self.form.cleaned_data['end'],
                 )
     else:
         self.ventures = Venture.objects.none()
     if self.request.GET.get('export') == 'csv':
         return self.export_csv()
     return super(ReportVentures, self).get(*args, **kwargs)
Exemplo n.º 6
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')
         for venture in self.ventures:
             (venture.total, venture.count,
              venture.count_now) = total_cost_count(
                  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
                             )).distinct(),
                  self.form.cleaned_data['start'],
                  self.form.cleaned_data['end'],
              )
     else:
         self.ventures = Venture.objects.none()
     if self.request.GET.get('export') == 'csv':
         return self.export_csv()
     return super(ReportVentures, self).get(*args, **kwargs)
Exemplo n.º 7
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)
        cost, count, count_now = total_cost_count(
                query.filter(extra=extra), 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=()))