Merge branch 'usage_tracking'

This commit is contained in:
Tanner Collin 2022-02-04 01:52:36 +00:00
commit 6cd1b91dfd
2 changed files with 75 additions and 32 deletions

View File

@ -165,15 +165,19 @@ 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 Usage(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 username = models.CharField(max_length=64, blank=True) # incase of LDAP-Spaceport mismatch
# username = models.CharField(max_length=64) # allows us to match non-Spaceport users later
# device = 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) updated_at = models.DateTimeField(auto_now=True)
# num_seconds = models.IntegerField() num_seconds = models.IntegerField()
memo = models.TextField(blank=True)
should_bill = models.BooleanField(default=True)
history = HistoricalRecords(excluded_fields=['num_seconds', 'updated_at'])
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

@ -614,32 +614,71 @@ class StatsViewSet(viewsets.ViewSet, List):
username = request.data['username'] username = request.data['username']
first_name = username.split('.')[0].title() first_name = username.split('.')[0].title()
track[devicename] = dict(time=time.time(), username=first_name) track[devicename] = dict(
time=time.time(),
username=username,
first_name=first_name,
)
cache.set('track', track) cache.set('track', track)
## update device usage return Response(200)
## 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( @action(detail=False, methods=['post'])
# user=user, def usage(self, request):
# username=username, #if 'seconds' not in request.data:
# devicename=devicename, # raise exceptions.ValidationError(dict(seconds='This field is required.'))
# num_seconds=0,
# ) if 'device' not in request.data:
# logging.info('New ' + devicename + ' session created for: ' + username) raise exceptions.ValidationError(dict(device='This field is required.'))
#else:
# last_session.num_seconds = F('num_seconds') + 10 device = request.data['device']
# last_session.save(update_fields=['num_seconds']) data = request.data.get('data', None)
seconds = request.data.get('seconds', 20)
if 'username' in request.data:
username = request.data['username']
else:
track = cache.get('track', {})
try:
username = track[device]['username']
except KeyError:
msg = 'Usage tracker problem finding username for device: {}'.format(device)
#utils.alert_tanner(msg)
logger.error(msg)
username = ''
last_session = models.Usage.objects.filter(device=device).last()
if not last_session or last_session.username != username:
try:
user = User.objects.get(username__iexact=username)
except User.DoesNotExist:
msg = 'Usage trackerproblem finding user for username: {}'.format(username or '[no username]')
#utils.alert_tanner(msg)
logger.error(msg)
user = None
last_session = models.Usage.objects.create(
user=user,
username=username,
device=device,
num_seconds=0,
memo='',
)
logging.info('New %s session created for: %s', device, username or '[no username]')
logging.debug('Device %s data: %s', device, data)
if device == 'TROTECS300' and data and int(data) > 3:
should_count = True
else:
should_count = False
if should_count:
logging.debug('Counting %s seconds.', seconds)
last_session.num_seconds = F('num_seconds') + seconds
last_session.save(update_fields=['num_seconds'])
return Response(200) return Response(200)