diff --git a/SAS/OTDB/sql/create_base_tables.sql b/SAS/OTDB/sql/create_base_tables.sql index ffdd15974233d62a206bed8e5ad2ef6aa1656d78..455c46062a91dc1db193430cb0c6113498c77d91 100644 --- a/SAS/OTDB/sql/create_base_tables.sql +++ b/SAS/OTDB/sql/create_base_tables.sql @@ -297,6 +297,17 @@ CREATE TABLE operator ( INSERT INTO operator VALUES (1, 'eucalypta', '0612345678'); INSERT INTO operator VALUES (2, 'gargamel', '0123456789'); +-- +-- otdb_admin table +-- +-- Internal administration. Tables always has 1 record! +-- NEVER DROP THIS TABLE! +-- +CREATE TABLE otdb_admin ( + treestatusevent timestamp(6) +) WITHOUT OIDS; +INSERT INTO otdb_admin VALUES(now()); + -- -- ProcessType table -- diff --git a/SAS/OTDB/sql/upgradeOTDB.sql b/SAS/OTDB/sql/upgradeOTDB.sql index 5071b23d97e6160fb6b9de48a62658e128752573..74b24c91e0fcb9893b22c18390add0af949b6e62 100644 --- a/SAS/OTDB/sql/upgradeOTDB.sql +++ b/SAS/OTDB/sql/upgradeOTDB.sql @@ -4,6 +4,11 @@ ALTER TABLE statehistory CREATE INDEX statehist_creation_idx ON statehistory(creation); +CREATE TABLE otdb_admin ( + treestatusevent timestamp(6) +) WITHOUT OIDS; +INSERT INTO otdb_admin VALUES(now()); + -- Load new functions \i getStateChanges.sql diff --git a/SAS/OTDB_Services/TreeStatusEvents.py b/SAS/OTDB_Services/TreeStatusEvents.py index 4ac76976e879000116bfdfdd2afe81895b0fc1ec..a9264f7b5780df6148c16cfcf22a09626a3416a3 100755 --- a/SAS/OTDB_Services/TreeStatusEvents.py +++ b/SAS/OTDB_Services/TreeStatusEvents.py @@ -112,6 +112,10 @@ if __name__ == "__main__": try: otdb_connection = pg.connect(user="postgres", host=options.dbHost, dbname=options.dbName) connected = True + # Get list of allowed tree states + allowed_states = {} + for (state_nr, name) in otdb_connection.query("select id,name from treestate").getresult(): + allowed_states[state_nr] = name except (TypeError, SyntaxError, pg.InternalError): connected = False print "DatabaseError: Connection to database could not be made, reconnect attempt in 5 seconds" @@ -122,8 +126,8 @@ if __name__ == "__main__": # Get start_time (= creation time of last retrieved record if any) start_time = '' try: - start_time = open('time_save.txt', 'rb').read() - except IOError: + start_time = otdb_connection.query("select treestatusevent from otdb_admin").getresult()[0][0] + except IndexError, QUERY_EXCEPTIONS: start_time = "2015-01-01 00:00:00.00" print "start_time=", start_time @@ -133,11 +137,12 @@ if __name__ == "__main__": print exc_info else: for (treeid, state, modtime, creation) in record_list: - content = { "treeID" : treeid, "state" : state, "time_of_change" : modtime } + content = { "treeID" : treeid, "state" : allowed_states.get(state, "unknwon_state"), + "time_of_change" : modtime } msg = EventMessage(context="otdb.treestatus", content=content) - print treeid, state, modtime, creation + print treeid, allowed_states.get(state, "unknwon_state"), modtime, creation send_bus.send(msg) - open('time_save.txt', 'wb').write(creation) + otdb_connection.query("update otdb_admin set treestatusevent = '%s'" % start_time) start_time = creation print "===" diff --git a/SAS/OTDB_Services/test/t_TreeStatusEvents.py b/SAS/OTDB_Services/test/t_TreeStatusEvents.py index 5ff6e76ac9bbe6a2c90c09c535af4fe332c59eb1..89ba25f40103594daae937d66f800475546ce6c3 100644 --- a/SAS/OTDB_Services/test/t_TreeStatusEvents.py +++ b/SAS/OTDB_Services/test/t_TreeStatusEvents.py @@ -84,7 +84,7 @@ if __name__ == "__main__": frombus.ack(msg) msg.show() try: - ok = (msg.content['treeID'] == 1099266 and msg.content['state'] == 500) + ok = (msg.content['treeID'] == 1099266 and msg.content['state'] == 'queued') except IndexError: ok = False diff --git a/SAS/OTDB_Services/test/unittest_db.dump.gz b/SAS/OTDB_Services/test/unittest_db.dump.gz index 145e5613516d3e0018a1e15fbe82124d27d98832..2fcaba7c9fcdafb08643bf5256bb8fbe1b012a9c 100644 Binary files a/SAS/OTDB_Services/test/unittest_db.dump.gz and b/SAS/OTDB_Services/test/unittest_db.dump.gz differ