Skip to content

Commit 0b1bef4

Browse files
committed
[change] Refactored WhoIsService, tasks, test cases
Moved all tasks to service file to keep one source. Refactored the way whois_info is fetcehd for a device. Modified the order of whois checks to keep expensive checks at the bottom. As in views like `DeviceChecksumView` whole device instance is being cached, now org settings will be fetched from db rather than through device to maintain consistency. This has led to increase in queries in some test cases. Signed-off-by: DragnEmperor <[email protected]>
1 parent 09e16c8 commit 0b1bef4

File tree

10 files changed

+306
-274
lines changed

10 files changed

+306
-274
lines changed

openwisp_controller/config/base/device.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from functools import cached_property
12
from hashlib import md5
23

34
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied, ValidationError
@@ -18,7 +19,6 @@
1819
management_ip_changed,
1920
)
2021
from ..validators import device_name_validator, mac_address_validator
21-
from ..whois.services import WhoIsService
2222
from .base import BaseModel
2323

2424

@@ -381,8 +381,7 @@ def _check_last_ip(self):
381381
if self._initial_last_ip == models.DEFERRED:
382382
return
383383

384-
service = WhoIsService(self)
385-
service.trigger_whois_lookup()
384+
self.whois_service.trigger_whois_lookup()
386385

387386
self._initial_last_ip = self.last_ip
388387

@@ -424,13 +423,15 @@ def status(self):
424423
"""
425424
return self._get_config_attr("get_status_display")
426425

427-
@property
428-
def whois_info(self):
426+
@cached_property
427+
def whois_service(self):
429428
"""
430-
Used as a shortcut to get WhoIsInfo
429+
Used as a shortcut to get WhoIsService instance
430+
for the device.
431431
"""
432-
service = WhoIsService(self)
433-
return service.get_whois_info()
432+
from ..whois.service import WhoIsService
433+
434+
return WhoIsService(self)
434435

435436
def get_default_templates(self):
436437
"""

openwisp_controller/config/controller/views.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ def get_object(self, *args, **kwargs):
5555
"organization__modified",
5656
)
5757
queryset = (
58-
self.model.objects.select_related(
59-
"organization", "config", "organization__config_settings"
60-
)
58+
self.model.objects.select_related("organization", "config")
6159
.defer(*defer)
6260
.exclude(config__status="deactivated")
6361
)

openwisp_controller/config/tests/test_controller.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def test_get_cached_checksum(self):
181181
url = reverse("controller:device_checksum", args=[d.pk])
182182

183183
with self.subTest("first request does not return value from cache"):
184-
with self.assertNumQueries(3):
184+
with self.assertNumQueries(4):
185185
with patch.object(
186186
controller_views_logger, "debug"
187187
) as mocked_view_debug:
@@ -1314,12 +1314,12 @@ def test_ip_fields_not_duplicated(self):
13141314
c2 = self._create_config(device=d2)
13151315
org2 = self._create_org(name="org2", shared_secret="123456")
13161316
c3 = self._create_config(organization=org2)
1317-
with self.assertNumQueries(6):
1317+
with self.assertNumQueries(7):
13181318
self.client.get(
13191319
reverse("controller:device_checksum", args=[c3.device.pk]),
13201320
{"key": c3.device.key, "management_ip": "192.168.1.99"},
13211321
)
1322-
with self.assertNumQueries(6):
1322+
with self.assertNumQueries(7):
13231323
self.client.get(
13241324
reverse("controller:device_checksum", args=[c1.device.pk]),
13251325
{"key": c1.device.key, "management_ip": "192.168.1.99"},
@@ -1332,7 +1332,7 @@ def test_ip_fields_not_duplicated(self):
13321332
)
13331333
# triggers more queries because devices with conflicting addresses
13341334
# need to be updated, luckily it does not happen often
1335-
with self.assertNumQueries(12):
1335+
with self.assertNumQueries(9):
13361336
self.client.get(
13371337
reverse("controller:device_checksum", args=[c2.device.pk]),
13381338
{"key": c2.device.key, "management_ip": "192.168.1.99"},
@@ -1361,14 +1361,14 @@ def test_organization_shares_management_ip_address_space(self):
13611361
org1_config = self._create_config(organization=org1)
13621362
org2 = self._create_org(name="org2", shared_secret="org2")
13631363
org2_config = self._create_config(organization=org2)
1364-
with self.assertNumQueries(6):
1364+
with self.assertNumQueries(7):
13651365
self.client.get(
13661366
reverse("controller:device_checksum", args=[org1_config.device_id]),
13671367
{"key": org1_config.device.key, "management_ip": "192.168.1.99"},
13681368
)
13691369
# Device from another organization sends conflicting management IP
13701370
# Extra queries due to conflict resolution
1371-
with self.assertNumQueries(12):
1371+
with self.assertNumQueries(9):
13721372
self.client.get(
13731373
reverse("controller:device_checksum", args=[org2_config.device_id]),
13741374
{"key": org2_config.device.key, "management_ip": "192.168.1.99"},

openwisp_controller/config/utils.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from django.urls import path, re_path
88
from openwisp_notifications.utils import _get_object_link
99

10-
from .whois.services import WhoIsService
11-
1210
logger = logging.getLogger(__name__)
1311

1412

@@ -76,9 +74,8 @@ def update_last_ip(device, request):
7674
device.last_ip = ip
7775
update_fields.append("last_ip")
7876
# for cases of devices who do not have whois record
79-
elif not getattr(device, "whois_info", None):
80-
service = WhoIsService(device)
81-
service.trigger_whois_lookup()
77+
elif not device.whois_service.get_whois_info():
78+
device.whois_service.trigger_whois_lookup()
8279
if device.management_ip != management_ip:
8380
device.management_ip = management_ip
8481
update_fields.append("management_ip")
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
# This file is created to allow test runners to discover the tests
2-
# defined in this directory.
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from django.db import transaction
22

3-
from .services import WhoIsService
3+
from .service import WhoIsService
44

55

66
# Remove the related WhoIs record for that ip from db and cache
77
# If other active devices are linked to it, then new lookup will
88
# be triggered for them.
99
def device_whois_info_delete_handler(instance, **kwargs):
10-
transaction.on_commit(lambda: WhoIsService.delete_whois_record(instance.last_ip))
10+
transaction.on_commit(
11+
lambda: WhoIsService.delete_whois_record.delay(instance.last_ip)
12+
)

0 commit comments

Comments
 (0)