Don't count Usages where should_bill=False

This commit is contained in:
Tanner Collin 2022-05-06 05:01:09 +00:00
parent 500c5a42e1
commit 9f91cca187

View File

@ -735,7 +735,7 @@ class StatsViewSet(viewsets.ViewSet, List):
if should_count: if should_count:
start_new_use = not last_use or last_use.finished_at or last_use.username != username start_new_use = not last_use or last_use.finished_at or last_use.username != username
if start_new_use: if start_new_use:
if username_isfrom_track and time.time() - track[device]['time'] > 300: if username_isfrom_track and time.time() - track[device]['time'] > 20*60:
msg = 'Usage tracker problem expired username {} for device: {}'.format(username, device) msg = 'Usage tracker problem expired username {} for device: {}'.format(username, device)
utils.alert_tanner(msg) utils.alert_tanner(msg)
logger.error(msg) logger.error(msg)
@ -775,7 +775,10 @@ class StatsViewSet(viewsets.ViewSet, List):
if 'device' not in request.query_params: if 'device' not in request.query_params:
raise exceptions.ValidationError(dict(device='This field is required.')) raise exceptions.ValidationError(dict(device='This field is required.'))
if not utils.is_request_from_protospace(request): if not (
is_admin_director(self.request.user) or
utils.is_request_from_protospace(request)
):
raise exceptions.PermissionDenied() raise exceptions.PermissionDenied()
device = request.query_params['device'] device = request.query_params['device']
@ -814,11 +817,15 @@ class StatsViewSet(viewsets.ViewSet, List):
month_start = today_start.replace(day=1) month_start = today_start.replace(day=1)
today_total = device_uses.filter( today_total = device_uses.filter(
user=user, started_at__gte=today_start, user=user, started_at__gte=today_start, should_bill=True,
).aggregate(Sum('num_seconds'))['num_seconds__sum'] or 0 + running_cut_time ).aggregate(Sum('num_seconds'))['num_seconds__sum'] or 0
month_total = device_uses.filter( month_total = device_uses.filter(
user=user, started_at__gte=month_start, user=user, started_at__gte=month_start, should_bill=True,
).aggregate(Sum('num_seconds'))['num_seconds__sum'] or 0 + running_cut_time ).aggregate(Sum('num_seconds'))['num_seconds__sum'] or 0
today_total += running_cut_time
month_total += running_cut_time
try: try:
track = cache.get('track', {})[device] track = cache.get('track', {})[device]
@ -979,9 +986,9 @@ class UsageViewSet(Base):
# TODO: add filtering by device # TODO: add filtering by device
@action(detail=False, methods=['get']) @action(detail=False, methods=['get'])
def csv(self, request): def csv(self, request):
usages = models.Usage.objects.order_by('id') usages = models.Usage.objects.order_by('id').filter(should_bill=True)
month = self.request.query_params.get('month', None)
month = self.request.query_params.get('month', None)
if month: if month:
try: try:
dt = datetime.datetime.strptime(month, '%Y-%m') dt = datetime.datetime.strptime(month, '%Y-%m')
@ -1005,7 +1012,8 @@ class UsageViewSet(Base):
writer.writeheader() writer.writeheader()
for u in usages.values(*fieldnames): for u in usages.values(*fieldnames):
u['started_at'] = u['started_at'].astimezone(utils.TIMEZONE_CALGARY) u['started_at'] = u['started_at'].astimezone(utils.TIMEZONE_CALGARY)
u['finished_at'] = u['finished_at'].astimezone(utils.TIMEZONE_CALGARY) if u['finished_at']:
u['finished_at'] = u['finished_at'].astimezone(utils.TIMEZONE_CALGARY)
writer.writerow(u) writer.writerow(u)
return response return response