Skip to content

Commit

Permalink
[fix] Fixed cache invalidation issue in DeviceMetricView
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed Nov 13, 2024
1 parent 1ec30f1 commit b7051b0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion openwisp_monitoring/device/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def get_fields(self, request, obj=None):
fields = list(super().get_fields(request, obj))
if obj and not obj._state.adding:
fields.insert(fields.index('last_ip'), 'health_status')
if not obj or obj.monitoring.status in ['ok', 'unknown']:
if not obj or obj.monitoring.status in ['ok', 'unknown', 'deactivated']:
return fields
fields.insert(fields.index('health_status') + 1, 'health_checks')
return fields
Expand Down
11 changes: 11 additions & 0 deletions openwisp_monitoring/device/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ def connect_device_signals(self):
sender=Device,
dispatch_uid='device_deactivated_update_devicemonitoring',
)
device_deactivated.connect(
DeviceMetricView.invalidate_get_device_cache,
sender=Device,
dispatch_uid=('deactivate_device_invalidate_view_device_cache'),
)
device_deactivated.connect(
DeviceMetricView.invalidate_get_charts_cache,
sender=Device,
dispatch_uid=('deactivate_device_invalidate_view_charts_cache'),
)

@classmethod
def device_post_save_receiver(cls, instance, created, **kwargs):
Expand Down Expand Up @@ -340,6 +350,7 @@ def register_dashboard_items(self):
'problem': '#ffb442',
'critical': '#a72d1d',
'unknown': '#353c44',
'deactivated': '#000',
},
'labels': {
'ok': app_settings.HEALTH_STATUS_LABELS['ok'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.health-unknown,
.health-deactivated {
color: #000;
}
.health-unknown {
color: grey;
}
.health-ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ td.field-health_status,
font-weight: bold;
text-transform: uppercase;
}
.health-unknown,
.health-deactivated {
color: #000;
}
.health-unknown {
color: grey;
}
.health-ok {
Expand Down
11 changes: 11 additions & 0 deletions openwisp_monitoring/device/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ def test_200_none(self):
def test_200_create(self):
self.create_test_data(no_resources=True)

def test_device_deactivate_cache_invalidation(self):
device = self._create_device()
# Ensure device ID is cached by the view
data = {'type': 'DeviceMonitoring'}
response = self._post_data(device.id, device.key, data)
self.assertEqual(response.status_code, 200)
device.deactivate()

response = self._post_data(device.id, device.key, data)
self.assertEqual(response.status_code, 404)

@patch('openwisp_monitoring.device.tasks.write_device_metrics.delay')
def test_background_write(self, mocked_task):
device = self._create_device(organization=self._create_org())
Expand Down

0 comments on commit b7051b0

Please sign in to comment.