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 #840 from WoTTsecurity/revert-833-829-issues-from-823
Browse files Browse the repository at this point in the history
Revert "Resolve issues left over from #823"
  • Loading branch information
vpetersson committed Apr 3, 2020
2 parents ec6bee8 + 439b133 commit 7728349
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 119 deletions.
3 changes: 1 addition & 2 deletions backend/backend/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ def check_ip_range(ipr):
int(os.getenv('REDIS_PORT', '6379'))
)

SAMPLE_HISTORY_AT = 17
CELERY_BEAT_SCHEDULE = {
'update_celery_pulse_timestamp': {
'task': 'monitoring.tasks.update_celery_pulse_timestamp',
Expand Down Expand Up @@ -275,7 +274,7 @@ def check_ip_range(ipr):
},
'sample_history': {
'task': 'device_registry.tasks.sample_history',
'schedule': crontab(hour=SAMPLE_HISTORY_AT, minute=0) # Execute once a day at 5PM.
'schedule': crontab(hour=17, minute=0) # Execute once a day at 5PM.
}
}

Expand Down
14 changes: 5 additions & 9 deletions backend/device_registry/templates/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -316,20 +316,16 @@ <h1>2.5 day</h1><!--HARDCODE-->
<circle id="trust-score-circle-segment" class="donut-segment" cx="21" cy="21" r="15.91549430918954"
fill="transparent" stroke="#33D3EC" stroke-width="2" stroke-linecap="round"
stroke-dashoffset="75"
stroke-dasharray="{{ actions_resolved_this_quarter_part_of_100 }} {{ actions_resolved_this_quarter_left_for_100 }}">
stroke-dasharray="{{ actions_resolved_this_quarter.1 }} {{ actions_resolved_this_quarter.2 }}">
</circle>
<g class="wott-chart-block-info">
{% if actions_resolved_this_quarter < 10 %}
{% if actions_resolved_this_quarter.0 < 10 %}
<text id="trust-score-percentage" x="45%" y="55%" class="wott-chart-big-info">
{{ actions_resolved_this_quarter }}
</text>
{% elif actions_resolved_this_quarter < 100 %}
<text id="trust-score-percentage" x="38%" y="55%" class="wott-chart-big-info">
{{ actions_resolved_this_quarter }}
{{ actions_resolved_this_quarter.0 }}
</text>
{% else %}
<text id="trust-score-percentage" x="31%" y="55%" class="wott-chart-big-info">
{{ actions_resolved_this_quarter }}
<text id="trust-score-percentage" x="38%" y="55%" class="wott-chart-big-info">
{{ actions_resolved_this_quarter.0 }}
</text>
{% endif %}
<text class="wott-chart-small-info" x="43%" y="68%">
Expand Down
8 changes: 4 additions & 4 deletions backend/device_registry/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ def get_context_data(self, **kwargs):
},
)
actions_resolved_this_quarter = self.request.user.profile.actions_resolved_this_quarter
# 60 is the quarterly resolved RAs target (see https://github.com/WoTTsecurity/api/issues/819)
actions_resolved_this_quarter_part_of_100 = actions_resolved_this_quarter / 60 * 100
context.update(
actions=actions,
weekly_progress=resolved_count,
weekly_progress_percent=round(min(resolved_count, settings.MAX_WEEKLY_RA) * 100 / settings.MAX_WEEKLY_RA),
actions_resolved_this_quarter=actions_resolved_this_quarter,
# 60 is the quarterly resolved RAs target (see https://github.com/WoTTsecurity/api/issues/819)
actions_resolved_this_quarter_part_of_100=actions_resolved_this_quarter / 60 * 100,
actions_resolved_this_quarter_left_for_100=100 - actions_resolved_this_quarter / 60 * 100,
actions_resolved_this_quarter=(actions_resolved_this_quarter, actions_resolved_this_quarter_part_of_100,
100 - actions_resolved_this_quarter_part_of_100),
current_weekly_streak=self.request.user.profile.current_weekly_streak
)
return context
Expand Down
32 changes: 3 additions & 29 deletions backend/profile_page/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from django.db.models import Q, Avg, Max, Window, Count, Sum
from django.db.models.functions import Coalesce
from django.db.models.signals import pre_save
from django.core.exceptions import ObjectDoesNotExist
from django.dispatch import receiver
from django.utils import timezone

Expand Down Expand Up @@ -89,28 +88,6 @@ def actions_resolved_since_monday(self):
resolved = self.actions_weekly[1]
return min(resolved.count(), settings.MAX_WEEKLY_RA)

@property
def actions_resolved_today(self):
"""
A method for finding the number of RAs resolved today that are not reflected in a history record.
If no history record for today - return the number of RAs resolved today.
Otherwise - return 0.
"""
now = timezone.now()
day_ago = now - timezone.timedelta(hours=24)
if self.user.history_records.filter(sampled_at__date=now.date()).exists():
return 0
else:
try:
yesterday_history_record = self.user.history_records.get(sampled_at__date=day_ago.date())
except ObjectDoesNotExist:
day_ago = day_ago.replace(hour=settings.SAMPLE_HISTORY_AT, minute=0, second=0, microsecond=0)
else:
day_ago = yesterday_history_record.sampled_at
return RecommendedActionStatus.objects.filter(
status=RecommendedAction.Status.NOT_AFFECTED, resolved_at__gt=day_ago, resolved_at__lte=now,
device__owner=self.user).values('ra__pk').distinct().count()

@property
def actions_resolved_this_quarter(self):
"""
Expand All @@ -119,10 +96,9 @@ def actions_resolved_this_quarter(self):
now = timezone.now()
# Timestamp for the very beginning of the current quarter.
quarter_start_ts = timezone.datetime(now.year, (now.month - 1) // 3 * 3 + 1, 1, tzinfo=now.tzinfo)
actions_number_from_history = self.user.history_records.filter(
return self.user.history_records.filter(
sampled_at__gt=quarter_start_ts).aggregate(Sum('recommended_actions_resolved')
)['recommended_actions_resolved__sum'] or 0
return actions_number_from_history + self.actions_resolved_today

@property
def current_weekly_streak(self):
Expand All @@ -140,10 +116,8 @@ def current_weekly_streak(self):
while True:
actions_resolved = self.user.history_records.filter(
sampled_at__gt=monday, sampled_at__lt=end_ts).aggregate(Sum('recommended_actions_resolved')
)['recommended_actions_resolved__sum'] or 0
if current_week:
actions_resolved += self.actions_resolved_today
if actions_resolved >= settings.MAX_WEEKLY_RA:
)['recommended_actions_resolved__sum']
if actions_resolved and actions_resolved >= settings.MAX_WEEKLY_RA:
streak += 1
else:
if not current_week:
Expand Down
75 changes: 0 additions & 75 deletions backend/profile_page/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@
from django.contrib.auth import get_user_model
from django.urls import reverse
from django.utils import timezone
from django.conf import settings

from freezegun import freeze_time

from profile_page.forms import RegistrationForm
from profile_page.models import Profile
from device_registry.models import Device, HistoryRecord, RecommendedActionStatus, RecommendedAction


class ProfileViewsTest(TestCase):
Expand Down Expand Up @@ -163,73 +158,3 @@ def test_tracking(self):
response = self.client.get(reverse('profile'))
self.assertFalse('signed_up' in response.context)
self.assertFalse('signed_in' in response.context)


class ProfileModelTests(TestCase):
def setUp(self):
User = get_user_model()
self.user = User.objects.create_user('user@gmail.com')
self.user.save()
Profile.objects.create(user=self.user)
self.device = Device.objects.create(device_id='device0.d.wott-dev.local', owner=self.user)

@freeze_time("2020-04-01 10:00:00", tz_offset=4)
def test_actions_resolved_today(self):
now = timezone.now()
# Take resolved RAs info from RecommendedActionStatus model.
RecommendedActionStatus.objects.create(
device=self.device, ra=RecommendedAction.objects.create(action_class='ClassOne'),
status=RecommendedAction.Status.NOT_AFFECTED, resolved_at=now)
RecommendedActionStatus.objects.create(
device=self.device, ra=RecommendedAction.objects.create(action_class='ClassTwo'),
status=RecommendedAction.Status.NOT_AFFECTED, resolved_at=now)
self.assertEqual(self.user.profile.actions_resolved_today, 2)
# Return 0 because the today's history record exists.
HistoryRecord.objects.create(owner=self.user, recommended_actions_resolved=5)
self.assertEqual(self.user.profile.actions_resolved_today, 0)

def test_actions_resolved_this_quarter(self):
# 2 days ago (previous quarter). this history record should not be counted.
with freeze_time("2020-03-31 10:00:00", tz_offset=4):
HistoryRecord.objects.create(owner=self.user, recommended_actions_resolved=5)
# 1 day ago (current quarter), should be counted.
with freeze_time("2020-04-01 10:00:00", tz_offset=4):
HistoryRecord.objects.create(owner=self.user, recommended_actions_resolved=6)
with freeze_time("2020-04-02 10:00:00", tz_offset=4):
self.assertEqual(self.user.profile.actions_resolved_this_quarter, 6)
# Add a RA resolved today, not yet reflected in history records.
RecommendedActionStatus.objects.create(
device=self.device, ra=RecommendedAction.objects.create(action_class='ClassTwo'),
status=RecommendedAction.Status.NOT_AFFECTED, resolved_at=timezone.now() - timezone.timedelta(hours=1))
self.assertEqual(self.user.profile.actions_resolved_this_quarter, 7)

def test_current_weekly_streak(self):
# 1 week ago.
with freeze_time("2020-03-25 10:00:00", tz_offset=4):
HistoryRecord.objects.create(owner=self.user, recommended_actions_resolved=settings.MAX_WEEKLY_RA)
# 2 weeks ago.
with freeze_time("2020-03-18 10:00:00", tz_offset=4):
hr = HistoryRecord.objects.create(owner=self.user, recommended_actions_resolved=settings.MAX_WEEKLY_RA)
# 3 weeks ago.
with freeze_time("2020-03-11 10:00:00", tz_offset=4):
HistoryRecord.objects.create(owner=self.user, recommended_actions_resolved=settings.MAX_WEEKLY_RA)
# Today.
with freeze_time("2020-04-02 10:00:00", tz_offset=4):
self.assertEqual(self.user.profile.current_weekly_streak, 3)
# Reduce the number of resolved RAs 2 weeks ago below the threshold.
hr.recommended_actions_resolved = settings.MAX_WEEKLY_RA - 1
hr.save(update_fields=['recommended_actions_resolved'])
self.assertEqual(self.user.profile.current_weekly_streak, 1)
# Increase the number of resolved RAs 2 weeks ago above the threshold.
hr.recommended_actions_resolved = settings.MAX_WEEKLY_RA + 1
hr.save(update_fields=['recommended_actions_resolved'])
self.assertEqual(self.user.profile.current_weekly_streak, 3)
# Delete the history record for 2 weeks ago.
hr.delete()
self.assertEqual(self.user.profile.current_weekly_streak, 1)
# Yesterday (current week).
with freeze_time("2020-04-01 10:00:00", tz_offset=4):
HistoryRecord.objects.create(owner=self.user, recommended_actions_resolved=settings.MAX_WEEKLY_RA)
# Today.
with freeze_time("2020-04-02 10:00:00", tz_offset=4):
self.assertEqual(self.user.profile.current_weekly_streak, 2)

0 comments on commit 7728349

Please sign in to comment.