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) date = models.DateField(default=today_alberta_tz)
card_scans = models.IntegerField() card_scans = models.IntegerField()
class UsageTrack(models.Model): #class UsageTrack(models.Model):
user = models.ForeignKey(User, related_name='usages', blank=True, null=True, on_delete=models.SET_NULL) # 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 # # 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 # username = models.CharField(max_length=64) # allows us to match non-Spaceport users later
#
devicename = models.CharField(max_length=64) # devicename = models.CharField(max_length=64)
start_time = models.DateTimeField(auto_now_add=True) # start_time = models.DateTimeField(auto_now_add=True)
num_seconds = models.IntegerField() # num_seconds = models.IntegerField()
class HistoryIndex(models.Model): class HistoryIndex(models.Model):
content_type = models.ForeignKey(ContentType, null=True, on_delete=models.SET_NULL) 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 models, fields, utils, utils_ldap, utils_auth
from .. import settings, secrets from .. import settings, secrets
class UsageSerializer(serializers.ModelSerializer): #class UsageSerializer(serializers.ModelSerializer):
class Meta: # class Meta:
model = models.UsageTrack # model = models.UsageTrack
fields = '__all__' # fields = '__all__'
class TransactionSerializer(serializers.ModelSerializer): class TransactionSerializer(serializers.ModelSerializer):
# fields directly from old portal. replace with slugs we want # fields directly from old portal. replace with slugs we want
@ -267,7 +267,7 @@ class AdminSearchSerializer(serializers.Serializer):
member = serializers.SerializerMethodField() member = serializers.SerializerMethodField()
training = serializers.SerializerMethodField() training = serializers.SerializerMethodField()
transactions = serializers.SerializerMethodField() transactions = serializers.SerializerMethodField()
usages = serializers.SerializerMethodField() #usages = serializers.SerializerMethodField()
def get_member(self, obj): def get_member(self, obj):
serializer = AdminMemberSerializer(obj) serializer = AdminMemberSerializer(obj)
@ -302,14 +302,14 @@ class AdminSearchSerializer(serializers.Serializer):
serializer.is_valid() serializer.is_valid()
return serializer.data return serializer.data
def get_usages(self, obj): #def get_usages(self, obj):
if obj.user: # if obj.user:
queryset = obj.user.usages.order_by('-start_time') # queryset = obj.user.usages.order_by('-start_time')
else: # else:
queryset = [] # queryset = []
serializer = UsageSerializer(data=queryset, many=True) # serializer = UsageSerializer(data=queryset, many=True)
serializer.is_valid() # serializer.is_valid()
return serializer.data # return serializer.data
class CardSerializer(serializers.ModelSerializer): class CardSerializer(serializers.ModelSerializer):
@ -460,7 +460,7 @@ class UserSerializer(serializers.ModelSerializer):
'is_staff', 'is_staff',
'door_code', 'door_code',
'wifi_pass', 'wifi_pass',
'usages', #'usages',
] ]
depth = 1 depth = 1

View File

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