From b340fac57f252ffca918e4b1fce04f090c83120c Mon Sep 17 00:00:00 2001 From: Ruud Overeem <overeem@astron.nl> Date: Tue, 3 Nov 2015 10:27:39 +0000 Subject: [PATCH] Task #8531: Nieuw function for the TreeStatusEvent daemon. --- .gitattributes | 1 + SAS/OTDB/sql/create_tree_table.sql | 6 +- SAS/OTDB/sql/getStateChanges_func.sql | 88 +++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 SAS/OTDB/sql/getStateChanges_func.sql diff --git a/.gitattributes b/.gitattributes index 1e07452a502..b752dfb71c1 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4807,6 +4807,7 @@ SAS/OTDB/sql/getDefaultTemplates_func.sql -text SAS/OTDB/sql/getModifiedTrees_func.sql -text SAS/OTDB/sql/getMomID2treeID_func.sql -text SAS/OTDB/sql/getSchedulerInfo_func.sql -text +SAS/OTDB/sql/getStateChanges_func.sql -text SAS/OTDB/sql/getTreeGroup_func.sql -text SAS/OTDB/sql/getTreesInPeriod_func.sql -text SAS/OTDB/sql/getVTitem_func.sql -text diff --git a/SAS/OTDB/sql/create_tree_table.sql b/SAS/OTDB/sql/create_tree_table.sql index cd314a101bc..fefee8dd14a 100644 --- a/SAS/OTDB/sql/create_tree_table.sql +++ b/SAS/OTDB/sql/create_tree_table.sql @@ -37,6 +37,7 @@ DROP SEQUENCE IF EXISTS OTDBtreeID; DROP SEQUENCE IF EXISTS OTDBgroupID; DROP TABLE IF EXISTS StateHistory CASCADE; DROP INDEX IF EXISTS otdbtree_treeid_indx; +DROP INDEX IF EXISTS statehist_creation_idx; CREATE SEQUENCE OTDBtreeID START 1; -- Create a new start number based on current time. To prevent overlap in treeid's @@ -98,6 +99,9 @@ CREATE TABLE StateHistory ( momID INT4 NOT NULL, state INT2 NOT NULL, userID INT4 NOT NULL REFERENCES operator(ID), - timestamp TIMESTAMP(0) DEFAULT now() + timestamp TIMESTAMP(0) DEFAULT now(), + creation TIMESTAMP(6) DEFAULT now() ) WITHOUT OIDS; +CREATE INDEX statehist_creation_idx ON statehistory(creation); + diff --git a/SAS/OTDB/sql/getStateChanges_func.sql b/SAS/OTDB/sql/getStateChanges_func.sql new file mode 100644 index 00000000000..c4c71ae4320 --- /dev/null +++ b/SAS/OTDB/sql/getStateChanges_func.sql @@ -0,0 +1,88 @@ +-- +-- getStateChanges.sql: function for getting state changes in a given period +-- +-- Copyright (C) 2015 +-- ASTRON (Netherlands Foundation for Research in Astronomy) +-- P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, softwaresupport@astron.nl +-- +-- This program 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 2 of the License, or +-- (at your option) any later version. +-- +-- This program 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 this program; if not, write to the Free Software +-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +-- +-- $Id: getStateList_func.sql 30919 2015-02-05 15:26:22Z amesfoort $ +-- + +-- +-- getStateChanges(begindate, enddate) +-- +-- Get a list of statechanges. +-- +-- Authorisation: none +-- +-- Tables: otdbtree read +-- otdbuser read +-- +-- Types: treeState +-- +DROP TYPE IF EXISTS stateChange CASCADE; +CREATE TYPE stateChange AS ( + -- $Id: create_types.sql 30919 2015-02-05 15:26:22Z amesfoort $ + treeID INT4, -- OTDBtree.treeID%TYPE, + momID INT4, -- OTDBtree.momID%TYPE, + state INT2, -- treestate.ID%TYPE, + username VARCHAR(20), -- OTDBuser.username%TYPE, + modtime timestamp(0), + creation timestamp(6) +); + +CREATE OR REPLACE FUNCTION getStateChanges(TIMESTAMP(6), TIMESTAMP(6)) + RETURNS SETOF stateChange AS ' + -- $Id: getStateList_func.sql 30919 2015-02-05 15:26:22Z amesfoort $ + DECLARE + vRecord RECORD; + vQuery TEXT; + vKeyField TEXT; + + BEGIN + -- create query for treeID (when filled) + vQuery := \'WHERE \'; + IF $1 IS NOT NULL THEN + IF $2 IS NULL THEN + vQuery := vQuery || \'s.creation >\' || chr(39) || $1 || chr(39); + ELSE + vQuery := vQuery || \'s.creation >\' || chr(39) || $1 || chr(39) + || \' AND s.creation <=\' || chr(39) || $2 || chr(39); + END IF; + END IF; + + -- do selection + FOR vRecord IN EXECUTE \' + SELECT s.treeID, + s.momID, + s.state, + u.username, + s.timestamp, + s.creation + FROM StateHistory s + INNER JOIN OTDBuser u ON s.userid = u.userid + \' || vQuery || \' + ORDER BY s.creation ASC\' + LOOP + RETURN NEXT vRecord; + END LOOP; + RETURN; + END +' LANGUAGE plpgsql; + + + -- GitLab