diff --git a/.gitattributes b/.gitattributes index fb7921dabc8fec880496129db5f40ef44c63b310..09d5e2adb0e4dee14485691bf2c0c5bce942275d 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2841,6 +2841,7 @@ SAS/OTDB/sql/fresh_sas001_database.sh -text SAS/OTDB/sql/getAllTaskID_func.sql -text SAS/OTDB/sql/getBrokenHardware_func.sql -text SAS/OTDB/sql/getDefaultTemplates_func.sql -text +SAS/OTDB/sql/getMomID2treeID_func.sql -text SAS/OTDB/sql/getSchedulerInfo_func.sql -text SAS/OTDB/sql/getTreeGroup_func.sql -text SAS/OTDB/sql/getTreesInPeriod_func.sql -text diff --git a/SAS/OTDB/include/OTDB/OTDBconnection.h b/SAS/OTDB/include/OTDB/OTDBconnection.h index 2a448e90f558d3f32f92acb20921aa708e7faa5b..09bc228846e9d80da5d94ca14668c52e74d88a36 100644 --- a/SAS/OTDB/include/OTDB/OTDBconnection.h +++ b/SAS/OTDB/include/OTDB/OTDBconnection.h @@ -32,6 +32,7 @@ #include <OTDB/TreeState.h> #include <OTDB/DefaultTemplate.h> #include <Common/lofar_vector.h> +#include <Common/lofar_map.h> using namespace pqxx; @@ -105,6 +106,9 @@ public: const ptime& endDate = ptime(max_date_time)); + // Get a map to translate MoMIds to treeID's + map<uint, uint> getMomID2treeIDMap(); + // Get a new unique groupID uint32 newGroupID(); @@ -127,7 +131,7 @@ private: string itsPassword; string itsDatabase; string itsHost; - string itsPort; + string itsPort; bool itsIsConnected; connection* itsConnection; uint32 itsAuthToken; diff --git a/SAS/OTDB/sql/create_OTDB.sql b/SAS/OTDB/sql/create_OTDB.sql index 9d82f47f836dfc71c157174be63a0540cfeb0c0f..2ad865403b24f31a90cb991488a2f91a305701e2 100644 --- a/SAS/OTDB/sql/create_OTDB.sql +++ b/SAS/OTDB/sql/create_OTDB.sql @@ -76,6 +76,7 @@ \i classify_func.sql \i setMomInfo_func.sql \i setDescription_func.sql +\i getMomID2treeID_func.sql -- campaign \i campaignAPI.sql diff --git a/SAS/OTDB/sql/getMomID2treeID_func.sql b/SAS/OTDB/sql/getMomID2treeID_func.sql new file mode 100644 index 0000000000000000000000000000000000000000..80b4a71e72004ae2363c8e0b88179ac09eb5d0b8 --- /dev/null +++ b/SAS/OTDB/sql/getMomID2treeID_func.sql @@ -0,0 +1,50 @@ +-- +-- getMomID2treeID.sql: function for changing the Mom information of a tree +-- +-- Copyright (C) 2012 +-- 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: setMomInfo_func.sql 18624 2011-07-27 15:34:51Z schoenmakers $ +-- + +-- +-- getMomID2treeID() +-- +-- Authorisation: no +-- +-- Tables: otdbtree read +-- +-- Types: none +-- + +CREATE TYPE treeIDmomID AS (treeID INTEGER, momID INTEGER); + +CREATE OR REPLACE FUNCTION getMomID2treeID() + RETURNS SETOF treeIDmomID AS $$ + DECLARE + vRecord RECORD; + + BEGIN + FOR vRecord IN SELECT treeID,MomID FROM OTDBtree WHERE momID != '0' ORDER BY momID + LOOP + RETURN NEXT vRecord; + END LOOP; + RETURN; + END +$$ LANGUAGE plpgsql; + diff --git a/SAS/OTDB/src/OTDBconnection.cc b/SAS/OTDB/src/OTDBconnection.cc index 2c9dbe2f237bb77573c8b69917a477603554c4a9..c23489d3d64f40ac5c4dbc944a58bbe986bf514f 100644 --- a/SAS/OTDB/src/OTDBconnection.cc +++ b/SAS/OTDB/src/OTDBconnection.cc @@ -41,7 +41,7 @@ OTDBconnection::OTDBconnection (const string& username, const string& passwd, const string& database, const string& hostname, - const string & port): + const string& port): itsUser (username), itsPassword (passwd), itsDatabase (database), @@ -461,6 +461,49 @@ vector<OTDBtree> OTDBconnection::getTreesInPeriod( return (empty); } +// +// getMomID2treeIDMap(): map[momID] +// +// Get a map to convert a MomID to a treeID +// +map<uint, uint> OTDBconnection::getMomID2treeIDMap() +{ + map<uint, uint> theMap; + if (!itsIsConnected && !connect()) { + return (theMap); + } + + LOG_TRACE_FLOW_STR ("OTDB:getMomID2treeIDMap()"); + try { + // construct a query that calls a stored procedure. + work xAction(*itsConnection, "getMomID2treeIDMap"); + string query("SELECT * from getMomID2treeID()"); + + // execute query + result res = xAction.exec(query); + + // any records found? + if (res.empty()) { + return (theMap); + } + + // construct map + result::size_type nrRecords = res.size(); + for (result::size_type i = 0; i < nrRecords; ++i) { + uint treeID; + res[i][0].to(treeID); + uint MomID; + res[i][1].to(MomID); + theMap[MomID] = treeID; + } + } + catch (std::exception& ex) { + itsError = string("Exception during getMoID2treeID:") + ex.what(); + } + + return (theMap); +} + // // newGroupID(): groupID // diff --git a/SAS/OTDB/test/tConnection.cc b/SAS/OTDB/test/tConnection.cc index e6954a0a7aeaa0f310d6fc4845ed1278e7064101..f3e7cc2263843c1dbd7f7cdd83ab3e56c6f5326b 100644 --- a/SAS/OTDB/test/tConnection.cc +++ b/SAS/OTDB/test/tConnection.cc @@ -241,6 +241,25 @@ int main (int argc, char* argv[]) { cout << DTlist.size() << " records" << endl << endl; } + + LOG_INFO("=== Testing MoM 2 treeID converter ==="); + LOG_INFO("getMomID2treeID()"); + map<uint, uint> theMap = conn.getMomID2treeIDMap(); + if (theMap.size() == 0) { + LOG_INFO_STR("Error:" << conn.errorMsg()); + } + else { + cout << " MoMID | treeID" << endl; + cout << "---------+--------" << endl; + map<uint,uint>::const_iterator end = theMap.end(); + map<uint,uint>::const_iterator iter = theMap.begin(); + while (iter != end) { + string row(formatString("%8d |%8d", iter->first, iter->second)); + cout << row << endl; + iter++; + } + cout << theMap.size() << " records" << endl << endl; + } } catch (std::exception& ex) { LOG_FATAL_STR("Unexpected exception: " << ex.what());