From fef04ee601fb4f9e0975a5973ca2ffd66db697e2 Mon Sep 17 00:00:00 2001 From: Ruud Overeem <overeem@astron.nl> Date: Thu, 9 Mar 2006 15:12:58 +0000 Subject: [PATCH] BugID: 664 A couple of minor changes are made to meet the requirement of OTB: - The OTDBtree info structure was extended with an description field and a function for setting this description was added. - The momID of the OTDBtree structure is now private. - A function setSchedule was added to set the scheduling time of a VH tree. - The function saveComponentParam was reamed to saveParam because it is also used for saving VT and VH parameters. - The test programs are updated to include the new functions. --- SAS/OTDB/include/OTDB/OTDBtree.h | 9 ++- SAS/OTDB/include/OTDB/TreeMaintenance.h | 15 ++++- SAS/OTDB/sql/create_OTDB.sql | 7 +- SAS/OTDB/sql/create_tree_table.sql | 1 + SAS/OTDB/sql/create_types.sql | 3 +- SAS/OTDB/sql/getTreeInfo_func.sql | 6 +- SAS/OTDB/sql/getTreeList_func.sql | 3 +- SAS/OTDB/sql/setDescription_func.sql | 59 +++++++++++++++++ SAS/OTDB/sql/setSchedule_func.sql | 79 ++++++++++++++++++++++ SAS/OTDB/src/OTDBtree.cc | 24 +++---- SAS/OTDB/src/TreeMaintenance.cc | 88 +++++++++++++++++++++++++ SAS/OTDB/src/loadCompFile.cc | 10 +-- SAS/OTDB/test/tVHvalue.cc | 26 +++++++- SAS/OTDB/test/tVICcomp.cc | 2 +- SAS/OTDB/test/tVTtree.cc | 5 ++ 15 files changed, 306 insertions(+), 31 deletions(-) create mode 100644 SAS/OTDB/sql/setDescription_func.sql create mode 100644 SAS/OTDB/sql/setSchedule_func.sql diff --git a/SAS/OTDB/include/OTDB/OTDBtree.h b/SAS/OTDB/include/OTDB/OTDBtree.h index daab88d651a..3dd85864cb1 100644 --- a/SAS/OTDB/include/OTDB/OTDBtree.h +++ b/SAS/OTDB/include/OTDB/OTDBtree.h @@ -50,16 +50,18 @@ class OTDBconnection; // The the last few fields will be empty for PIC trees. class OTDBtree { public: - OTDBtree() : momID(0), itsTreeID(0) {}; + OTDBtree() : itsTreeID(0), itsMomID(0) {}; ~OTDBtree() {}; treeIDType treeID() const { return (itsTreeID); } - treeIDType momID; + treeIDType momID() const { return (itsMomID); } classifType classification; // development / test / operational string creator; ptime creationDate; treeType type; // hardware / VItemplate / VHtree treeState state; // idle / configure / ... / active / ... + string description; // free text + // -- VIC only -- treeIDType originalTree; string campaign; @@ -74,10 +76,11 @@ public: private: //# Prevent changing the database keys - OTDBtree(treeIDType aTreeID) : momID(0), itsTreeID(aTreeID) {}; + OTDBtree(treeIDType aTreeID) : itsTreeID(aTreeID), itsMomID(0) {}; OTDBtree(const pqxx::result::tuple& row); treeIDType itsTreeID; + treeIDType itsMomID; }; //# diff --git a/SAS/OTDB/include/OTDB/TreeMaintenance.h b/SAS/OTDB/include/OTDB/TreeMaintenance.h index 2ff8a49f383..1d0a1710fd9 100644 --- a/SAS/OTDB/include/OTDB/TreeMaintenance.h +++ b/SAS/OTDB/include/OTDB/TreeMaintenance.h @@ -81,9 +81,6 @@ public: // Save new node or update the limits and description fields of the node. bool saveComponentNode (VICnodeDef& aNode); - // Save the parameter definition - bool saveComponentParam (OTDBparam& aParam); - //# --- VIC maintenance : Templates --- // From a component tree a template tree can be constructed. In a template @@ -110,6 +107,9 @@ public: OTDBparam getParam (treeIDType aTreeID, nodeIDType aParamID); + // Save the parameter definition + bool saveParam (OTDBparam& aParam); + // Get a number of levels of children. vector<OTDBnode> getItemList (treeIDType aTreeID, nodeIDType topNode, @@ -183,6 +183,15 @@ public: bool setTreeState(treeIDType aTreeID, treeState aState); + // Update the description of a tree. + bool setDescription(treeIDType aTreeID, + string aDescription); + + // Set the scheduling times of the tree + bool setSchedule(treeIDType aTreeID, + const ptime& aStartTime, + const ptime& aStopTime); + // Whenever an error occurs in one the OTDB functions the message can // be retrieved with this function. inline string errorMsg() const; diff --git a/SAS/OTDB/sql/create_OTDB.sql b/SAS/OTDB/sql/create_OTDB.sql index fa0ba5e2b8b..73e0c4abe5b 100644 --- a/SAS/OTDB/sql/create_OTDB.sql +++ b/SAS/OTDB/sql/create_OTDB.sql @@ -9,7 +9,6 @@ \i create_log_system.sql \i security_func.sql -\i classify_func.sql \i misc_func.sql -- State history @@ -21,7 +20,6 @@ \i getTreeList_func.sql \i getTreeInfo_func.sql \i newTree_func.sql -\i setMomInfo_func.sql -- PICtree \i create_PIC_tables.sql @@ -57,6 +55,7 @@ \i getVHitemList_func.sql \i exportTree_func.sql \i searchVHinPeriod_func.sql +\i setSchedule_func.sql -- multi treetype \i getTopNode_func.sql @@ -66,5 +65,7 @@ \i copyTree_func.sql \i deleteTree_func.sql \i addKVT_func.sql - +\i classify_func.sql +\i setMomInfo_func.sql +\i setDescription_func.sql diff --git a/SAS/OTDB/sql/create_tree_table.sql b/SAS/OTDB/sql/create_tree_table.sql index 9b51cddbafa..b064ab28a49 100644 --- a/SAS/OTDB/sql/create_tree_table.sql +++ b/SAS/OTDB/sql/create_tree_table.sql @@ -56,6 +56,7 @@ CREATE TABLE OTDBtree ( starttime TIMESTAMP(0), stoptime TIMESTAMP(0), owner INT4 REFERENCES operator(ID), + description TEXT, -- contraints CONSTRAINT tree_uniq UNIQUE (treeID) diff --git a/SAS/OTDB/sql/create_types.sql b/SAS/OTDB/sql/create_types.sql index dbd3ac3e6a5..1f3d1be103d 100644 --- a/SAS/OTDB/sql/create_types.sql +++ b/SAS/OTDB/sql/create_types.sql @@ -44,7 +44,8 @@ CREATE TYPE treeInfo AS ( originalTree INT4, -- OTDBtree.treeID%TYPE, campaign VARCHAR(30), -- campaign.name%TYPE, starttime timestamp(0), - stoptime timestamp(0) + stoptime timestamp(0), + description TEXT ); CREATE TYPE stateInfo AS ( diff --git a/SAS/OTDB/sql/getTreeInfo_func.sql b/SAS/OTDB/sql/getTreeInfo_func.sql index fff0fc6ed1a..51a11fb3056 100644 --- a/SAS/OTDB/sql/getTreeInfo_func.sql +++ b/SAS/OTDB/sql/getTreeInfo_func.sql @@ -54,7 +54,8 @@ CREATE OR REPLACE FUNCTION getTreeInfo(INT4, BOOLEAN) t.originID, c.name, t.starttime, - t.stoptime + t.stoptime, + t.description INTO vRecord FROM OTDBtree t INNER JOIN OTDBuser u ON t.creator = u.userid @@ -75,7 +76,8 @@ CREATE OR REPLACE FUNCTION getTreeInfo(INT4, BOOLEAN) t.originID, c.name, t.starttime, - t.stoptime + t.stoptime, + t.description INTO vRecord FROM OTDBtree t INNER JOIN OTDBuser u ON t.creator = u.userid diff --git a/SAS/OTDB/sql/getTreeList_func.sql b/SAS/OTDB/sql/getTreeList_func.sql index 5d579dc3aac..d9c5a156e0f 100644 --- a/SAS/OTDB/sql/getTreeList_func.sql +++ b/SAS/OTDB/sql/getTreeList_func.sql @@ -73,7 +73,8 @@ CREATE OR REPLACE FUNCTION getTreeList(INT2, INT2) t.originID, c.name, t.starttime, - t.stoptime + t.stoptime, + t.description FROM OTDBtree t INNER JOIN OTDBuser u ON t.creator = u.userid INNER JOIN campaign c ON c.ID = t.campaign diff --git a/SAS/OTDB/sql/setDescription_func.sql b/SAS/OTDB/sql/setDescription_func.sql new file mode 100644 index 00000000000..f4fd039d513 --- /dev/null +++ b/SAS/OTDB/sql/setDescription_func.sql @@ -0,0 +1,59 @@ +-- +-- setDescription.sql: function for changing the description of a tree +-- +-- Copyright (C) 2006 +-- ASTRON (Netherlands Foundation for Research in Astronomy) +-- P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@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$ +-- + +-- +-- setDescription (authToken, treeID, description) +-- +-- Authorisation: yes +-- +-- Tables: otdbtree update +-- +-- Types: none +-- +CREATE OR REPLACE FUNCTION setDescription(INT4, INT4, TEXT) + RETURNS BOOLEAN AS ' + DECLARE + vFunction INT2 := 1; + vIsAuth BOOLEAN; + vAuthToken ALIAS FOR $1; + vCampaignID campaign.id%TYPE; + + BEGIN + -- check authorisation(authToken, treeID, func, none) + vIsAuth := FALSE; + SELECT isAuthorized(vAuthToken, $2, vFunction, 0) + INTO vIsAuth; + IF NOT vIsAuth THEN + RAISE EXCEPTION \'Not authorized.\'; + RETURN FALSE; + END IF; + + -- update the tree + UPDATE OTDBtree + SET description = $3 + WHERE treeID = $2; + + RETURN TRUE; + END; +' LANGUAGE plpgsql; diff --git a/SAS/OTDB/sql/setSchedule_func.sql b/SAS/OTDB/sql/setSchedule_func.sql new file mode 100644 index 00000000000..f84563d7864 --- /dev/null +++ b/SAS/OTDB/sql/setSchedule_func.sql @@ -0,0 +1,79 @@ +-- +-- SetSchedule.sql: function for changing the scheduling times of a tree. +-- +-- Copyright (C) 2006 +-- ASTRON (Netherlands Foundation for Research in Astronomy) +-- P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@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$ +-- + +-- +-- setSchedule (authToken, treeID, aStartTime, aStopTime) +-- +-- Authorisation: yes +-- +-- Tables: otdbtree update +-- +-- Types: none +-- +CREATE OR REPLACE FUNCTION setSchedule(INT4, INT4, TIMESTAMP, TIMESTAMP) + RETURNS BOOLEAN AS ' + DECLARE + vFunction INT2 := 1; + TSactive CONSTANT INT2 := 600; + TThierarchy CONSTANT INT2 := 30; + vIsAuth BOOLEAN; + vAuthToken ALIAS FOR $1; + vCampaignID campaign.id%TYPE; + vTreeType OTDBtree.treetype%TYPE; + vState OTDBtree.state%TYPE; + + BEGIN + -- check authorisation(authToken, treeID, func, none) + vIsAuth := FALSE; + SELECT isAuthorized(vAuthToken, $2, vFunction, 0) + INTO vIsAuth; + IF NOT vIsAuth THEN + RAISE EXCEPTION \'Not authorized.\'; + RETURN FALSE; + END IF; + + -- Only non-active VH trees can be scheduled. + SELECT treetype, state + INTO vTreeType, vState + FROM OTDBtree + WHERE treeID = $2; + + IF vTreeType <> TThierarchy THEN + RAISE EXCEPTION \'Only VH trees can be scheduled.\'; + END IF; + + IF vState = TSactive THEN + RAISE EXCEPTION \'Tree may not be active\'; + END IF; + + -- Finally update tree + UPDATE OTDBtree + SET starttime = $3, + stoptime = $4 + WHERE treeID = $2; + + RETURN TRUE; + END; +' LANGUAGE plpgsql; + diff --git a/SAS/OTDB/src/OTDBtree.cc b/SAS/OTDB/src/OTDBtree.cc index 4c2063e48e4..bf4fa0fddcf 100644 --- a/SAS/OTDB/src/OTDBtree.cc +++ b/SAS/OTDB/src/OTDBtree.cc @@ -38,7 +38,7 @@ OTDBtree::OTDBtree(const result::tuple& row) // construct OTDBtree class with right ID // Note: names refer to SQL OTDBtree type row["treeid"].to(itsTreeID); - row["momid"].to(momID); + row["momid"].to(itsMomID); // fill in rest of the fields row["classification"].to(classification); @@ -62,6 +62,7 @@ OTDBtree::OTDBtree(const result::tuple& row) if (stop.length() > 0) { stoptime = time_from_string(stop); } + row["description"].to(description); } // @@ -70,17 +71,18 @@ OTDBtree::OTDBtree(const result::tuple& row) // Show Tree charateristics. ostream& OTDBtree::print (ostream& os) const { - os << "treeID : " << itsTreeID << endl; - os << "MomID : " << momID << endl; + os << "treeID : " << itsTreeID << endl; + os << "MomID : " << itsMomID << endl; os << "classification: " << classification << endl; - os << "creator : " << creator << endl; - os << "creationdate : " << creationDate << endl; - os << "tree type : " << type << endl; - os << "state : " << state << endl; - os << "original tree : " << originalTree << endl; - os << "campaign : " << campaign << endl; - os << "starttime : " << starttime << endl; - os << "stoptime : " << stoptime << endl; + os << "creator : " << creator << endl; + os << "creationdate : " << creationDate << endl; + os << "tree type : " << type << endl; + os << "state : " << state << endl; + os << "original tree : " << originalTree << endl; + os << "campaign : " << campaign << endl; + os << "starttime : " << starttime << endl; + os << "stoptime : " << stoptime << endl; + os << "description : " << description << endl; return (os); } diff --git a/SAS/OTDB/src/TreeMaintenance.cc b/SAS/OTDB/src/TreeMaintenance.cc index 7e8f07a9d5f..e628b8a2464 100644 --- a/SAS/OTDB/src/TreeMaintenance.cc +++ b/SAS/OTDB/src/TreeMaintenance.cc @@ -25,6 +25,7 @@ //# Includes #include <Common/LofarLogger.h> +#include <Common/lofar_datetime.h> #include <fstream> #include <OTDB/TreeMaintenance.h> #include <OTDB/OTDBnode.h> @@ -1064,5 +1065,92 @@ bool TreeMaintenance::setTreeState(treeIDType aTreeID, } +// +// setDescription(treeID, description): bool +// +// Set the description of the tree. +bool TreeMaintenance::setDescription(treeIDType aTreeID, + string aDescription) +{ + // Check connection + if (!itsConn->connect()) { + itsError = itsConn->errorMsg(); + return (false); + } + + work xAction(*(itsConn->getConn()), "setDescription"); + try { + // construct a query that calls a stored procedure. + result res = xAction.exec( + formatString("SELECT setDescription(%d,%d,'%s'::text)", + itsConn->getAuthToken(), + aTreeID, + aDescription.c_str())); + + // Analyse result. + bool succes; + res[0]["setDescription"].to(succes); + if (!succes) { + itsError = "Unable to save the description"; + return (false); + } + + xAction.commit(); + return (true); + } + catch (Exception& ex) { + itsError = string("Exception during setDescription:") + ex.what(); + LOG_FATAL(itsError); + return (false); + } + + return (false); +} + +// +// setSchedule(treeID, startTime, stopTime): bool +// +// Set the Executiontime of a tree +bool TreeMaintenance::setSchedule(treeIDType aTreeID, + const ptime& aStartTime, + const ptime& aStopTime) +{ + // Check connection + if (!itsConn->connect()) { + itsError = itsConn->errorMsg(); + return (false); + } + + work xAction(*(itsConn->getConn()), "setSchedule"); + try { + // construct a query that calls a stored procedure. + result res = xAction.exec( + formatString("SELECT setSchedule(%d,%d,'%s','%s')", + itsConn->getAuthToken(), + aTreeID, + to_simple_string(aStartTime).c_str(), + to_simple_string(aStopTime).c_str())); + + // Analyse result. + bool succes; + res[0]["setSchedule"].to(succes); + if (!succes) { + itsError = "Unable to set the schedule times:"; + return (false); + } + + xAction.commit(); + return (true); + } + catch (Exception& ex) { + itsError = string("Exception during setSchedule:") + ex.what(); + LOG_FATAL(itsError); + return (false); + } + + return (false); +} + + } // namespace OTDB } // namespace LOFAR diff --git a/SAS/OTDB/src/loadCompFile.cc b/SAS/OTDB/src/loadCompFile.cc index ad400f2e0e6..2ea15943939 100644 --- a/SAS/OTDB/src/loadCompFile.cc +++ b/SAS/OTDB/src/loadCompFile.cc @@ -40,9 +40,9 @@ namespace LOFAR { namespace OTDB { // -// saveComponentParam(OTDBparam) : bool +// saveParam(OTDBparam) : bool // -bool TreeMaintenance::saveComponentParam(OTDBparam& aParam) +bool TreeMaintenance::saveParam(OTDBparam& aParam) { // Check connection if (!itsConn->connect()) { @@ -346,7 +346,7 @@ nodeIDType TreeMaintenance::loadComponentFile (const string& filename) baseParam.runtimeMod = false; baseParam.limits = "1+"; baseParam.description = "Number of instances"; - saveComponentParam (baseParam); + saveParam (baseParam); } // -- USES -- else if (!args[0].compare("uses")) { @@ -385,7 +385,7 @@ nodeIDType TreeMaintenance::loadComponentFile (const string& filename) AttachedChild.runtimeMod = false; AttachedChild.limits = args[4]; AttachedChild.description = ""; - saveComponentParam (AttachedChild); + saveParam (AttachedChild); } // -- PAR -- else if (!args[0].compare("par")) { @@ -415,7 +415,7 @@ nodeIDType TreeMaintenance::loadComponentFile (const string& filename) AttachedChild.runtimeMod = (args[2].find("O",0)) ? true : false; AttachedChild.limits = args[7]; AttachedChild.description = args[9]; - saveComponentParam (AttachedChild); + saveParam (AttachedChild); // TODO: args[8] constraint, args[6] valmoment } // -- UNKNOWN -- diff --git a/SAS/OTDB/test/tVHvalue.cc b/SAS/OTDB/test/tVHvalue.cc index 8c58e0819d1..3ba85904940 100644 --- a/SAS/OTDB/test/tVHvalue.cc +++ b/SAS/OTDB/test/tVHvalue.cc @@ -33,6 +33,7 @@ #include <OTDB/OTDBtypes.h> #include <OTDB/OTDBnode.h> #include <OTDB/TreeTypeConv.h> +#include <OTDB/TreeStateConv.h> #include <OTDB/ClassifConv.h> using namespace LOFAR; @@ -139,6 +140,7 @@ int main (int argc, char* argv[]) { // Use converters in this testprogram TreeTypeConv TTconv(&conn); + TreeStateConv TSconv(&conn); ClassifConv CTconv(&conn); try { @@ -158,6 +160,29 @@ int main (int argc, char* argv[]) { OTDBtree treeInfo = conn.getTreeInfo(treeID); LOG_INFO_STR(treeInfo); + LOG_INFO("Trying to set the schedule time to 2006-01-20 12:00:00.000"); + LOG_INFO("THIS SHOULD FAIL BECAUSE STATUS IS ACTIVE"); + TreeMaintenance tm(&conn); + try { + tm.setSchedule (treeID, time_from_string("2006-01-20 12:00:00.000"), + time_from_string("2006-01-20 14:53:12.000")); + ASSERTSTR(false, "THIS SHOULD HAVE FAILED"); + } + catch (std::exception& ex) { + LOG_INFO_STR("Caught exception, protection works!"); + } + + LOG_INFO_STR ("Setting tree to scheduled"); + tm.setTreeState(treeID, TSconv.get("scheduled")); + treeInfo = conn.getTreeInfo(treeID); + LOG_INFO_STR(treeInfo); + + LOG_INFO("Retrying to set the schedule time to 2006-01-20 12:00:00"); + tm.setSchedule (treeID, time_from_string("2006-01-20 12:00:00.000"), + time_from_string("2006-01-20 14:53:12.000")); + treeInfo = conn.getTreeInfo(treeID); + LOG_INFO_STR(treeInfo); + LOG_INFO("Trying to construct a TreeValue object"); TreeValue tv(&conn, treeID); @@ -190,7 +215,6 @@ int main (int argc, char* argv[]) { LOG_INFO("Could NOT add the vector of OTDBvalue classes"); } - TreeMaintenance tm(&conn); OTDBnode topNode = tm.getTopNode(treeID); for (int i = 1; i < 4; ++i) { LOG_INFO_STR("searchInPeriod(" << topNode.nodeID() << "," << i diff --git a/SAS/OTDB/test/tVICcomp.cc b/SAS/OTDB/test/tVICcomp.cc index 1cc41c6c98d..b0f3c3a0181 100644 --- a/SAS/OTDB/test/tVICcomp.cc +++ b/SAS/OTDB/test/tVICcomp.cc @@ -249,7 +249,7 @@ int main (int argc, char* argv[]) { LOG_INFO("Modifying parameter '%nrInstances' to 1..5"); testPar.limits = "1..5"; - ASSERTSTR (tm.saveComponentParam(testPar), "update of parameter failed"); + ASSERTSTR (tm.saveParam(testPar), "update of parameter failed"); testPar = tm.getParam(0, pref); LOG_INFO_STR(testPar); diff --git a/SAS/OTDB/test/tVTtree.cc b/SAS/OTDB/test/tVTtree.cc index f73d6723b47..6696b28ebce 100644 --- a/SAS/OTDB/test/tVTtree.cc +++ b/SAS/OTDB/test/tVTtree.cc @@ -148,6 +148,11 @@ int main (int argc, char* argv[]) { LOG_INFO_STR ("Using tree " << VTtreeID << " for the tests"); OTDBtree treeInfo = conn.getTreeInfo(VTtreeID); LOG_INFO_STR(treeInfo); + + LOG_INFO ("Changing the description to 'test_tree'"); + tm.setDescription(VTtreeID, "test_tree"); + treeInfo = conn.getTreeInfo(VTtreeID); + LOG_INFO_STR(treeInfo); LOG_INFO("Trying to get the topnode of the tree"); OTDBnode topNode = tm.getTopNode(VTtreeID); -- GitLab