diff --git a/.gitattributes b/.gitattributes index 24becc2b1ab6e22e2b7f13c0a534ed0e5f3e0975..492376a178234143900b7631e730be06b34ed01a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -5042,6 +5042,7 @@ SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/add_notifications.sql -tex SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/add_resource_allocation_statics.sql -text SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/add_triggers.sql -text SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/add_virtual_instrument.sql -text +SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/create_add_notifications.sql.py -text SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/create_add_virtual_instrument.sql.py -text SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/create_and_populate_database.sql -text SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/create_database.sql -text diff --git a/SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/add_notifications.sql b/SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/add_notifications.sql index ba72605e67e61bebe468d12833f175dd44755684..afc072544f1f1edb1f6f9221a1d49afdcbb5f9f6 100644 --- a/SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/add_notifications.sql +++ b/SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/add_notifications.sql @@ -1,4 +1,10 @@ ---this file was generated by make_radb_postgres_notification_queries() in python module lofar.sas.resourceassignment.database.radbpglistener +--this file was generated by create_add_notifications.sql.py +--it creates triggers and functions which fire postgres notify events upon the given table actions +--these notify events can be listened to implenting a subclass of the PostgresListener in the lofar.common.postgres python module +--for the radb such a subclass has been made, which listens specifically to the notifications defined below +--RADBPGListener in module lofar.sas.resourceassignment.database.radbpglistener +--this RADBPGListener then broadcasts the event on the lofar bus. + DROP TRIGGER IF EXISTS TRIGGER_NOTIFY_NOTIFY_task_INSERT_with_task_view ON resource_allocation.task CASCADE; DROP FUNCTION IF EXISTS resource_allocation.NOTIFY_task_INSERT_with_task_view(); @@ -153,3 +159,44 @@ CREATE TRIGGER TRIGGER_NOTIFY_NOTIFY_resource_claim_DELETE AFTER DELETE ON resource_allocation.resource_claim FOR EACH ROW EXECUTE PROCEDURE resource_allocation.NOTIFY_resource_claim_DELETE(); + + +DROP TRIGGER IF EXISTS TRIGGER_NOTIFY_NOTIFY_resource_availability_UPDATE ON resource_monitoring.resource_availability CASCADE; +DROP FUNCTION IF EXISTS resource_monitoring.NOTIFY_resource_availability_UPDATE(); + + +CREATE OR REPLACE FUNCTION resource_monitoring.NOTIFY_resource_availability_UPDATE() +RETURNS TRIGGER AS $$ +BEGIN +PERFORM pg_notify(CAST('resource_availability_update' AS text), +'{"old":' || row_to_json(OLD)::text || ',"new":' || row_to_json(NEW)::text || '}'); +RETURN NEW; +END; +$$ LANGUAGE plpgsql; + + +CREATE TRIGGER TRIGGER_NOTIFY_NOTIFY_resource_availability_UPDATE +AFTER UPDATE ON resource_monitoring.resource_availability +FOR EACH ROW +EXECUTE PROCEDURE resource_monitoring.NOTIFY_resource_availability_UPDATE(); + + +DROP TRIGGER IF EXISTS TRIGGER_NOTIFY_NOTIFY_resource_capacity_UPDATE ON resource_monitoring.resource_capacity CASCADE; +DROP FUNCTION IF EXISTS resource_monitoring.NOTIFY_resource_capacity_UPDATE(); + + +CREATE OR REPLACE FUNCTION resource_monitoring.NOTIFY_resource_capacity_UPDATE() +RETURNS TRIGGER AS $$ +BEGIN +PERFORM pg_notify(CAST('resource_capacity_update' AS text), +'{"old":' || row_to_json(OLD)::text || ',"new":' || row_to_json(NEW)::text || '}'); +RETURN NEW; +END; +$$ LANGUAGE plpgsql; + + +CREATE TRIGGER TRIGGER_NOTIFY_NOTIFY_resource_capacity_UPDATE +AFTER UPDATE ON resource_monitoring.resource_capacity +FOR EACH ROW +EXECUTE PROCEDURE resource_monitoring.NOTIFY_resource_capacity_UPDATE(); + diff --git a/SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/create_add_notifications.sql.py b/SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/create_add_notifications.sql.py new file mode 100755 index 0000000000000000000000000000000000000000..3c5234398c9dda815f1ed213231925041dedb56b --- /dev/null +++ b/SAS/ResourceAssignment/ResourceAssignmentDatabase/sql/create_add_notifications.sql.py @@ -0,0 +1,51 @@ +#!/usr/bin/python + +# Copyright (C) 2012-2015 ASTRON (Netherlands Institute for Radio Astronomy) +# P.O. Box 2, 7990 AA Dwingeloo, The Netherlands +# +# This file is part of the LOFAR software suite. +# The LOFAR software suite is free software: you can redistribute it and/or +# modify it under the terms of the GNU General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# The LOFAR software suite is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>. + +# $Id: radb.py 33394 2016-01-25 15:53:55Z schaap $ + +''' +TODO: documentation +''' +import logging +import psycopg2 +import psycopg2.extras + +from lofar.common.postgres import makePostgresNotificationQueries + +logger = logging.getLogger(__name__) + +if __name__ == '__main__': + with open('add_notifications.sql', 'wt') as f: + f.write('--this file was generated by create_add_notifications.sql.py\n') + f.write('--it creates triggers and functions which fire postgres notify events upon the given table actions\n') + f.write('--these notify events can be listened to implenting a subclass of the PostgresListener in the lofar.common.postgres python module\n') + f.write('--for the radb such a subclass has been made, which listens specifically to the notifications defined below\n') + f.write('--RADBPGListener in module lofar.sas.resourceassignment.database.radbpglistener\n') + f.write('--this RADBPGListener then broadcasts the event on the lofar bus.\n') + f.write('\n') + + f.writelines(makePostgresNotificationQueries('resource_allocation', 'task', 'INSERT', view_for_row='task_view')) + f.writelines(makePostgresNotificationQueries('resource_allocation', 'task', 'UPDATE', view_for_row='task_view')) + f.writelines(makePostgresNotificationQueries('resource_allocation', 'task', 'DELETE')) + f.writelines(makePostgresNotificationQueries('resource_allocation', 'specification', 'UPDATE', view_for_row='task_view', view_selection_id='specification_id')) + f.writelines(makePostgresNotificationQueries('resource_allocation', 'resource_claim', 'INSERT', view_for_row='resource_claim_view')) + f.writelines(makePostgresNotificationQueries('resource_allocation', 'resource_claim', 'UPDATE', view_for_row='resource_claim_view')) + f.writelines(makePostgresNotificationQueries('resource_allocation', 'resource_claim', 'DELETE')) + f.writelines(makePostgresNotificationQueries('resource_monitoring', 'resource_availability', 'UPDATE')) + f.writelines(makePostgresNotificationQueries('resource_monitoring', 'resource_capacity', 'UPDATE'))