def get_total_count(query, start, end): devices = HistoryCost.filter_span(start, end, query).values_list('device') count = devices.distinct().count() today = datetime.date.today() count_now = query.filter(end__gte=today).values_list( 'device').distinct().count() return count, count_now, devices
def _total_cost_count(query, start, end): total = query.aggregate( SpanSum( 'daily_cost', start=start.strftime('%Y-%m-%d'), end=end.strftime('%Y-%m-%d'), ), #Count('device'), ) count = HistoryCost.filter_span(start, end, query).values_list( 'device').distinct().count() return total['spansum'], count
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 = query.exclude(device__deleted=True) 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 = get_total_cost(query, date, date + one_day) total_count, now_count, devices = get_total_count( query, 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 total_cost_count(query, start, end): total = query.aggregate( SpanSum( 'daily_cost', start=start.strftime('%Y-%m-%d'), end=end.strftime('%Y-%m-%d'), ), ) count = HistoryCost.filter_span(start, end, query).values_list( 'device').distinct().count() today = datetime.date.today() count_now = query.filter(end__gte=today).values_list( 'device').distinct().count() return total['spansum'], count, count_now
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_total_count(query, start, end): """ Count the devices in the given HistoryCost query in the specified time span. The devices that are not in the query for the whole time are counted as a fraction. Additionally, the function returns the count of devices at the end of the time span, and a query with all the devices from the query. """ days = (end - start).days or 1 devices = HistoryCost.filter_span(start, end, query).values_list('device') today = datetime.date.today() count_now = query.filter( end__gte=today).values_list('device').distinct().count() count = float( query.aggregate( SpanCount( start=start.strftime('%Y-%m-%d'), end=end.strftime('%Y-%m-%d'), ), )['spansum'] or 0) / days return count, count_now, devices
def get_total_count(query, start, end): """ Count the devices in the given HistoryCost query in the specified time span. The devices that are not in the query for the whole time are counted as a fraction. Additionally, the function returns the count of devices at the end of the time span, and a query with all the devices from the query. """ days = (end - start).days or 1 devices = HistoryCost.filter_span(start, end, query).values_list('device') today = datetime.date.today() count_now = query.filter( end__gte=today ).values_list( 'device' ).distinct().count() count = float(query.aggregate( SpanCount( start=start.strftime('%Y-%m-%d'), end=end.strftime('%Y-%m-%d'), ), )['spansum'] or 0) / days return count, count_now, devices