Revert usage tracking

This commit is contained in:
Tanner Collin 2021-04-25 20:30:33 +00:00
parent 0c184e9b1d
commit 884cdafacb
3 changed files with 45 additions and 43 deletions

View File

@ -155,15 +155,15 @@ class StatsSpaceActivity(models.Model):
date = models.DateField(default=today_alberta_tz)
card_scans = models.IntegerField()
class UsageTrack(models.Model):
user = models.ForeignKey(User, related_name='usages', blank=True, null=True, on_delete=models.SET_NULL)
# member_id = models.IntegerField(blank=True, null=True) # restrict to current users for now
username = models.CharField(max_length=64) # allows us to match non-Spaceport users later
devicename = models.CharField(max_length=64)
start_time = models.DateTimeField(auto_now_add=True)
num_seconds = models.IntegerField()
#class UsageTrack(models.Model):
# user = models.ForeignKey(User, related_name='usages', blank=True, null=True, on_delete=models.SET_NULL)
#
# # member_id = models.IntegerField(blank=True, null=True) # restrict to current users for now
# username = models.CharField(max_length=64) # allows us to match non-Spaceport users later
#
# devicename = models.CharField(max_length=64)
# start_time = models.DateTimeField(auto_now_add=True)
# num_seconds = models.IntegerField()
class HistoryIndex(models.Model):
content_type = models.ForeignKey(ContentType, null=True, on_delete=models.SET_NULL)

View File

@ -14,10 +14,10 @@ import re
from . import models, fields, utils, utils_ldap, utils_auth
from .. import settings, secrets
class UsageSerializer(serializers.ModelSerializer):
class Meta:
model = models.UsageTrack
fields = '__all__'
#class UsageSerializer(serializers.ModelSerializer):
# class Meta:
# model = models.UsageTrack
# fields = '__all__'
class TransactionSerializer(serializers.ModelSerializer):
# fields directly from old portal. replace with slugs we want
@ -267,7 +267,7 @@ class AdminSearchSerializer(serializers.Serializer):
member = serializers.SerializerMethodField()
training = serializers.SerializerMethodField()
transactions = serializers.SerializerMethodField()
usages = serializers.SerializerMethodField()
#usages = serializers.SerializerMethodField()
def get_member(self, obj):
serializer = AdminMemberSerializer(obj)
@ -302,14 +302,14 @@ class AdminSearchSerializer(serializers.Serializer):
serializer.is_valid()
return serializer.data
def get_usages(self, obj):
if obj.user:
queryset = obj.user.usages.order_by('-start_time')
else:
queryset = []
serializer = UsageSerializer(data=queryset, many=True)
serializer.is_valid()
return serializer.data
#def get_usages(self, obj):
# if obj.user:
# queryset = obj.user.usages.order_by('-start_time')
# else:
# queryset = []
# serializer = UsageSerializer(data=queryset, many=True)
# serializer.is_valid()
# return serializer.data
class CardSerializer(serializers.ModelSerializer):
@ -460,7 +460,7 @@ class UserSerializer(serializers.ModelSerializer):
'is_staff',
'door_code',
'wifi_pass',
'usages',
#'usages',
]
depth = 1

View File

@ -520,27 +520,29 @@ class StatsViewSet(viewsets.ViewSet, List):
track[devicename] = dict(time=time.time(), username=first_name)
cache.set('track', track)
# update device usage
last_session = models.UsageTrack.objects.filter(devicename=devicename).last()
if not last_session or last_session.username != username:
try:
user = User.objects.get(username__iexact=username)
except User.DoesNotExist:
msg = 'Device tracker problem finding username: ' + username
utils.alert_tanner(msg)
logger.error(msg)
user = None
## update device usage
## issue: sometimes two sessions are created
## issue: sometimes two /track/ requests are sent and double time is counted
#last_session = models.UsageTrack.objects.filter(devicename=devicename).last()
#if not last_session or last_session.username != username:
# try:
# user = User.objects.get(username__iexact=username)
# except User.DoesNotExist:
# msg = 'Device tracker problem finding username: ' + username
# utils.alert_tanner(msg)
# logger.error(msg)
# user = None
models.UsageTrack.objects.create(
user=user,
username=username,
devicename=devicename,
num_seconds=0,
)
logging.info('New ' + devicename + ' session created for: ' + username)
else:
last_session.num_seconds = F('num_seconds') + 10
last_session.save(update_fields=['num_seconds'])
# models.UsageTrack.objects.create(
# user=user,
# username=username,
# devicename=devicename,
# num_seconds=0,
# )
# logging.info('New ' + devicename + ' session created for: ' + username)
#else:
# last_session.num_seconds = F('num_seconds') + 10
# last_session.save(update_fields=['num_seconds'])
return Response(200)