diff --git a/krill/krill/views/home.py b/krill/krill/views/home.py index 9b8d444..fdfe1ad 100644 --- a/krill/krill/views/home.py +++ b/krill/krill/views/home.py @@ -289,6 +289,9 @@ def post(self, request): from django.http import HttpResponseForbidden return HttpResponseForbidden("Only lab admins can change site configuration.") + if request.POST.get('form') == 'alert_levels': + return self._save_alert_levels(request) + config = SiteConfiguration.load() consumables_enabled = request.POST.get('consumables_enabled') == 'on' consumables_ordering_enabled = ( @@ -316,3 +319,33 @@ def post(self, request): from django.contrib import messages messages.success(request, 'Site configuration saved.') return redirect('settings') + + def _save_alert_levels(self, request): + """Save the lab-wide low-count levels. Leaves the feature switches alone.""" + from django.contrib import messages + try: + warn = int(request.POST.get('low_count_warn', '')) + alert = int(request.POST.get('low_count_alert', '')) + except ValueError: + warn = alert = -1 + if warn < 0 or alert < 0: + messages.error(request, 'Enter whole numbers, zero or more, for both levels.') + elif alert > warn: + messages.error(request, 'The alert level cannot be higher than the warn level.') + else: + config = SiteConfiguration.load() + config.low_count_warn = warn + config.low_count_alert = alert + config.updated_by = request.user + config.save() + UserAuditLog.log_action( + user=request.user, + action='update', + target_type='SiteConfiguration', + target_id=config.pk, + target_name='Site Configuration', + details={'low_count_warn': warn, 'low_count_alert': alert}, + request=request, + ) + messages.success(request, f'Low-count levels saved: warn at {warn} or fewer, alert at {alert} or fewer.') + return redirect('settings') diff --git a/krill/person/migrations/0006_low_count_levels.py b/krill/person/migrations/0006_low_count_levels.py new file mode 100644 index 0000000..4c1a078 --- /dev/null +++ b/krill/person/migrations/0006_low_count_levels.py @@ -0,0 +1,23 @@ +# Generated by Django 6.1.3 on 2026-11-11 17:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('person', '0005_bigint_object_ids'), + ] + + operations = [ + migrations.AddField( + model_name='siteconfiguration', + name='low_count_alert', + field=models.PositiveIntegerField(default=3, help_text='Alert when a sample has this many stored aliquots or fewer'), + ), + migrations.AddField( + model_name='siteconfiguration', + name='low_count_warn', + field=models.PositiveIntegerField(default=5, help_text='Warn when a sample has this many stored aliquots or fewer'), + ), + ] diff --git a/krill/person/models.py b/krill/person/models.py index c43b59d..a99e3ba 100644 --- a/krill/person/models.py +++ b/krill/person/models.py @@ -204,6 +204,14 @@ class SiteConfiguration(models.Model): """Singleton site-wide feature flags. Access via SiteConfiguration.load().""" consumables_enabled = models.BooleanField(default=False) consumables_ordering_enabled = models.BooleanField(default=False) + low_count_warn = models.PositiveIntegerField( + default=5, + help_text='Warn when a sample has this many stored aliquots or fewer', + ) + low_count_alert = models.PositiveIntegerField( + default=3, + help_text='Alert when a sample has this many stored aliquots or fewer', + ) updated_at = models.DateTimeField(auto_now=True) updated_by = models.ForeignKey( User, diff --git a/krill/reports/README.md b/krill/reports/README.md index 2bf9e3e..9c7ca89 100644 --- a/krill/reports/README.md +++ b/krill/reports/README.md @@ -55,9 +55,11 @@ The pill in the Dashboard header ("All clear", or "N need attention", amber for - **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). +- **Low sample counts:** a sample with few stored aliquots left. It is an alert at the alert level or fewer and a warning at the warn level or fewer (lab default: warn at 5, alert at 3; a lab admin sets these in **Settings > Low-count alerts**). A sample's count is its aliquots whose disposition is Stored. A box position is not needed, and exhausted, in-use, disposed and deleted aliquots do not count. A sample with no aliquots at all is not checked. A sample can have its own levels, or switch the check off, in its **Edit** form (fields **Low-count alerts**, **Warn at**, **Alert at**). The Samples list shows each sample's stored count with a badge, and has a **Low count** filter. -**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. +The two consumables sources only appear when Consumables are turned on in Settings; low sample counts always appear. 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 or a sample that is deliberately kept low. The switch lives on the record itself: a consumable's `alerts_silenced` (which also takes it off the Low Stock and Expiring Soon tiles, their filtered lists and the reorder list), or a sample's low-count check being switched off. 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. diff --git a/krill/reports/attention.py b/krill/reports/attention.py index b88d8b0..83c1260 100644 --- a/krill/reports/attention.py +++ b/krill/reports/attention.py @@ -5,9 +5,10 @@ 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. +An item is silenced by a flag on the record it is about (Consumable.alerts_silenced, or a sample's +low_count_check being off), 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 @@ -126,6 +127,34 @@ def expiring_items(): return items +# --- low sample counts ------------------------------------------------------- + +def low_count_items(): + from sample import low_count + items = [] + for row in low_count.low_samples(): + sample, count = row['sample'], row['count'] + left = 'No stored aliquots left' if count == 0 else f'{count} stored aliquot{"s" if count != 1 else ""} left' + items.append(AttentionItem( + source='low_count', + key=f'low_count:sample:{sample.pk}', + title=sample.name, + detail=f'{left} (alert at {row["alert"]} or fewer, warn at {row["warn"]} or fewer)', + level=LEVEL_ALERT if row['state'] == low_count.ALERT else LEVEL_WARN, + url=reverse('sample:detail', kwargs={'type': 'sample', 'pk': sample.pk}), + silenced=row['silenced'], + target_type='sample', + target_id=sample.pk, + )) + return items + + +def _silence_sample(target_id, silenced): + from sample.models.sample import Sample + # Silenced means the low-count check is switched off for this sample + return bool(Sample.objects.filter(pk=target_id).update(low_count_check=not silenced)) + + 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)) @@ -134,6 +163,8 @@ def _silence_consumable(target_id, 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) +register_source('low_count', 'Low sample counts', 'science', low_count_items) +register_silencer('sample', _silence_sample) # --- reading it -------------------------------------------------------------- diff --git a/krill/reports/test_attention.py b/krill/reports/test_attention.py index 0ef60f6..bca685f 100644 --- a/krill/reports/test_attention.py +++ b/krill/reports/test_attention.py @@ -117,9 +117,16 @@ def test_all_clear(self): 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(), []) + # Only the sources that do not depend on Consumables remain + self.assertEqual([g['source'].name for g in attention.groups()], ['low_count']) self.assertEqual(attention.summary(), {'count': 0, 'level': None}) + def test_no_groups_at_all_when_no_source_is_available(self): + from unittest.mock import patch + with patch.object(attention, 'SOURCES', []): + self.assertEqual(attention.groups(), []) + self.assertEqual(attention.summary(), {'count': 0, 'level': None}) + class SilenceTest(AttentionFixture): def test_silence_and_unsilence_a_consumable(self): @@ -172,9 +179,18 @@ def test_all_clear_and_empty_groups(self): self.assertContains(response, 'Nothing needs attention.') def test_explains_when_nothing_is_watched(self): + from unittest.mock import patch + self.client.force_login(self.user) + with patch.object(attention, 'SOURCES', []): + self.assertContains(self.client.get(self.url), 'Nothing is being watched yet.') + + def test_consumable_groups_are_absent_when_the_feature_is_off(self): self.set_consumables(False) self.client.force_login(self.user) - self.assertContains(self.client.get(self.url), 'Nothing is being watched yet.') + response = self.client.get(self.url) + self.assertContains(response, 'Low sample counts (0)') + self.assertNotContains(response, 'Low stock (') + self.assertNotContains(response, 'Expiring soon (') def test_silenced_items_are_displayed_separately(self): self.consumable('Quiet', alerts_silenced=True) diff --git a/krill/sample/forms.py b/krill/sample/forms.py index efc98df..4c0c06c 100644 --- a/krill/sample/forms.py +++ b/krill/sample/forms.py @@ -6,7 +6,15 @@ class SampleForm(forms.ModelForm): class Meta: model = Sample - fields = ['name', 'experiment', 'source', 'notes', 'access_level'] + fields = [ + 'name', 'experiment', 'source', 'notes', 'access_level', + 'low_count_check', 'low_count_warn', 'low_count_alert', + ] + labels = { + 'low_count_check': 'Low-count alerts', + 'low_count_warn': 'Warn at (stored aliquots)', + 'low_count_alert': 'Alert at (stored aliquots)', + } help_texts = { 'name': 'Unique identifier for this sample (e.g., cell line name)', 'experiment': 'Experiment identifier or number associated with this sample', @@ -18,6 +26,13 @@ class Meta: 'notes': forms.Textarea(attrs={'rows': 4}), } + def clean(self): + cleaned = super().clean() + warn, alert = cleaned.get('low_count_warn'), cleaned.get('low_count_alert') + if warn is not None and alert is not None and alert > warn: + self.add_error('low_count_alert', 'The alert level cannot be higher than the warn level.') + return cleaned + class AliquotForm(forms.ModelForm): # count: number of aliquots to create at once (not a model field) count = forms.IntegerField( diff --git a/krill/sample/low_count.py b/krill/sample/low_count.py new file mode 100644 index 0000000..8cd4cb4 --- /dev/null +++ b/krill/sample/low_count.py @@ -0,0 +1,65 @@ +"""Low-count alerts: warn when a sample has few stored aliquots left. + +A sample's count is its aliquots whose disposition is Stored (no box position needed; soft-deleted +aliquots are ignored). It is **alert** at or below the alert level and **warn** at or below the warn +level. The levels come from the lab default (SiteConfiguration, set in Settings) unless the sample +has its own. A sample with no aliquots at all is not checked, and a sample can opt out with +Sample.low_count_check. +""" +from typing import Optional + +from person.models import SiteConfiguration + +ALERT = 'alert' +WARN = 'warn' + + +def levels_for(sample, config=None): + """(warn, alert) for a sample. The alert level is never above the warn level.""" + config = config or SiteConfiguration.load() + warn = config.low_count_warn if sample.low_count_warn is None else sample.low_count_warn + alert = config.low_count_alert if sample.low_count_alert is None else sample.low_count_alert + return warn, min(alert, warn) + + +def state_for_count(count, warn, alert) -> Optional[str]: + if count <= alert: + return ALERT + if count <= warn: + return WARN + return None + + +def state_for(sample, config=None) -> Optional[str]: + """'alert', 'warn' or None for a sample annotated by Sample.objects.with_stored_count(). + + Ignores the opt-out switch, so a silenced sample still shows how low it is. + """ + if not sample.aliquot_total: + return None + warn, alert = levels_for(sample, config) + return state_for_count(sample.stored_count, warn, alert) + + +def low_samples(): + """Every sample that is at or below its warn level, as dicts with its counts and levels. + + Samples that opted out are included with silenced=True; callers decide what to do with them. + """ + from .models.sample import Sample + + config = SiteConfiguration.load() + rows = [] + for sample in Sample.objects.with_stored_count().filter(aliquot_total__gt=0).order_by('name', 'id'): + warn, alert = levels_for(sample, config) + state = state_for_count(sample.stored_count, warn, alert) + if state: + rows.append({ + 'sample': sample, + 'count': sample.stored_count, + 'warn': warn, + 'alert': alert, + 'state': state, + 'silenced': not sample.low_count_check, + }) + return rows diff --git a/krill/sample/migrations/0009_low_count_levels.py b/krill/sample/migrations/0009_low_count_levels.py new file mode 100644 index 0000000..4f474b7 --- /dev/null +++ b/krill/sample/migrations/0009_low_count_levels.py @@ -0,0 +1,28 @@ +# Generated by Django 6.1.3 on 2026-11-11 17:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('sample', '0008_aliquot_checked_out_by_at'), + ] + + operations = [ + migrations.AddField( + model_name='sample', + name='low_count_alert', + field=models.PositiveIntegerField(blank=True, help_text='Alert at this many stored aliquots or fewer. Leave blank to use the lab default.', null=True), + ), + migrations.AddField( + model_name='sample', + name='low_count_check', + field=models.BooleanField(default=True, help_text='Warn when few stored aliquots are left. Turn off for a sample that is deliberately kept low.'), + ), + migrations.AddField( + model_name='sample', + name='low_count_warn', + field=models.PositiveIntegerField(blank=True, help_text='Warn at this many stored aliquots or fewer. Leave blank to use the lab default.', null=True), + ), + ] diff --git a/krill/sample/models/sample.py b/krill/sample/models/sample.py index c5d18b3..c86014a 100644 --- a/krill/sample/models/sample.py +++ b/krill/sample/models/sample.py @@ -1,6 +1,24 @@ from django.db import models +from django.db.models import Count, Q from storage.models import Box + +class SampleQuerySet(models.QuerySet): + def with_stored_count(self): + """Add stored_count (aliquots whose disposition is Stored) and aliquot_total (all aliquots). + + Soft-deleted aliquots are left out of both. A box position is not required: many stored + aliquots have not been given one. + """ + return self.annotate( + stored_count=Count( + 'aliquots', + filter=Q(aliquots__deleted=False, aliquots__disposition__disposition_type='stored'), + ), + aliquot_total=Count('aliquots', filter=Q(aliquots__deleted=False)), + ) + + class Sample(models.Model): ACCESS_LEVEL_CHOICES = [ ('admins_only', 'Admins Only'), @@ -19,6 +37,23 @@ class Sample(models.Model): help_text="Restrict access to specific user tiers" ) + low_count_check = models.BooleanField( + default=True, + help_text='Warn when few stored aliquots are left. Turn off for a sample that is deliberately kept low.', + ) + low_count_warn = models.PositiveIntegerField( + null=True, + blank=True, + help_text='Warn at this many stored aliquots or fewer. Leave blank to use the lab default.', + ) + low_count_alert = models.PositiveIntegerField( + null=True, + blank=True, + help_text='Alert at this many stored aliquots or fewer. Leave blank to use the lab default.', + ) + + objects = SampleQuerySet.as_manager() + class Meta: ordering = ['name'] diff --git a/krill/sample/tests/test_dashboard_sections.py b/krill/sample/tests/test_dashboard_sections.py index 8dad199..9f128f1 100644 --- a/krill/sample/tests/test_dashboard_sections.py +++ b/krill/sample/tests/test_dashboard_sections.py @@ -7,6 +7,7 @@ from consumables.models.consumable import Consumable from consumables.models.consumable_type import ConsumableType +from ..models.sample import Sample from person.models import SiteConfiguration from ..models.aliquot import Aliquot, AliquotDisposition from .test_views import SampleViewTest @@ -100,6 +101,8 @@ def setUp(self): config = SiteConfiguration.load() config.consumables_enabled = True config.save() + # The fixture's sample has one stored aliquot, which would count as low; these tests are about consumables + Sample.objects.update(low_count_check=False) def _consumable(self, quantity='3', threshold='5', **kwargs): return Consumable.objects.create( diff --git a/krill/sample/tests/test_lineage_and_remaining.py b/krill/sample/tests/test_lineage_and_remaining.py index 515dedc..d3b1a14 100644 --- a/krill/sample/tests/test_lineage_and_remaining.py +++ b/krill/sample/tests/test_lineage_and_remaining.py @@ -95,8 +95,8 @@ def setUp(self): def _disp(self, name, kind): return AliquotDisposition.objects.get_or_create(name=name, defaults={'disposition_type': kind})[0] - def test_lists_only_stored_aliquots_in_a_box(self): - # self.aliquot is 'stored' but has no box position, so it is not "remaining" + def test_lists_every_stored_aliquot_whether_or_not_it_has_a_box_position(self): + # self.aliquot is 'stored' with no box position: it still counts as remaining placed = Aliquot.objects.create( sample=self.sample, aliquot_type=self.aliquot_type, disposition=self.stored_disposition, passage='p8', sample_date=datetime.date(2024, 3, 5), @@ -110,15 +110,27 @@ def test_lists_only_stored_aliquots_in_a_box(self): other.store_in_location(self.box, 1, 3) response = self.client.get(self.url) - self.assertEqual(response.context['remaining_aliquots'], [placed]) - self.assertEqual(response.context['remaining_count'], 1) - self.assertContains(response, 'Remaining aliquots (1)') + self.assertCountEqual(response.context['remaining_aliquots'], [placed, self.aliquot]) + self.assertEqual(response.context['remaining_count'], 2) + self.assertContains(response, 'Remaining aliquots (2)') for text in ('p8', '2024-03-05', 'Test Type', 'Test Device', 'Test Box', '(1, 2)'): self.assertContains(response, text) - for gone in (in_use, exhausted, disposed, other, self.aliquot): + for gone in (in_use, exhausted, disposed, other): self.assertNotIn(gone, response.context['remaining_aliquots']) + def test_aliquot_without_a_position_shows_a_dash_for_its_location(self): + response = self.client.get(self.url) + self.assertEqual(response.context['remaining_aliquots'], [self.aliquot]) + self.assertRegex(response.content.decode(), r'—\s*') + + def test_soft_deleted_aliquots_are_not_remaining(self): + self.aliquot.deleted = True + self.aliquot.save() + self.assertEqual(self.client.get(self.url).context['remaining_aliquots'], []) + def test_no_stored_aliquots_shows_zero_and_message(self): + self.aliquot.disposition = self._disp('Used', 'exhausted') + self.aliquot.save() response = self.client.get(self.url) self.assertEqual(response.context['remaining_count'], 0) self.assertContains(response, 'Remaining aliquots (0)') diff --git a/krill/sample/tests/test_low_count.py b/krill/sample/tests/test_low_count.py new file mode 100644 index 0000000..59cdefd --- /dev/null +++ b/krill/sample/tests/test_low_count.py @@ -0,0 +1,416 @@ +from django.contrib.auth import get_user_model +from django.urls import reverse + +from person.models import SiteConfiguration, UserAuditLog +from reports import attention +from .. import low_count +from ..forms import SampleForm +from ..models.aliquot import Aliquot, AliquotDisposition +from ..models.sample import Sample +from .test_views import SampleViewTest + +User = get_user_model() + + +class LowCountFixture(SampleViewTest): + """The base fixture's 'Test Sample' has one stored aliquot; start each test from a clean slate.""" + + def setUp(self): + super().setUp() + Aliquot.objects.all().delete() + self.in_use = self.disp('In Use', 'in_use') + self.exhausted = self.disp('Used', 'exhausted') + self.disposed = self.disp('Disposed', 'disposed') + + def disp(self, name, kind): + return AliquotDisposition.objects.get_or_create(name=name, defaults={'disposition_type': kind})[0] + + def stored(self, sample, n): + return [Aliquot.objects.create(sample=sample, disposition=self.stored_disposition) for _ in range(n)] + + def make_sample(self, name, **kwargs): + return Sample.objects.create(name=name, source=self.source, **kwargs) + + def annotated(self, sample): + return Sample.objects.with_stored_count().get(pk=sample.pk) + + def login_as(self, role): + user = User.objects.create_user(username=f'{role}-user', password='x') + user.role.role = role + user.role.save() + self.client.force_login(user) + return user + + +class CountingRulesTest(LowCountFixture): + def test_only_the_stored_disposition_counts(self): + self.stored(self.sample, 2) + Aliquot.objects.create(sample=self.sample, disposition=self.in_use) + Aliquot.objects.create(sample=self.sample, disposition=self.exhausted) + Aliquot.objects.create(sample=self.sample, disposition=self.disposed) + counted = self.annotated(self.sample) + self.assertEqual((counted.stored_count, counted.aliquot_total), (2, 5)) + + def test_a_box_position_is_not_required(self): + placed, unplaced = self.stored(self.sample, 2) + placed.store_in_location(self.box, 1, 1) + self.assertEqual(self.annotated(self.sample).stored_count, 2) + + def test_soft_deleted_aliquots_are_ignored(self): + kept, gone = self.stored(self.sample, 2) + gone.deleted = True + gone.save() + counted = self.annotated(self.sample) + self.assertEqual((counted.stored_count, counted.aliquot_total), (1, 1)) + + def test_counts_do_not_leak_between_samples(self): + other = self.make_sample('Other') + self.stored(self.sample, 3) + self.stored(other, 7) + self.assertEqual(self.annotated(self.sample).stored_count, 3) + self.assertEqual(self.annotated(other).stored_count, 7) + + def test_a_sample_with_no_aliquots_at_all_is_not_checked(self): + counted = self.annotated(self.sample) + self.assertEqual(counted.aliquot_total, 0) + self.assertIsNone(low_count.state_for(counted)) + self.assertEqual(low_count.low_samples(), []) + + def test_a_sample_whose_aliquots_are_all_used_up_is_an_alert_at_zero(self): + Aliquot.objects.create(sample=self.sample, disposition=self.exhausted) + counted = self.annotated(self.sample) + self.assertEqual(counted.stored_count, 0) + self.assertEqual(low_count.state_for(counted), 'alert') + + +class LevelsAndBoundariesTest(LowCountFixture): + def test_lab_defaults_are_warn_5_alert_3(self): + config = SiteConfiguration.load() + self.assertEqual((config.low_count_warn, config.low_count_alert), (5, 3)) + self.assertEqual(low_count.levels_for(self.sample), (5, 3)) + + def test_alert_at_or_below_the_alert_level_and_warn_at_or_below_the_warn_level(self): + expected = {0: 'alert', 3: 'alert', 4: 'warn', 5: 'warn', 6: None, 20: None} + for count, state in expected.items(): + with self.subTest(count=count): + self.assertEqual(low_count.state_for_count(count, 5, 3), state) + + def test_changing_the_lab_defaults_changes_the_result(self): + self.stored(self.sample, 5) + self.assertEqual(low_count.state_for(self.annotated(self.sample)), 'warn') + config = SiteConfiguration.load() + config.low_count_warn, config.low_count_alert = 6, 5 + config.save() + self.assertEqual(low_count.state_for(self.annotated(self.sample)), 'alert') + + def test_a_sample_can_override_either_level(self): + sample = self.make_sample('Own levels', low_count_warn=10, low_count_alert=8) + self.assertEqual(low_count.levels_for(sample), (10, 8)) + only_warn = self.make_sample('Only warn', low_count_warn=2) + self.assertEqual(low_count.levels_for(only_warn), (2, 2)) # default alert 3 is capped to the warn level + + def test_override_applies_to_the_state(self): + sample = self.make_sample('Relaxed', low_count_warn=1, low_count_alert=0) + self.stored(sample, 3) + self.assertIsNone(low_count.state_for(self.annotated(sample))) # 3 is above its warn level of 1 + + +class LowSamplesTest(LowCountFixture): + def test_lists_only_low_samples_with_their_counts_and_levels(self): + low = self.make_sample('Low') + fine = self.make_sample('Fine') + self.stored(low, 2) + self.stored(fine, 9) + [row] = low_count.low_samples() + self.assertEqual((row['sample'], row['count'], row['warn'], row['alert'], row['state'], row['silenced']), + (low, 2, 5, 3, 'alert', False)) + + def test_opted_out_samples_are_listed_as_silenced(self): + quiet = self.make_sample('Quiet', low_count_check=False) + self.stored(quiet, 1) + [row] = low_count.low_samples() + self.assertTrue(row['silenced']) + + +class AttentionSourceTest(LowCountFixture): + def _items(self): + return attention.low_count_items() + + def test_item_fields(self): + self.stored(self.sample, 4) + [item] = self._items() + self.assertEqual(item.key, f'low_count:sample:{self.sample.pk}') + self.assertEqual(item.title, 'Test Sample') + self.assertEqual(item.detail, '4 stored aliquots left (alert at 3 or fewer, warn at 5 or fewer)') + self.assertEqual(item.level, 'warn') + self.assertEqual(item.url, reverse('sample:detail', kwargs={'type': 'sample', 'pk': self.sample.pk})) + self.assertEqual((item.target_type, item.target_id, item.silenced), ('sample', self.sample.pk, False)) + + def test_wording_for_one_and_for_none(self): + self.stored(self.sample, 1) + self.assertIn('1 stored aliquot left', self._items()[0].detail) + other = self.make_sample('Empty') + Aliquot.objects.create(sample=other, disposition=self.exhausted) + details = {i.title: i.detail for i in self._items()} + self.assertTrue(details['Empty'].startswith('No stored aliquots left')) + + def test_levels(self): + low, mid = self.make_sample('Low'), self.make_sample('Mid') + self.stored(low, 3) + self.stored(mid, 5) + levels = {i.title: i.level for i in self._items()} + self.assertEqual(levels, {'Low': 'alert', 'Mid': 'warn'}) + + def test_silenced_when_the_check_is_off_and_not_counted(self): + self.stored(self.sample, 2) + self.sample.low_count_check = False + self.sample.save() + self.assertTrue(self._items()[0].silenced) + self.assertEqual(attention.summary(), {'count': 0, 'level': None}) + + def test_summary_counts_unsilenced_low_samples_and_takes_the_worst_level(self): + a, b = self.make_sample('A'), self.make_sample('B') + self.stored(a, 5) + self.stored(b, 1) + self.assertEqual(attention.summary(), {'count': 2, 'level': 'alert'}) + + def test_silence_and_unsilence_via_the_shared_helper(self): + self.stored(self.sample, 2) + self.assertTrue(attention.silence('sample', self.sample.pk, True)) + self.sample.refresh_from_db() + self.assertFalse(self.sample.low_count_check) + self.assertTrue(attention.silence('sample', self.sample.pk, False)) + self.sample.refresh_from_db() + self.assertTrue(self.sample.low_count_check) + self.assertFalse(attention.silence('sample', 99999, True)) + + def test_overview_page_group_item_and_silence_flow(self): + self.stored(self.sample, 2) + self.login_as('lab_manager') + page = self.client.get(reverse('reports:alert_list')) + self.assertContains(page, 'Low sample counts (1)') + self.assertContains(page, 'Test Sample') + self.assertContains(page, 'name="target_type" value="sample"') + response = self.client.post(reverse('reports:silence_attention'), + {'target_type': 'sample', 'target_id': self.sample.pk, 'silenced': '1'}) + self.assertRedirects(response, reverse('reports:alert_list'), fetch_redirect_response=False) + self.sample.refresh_from_db() + self.assertFalse(self.sample.low_count_check) + after = self.client.get(reverse('reports:alert_list')) + self.assertContains(after, 'Low sample counts (0)') + self.assertContains(after, 'Silenced (1)') + self.assertTrue(UserAuditLog.objects.filter(action='alerts_silenced', target_type='Sample').exists()) + + def test_pill_counts_low_samples(self): + self.stored(self.sample, 2) + self.client.force_login(self.user) + response = self.client.get(reverse('home')) + self.assertEqual(response.context['attention'], {'count': 1, 'level': 'alert'}) + self.assertContains(response, 'class="header-alerts level-alert"') + + +class SamplesListTest(LowCountFixture): + def setUp(self): + super().setUp() + self.client.force_login(self.user) + self.url = reverse('sample:sample_list') + + def test_stored_column_and_badges(self): + alert, warn, fine, quiet = (self.make_sample(n) for n in ('Alert', 'Warn', 'Fine', 'Quiet')) + quiet.low_count_check = False + quiet.save() + for sample, n in ((alert, 2), (warn, 5), (fine, 12), (quiet, 1)): + self.stored(sample, n) + response = self.client.get(self.url) + html = response.content.decode() + self.assertIn('') + self.assertNotIn('low_count', on.context['low_count_toggle_url']) + self.assertIn('q=Low', on.context['low_count_toggle_url']) + self.assertNotIn('page=', on.context['low_count_toggle_url']) + + def test_empty_message_when_nothing_is_low(self): + self.stored(self.make_sample('Fine'), 12) + self.assertContains(self.client.get(self.url, {'low_count': '1'}), 'No samples are running low.') + + def test_sort_by_stored_count(self): + few, many = self.make_sample('Few'), self.make_sample('Many') + self.stored(few, 1) + self.stored(many, 8) + asc = [s.name for s in self.client.get(self.url, {'sort': 'stored', 'order': 'asc'}).context['items']] + desc = [s.name for s in self.client.get(self.url, {'sort': 'stored', 'order': 'desc'}).context['items']] + self.assertEqual(asc, ['Test Sample', 'Few', 'Many']) + self.assertEqual(desc, ['Many', 'Few', 'Test Sample']) + + def test_other_list_types_are_unaffected(self): + self.assertEqual(self.client.get(self.url, {'type': 'source'}).status_code, 200) + self.assertNotContains(self.client.get(self.url, {'type': 'aliquot'}), 'Low count ·') + + +class SamplePageTest(LowCountFixture): + def setUp(self): + super().setUp() + self.client.force_login(self.user) + self.url = reverse('sample:detail', kwargs={'type': 'sample', 'pk': self.sample.pk}) + + def test_shows_count_badge_and_levels(self): + self.stored(self.sample, 4) + response = self.client.get(self.url) + self.assertEqual(response.context['low_count'], + {'count': 4, 'warn': 5, 'alert': 3, 'state': 'warn', 'enabled': True}) + self.assertContains(response, 'Stored aliquots:') + self.assertContains(response, 'class="low-badge warn"') + self.assertContains(response, 'On: warn at 5 or fewer, alert at 3 or fewer') + + def test_no_badge_when_comfortably_stocked(self): + self.stored(self.sample, 10) + self.assertNotContains(self.client.get(self.url), 'class="low-badge') + + def test_opted_out_sample_says_so_and_shows_no_badge(self): + self.stored(self.sample, 1) + self.sample.low_count_check = False + self.sample.save() + response = self.client.get(self.url) + self.assertContains(response, 'Off for this sample') + self.assertNotContains(response, 'class="low-badge') + + def test_count_matches_the_remaining_list(self): + self.stored(self.sample, 3) + response = self.client.get(self.url) + self.assertEqual(response.context['low_count']['count'], response.context['remaining_count']) + + +class SampleFormTest(LowCountFixture): + def _form(self, **extra): + data = {'name': 'S', 'source': self.source.pk, 'access_level': 'all_members', + 'low_count_check': 'on', **extra} + return SampleForm(data) + + def test_overrides_are_optional(self): + self.assertTrue(self._form().is_valid()) + + def test_valid_overrides_are_saved(self): + form = self._form(low_count_warn='8', low_count_alert='4') + self.assertTrue(form.is_valid(), form.errors) + sample = form.save() + self.assertEqual((sample.low_count_warn, sample.low_count_alert), (8, 4)) + + def test_alert_above_warn_is_rejected(self): + form = self._form(low_count_warn='3', low_count_alert='5') + self.assertFalse(form.is_valid()) + self.assertIn('low_count_alert', form.errors) + + def test_negative_numbers_are_rejected(self): + self.assertFalse(self._form(low_count_warn='-1').is_valid()) + + def test_unticking_turns_the_check_off(self): + data = {'name': 'S', 'source': self.source.pk, 'access_level': 'all_members'} + form = SampleForm(data) + self.assertTrue(form.is_valid()) + self.assertFalse(form.save().low_count_check) + + def test_editing_a_sample_through_its_page_saves_the_low_count_fields(self): + self.client.force_login(self.user) + url = reverse('sample:detail', kwargs={'type': 'sample', 'pk': self.sample.pk}) + self.client.post(url, {'name': 'Test Sample', 'source': self.source.pk, 'access_level': 'all_members', + 'low_count_warn': '9', 'low_count_alert': '2'}) + self.sample.refresh_from_db() + self.assertEqual((self.sample.low_count_warn, self.sample.low_count_alert, self.sample.low_count_check), + (9, 2, False)) + + +class SettingsLevelsTest(LowCountFixture): + def setUp(self): + super().setUp() + self.url = reverse('settings') + + def _post(self, warn, alert, **extra): + return self.client.post(self.url, {'form': 'alert_levels', 'low_count_warn': warn, + 'low_count_alert': alert, **extra}) + + def test_admin_sees_the_form_with_current_values(self): + self.login_as('lab_admin') + response = self.client.get(self.url) + self.assertContains(response, 'id="alert-levels-form"') + self.assertContains(response, 'name="low_count_warn"') + self.assertContains(response, 'value="5"') + + def test_members_do_not_see_the_form(self): + self.client.force_login(self.user) + self.assertNotContains(self.client.get(self.url), 'id="alert-levels-form"') + + def test_admin_saves_new_levels_and_it_is_logged_and_shown(self): + admin = self.login_as('lab_admin') + response = self._post('6', '4') + self.assertRedirects(response, self.url, fetch_redirect_response=False) + config = SiteConfiguration.load() + self.assertEqual((config.low_count_warn, config.low_count_alert, config.updated_by), (6, 4, admin)) + self.assertTrue(UserAuditLog.objects.filter(user=admin, target_type='SiteConfiguration').exists()) + self.assertContains(self.client.get(self.url), 'Low-count levels saved: warn at 6 or fewer, alert at 4 or fewer.') + + def test_saving_levels_does_not_touch_the_feature_switches(self): + config = SiteConfiguration.load() + config.consumables_enabled = True + config.consumables_ordering_enabled = True + config.save() + self.login_as('lab_admin') + self._post('7', '2') + config.refresh_from_db() + self.assertTrue(config.consumables_enabled and config.consumables_ordering_enabled) + + def test_saving_the_feature_switches_does_not_touch_the_levels(self): + config = SiteConfiguration.load() + config.low_count_warn, config.low_count_alert = 9, 6 + config.save() + self.login_as('lab_admin') + self.client.post(self.url, {'consumables_enabled': 'on'}) + config.refresh_from_db() + self.assertEqual((config.low_count_warn, config.low_count_alert, config.consumables_enabled), (9, 6, True)) + + def test_invalid_levels_are_refused_with_a_message(self): + self.login_as('lab_admin') + for warn, alert, message in (('3', '5', 'cannot be higher'), ('x', '1', 'whole numbers'), + ('-1', '1', 'whole numbers'), ('', '', 'whole numbers')): + with self.subTest(warn=warn, alert=alert): + self._post(warn, alert) + self.assertContains(self.client.get(self.url), message) + config = SiteConfiguration.load() + self.assertEqual((config.low_count_warn, config.low_count_alert), (5, 3)) + + def test_non_admins_cannot_change_the_levels(self): + self.login_as('lab_manager') + self.assertEqual(self._post('9', '8').status_code, 403) + self.assertEqual(SiteConfiguration.load().low_count_warn, 5) diff --git a/krill/sample/views/detail.py b/krill/sample/views/detail.py index 9ddaf30..6d2f0ce 100644 --- a/krill/sample/views/detail.py +++ b/krill/sample/views/detail.py @@ -11,16 +11,20 @@ from ..models.source import Source from ..forms import SampleForm, AliquotForm, AliquotTypeForm, SourceForm, AliquotMoveForm from ..models.aliquot import AliquotDisposition -from person.models import UserAuditLog +from person.models import SiteConfiguration, UserAuditLog +from .. import low_count from .next_url import safe_next def remaining_aliquots(sample): - """Aliquots of a sample still in the freezer: stored disposition and a box position.""" + """The aliquots of a sample that are still stored (disposition Stored; a box position is not required). + + This is the same count the low-count alerts use. + """ return ( Aliquot.objects.filter( sample=sample, + deleted=False, disposition__disposition_type='stored', - location__isnull=False, ) .select_related( 'aliquot_type', @@ -62,6 +66,16 @@ def get_context_data(self, **kwargs): remaining = list(remaining_aliquots(self.object)) context['remaining_aliquots'] = remaining context['remaining_count'] = len(remaining) + counted = Sample.objects.with_stored_count().get(pk=self.object.pk) + config = SiteConfiguration.load() + warn, alert = low_count.levels_for(counted, config) + context['low_count'] = { + 'count': counted.stored_count, + 'warn': warn, + 'alert': alert, + 'state': low_count.state_for(counted, config), + 'enabled': counted.low_count_check, + } # Add storage location information for aliquots if self.kwargs.get('type') == 'aliquot': context['children'] = self.object.children.select_related('aliquot_type', 'disposition') diff --git a/krill/sample/views/list.py b/krill/sample/views/list.py index e173802..aea98d3 100644 --- a/krill/sample/views/list.py +++ b/krill/sample/views/list.py @@ -1,11 +1,12 @@ from django.views.generic import ListView from django.contrib.auth.mixins import LoginRequiredMixin -from django.db.models import CharField, F, Q -from django.db.models.functions import Cast +from django.db.models import CharField, F, Q, Value +from django.db.models.functions import Cast, Coalesce +from .. import low_count from ..models.sample import Sample from ..models.aliquot import Aliquot, AliquotType, AliquotDisposition, AliquotLocation from ..models.source import Source -from person.models import UserRole +from person.models import SiteConfiguration, UserRole class SampleListView(LoginRequiredMixin, ListView): template_name = 'sample/list.html' @@ -27,7 +28,7 @@ def get_queryset(self): elif model_type == 'source': queryset = Source.objects.all() else: - queryset = Sample.objects.select_related('source') + queryset = Sample.objects.select_related('source').with_stored_count() # Text search search_query = self.request.GET.get('q', '') @@ -37,6 +38,15 @@ def get_queryset(self): elif model_type in ('sample', 'source', 'aliquot-type'): queryset = queryset.filter(name__icontains=search_query) + # Samples running low on stored aliquots (and not opted out of the check) + if model_type == 'sample' and self.request.GET.get('low_count') == '1': + warn_default = SiteConfiguration.load().low_count_warn + queryset = ( + queryset.filter(aliquot_total__gt=0, low_count_check=True) + .annotate(warn_level=Coalesce('low_count_warn', Value(warn_default))) + .filter(stored_count__lte=F('warn_level')) + ) + # Aliquot-specific filters if model_type == 'aliquot': disposition_filter = self.request.GET.get('disposition', '') @@ -98,6 +108,8 @@ def get_queryset(self): queryset = queryset.order_by(f'{sort_prefix}passage', 'id') elif model_type == 'aliquot' and sort_by == 'location': queryset = queryset.order_by(f'{sort_prefix}location__box__rack__shelf__device__name', 'id') + elif model_type == 'sample' and sort_by == 'stored': + queryset = queryset.order_by(f'{sort_prefix}stored_count', 'name', 'id') elif model_type == 'sample' and sort_by == 'source': queryset = queryset.order_by(f'{sort_prefix}source__name', 'id') elif hasattr(queryset.model, sort_by): @@ -178,6 +190,16 @@ def get_context_data(self, **kwargs): else: context['model_name'] = 'Samples' + if model_type == 'sample': + config = SiteConfiguration.load() + items = list(context['items']) + for sample in items: + sample.low_state = low_count.state_for(sample, config) + context['items'] = items + context['low_count_on'] = self.request.GET.get('low_count') == '1' + context['low_count_toggle_url'] = self._toggle_url('low_count', '1') + context['low_count_pill_count'] = sum(1 for row in low_count.low_samples() if not row['silenced']) + context['search_query'] = self.request.GET.get('q', '') context['sort_by'] = self.request.GET.get('sort', 'name') context['sort_order'] = self.request.GET.get('order', 'asc') diff --git a/krill/static/krill/style.css b/krill/static/krill/style.css index 6c6326e..8f76829 100644 --- a/krill/static/krill/style.css +++ b/krill/static/krill/style.css @@ -2697,6 +2697,30 @@ main table tbody tr:last-child td{ margin: 0; } +.low-badge { + display: inline-block; + margin-left: 0.4rem; + padding: 2px 8px; + border-radius: 12px; + font-size: 0.75rem; + font-weight: 600; +} + +.low-badge.alert { + background: rgba(255, 0, 96, 0.2); + color: var(--color-danger); +} + +.low-badge.warn { + background: rgba(247, 208, 96, 0.2); + color: var(--color-warning); +} + +.low-badge.silenced { + background: var(--color-light); + color: var(--color-dark-variant); +} + .toggle-pills { display: flex; flex-wrap: wrap; diff --git a/krill/templates/krill/settings.html b/krill/templates/krill/settings.html index ae7d099..1312e5f 100644 --- a/krill/templates/krill/settings.html +++ b/krill/templates/krill/settings.html @@ -12,6 +12,10 @@

Settings

Manage your application preferences and configurations

+ {% for message in messages %} +

{{ message }}

+ {% endfor %} +
@@ -93,6 +97,40 @@

+ + +
+

+ notifications_active + Low-count alerts +

+
+
+ {% csrf_token %} + +

+ A sample is flagged when it has this many stored aliquots or fewer. A sample can have its own levels, + or switch the check off, on its page. +

+
+ + +
+
+ + +
+
+ +
+
+
+
{% endif %} diff --git a/krill/templates/sample/_sample_remaining_aliquots.html b/krill/templates/sample/_sample_remaining_aliquots.html index 5f678f8..e1baea0 100644 --- a/krill/templates/sample/_sample_remaining_aliquots.html +++ b/krill/templates/sample/_sample_remaining_aliquots.html @@ -13,7 +13,7 @@

inventory_2 Remaining aliquots ({{ remai {{ a.aliquot_type.name|default:"—" }} {{ a.passage|default:"—" }} {{ a.sample_date|date:"Y-m-d"|default:"—" }} - {{ a.location.box.rack.shelf.device.name }} › {{ a.location.box.name }} › ({{ a.location.row }}, {{ a.location.column }}) + {% if a.location %}{{ a.location.box.rack.shelf.device.name }} › {{ a.location.box.name }} › ({{ a.location.row }}, {{ a.location.column }}){% else %}—{% endif %} {% endfor %} diff --git a/krill/templates/sample/detail.html b/krill/templates/sample/detail.html index 32892b9..967a1e6 100644 --- a/krill/templates/sample/detail.html +++ b/krill/templates/sample/detail.html @@ -95,6 +95,15 @@

location_on Storage Location

{{ object.notes|default:"No notes" }} +
+ + {{ low_count.count }}{% if low_count.state and low_count.enabled %} + {% if low_count.state == 'alert' %}Alert{% else %}Low{% endif %}{% endif %} +
+
+ + {% if low_count.enabled %}On: warn at {{ low_count.warn }} or fewer, alert at {{ low_count.alert }} or fewer{% else %}Off for this sample{% endif %} +
diff --git a/krill/templates/sample/list.html b/krill/templates/sample/list.html index d711355..89ae77c 100644 --- a/krill/templates/sample/list.html +++ b/krill/templates/sample/list.html @@ -103,6 +103,7 @@

{{ model_name }}

{% if request.GET.type %} {% endif %} + {% if low_count_on %}{% endif %}
@@ -119,6 +120,7 @@

{{ model_name }}

{% elif model_name == 'Samples' %} + {% endif %} @@ -146,6 +148,15 @@

{{ model_name }}

+ {% if model_type == 'sample' %} + + {% endif %}
{% endif %} @@ -245,6 +256,7 @@

{{ model_name }}

Name Source + Stored Experiment # Notes Access @@ -256,6 +268,11 @@

{{ model_name }}

{{ item.name }} {{ item.source.name|default:"—" }} + + {{ item.stored_count }} + {% if item.low_state %}{% if item.low_count_check %}{% if item.low_state == 'alert' %}Alert{% else %}Low{% endif %}{% else %}Silenced{% endif %}{% endif %} + {{ item.experiment|default:"—" }} {{ item.notes|truncatechars:60|default:"—" }} {{ item.get_access_level_display }} @@ -266,7 +283,7 @@

{{ model_name }}

{% empty %} - + {% if search_query %}No samples match "{{ search_query }}".{% else %}No samples yet. Create your first sample to get started.{% endif %} {% endfor %} @@ -413,7 +430,11 @@

{{ item.name }}

search_off

No {{ model_name|lower }} found

+ {% if low_count_on and not search_query %} +

No samples are running low.

+ {% else %}

Try adjusting your search criteria or create a new {{ model_name|slice:":-1"|lower }}.

+ {% endif %}