Skip to content

Commit 6bcc18e

Browse files
authored
Add created amd updated fields to MonitoredVariable (#269)
* Add created amd updated fields to MonitoredVariable * Fix migration conflict * Fix stored procedure to include created and updated fields * Revert * Minor issue fix * Fix systemtests: Update test_setInstrumentPVs.py to use current database schema
1 parent 2c94fe8 commit 6bcc18e

File tree

6 files changed

+78
-16
lines changed

6 files changed

+78
-16
lines changed

src/webmon_app/reporting/pvmon/admin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class PVNameAdmin(admin.ModelAdmin):
2121

2222

2323
class MonitoredVariableAdmin(admin.ModelAdmin):
24-
list_display = ("id", "instrument", "pv_name", "rule_name")
24+
list_display = ("id", "instrument", "pv_name", "rule_name", "created", "updated")
25+
readonly_fields = ("created", "updated")
2526

2627

2728
admin.site.register(PVName, PVNameAdmin)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Generated by Django 4.2.23 on 2025-09-10 12:00
2+
3+
import django.utils.timezone
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
dependencies = [
9+
("pvmon", "0003_pv_timestamp_pvcache_timestamp_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name="monitoredvariable",
15+
name="created",
16+
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
17+
preserve_default=False,
18+
),
19+
migrations.AddField(
20+
model_name="monitoredvariable",
21+
name="updated",
22+
field=models.DateTimeField(auto_now=True),
23+
),
24+
]

src/webmon_app/reporting/pvmon/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,5 @@ class MonitoredVariable(models.Model):
7777
instrument = models.ForeignKey(Instrument, on_delete=models.CASCADE)
7878
pv_name = models.ForeignKey(PVName, null=True, blank=True, on_delete=models.CASCADE)
7979
rule_name = models.CharField(max_length=50, default="", blank=True)
80+
created = models.DateTimeField(auto_now_add=True)
81+
updated = models.DateTimeField(auto_now=True)

src/webmon_app/reporting/pvmon/sql/stored_procs.sql

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,32 +175,45 @@ BEGIN
175175
RAISE EXCEPTION 'Instrument % does not exist', instrument;
176176
END IF;
177177

178-
-- Delete the existing monitored variables for this instrument
178+
-- Delete monitored variables for this instrument that are NOT in the input list
179179
DELETE
180180
FROM pvmon_monitoredvariable
181-
WHERE instrument_id = n_instrument;
181+
WHERE instrument_id = n_instrument
182+
AND pv_name_id NOT IN (
183+
SELECT id
184+
FROM pvmon_pvname
185+
WHERE name = ANY(pvs)
186+
);
182187

183188
-- Insert the new monitored variables without duplicates
184189
FOREACH n_pv_name IN ARRAY pvs
185190
LOOP
186191
SELECT id INTO n_pv_id FROM pvmon_pvname WHERE name = n_pv_name;
187192
IF n_pv_id IS NOT NULL THEN
188-
INSERT INTO pvmon_monitoredvariable (instrument_id, pv_name_id, rule_name)
189-
SELECT pvmon_pvcache.instrument_id, pvmon_pvcache.name_id, ''
190-
FROM pvmon_pvcache
191-
WHERE pvmon_pvcache.instrument_id = n_instrument AND pvmon_pvcache.name_id = n_pv_id
192-
UNION
193-
SELECT pvmon_pvstringcache.instrument_id, pvmon_pvstringcache.name_id, ''
194-
FROM pvmon_pvstringcache
195-
WHERE pvmon_pvstringcache.instrument_id = n_instrument AND pvmon_pvstringcache.name_id = n_pv_id;
193+
-- Only insert if not already present
194+
SELECT COUNT(*) INTO n_count
195+
FROM pvmon_monitoredvariable
196+
WHERE instrument_id = n_instrument AND pv_name_id = n_pv_id;
197+
198+
IF n_count = 0 THEN
199+
INSERT INTO pvmon_monitoredvariable (instrument_id, pv_name_id, rule_name, created, updated)
200+
SELECT pvmon_pvcache.instrument_id, pvmon_pvcache.name_id, '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
201+
FROM pvmon_pvcache
202+
WHERE pvmon_pvcache.instrument_id = n_instrument AND pvmon_pvcache.name_id = n_pv_id
203+
UNION
204+
SELECT pvmon_pvstringcache.instrument_id, pvmon_pvstringcache.name_id, '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
205+
FROM pvmon_pvstringcache
206+
WHERE pvmon_pvstringcache.instrument_id = n_instrument AND pvmon_pvstringcache.name_id = n_pv_id;
207+
END IF;
196208
END IF;
197209
END LOOP;
198210
SELECT count(*)
199211
INTO n_count
200-
FROM pvmon_monitoredvariable;
212+
FROM pvmon_monitoredvariable
213+
WHERE instrument_id = n_instrument;
201214
IF n_count = 0 THEN
202-
INSERT INTO pvmon_monitoredvariable (instrument_id, rule_name)
203-
VALUES (n_instrument, '');
215+
INSERT INTO pvmon_monitoredvariable (instrument_id, rule_name, created, updated)
216+
VALUES (n_instrument, '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
204217
END IF;
205218
END;
206219
$$ LANGUAGE plpgsql VOLATILE

src/webmon_app/reporting/tests/test_pvmon/test_models.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,28 @@ def test_rule_name_max_length(self):
145145
max_len = pv._meta.get_field("rule_name").max_length
146146
self.assertEqual(max_len, 50)
147147

148+
def test_created_field_exists(self):
149+
pv = MonitoredVariable.objects.get(id=1)
150+
self.assertTrue(hasattr(pv, "created"))
151+
self.assertIsNotNone(pv.created)
152+
153+
def test_updated_field_exists(self):
154+
pv = MonitoredVariable.objects.get(id=1)
155+
self.assertTrue(hasattr(pv, "updated"))
156+
self.assertIsNotNone(pv.updated)
157+
158+
def test_created_auto_now_add(self):
159+
# Test that created field has auto_now_add=True
160+
pv = MonitoredVariable.objects.get(id=1)
161+
created_field = pv._meta.get_field("created")
162+
self.assertTrue(created_field.auto_now_add)
163+
164+
def test_updated_auto_now(self):
165+
# Test that updated field has auto_now=True
166+
pv = MonitoredVariable.objects.get(id=1)
167+
updated_field = pv._meta.get_field("updated")
168+
self.assertTrue(updated_field.auto_now)
169+
148170

149171
if __name__ == "__main__":
150172
pytest.main()

tests/test_setInstrumentPVs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def insert_test_data(cls):
5252
for each in pv_ids:
5353
cursor.execute(
5454
"INSERT INTO pvmon_pvcache (instrument_id, name_id, value, status, timestamp) "
55-
"VALUES (%s, %s, 0.0, 0, '2025-08-28 14:30:00');",
55+
"VALUES (%s, %s, 0.0, 0, CURRENT_TIMESTAMP);",
5656
[inst_id, each],
5757
)
5858
clean_pv_ids.append((inst_id[0], each[0]))
@@ -63,7 +63,7 @@ def insert_test_data(cls):
6363
for each in stringpv_ids:
6464
cursor.execute(
6565
"INSERT INTO pvmon_pvstringcache (instrument_id, name_id, value, status, timestamp)"
66-
"VALUES (%s, %s, 'test_string_value', 0, '2025-08-28 14:30:00');",
66+
"VALUES (%s, %s, 'test_string_value', 0, CURRENT_TIMESTAMP);",
6767
[inst_id, each],
6868
)
6969
clean_pv_ids.append((inst_id[0], each[0]))

0 commit comments

Comments
 (0)