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()} ) self.extra_types = list( VentureExtraCostType.objects.annotate(cost_count=db.Count("ventureextracost")) .filter(cost_count__gt=0) .order_by("name") ) 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"] self.venture_data = self._get_venture_data(start, end, self.ventures, self.extra_types) else: self.ventures = Venture.objects.none() self.venture_data = [] if self.request.GET.get("export") == "csv": return self.export_csv() return super(ReportVentures, self).get(*args, **kwargs)
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)
def get(self, *args, **kwargs): if "start" in self.request.GET: self.form = DateRangeForm(self.request.GET) if not self.form.is_valid(): messages.error(self.request, "Invalid date range") else: initial = {"start": datetime.date.today() - datetime.timedelta(days=30), "end": datetime.date.today()} self.form = DateRangeForm(initial) self.form.is_valid() self.set_venture() has_perm = self.request.user.get_profile().has_perm if not has_perm(Perm.list_devices_financial, self.venture if self.venture and self.venture != "*" else None): return HttpResponseForbidden("You don't have permission to see this.") return super(VenturesVenture, self).get(*args, **kwargs)
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)
def get(self, *args, **kwargs): if 'start' in self.request.GET: self.form = DateRangeForm(self.request.GET) if not self.form.is_valid(): messages.error(self.request, "Invalid date range") else: initial = { 'start': datetime.date.today() - datetime.timedelta(days=30), 'end': datetime.date.today(), } self.form = DateRangeForm(initial) self.form.is_valid() self.set_venture() has_perm = self.request.user.get_profile().has_perm if not has_perm(Perm.list_devices_financial, self.venture if self.venture and self.venture != '*' else None): return HttpResponseForbidden( "You don't have permission to see this.") return super(VenturesVenture, self).get(*args, **kwargs)
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)
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)
class VenturesVenture(SidebarVentures, Base): template_name = 'ui/ventures-venture.html' def get(self, *args, **kwargs): if 'start' in self.request.GET: self.form = DateRangeForm(self.request.GET) if not self.form.is_valid(): messages.error(self.request, "Invalid date range") else: initial = { 'start': datetime.date.today() - datetime.timedelta(days=30), 'end': datetime.date.today(), } self.form = DateRangeForm(initial) self.form.is_valid() self.set_venture() has_perm = self.request.user.get_profile().has_perm if not has_perm(Perm.list_devices_financial, self.venture if self.venture and self.venture != '*' else None): return HttpResponseForbidden( "You don't have permission to see this.") return super(VenturesVenture, self).get(*args, **kwargs) 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
def get(self, *args, **kwargs): self.venture_data = [] self.task_in_progress = False profile = self.request.user.get_profile() 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(), }) self.extra_types = list( VentureExtraCostType.objects.annotate( cost_count=db.Count('ventureextracost')).filter( cost_count__gt=0).order_by('name')) 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).values_list( 'id', flat=True, ).order_by('path') start = self.form.cleaned_data['start'] end = self.form.cleaned_data['end'] if self.request.GET.get('invalidate-cache') == 'true': self.invalidate_data( start, end, self.ventures, self.extra_types, ) return HttpResponseRedirect( _get_after_report_invalidation_link( self.request, 'invalidate-cache', ), ) self.venture_data = self.get_data( start, end, self.ventures, self.extra_types, ) if self.venture_data is None: self.task_in_progress = True messages.info( self.request, "Report processing in progress. Please wait...", ) else: self.ventures = Venture.objects.none() self.venture_data = [] if (self.request.GET.get('export') == 'csv' and self.venture_data is not None): return make_csv_response( data=self.export_csv(self.venture_data, self.extra_types), filename='ReportVentures.csv', ) return super(ReportVentures, self).get(*args, **kwargs)
class ReportVentures(SidebarReports, AsyncReportMixin, Base): template_name = 'ui/report_ventures.html' subsection = 'ventures' perms = [ { 'perm': Perm.read_device_info_reports, 'msg': _("You don't have permission to see reports."), }, ] data_provider = _report_ventures_data_provider def export_csv(self, venture_data, extra_types): yield [ 'Venture ID', 'Venture', 'Path', 'Department', 'Default margin', 'Device count', 'Core count', 'Virtual core count', 'Cloud use', 'Cloud cost', ] + [extra_type.name for extra_type in extra_types] + [ 'Splunk cost', 'Hardware cost', 'Total cost', ] for data in venture_data: yield [ '%d' % data['id'], data['name'], data['path'], data['department'], '%d%%' % data['margin'], '%d' % (data['count'] or 0), '%d' % (data['core_count'] or 0), '%d' % (data['virtual_core_count'] or 0), '%f' % (data['cloud_use'] or 0), _currency(data['cloud_cost']), ] + [_currency(v) for v in data['extras']] + [ _currency(data['splunk_cost']), _currency(data['hardware_cost']), _currency(data['total']), ] @ralph_permission(perms) def get(self, *args, **kwargs): self.venture_data = [] self.task_in_progress = False profile = self.request.user.get_profile() 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(), }) self.extra_types = list( VentureExtraCostType.objects.annotate( cost_count=db.Count('ventureextracost')).filter( cost_count__gt=0).order_by('name')) 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).values_list( 'id', flat=True, ).order_by('path') start = self.form.cleaned_data['start'] end = self.form.cleaned_data['end'] if self.request.GET.get('invalidate-cache') == 'true': self.invalidate_data( start, end, self.ventures, self.extra_types, ) return HttpResponseRedirect( _get_after_report_invalidation_link( self.request, 'invalidate-cache', ), ) self.venture_data = self.get_data( start, end, self.ventures, self.extra_types, ) if self.venture_data is None: self.task_in_progress = True messages.info( self.request, "Report processing in progress. Please wait...", ) else: self.ventures = Venture.objects.none() self.venture_data = [] if (self.request.GET.get('export') == 'csv' and self.venture_data is not None): return make_csv_response( data=self.export_csv(self.venture_data, self.extra_types), filename='ReportVentures.csv', ) return super(ReportVentures, self).get(*args, **kwargs) def get_context_data(self, **kwargs): context = super(ReportVentures, self).get_context_data(**kwargs) context.update({ 'form': self.form, 'ventures': self.ventures, 'venture_data': self.venture_data, 'profile': self.request.user.get_profile(), 'extra_types': self.extra_types, 'task_in_progress': self.task_in_progress, }) return context
class ReportVentures(SidebarReports, Base): template_name = "ui/report_ventures.html" subsection = "ventures" def export_csv(self): def iter_rows(): yield [ "Venture ID", "Venture", "Path", "Department", "Default margin", "Device count", "Core count", "Virtual core count", "Cloud use", "Cloud cost", ] + [extra_type.name for extra_type in self.extra_types] + ["Hardware cost", "Total cost"] for data in self.venture_data: yield [ "%d" % data["id"], data["name"], data["path"], data["department"], "%d%%" % data["margin"], "%d" % (data["count"] or 0), "%d" % (data["core_count"] or 0), "%d" % (data["virtual_core_count"] or 0), "%f" % (data["cloud_use"] or 0), _currency(data["cloud_cost"]), ] + [_currency(v) for v in data["extras"]] + [ _currency(data["hardware_cost"]), _currency(data["total"]), ] f = StringIO.StringIO() csvutil.UnicodeWriter(f).writerows(iter_rows()) response = HttpResponse(f.getvalue(), content_type="application/csv") response["Content-Disposition"] = "attachment; filename=ventures.csv" return response 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, } 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 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()} ) self.extra_types = list( VentureExtraCostType.objects.annotate(cost_count=db.Count("ventureextracost")) .filter(cost_count__gt=0) .order_by("name") ) 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"] self.venture_data = self._get_venture_data(start, end, self.ventures, self.extra_types) else: self.ventures = Venture.objects.none() self.venture_data = [] if self.request.GET.get("export") == "csv": return self.export_csv() return super(ReportVentures, self).get(*args, **kwargs) def get_context_data(self, **kwargs): context = super(ReportVentures, self).get_context_data(**kwargs) context.update( { "form": self.form, "ventures": self.ventures, "venture_data": self.venture_data, "profile": self.request.user.get_profile(), "extra_types": self.extra_types, } ) return context
class ReportVentures(SidebarReports, Base): template_name = 'ui/report_ventures.html' subsection = 'ventures' def export_csv(self): def iter_rows(): yield [ 'Venture', 'Path', 'Department', 'Default margin', 'Device count', 'Total cost' ] for venture in self.ventures: total = venture.total or 0 yield [ venture.name, venture.path, unicode(venture.department) if venture.department else '', ('%d%%' % venture.margin_kind.margin ) if venture.margin_kind else '', venture.count or 0, '{:,.2f} {}'.format(total, settings.CURRENCY).replace(',', ' '), ] f = StringIO.StringIO() csvutil.UnicodeWriter(f).writerows(iter_rows()) response = HttpResponse(f.getvalue(), content_type='application/csv') response['Content-Disposition'] = 'attachment; filename=ventures.csv' return response 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) def get_context_data(self, **kwargs): context = super(ReportVentures, self).get_context_data(**kwargs) context.update({ 'form': self.form, 'ventures': self.ventures, 'profile': self.request.user.get_profile(), }) return context
class ReportVentures(SidebarReports, Base): template_name = 'ui/report_ventures.html' subsection = 'ventures' def export_csv(self): def iter_rows(): yield [ 'Venture ID', 'Venture', 'Path', 'Department', 'Default margin', 'Device count', 'Core count', 'Virtual core count', 'Cloud use', 'Total cost' ] for venture in self.ventures: total = venture.total or 0 yield [ '%d' % venture.id, venture.name, venture.path, unicode(venture.department) if venture.department else '', ('%d%%' % venture.margin_kind.margin) if venture.margin_kind else '', '%d' % (venture.count or 0), '%d' % (venture.core_count or 0), '%d' % (venture.virtual_core_count or 0), '%f' % (venture.cloud_use or 0), '{:,.2f} {}'.format(total, settings.CURRENCY).replace(',', ' '), ] f = StringIO.StringIO() csvutil.UnicodeWriter(f).writerows(iter_rows()) response = HttpResponse(f.getvalue(), content_type='application/csv') response['Content-Disposition'] = 'attachment; filename=ventures.csv' return response 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) def get_context_data(self, **kwargs): context = super(ReportVentures, self).get_context_data(**kwargs) context.update({ 'form': self.form, 'ventures': self.ventures, 'profile': self.request.user.get_profile(), }) return context
class VenturesVenture(SidebarVentures, Base): template_name = "ui/ventures-venture.html" def get(self, *args, **kwargs): if "start" in self.request.GET: self.form = DateRangeForm(self.request.GET) if not self.form.is_valid(): messages.error(self.request, "Invalid date range") else: initial = {"start": datetime.date.today() - datetime.timedelta(days=30), "end": datetime.date.today()} self.form = DateRangeForm(initial) self.form.is_valid() self.set_venture() has_perm = self.request.user.get_profile().has_perm if not has_perm(Perm.list_devices_financial, self.venture if self.venture and self.venture != "*" else None): return HttpResponseForbidden("You don't have permission to see this.") return super(VenturesVenture, self).get(*args, **kwargs) 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
def get(self, *args, **kwargs): self.venture_data = [] self.task_in_progress = False profile = self.request.user.get_profile() 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(), }) self.extra_types = list(VentureExtraCostType.objects.annotate( cost_count=db.Count('ventureextracost') ).filter(cost_count__gt=0).order_by('name')) 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 ).values_list( 'id', flat=True, ).order_by('path') start = self.form.cleaned_data['start'] end = self.form.cleaned_data['end'] if self.request.GET.get('invalidate-cache') == 'true': self.invalidate_data( start, end, self.ventures, self.extra_types, ) return HttpResponseRedirect( _get_after_report_invalidation_link( self.request, 'invalidate-cache', ), ) self.venture_data = self.get_data( start, end, self.ventures, self.extra_types, ) if self.venture_data is None: self.task_in_progress = True messages.info( self.request, "Report processing in progress. Please wait...", ) else: self.ventures = Venture.objects.none() self.venture_data = [] if (self.request.GET.get('export') == 'csv' and self.venture_data is not None): return make_csv_response( data=self.export_csv(self.venture_data, self.extra_types), filename='ReportVentures.csv', ) return super(ReportVentures, self).get(*args, **kwargs)
class ReportVentures(SidebarReports, AsyncReportMixin, Base): template_name = 'ui/report_ventures.html' subsection = 'ventures' perms = [ { 'perm': Perm.read_device_info_reports, 'msg': _("You don't have permission to see reports."), }, ] data_provider = _report_ventures_data_provider def export_csv(self, venture_data, extra_types): yield [ 'Venture ID', 'Venture', 'Path', 'Department', 'Default margin', 'Device count', 'Core count', 'Virtual core count', 'Cloud use', 'Cloud cost', ] + [extra_type.name for extra_type in extra_types] + [ 'Splunk cost', 'Hardware cost', 'Total cost', ] for data in venture_data: yield [ '%d' % data['id'], data['name'], data['path'], data['department'], '%d%%' % data['margin'], '%d' % (data['count'] or 0), '%d' % (data['core_count'] or 0), '%d' % (data['virtual_core_count'] or 0), '%f' % (data['cloud_use'] or 0), _currency(data['cloud_cost']), ] + [_currency(v) for v in data['extras']] + [ _currency(data['splunk_cost']), _currency(data['hardware_cost']), _currency(data['total']), ] @ralph_permission(perms) def get(self, *args, **kwargs): self.venture_data = [] self.task_in_progress = False profile = self.request.user.get_profile() 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(), }) self.extra_types = list(VentureExtraCostType.objects.annotate( cost_count=db.Count('ventureextracost') ).filter(cost_count__gt=0).order_by('name')) 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 ).values_list( 'id', flat=True, ).order_by('path') start = self.form.cleaned_data['start'] end = self.form.cleaned_data['end'] if self.request.GET.get('invalidate-cache') == 'true': self.invalidate_data( start, end, self.ventures, self.extra_types, ) return HttpResponseRedirect( _get_after_report_invalidation_link( self.request, 'invalidate-cache', ), ) self.venture_data = self.get_data( start, end, self.ventures, self.extra_types, ) if self.venture_data is None: self.task_in_progress = True messages.info( self.request, "Report processing in progress. Please wait...", ) else: self.ventures = Venture.objects.none() self.venture_data = [] if (self.request.GET.get('export') == 'csv' and self.venture_data is not None): return make_csv_response( data=self.export_csv(self.venture_data, self.extra_types), filename='ReportVentures.csv', ) return super(ReportVentures, self).get(*args, **kwargs) def get_context_data(self, **kwargs): context = super(ReportVentures, self).get_context_data(**kwargs) context.update({ 'form': self.form, 'ventures': self.ventures, 'venture_data': self.venture_data, 'profile': self.request.user.get_profile(), 'extra_types': self.extra_types, 'task_in_progress': self.task_in_progress, }) return context