Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #308 from GreatFruitOmsk/issue_299
Browse files Browse the repository at this point in the history
User activity tracking
  • Loading branch information
vpetersson authored Jul 8, 2019
2 parents 629264a + 05080bb commit 3c6605e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions backend/backend/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def check_ip_range(ipr):
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'profile_page.middleware.UserActivityMiddleware'
]

ROOT_URLCONF = 'backend.urls'
Expand Down
10 changes: 6 additions & 4 deletions backend/device_registry/management/commands/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ def average_trust_score(devices):
return mean(scores) if scores else 0

now = timezone.now()
day_ago = now - timezone.timedelta(hours=24)
today = now.date()
month_ago_date = today - timezone.timedelta(days=30)
day_ago_date = today - timezone.timedelta(days=1)
week_ago = now - timezone.timedelta(days=7)
month_ago = now - timezone.timedelta(days=30)

all_users = User.objects.count()
all_devices = Device.objects.count()
active_users_monthly = User.objects.filter(last_login__gte=month_ago).count()
active_users_daily = User.objects.filter(last_login__gte=day_ago).count()
active_users_monthly = User.objects.filter(profile__last_active__gte=month_ago_date).count()
active_users_daily = User.objects.filter(profile__last_active__gte=day_ago_date).count()
active_devices = Device.objects.filter(last_ping__gte=week_ago)
inactive_devices = Device.objects.filter(last_ping__lt=week_ago)
metrics = {
Expand Down
24 changes: 24 additions & 0 deletions backend/profile_page/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.utils import timezone

from .models import Profile


class UserActivityMiddleware:
"""
Save user last activity date.
Do actual DB hitting only once a day.
"""

def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
if request.user.is_authenticated:
today = timezone.localdate()
profile, _ = Profile.objects.get_or_create(user=request.user)
if profile.last_active != today:
profile.last_active = today
profile.save(update_fields=['last_active'])

response = self.get_response(request)
return response
18 changes: 18 additions & 0 deletions backend/profile_page/migrations/0003_profile_last_active.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.1.10 on 2019-07-04 08:53

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('profile_page', '0002_auto_20190605_1012'),
]

operations = [
migrations.AddField(
model_name='profile',
name='last_active',
field=models.DateField(blank=True, null=True),
),
]
1 change: 1 addition & 0 deletions backend/profile_page/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ def user_save_lower(sender, instance, *args, **kwargs):
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
company_name = models.CharField(blank=True, null=True, max_length=128)
last_active = models.DateField(null=True, blank=True)
2 changes: 2 additions & 0 deletions backend/profile_page/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.test import TestCase, RequestFactory
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils import timezone


class ProfileViewTest(TestCase):
Expand All @@ -14,6 +15,7 @@ def test_get(self):
response = self.client.get(reverse('profile'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Profile Settings')
self.assertEqual(self.user0.profile.last_active, timezone.localdate())

def test_comment(self):
self.client.login(username='test', password='123')
Expand Down

0 comments on commit 3c6605e

Please sign in to comment.