diff --git a/krill/consumables/migrations/0002_consumable_alerts_silenced.py b/krill/consumables/migrations/0002_consumable_alerts_silenced.py new file mode 100644 index 0000000..896361e --- /dev/null +++ b/krill/consumables/migrations/0002_consumable_alerts_silenced.py @@ -0,0 +1,18 @@ +# Generated by Django 6.1.3 on 2026-11-11 16:38 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('consumables', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='consumable', + name='alerts_silenced', + field=models.BooleanField(default=False, help_text='Stop low-stock and expiry alerts and reorder suggestions for this consumable, for example one the lab is phasing out'), + ), + ] diff --git a/krill/consumables/models/consumable.py b/krill/consumables/models/consumable.py index 1de3191..5d0774f 100644 --- a/krill/consumables/models/consumable.py +++ b/krill/consumables/models/consumable.py @@ -12,9 +12,11 @@ def active(self): return self.filter(deleted=False) def low_stock(self): + """Low-stock consumables that still raise alerts (silenced ones are left out).""" return self.active().filter( low_stock_threshold__isnull=False, quantity__lte=F('low_stock_threshold'), + alerts_silenced=False, ) @@ -57,6 +59,12 @@ class Consumable(models.Model): blank=True, ) + alerts_silenced = models.BooleanField( + default=False, + help_text='Stop low-stock and expiry alerts and reorder suggestions for this consumable, ' + 'for example one the lab is phasing out', + ) + expiration_date = models.DateField(null=True, blank=True) notes = models.TextField(blank=True, null=True) specs = models.JSONField(default=dict, blank=True) diff --git a/krill/consumables/views/list.py b/krill/consumables/views/list.py index 25ecdaf..91234f4 100644 --- a/krill/consumables/views/list.py +++ b/krill/consumables/views/list.py @@ -63,6 +63,7 @@ def get_queryset(self): qs = qs.filter( low_stock_threshold__isnull=False, quantity__lte=F('low_stock_threshold'), + alerts_silenced=False, ) # Expiring soon filter (within 30 days) @@ -72,6 +73,7 @@ def get_queryset(self): expiration_date__isnull=False, expiration_date__lte=today + timedelta(days=30), expiration_date__gte=today, + alerts_silenced=False, ) # Sorting diff --git a/krill/krill/views/home.py b/krill/krill/views/home.py index c08a812..9b8d444 100644 --- a/krill/krill/views/home.py +++ b/krill/krill/views/home.py @@ -11,6 +11,7 @@ from sample.models.aliquot import Aliquot, AliquotLocation from storage.models.storage import Box from person.models import UserAuditLog, UserPreference, SiteConfiguration, UserRole +from reports import attention from person.decorators import require_minimum_role __all__ = ( @@ -26,10 +27,12 @@ class HomeView(View): def get(self, request): # Get dashboard statistics - stats = get_dashboard_statistics() + attention_summary = attention.summary() + stats = get_dashboard_statistics(attention_summary) freezers = get_freezer_cards() return render(request, self.template_name, { 'stats': stats, + 'attention': attention_summary, 'freezers': freezers, 'show_sites': len({card['device'].site_id for card in freezers}) > 1, 'checked_out': get_checked_out(request.user), @@ -44,7 +47,7 @@ def dashboard_stats(request): stats = get_dashboard_statistics() return JsonResponse(stats) -def get_dashboard_statistics(): +def get_dashboard_statistics(attention_summary=None): """Calculate dashboard statistics from database""" try: # Active samples (non-deleted) @@ -68,12 +71,8 @@ def get_dashboard_statistics(): action='report_generated', timestamp__gte=timezone.now() - timedelta(days=7) ).count() - # Alerts (using audit logs for now) - # In a real system, you'd have an Alert model - alerts = UserAuditLog.objects.filter( - action__in=['alert_created', 'warning_created'], - timestamp__gte=timezone.now() - timedelta(days=1) - ).count() + # Items needing attention: the same count the header pill and the overview page use + alerts = (attention_summary or attention.summary())['count'] # Consumables stats (only when feature is enabled) low_stock_count = 0 expiring_soon_count = 0 @@ -85,6 +84,7 @@ def get_dashboard_statistics(): expiration_date__isnull=False, expiration_date__lte=date.today() + timedelta(days=30), expiration_date__gte=date.today(), + alerts_silenced=False, ).count() return { diff --git a/krill/reports/README.md b/krill/reports/README.md index 31872f3..2bf9e3e 100644 --- a/krill/reports/README.md +++ b/krill/reports/README.md @@ -1,6 +1,6 @@ # Reports app -Reports is where you check that what Krill says is in storage is really there (the **storage audit**), where alerts are listed, and where aliquots checked out with no recorded owner are listed. Open it from **Reports** in the sidebar. +Reports is where you check that what Krill says is in storage is really there (the **storage audit**), where the items that need attention are listed, and where aliquots checked out with no recorded owner are listed. Open it from **Reports** in the sidebar. ## Storage audit @@ -48,9 +48,18 @@ A person's own checked-out aliquots are in the Aliquots list with the **Checked Code: `checked_out_no_owner` in `reports/views.py` (handles the list and the status change), template `reports/templates/reports/checked_out_no_owner.html`, tests in `reports/test_checked_out_no_owner.py`. -## Alerts +## Needs attention -**Reports > View Alerts** lists alerts with their severity (low, medium, high, critical), type (storage, sample, system, security, general) and status (active, acknowledged, resolved, dismissed). An active alert can be acknowledged and then resolved. The Reports page shows the count of active alerts and the latest five. +The pill in the Dashboard header ("All clear", or "N need attention", amber for a warning and red for an alert) opens **Needs Attention** (`/reports/alerts/`). It lists everything that needs action, grouped by what it is about: + +- **Low stock:** a consumable at or below its low-stock threshold. It is an alert when it is out of stock and a warning otherwise. +- **Expiring soon:** a consumable that expires within 30 days (warning). + +These two only appear when Consumables are turned on in Settings. Items come from your data; nobody types them in. The older manually created alerts are gone (the table was empty in the lab's backup, and the data model is kept). + +**Silence.** Lab managers and admins can press **Silence** on an item when the lab does not want to hear about it, for example a consumable it is phasing out. The switch lives on the consumable itself (`alerts_silenced`), so it also leaves the Low Stock and Expiring Soon tiles, their filtered lists and the reorder list. Silenced items stay on the page under **Silenced**, with **Turn back on**. Silencing is saved to the audit log. + +**How it is built.** `reports/attention.py` works everything out, and the pill, this page and the Reports page all read from it, so they always agree. Each item has a stable key such as `low_stock:consumable:12`. To add a source, write a function that returns a list of `AttentionItem`, register it with `register_source()`, and (if it can be silenced) register a silencer for its record type. Email notifications, when added, should use the same items and keys to avoid sending the same alert twice. ## Other pieces diff --git a/krill/reports/attention.py b/krill/reports/attention.py new file mode 100644 index 0000000..b88d8b0 --- /dev/null +++ b/krill/reports/attention.py @@ -0,0 +1,170 @@ +"""What needs attention: one place that works out the lab's pending action items. + +The header pill, the overview page and (later) email notifications all read from here, so they +always agree. To add a source, write a function that returns a list of AttentionItem and +register it with register_source(). Each item has a stable `key`, so silencing and email +de-duplication can refer to the same item over time. + +An item is silenced by a flag on the record it is about (for example Consumable.alerts_silenced), +not by a separate table, because the lab sets such things per record. Silenced items are still +returned, marked silenced, so the overview can show them; they are left out of the counts. +""" +from dataclasses import dataclass +from datetime import date, timedelta +from typing import Callable, List, Optional + +from django.db.models import F +from django.urls import reverse + +LEVEL_ALERT = 'alert' +LEVEL_WARN = 'warn' +_LEVEL_RANK = {LEVEL_WARN: 1, LEVEL_ALERT: 2} + +EXPIRING_SOON_DAYS = 30 + + +@dataclass(frozen=True) +class AttentionItem: + source: str # source name, e.g. 'low_stock' + key: str # stable id, e.g. 'low_stock:consumable:12' + title: str + detail: str + level: str # LEVEL_ALERT or LEVEL_WARN + url: str # where to go to deal with it + silenced: bool + target_type: str # what the item is about: 'consumable' + target_id: int + + +@dataclass(frozen=True) +class Source: + name: str + label: str + icon: str + collect: Callable[[], List[AttentionItem]] + available: Callable[[], bool] + + +SOURCES: List[Source] = [] +# target_type -> function(target_id, silenced) -> True if a record was changed +SILENCERS = {} + + +def register_source(name, label, icon, collect, available=lambda: True): + SOURCES.append(Source(name, label, icon, collect, available)) + + +def register_silencer(target_type, setter): + SILENCERS[target_type] = setter + + +def _number(value): + """Decimal('3.000') -> '3'""" + return format(value.normalize(), 'f') + + +# --- consumables ------------------------------------------------------------- + +def _consumables_enabled(): + from person.models import SiteConfiguration + return SiteConfiguration.load().consumables_enabled + + +def _consumable_url(consumable): + return reverse('consumables:detail', kwargs={'type': 'consumable', 'pk': consumable.pk}) + + +def low_stock_items(): + from consumables.models.consumable import Consumable + rows = Consumable.objects.active().filter( + low_stock_threshold__isnull=False, + quantity__lte=F('low_stock_threshold'), + ).order_by('name', 'id') + return [ + AttentionItem( + source='low_stock', + key=f'low_stock:consumable:{c.pk}', + title=c.name, + detail=( + f'Out of stock (reorder at {_number(c.low_stock_threshold)} {c.unit})' + if c.quantity <= 0 else + f'{_number(c.quantity)} {c.unit} left (reorder at {_number(c.low_stock_threshold)})' + ), + level=LEVEL_ALERT if c.quantity <= 0 else LEVEL_WARN, + url=_consumable_url(c), + silenced=c.alerts_silenced, + target_type='consumable', + target_id=c.pk, + ) + for c in rows + ] + + +def expiring_items(): + from consumables.models.consumable import Consumable + today = date.today() + rows = Consumable.objects.active().filter( + expiration_date__isnull=False, + expiration_date__gte=today, + expiration_date__lte=today + timedelta(days=EXPIRING_SOON_DAYS), + ).order_by('expiration_date', 'name', 'id') + items = [] + for c in rows: + days = (c.expiration_date - today).days + when = 'today' if days == 0 else f'in {days} day{"s" if days != 1 else ""}' + items.append(AttentionItem( + source='expiring', + key=f'expiring:consumable:{c.pk}', + title=c.name, + detail=f'Expires {c.expiration_date:%Y-%m-%d} ({when})', + level=LEVEL_WARN, + url=_consumable_url(c), + silenced=c.alerts_silenced, + target_type='consumable', + target_id=c.pk, + )) + return items + + +def _silence_consumable(target_id, silenced): + from consumables.models.consumable import Consumable + return bool(Consumable.objects.filter(pk=target_id, deleted=False).update(alerts_silenced=silenced)) + + +register_source('low_stock', 'Low stock', 'production_quantity_limits', low_stock_items, _consumables_enabled) +register_source('expiring', 'Expiring soon', 'event_busy', expiring_items, _consumables_enabled) +register_silencer('consumable', _silence_consumable) + + +# --- reading it -------------------------------------------------------------- + +def groups(): + """[{'source': Source, 'active': [...], 'silenced': [...]}] for every source that is available.""" + result = [] + for source in SOURCES: + if not source.available(): + continue + items = source.collect() + result.append({ + 'source': source, + 'active': [i for i in items if not i.silenced], + 'silenced': [i for i in items if i.silenced], + }) + return result + + +def worst_level(items) -> Optional[str]: + levels = [i.level for i in items] + return max(levels, key=lambda lv: _LEVEL_RANK[lv]) if levels else None + + +def summary(): + """{'count': int, 'level': 'alert' | 'warn' | None} over the items that are not silenced.""" + active = [i for g in groups() for i in g['active']] + return {'count': len(active), 'level': worst_level(active)} + + +def silence(target_type, target_id, silenced=True): + """Silence or unsilence everything about one record. Returns False if nothing matched.""" + setter = SILENCERS.get(target_type) + return bool(setter and setter(target_id, silenced)) diff --git a/krill/reports/templates/reports/reports_dashboard.html b/krill/reports/templates/reports/reports_dashboard.html index 9fa7dc4..1796b32 100644 --- a/krill/reports/templates/reports/reports_dashboard.html +++ b/krill/reports/templates/reports/reports_dashboard.html @@ -25,11 +25,11 @@

In Progress

{{ audits_in_progress }}

- - warning + + {% if attention.count %}warning{% else %}check_circle{% endif %}
-

Active Alerts

-

{{ active_alerts }}

+

Needs Attention

+

{% if attention.count %}{{ attention.count }} item{{ attention.count|pluralize }}{% else %}All clear{% endif %}

@@ -78,43 +78,6 @@

Recent Audits

{% endfor %} - - -
-
-

Recent Alerts

- -
-
- {% if recent_alerts %} - {% for alert in recent_alerts %} -
- - {% if alert.severity == 'critical' %} - error - {% elif alert.severity == 'high' %} - warning - {% else %} - info - {% endif %} - -
-

{{ alert.title }}

- {{ alert.message|truncatechars:60 }} - {{ alert.created_at|timesince }} ago -
-
- {% endfor %} - {% else %} -
- check_circle -
-

No active alerts

- System is running normally -
-
- {% endif %} -
-
{% endblock %} diff --git a/krill/reports/test_attention.py b/krill/reports/test_attention.py new file mode 100644 index 0000000..0ef60f6 --- /dev/null +++ b/krill/reports/test_attention.py @@ -0,0 +1,283 @@ +from datetime import date, timedelta +from decimal import Decimal + +from django.contrib.auth import get_user_model +from django.test import TestCase +from django.urls import reverse + +from consumables.models.consumable import Consumable +from consumables.models.consumable_type import ConsumableType +from person.models import SiteConfiguration, UserAuditLog +from reports import attention + +User = get_user_model() + + +class AttentionFixture(TestCase): + def setUp(self): + self.user = User.objects.create_user(username='member', password='x') + self.manager = User.objects.create_user(username='mgr', password='x') + self.manager.role.role = 'lab_manager' + self.manager.role.save() + self.kind = ConsumableType.objects.create(name='Antibody', category='antibody') + self.set_consumables(True) + + def set_consumables(self, enabled): + config = SiteConfiguration.load() + config.consumables_enabled = enabled + config.save() + + def consumable(self, name='Anti-GFP', quantity='3', threshold='5', expires=None, **kwargs): + return Consumable.objects.create( + name=name, consumable_type=self.kind, quantity=Decimal(quantity), + low_stock_threshold=None if threshold is None else Decimal(threshold), + expiration_date=expires, **kwargs) + + +class LowStockSourceTest(AttentionFixture): + def test_item_has_stable_key_text_level_and_link(self): + c = self.consumable(quantity='3', threshold='5') + [item] = attention.low_stock_items() + self.assertEqual(item.key, f'low_stock:consumable:{c.pk}') + self.assertEqual(item.title, 'Anti-GFP') + self.assertEqual(item.detail, '3 units left (reorder at 5)') + self.assertEqual(item.level, 'warn') + self.assertEqual(item.url, reverse('consumables:detail', kwargs={'type': 'consumable', 'pk': c.pk})) + self.assertEqual((item.target_type, item.target_id, item.silenced), ('consumable', c.pk, False)) + + def test_out_of_stock_is_an_alert(self): + self.consumable(quantity='0', threshold='5') + [item] = attention.low_stock_items() + self.assertEqual(item.level, 'alert') + self.assertIn('Out of stock', item.detail) + + def test_at_the_threshold_counts_and_above_does_not(self): + self.consumable('At', quantity='5', threshold='5') + self.consumable('Above', quantity='6', threshold='5') + self.consumable('No threshold', quantity='0', threshold=None) + self.assertEqual([i.title for i in attention.low_stock_items()], ['At']) + + def test_deleted_consumables_are_ignored(self): + self.consumable(deleted=True) + self.assertEqual(attention.low_stock_items(), []) + + def test_silenced_items_are_returned_but_marked(self): + self.consumable(alerts_silenced=True) + [item] = attention.low_stock_items() + self.assertTrue(item.silenced) + + +class ExpiringSourceTest(AttentionFixture): + def test_item_text_and_level(self): + soon = date.today() + timedelta(days=10) + c = self.consumable(threshold=None, expires=soon) + [item] = attention.expiring_items() + self.assertEqual(item.key, f'expiring:consumable:{c.pk}') + self.assertEqual(item.detail, f'Expires {soon:%Y-%m-%d} (in 10 days)') + self.assertEqual(item.level, 'warn') + + def test_today_and_singular_wording(self): + self.consumable('Today', threshold=None, expires=date.today()) + self.consumable('Tomorrow', threshold=None, expires=date.today() + timedelta(days=1)) + details = {i.title: i.detail for i in attention.expiring_items()} + self.assertTrue(details['Today'].endswith('(today)')) + self.assertTrue(details['Tomorrow'].endswith('(in 1 day)')) + + def test_window_is_today_to_30_days_like_the_dashboard_tile(self): + self.consumable('Past', threshold=None, expires=date.today() - timedelta(days=1)) + self.consumable('Edge', threshold=None, expires=date.today() + timedelta(days=30)) + self.consumable('Far', threshold=None, expires=date.today() + timedelta(days=31)) + self.consumable('None', threshold=None, expires=None) + self.assertEqual([i.title for i in attention.expiring_items()], ['Edge']) + + +class GroupsAndSummaryTest(AttentionFixture): + def test_groups_split_active_from_silenced(self): + self.consumable('Live', quantity='1', threshold='5') + self.consumable('Quiet', quantity='1', threshold='5', alerts_silenced=True) + low = next(g for g in attention.groups() if g['source'].name == 'low_stock') + self.assertEqual([i.title for i in low['active']], ['Live']) + self.assertEqual([i.title for i in low['silenced']], ['Quiet']) + + def test_summary_counts_only_unsilenced_items_across_sources(self): + self.consumable('A', quantity='1', threshold='5', expires=date.today() + timedelta(days=3)) + self.consumable('B', quantity='1', threshold='5', alerts_silenced=True, + expires=date.today() + timedelta(days=3)) + # A is low stock and expiring: two items; B is silenced for both + self.assertEqual(attention.summary(), {'count': 2, 'level': 'warn'}) + + def test_worst_level_wins(self): + self.consumable('Empty', quantity='0', threshold='5') + self.consumable('Low', quantity='1', threshold='5') + self.assertEqual(attention.summary()['level'], 'alert') + + def test_all_clear(self): + self.assertEqual(attention.summary(), {'count': 0, 'level': None}) + + def test_consumable_sources_are_hidden_when_the_feature_is_off(self): + self.consumable(quantity='0', threshold='5') + self.set_consumables(False) + self.assertEqual(attention.groups(), []) + self.assertEqual(attention.summary(), {'count': 0, 'level': None}) + + +class SilenceTest(AttentionFixture): + def test_silence_and_unsilence_a_consumable(self): + c = self.consumable() + self.assertTrue(attention.silence('consumable', c.pk, True)) + c.refresh_from_db() + self.assertTrue(c.alerts_silenced) + self.assertTrue(attention.silence('consumable', c.pk, False)) + c.refresh_from_db() + self.assertFalse(c.alerts_silenced) + + def test_unknown_type_or_missing_record_changes_nothing(self): + self.assertFalse(attention.silence('nonsense', 1, True)) + self.assertFalse(attention.silence('consumable', 99999, True)) + + def test_silenced_consumable_leaves_low_stock_reorder_and_tile_filters(self): + quiet = self.consumable('Quiet', alerts_silenced=True) + loud = self.consumable('Loud') + self.assertEqual(list(Consumable.objects.low_stock()), [loud]) + self.client.force_login(self.user) + listed = self.client.get(reverse('consumables:list'), {'type': 'consumable', 'low_stock': '1'}) + self.assertEqual([c.pk for c in listed.context['items']], [loud.pk]) + self.assertNotIn(quiet.pk, [c.pk for c in listed.context['items']]) + + +class OverviewPageTest(AttentionFixture): + def setUp(self): + super().setUp() + self.url = reverse('reports:alert_list') + + def test_requires_login(self): + self.assertEqual(self.client.get(self.url).status_code, 302) + + def test_lists_items_by_source_with_levels_and_links(self): + c = self.consumable('Anti-GFP', quantity='0', threshold='5', expires=date.today() + timedelta(days=5)) + self.client.force_login(self.user) + response = self.client.get(self.url) + self.assertEqual(response.context['total'], 2) + self.assertEqual(response.context['level'], 'alert') + self.assertContains(response, 'Low stock (1)') + self.assertContains(response, 'Expiring soon (1)') + self.assertContains(response, 'Out of stock') + self.assertContains(response, reverse('consumables:detail', kwargs={'type': 'consumable', 'pk': c.pk})) + self.assertContains(response, '2 items need attention') + + def test_all_clear_and_empty_groups(self): + self.client.force_login(self.user) + response = self.client.get(self.url) + self.assertContains(response, 'All clear') + self.assertContains(response, 'Nothing needs attention.') + + def test_explains_when_nothing_is_watched(self): + self.set_consumables(False) + self.client.force_login(self.user) + self.assertContains(self.client.get(self.url), 'Nothing is being watched yet.') + + def test_silenced_items_are_displayed_separately(self): + self.consumable('Quiet', alerts_silenced=True) + self.client.force_login(self.user) + response = self.client.get(self.url) + self.assertEqual(response.context['silenced_total'], 1) + self.assertContains(response, 'Silenced (1)') + self.assertContains(response, 'Quiet') + self.assertContains(response, 'All clear') # silenced items do not count + + def test_only_managers_get_silence_buttons(self): + self.consumable('Live') + self.consumable('Quiet', alerts_silenced=True) + self.client.force_login(self.user) + member = self.client.get(self.url) + self.assertFalse(member.context['can_silence']) + self.assertNotContains(member, 'name="silenced"') + self.client.force_login(self.manager) + manager = self.client.get(self.url) + self.assertTrue(manager.context['can_silence']) + self.assertContains(manager, 'name="silenced" value="1"') + self.assertContains(manager, 'name="silenced" value="0"') + self.assertContains(manager, 'Turn back on') + + def test_silence_uses_the_confirm_dialog_markup(self): + self.consumable('Live') + self.client.force_login(self.manager) + response = self.client.get(self.url) + self.assertContains(response, 'data-confirm="Silence alerts for Live?') + self.assertContains(response, 'data-confirm-label="Silence"') + + def test_manual_alert_creation_is_gone(self): + from django.urls import NoReverseMatch + self.client.force_login(self.manager) + self.assertNotContains(self.client.get(self.url), 'Create Alert') + for name, args in (('create_alert', []), ('acknowledge_alert', [1]), + ('resolve_alert', [1]), ('alert_list_cbv', [])): + with self.subTest(name=name), self.assertRaises(NoReverseMatch): + reverse(f'reports:{name}', args=args) + + +class SilenceEndpointTest(AttentionFixture): + def setUp(self): + super().setUp() + self.url = reverse('reports:silence_attention') + self.c = self.consumable() + + def _post(self, silenced='1', **extra): + return self.client.post(self.url, {'target_type': 'consumable', 'target_id': self.c.pk, + 'silenced': silenced, **extra}) + + def test_manager_silences_and_it_is_logged(self): + self.client.force_login(self.manager) + response = self._post('1') + self.assertRedirects(response, reverse('reports:alert_list'), fetch_redirect_response=False) + self.c.refresh_from_db() + self.assertTrue(self.c.alerts_silenced) + self.assertTrue(UserAuditLog.objects.filter(action='alerts_silenced', user=self.manager).exists()) + self.assertContains(self.client.get(reverse('reports:alert_list')), 'Alerts silenced.') + + def test_manager_turns_alerts_back_on(self): + self.c.alerts_silenced = True + self.c.save() + self.client.force_login(self.manager) + self._post('0') + self.c.refresh_from_db() + self.assertFalse(self.c.alerts_silenced) + self.assertTrue(UserAuditLog.objects.filter(action='alerts_unsilenced').exists()) + + def test_member_is_refused(self): + self.client.force_login(self.user) + self.assertEqual(self._post('1').status_code, 403) + self.c.refresh_from_db() + self.assertFalse(self.c.alerts_silenced) + + def test_get_is_not_allowed_and_login_required(self): + self.assertEqual(self.client.post(self.url, {}).status_code, 302) + self.client.force_login(self.manager) + self.assertEqual(self.client.get(self.url).status_code, 405) + + def test_bad_input_changes_nothing_and_says_so(self): + self.client.force_login(self.manager) + for data in ({'target_type': 'nonsense', 'target_id': self.c.pk, 'silenced': '1'}, + {'target_type': 'consumable', 'target_id': 'abc', 'silenced': '1'}, + {'target_type': 'consumable', 'target_id': 99999, 'silenced': '1'}): + with self.subTest(data=data): + self.client.post(self.url, data) + self.c.refresh_from_db() + self.assertFalse(self.c.alerts_silenced) + self.assertContains(self.client.get(reverse('reports:alert_list')), 'That item could not be found.') + + +class ReportsPageTileTest(AttentionFixture): + def test_reports_page_shows_the_needs_attention_count(self): + self.consumable(quantity='1', threshold='5') + self.client.force_login(self.user) + response = self.client.get(reverse('reports:reports_dashboard')) + self.assertEqual(response.context['attention'], {'count': 1, 'level': 'warn'}) + self.assertContains(response, 'Needs Attention') + self.assertNotContains(response, 'Recent Alerts') + self.assertNotContains(response, 'Active Alerts') + + def test_stats_api_alerts_key_is_the_attention_count(self): + self.consumable(quantity='1', threshold='5') + self.client.force_login(self.user) + self.assertEqual(self.client.get(reverse('reports:dashboard_stats')).json()['alerts'], 1) diff --git a/krill/reports/tests.py b/krill/reports/tests.py index 238c8c4..0b8ae00 100644 --- a/krill/reports/tests.py +++ b/krill/reports/tests.py @@ -5,7 +5,7 @@ import json import logging -from .models import Report, Alert, ReportTemplate +from .models import Report, ReportTemplate from . import views as reports_views from sample.models import Sample, Aliquot from sample.models.aliquot import AliquotLocation @@ -108,11 +108,9 @@ def test_dashboard_stats_multiple_failures(self): mock_sample.objects.count.side_effect = Exception("Sample error") mock_report = MagicMock() mock_report.objects.filter.return_value.count.side_effect = Exception("Report error") - mock_alert = MagicMock() - mock_alert.objects.filter.return_value.count.side_effect = Exception("Alert error") with patch.object(reports_views, 'Sample', mock_sample), \ patch.object(reports_views, 'Report', mock_report), \ - patch.object(reports_views, 'Alert', mock_alert): + patch.object(reports_views.attention, 'summary', side_effect=Exception("Alert error")): response = self.client.get(reverse('reports:dashboard_stats')) self.assertEqual(response.status_code, 200) data = response.json() @@ -174,8 +172,6 @@ def test_dashboard_stats_all_queries_fail(self): mock_box.objects.only.side_effect = Exception("Error 2") mock_report = MagicMock() mock_report.objects.filter.return_value.count.side_effect = Exception("Error 3") - mock_alert = MagicMock() - mock_alert.objects.filter.return_value.count.side_effect = Exception("Error 4") mock_audit = MagicMock() mock_audit.objects.filter.return_value.count.side_effect = Exception("Error 5") mock_device = MagicMock() @@ -187,7 +183,7 @@ def test_dashboard_stats_all_queries_fail(self): with patch.object(reports_views, 'Sample', mock_sample), \ patch.object(reports_views, 'Box', mock_box), \ patch.object(reports_views, 'Report', mock_report), \ - patch.object(reports_views, 'Alert', mock_alert), \ + patch.object(reports_views.attention, 'summary', side_effect=Exception("Error 4")), \ patch.object(reports_views, 'UserAuditLog', mock_audit), \ patch.object(reports_views, 'Device', mock_device), \ patch.object(reports_views, 'Aliquot', mock_aliquot), \ @@ -272,33 +268,6 @@ def test_report_list_grid_returns_20(self): self.assertEqual(len(response.context['reports']), 20) -class AlertListViewPageSizeTest(TestCase): - def setUp(self): - self.client = Client() - self.user = User.objects.create_user(username='alertuser', password='pass') - - def test_alert_list_defaults_to_50(self): - Alert.objects.bulk_create([ - Alert(title=f"Alert{i}", message="msg", severity='low', alert_type='general') - for i in range(60) - ]) - self.client.force_login(self.user) - response = self.client.get(reverse('reports:alert_list_cbv')) - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.context['alerts']), 50) - - def test_alert_list_grid_returns_20(self): - Alert.objects.bulk_create([ - Alert(title=f"Alert{i}", message="msg", severity='low', alert_type='general') - for i in range(30) - ]) - self.client.force_login(self.user) - response = self.client.get(reverse('reports:alert_list_cbv') + '?page_size=20') - self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.context['alerts']), 20) - - - class StorageAuditReportTest(ReportsViewTest): """The storage audit picks random stored aliquots and lists where to find them.""" diff --git a/krill/reports/urls.py b/krill/reports/urls.py index c6203eb..d3157b7 100644 --- a/krill/reports/urls.py +++ b/krill/reports/urls.py @@ -23,9 +23,7 @@ # Alert System path('alerts/', views.alert_list, name='alert_list'), - path('alerts//acknowledge/', views.acknowledge_alert, name='acknowledge_alert'), - path('alerts//resolve/', views.resolve_alert, name='resolve_alert'), - path('alerts/create/', views.create_alert, name='create_alert'), + path('alerts/silence/', views.silence_attention, name='silence_attention'), # Aliquots checked out with no recorded owner @@ -36,6 +34,5 @@ # Class-based views path('reports/', views.ReportListView.as_view(), name='report_list_cbv'), - path('alerts/list/', views.AlertListView.as_view(), name='alert_list_cbv'), ] diff --git a/krill/reports/views.py b/krill/reports/views.py index 938d314..ff2ced3 100644 --- a/krill/reports/views.py +++ b/krill/reports/views.py @@ -20,7 +20,8 @@ from django.views.decorators.csrf import csrf_exempt from django.utils.decorators import method_decorator -from .models import ReportTemplate, Report, ScheduledReport, Alert +from . import attention +from .models import ReportTemplate, Report, ScheduledReport from sample.models import Sample, Aliquot from sample.models.aliquot import AliquotDisposition, AliquotLocation from storage.models.storage import Device, Box @@ -104,8 +105,7 @@ def reports_dashboard(request): 'audits': audits, 'audit_count': Report.objects.filter(template__report_type='storage_audit', status='complete').count(), 'audits_in_progress': in_progress, - 'active_alerts': Alert.objects.filter(status='active').count(), - 'recent_alerts': Alert.objects.filter(status='active').order_by('-created_at')[:5], + 'attention': attention.summary(), }) @@ -157,10 +157,9 @@ def dashboard_stats(request): errors.append(f"recent_reports: {str(e)}") stats['recent_reports'] = 0 - # Active alerts count + # Items needing attention (low stock, expiring, ...) try: - active_alerts = Alert.objects.filter(status='active').count() - stats['alerts'] = active_alerts + stats['alerts'] = attention.summary()['count'] except Exception as e: logger.error(f"Error fetching active alerts count: {str(e)}", exc_info=True) errors.append(f"alerts: {str(e)}") @@ -476,76 +475,44 @@ def download_report(request, report_id): }) -# Alert System API +# Needs attention: the overview behind the Dashboard's header pill @login_required def alert_list(request): - """List all alerts""" - alerts = Alert.objects.all().order_by('-created_at') - - # Filter by status if provided - status_filter = request.GET.get('status') - if status_filter: - alerts = alerts.filter(status=status_filter) - - # Pagination - paginator = Paginator(alerts, 20) - page_number = request.GET.get('page') - page_obj = paginator.get_page(page_number) - - context = { - 'alerts': page_obj, - 'total_alerts': alerts.count(), - 'active_alerts': alerts.filter(status='active').count(), - 'critical_alerts': alerts.filter(severity='critical', status='active').count(), - } - return render(request, 'reports/alert_list.html', context) - - -@login_required -@require_http_methods(["POST"]) -def acknowledge_alert(request, alert_id): - """Acknowledge an alert""" - try: - alert = get_object_or_404(Alert, id=alert_id) - alert.acknowledge(request.user) - return JsonResponse({'success': True, 'message': 'Alert acknowledged'}) - except Exception as e: - return JsonResponse({'error': str(e)}, status=500) - - -@login_required -@require_http_methods(["POST"]) -def resolve_alert(request, alert_id): - """Resolve an alert""" - try: - alert = get_object_or_404(Alert, id=alert_id) - alert.resolve(request.user) - return JsonResponse({'success': True, 'message': 'Alert resolved'}) - except Exception as e: - return JsonResponse({'error': str(e)}, status=500) + """Everything that needs attention, grouped by source, with silenced items listed separately.""" + can_silence = UserRole.get_or_create_for_user(request.user).role in ('lab_manager', 'lab_admin') + groups = attention.groups() + active = [i for g in groups for i in g['active']] + return render(request, 'reports/alert_list.html', { + 'groups': groups, + 'total': len(active), + 'level': attention.worst_level(active), + 'silenced_total': sum(len(g['silenced']) for g in groups), + 'can_silence': can_silence, + }) @login_required -@require_http_methods(["POST"]) -def create_alert(request): - """Create a new alert""" - try: - data = json.loads(request.body) - alert = Alert.objects.create( - title=data['title'], - message=data['message'], - severity=data.get('severity', 'medium'), - alert_type=data.get('alert_type', 'general'), - target_type=data.get('target_type', ''), - target_id=data.get('target_id'), +@require_POST +def silence_attention(request): + """Silence or unsilence the alerts about one record. Lab managers and admins only.""" + if UserRole.get_or_create_for_user(request.user).role not in ('lab_manager', 'lab_admin'): + raise PermissionDenied("Only lab managers and admins can silence alerts.") + target_type = request.POST.get('target_type', '') + target_id = request.POST.get('target_id', '') + silenced = request.POST.get('silenced') == '1' + if target_id.isdigit() and attention.silence(target_type, int(target_id), silenced): + UserAuditLog.log_action( + user=request.user, + action='alerts_silenced' if silenced else 'alerts_unsilenced', + target_type=target_type.title(), + target_id=int(target_id), + target_name=f'{target_type} {target_id}', + request=request, ) - return JsonResponse({ - 'success': True, - 'alert_id': alert.id, - 'message': 'Alert created successfully' - }) - except Exception as e: - return JsonResponse({'error': str(e)}, status=500) + messages.success(request, 'Alerts silenced.' if silenced else 'Alerts turned back on.') + else: + messages.error(request, 'That item could not be found.') + return redirect('reports:alert_list') # Storage Management Dashboard API @@ -767,28 +734,3 @@ def get_context_data(self, **kwargs): context['pending_reports'] = Report.objects.filter(created_by=self.request.user, status='pending').count() context['completed_reports'] = Report.objects.filter(created_by=self.request.user, status='complete').count() return context - - -class AlertListView(LoginRequiredMixin, ListView): - model = Alert - template_name = 'reports/alert_list.html' - context_object_name = 'alerts' - - def get_paginate_by(self, queryset): - if self.request.GET.get('page_size') == '20': - return 20 - return 50 - - def get_queryset(self): - queryset = Alert.objects.all().order_by('-created_at') - status_filter = self.request.GET.get('status') - if status_filter: - queryset = queryset.filter(status=status_filter) - return queryset - - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - context['total_alerts'] = Alert.objects.count() - context['active_alerts'] = Alert.objects.filter(status='active').count() - context['critical_alerts'] = Alert.objects.filter(severity='critical', status='active').count() - return context diff --git a/krill/sample/tests/test_dashboard_sections.py b/krill/sample/tests/test_dashboard_sections.py index de4ae0e..8dad199 100644 --- a/krill/sample/tests/test_dashboard_sections.py +++ b/krill/sample/tests/test_dashboard_sections.py @@ -1,7 +1,12 @@ +from datetime import date, timedelta +from decimal import Decimal + from django.contrib.auth import get_user_model from django.urls import reverse from django.utils import timezone +from consumables.models.consumable import Consumable +from consumables.models.consumable_type import ConsumableType from person.models import SiteConfiguration from ..models.aliquot import Aliquot, AliquotDisposition from .test_views import SampleViewTest @@ -57,14 +62,6 @@ def test_storage_tiles_and_freezers_sit_in_the_storage_group(self): for expected in ('

Storage Used

', '

Audits

', '

Freezers

'): self.assertIn(expected, group) - def test_alerts_tile_is_kept_and_sits_after_audits_in_the_storage_group(self): - self._consumables(True) - html = self._html() - group = html[html.index('class="dashboard-group-label">Storage<'):html.index('class="dashboard-group-label">Consumables<')] - self.assertRegex(group, r'
\s*

Alerts

') - self.assertLess(group.index('

Audits

'), group.index('

Alerts

')) - self.assertIn(reverse('reports:alert_list'), group) - def test_consumable_tiles_sit_in_the_consumables_group_when_enabled(self): self._consumables(True) html = self._html() @@ -91,3 +88,61 @@ def test_manager_tile_is_in_the_samples_group(self): html = self._html() group = html[html.index('>Samples<'):html.index('class="dashboard-group-label">Storage<')] self.assertIn('

Lab Checkouts

', group) + + +class DashboardAttentionPillTest(SampleViewTest): + """The header pill shows how many items need attention and opens the overview.""" + + def setUp(self): + super().setUp() + self.client.force_login(self.user) + self.kind = ConsumableType.objects.create(name='Antibody', category='antibody') + config = SiteConfiguration.load() + config.consumables_enabled = True + config.save() + + def _consumable(self, quantity='3', threshold='5', **kwargs): + return Consumable.objects.create( + name=kwargs.pop('name', 'Anti-GFP'), consumable_type=self.kind, + quantity=Decimal(quantity), low_stock_threshold=Decimal(threshold), **kwargs) + + def test_alerts_are_a_header_pill_not_a_tile(self): + response = self.client.get(reverse('home')) + # The sidebar has an Alerts item; what must be gone is the stat tile + self.assertNotRegex(response.content.decode(), r'
\s*

Alerts

') + self.assertContains(response, 'class="header-alerts') + self.assertContains(response, reverse('reports:alert_list')) + + def test_all_clear_with_nothing_to_report(self): + response = self.client.get(reverse('home')) + self.assertEqual(response.context['attention'], {'count': 0, 'level': None}) + self.assertContains(response, 'All clear') + self.assertNotContains(response, 'class="header-alerts level-') + + def test_warn_is_amber_and_counts_computed_items(self): + self._consumable(quantity='3', threshold='5', expiration_date=date.today() + timedelta(days=4)) + response = self.client.get(reverse('home')) + self.assertEqual(response.context['attention'], {'count': 2, 'level': 'warn'}) + self.assertContains(response, 'class="header-alerts level-warn"') + self.assertRegex(response.content.decode(), r'2 need attention\s*') + + def test_out_of_stock_turns_the_pill_red(self): + self._consumable(quantity='0', threshold='5') + response = self.client.get(reverse('home')) + self.assertContains(response, 'class="header-alerts level-alert"') + + def test_singular_wording(self): + self._consumable(quantity='3', threshold='5') + self.assertRegex(self.client.get(reverse('home')).content.decode(), r'1 needs attention\s*') + + def test_silenced_items_are_not_counted(self): + self._consumable(alerts_silenced=True) + response = self.client.get(reverse('home')) + self.assertEqual(response.context['attention']['count'], 0) + self.assertContains(response, 'All clear') + + def test_dashboard_tiles_skip_silenced_consumables(self): + self._consumable(alerts_silenced=True, expiration_date=date.today() + timedelta(days=4)) + stats = self.client.get(reverse('home')).context['stats'] + self.assertEqual(stats['low_stock_count'], 0) + self.assertEqual(stats['expiring_soon_count'], 0) diff --git a/krill/templates/krill/home.html b/krill/templates/krill/home.html index 91042fc..2264d4a 100644 --- a/krill/templates/krill/home.html +++ b/krill/templates/krill/home.html @@ -5,6 +5,11 @@ - - warning -
-

Alerts

-

{% if stats.alerts %}{{ stats.alerts }} active{% else %}All clear{% endif %}

-
-
@@ -226,7 +224,15 @@

Recent Activity

.activity-actions .btn { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; line-height: 1.2; } .checked-out-card { margin-top: 16px; } .checked-out-empty { margin: 8px 0 16px; color: var(--color-dark-variant); } - .dashboard-header { margin-bottom: 20px; } + .dashboard-header { margin-bottom: 20px; display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; } + .header-alerts { display: inline-flex; align-items: center; gap: 6px; padding: 6px 14px; border: 1px solid var(--color-gray-border); + border-radius: 999px; color: var(--color-dark); font-size: 0.85rem; font-weight: 500; text-decoration: none; } + .header-alerts:hover { border-color: var(--color-primary); } + .header-alerts .material-icons-round { font-size: 1.1rem; color: var(--color-success); } + .header-alerts.level-warn { border-color: var(--color-warning); } + .header-alerts.level-warn .material-icons-round { color: var(--color-warning); } + .header-alerts.level-alert { border-color: var(--color-danger); } + .header-alerts.level-alert .material-icons-round { color: var(--color-danger); } .dashboard-group-label { margin: 24px 0 10px; font-size: 0.75rem; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--color-dark-variant); } .freezer-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; } diff --git a/krill/templates/reports/alert_list.html b/krill/templates/reports/alert_list.html index 291ac1f..9579336 100644 --- a/krill/templates/reports/alert_list.html +++ b/krill/templates/reports/alert_list.html @@ -1,743 +1,113 @@ {% extends 'base/layout.html' %} -{% load static %} -{% block content %} -
-
-

System Alerts

-
- -
-
+{% block title %}Needs Attention{% endblock %} - -
-
- notifications -
-

Total Alerts

-

{{ total_alerts }}

-
-
-
- warning -
-

Active

-

{{ active_alerts }}

-
-
-
- priority_high -
-

Critical

-

{{ critical_alerts }}

-
-
+{% block content %} +
+
+

Needs Attention

+

+ {% if total %}{{ total }} item{{ total|pluralize }} need{{ total|pluralize:"s," }} attention{% else %}All clear{% endif %} + {% if silenced_total %}· {{ silenced_total }} silenced{% endif %} +

- -
-
-

Recent Alerts

-
- - -
- - -
-
-
+ {% for message in messages %} +

{{ message }}

+ {% endfor %} - -
-
- - - - - - - - - - - - - {% for alert in alerts %} - - - - - - - - - {% empty %} - - {% endfor %} - -
TitleSeverityTypeStatusCreated
{{ alert.title }}{{ alert.get_severity_display }}{{ alert.get_alert_type_display }}{{ alert.get_status_display }}{{ alert.created_at|date:"Y-m-d" }} - - open_in_new - -
No active alerts. Alerts are raised automatically when a freezer crosses 90% capacity or a sample passes its expiry date.
+ {% for group in groups %} +
+
+ {{ group.source.icon }} +

{{ group.source.label }} ({{ group.active|length }})

-
- - -
- - -