Belle II Software development
MonitorDB.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include "daq/slc/database/MonitorDB.h"
9
10#include <daq/slc/database/DBHandlerException.h>
11#include <daq/slc/system/LogFile.h>
12
13using namespace Belle2;
14
15void MonitorDB::add(DBInterface& db, const std::string& table,
16 const std::string& vname, int val)
17{
18 try {
19 createTable(db, table);
20 db.execute("insert into %s (name, value_i) values ('%s', %d);",
21 table.c_str(), vname.c_str(), val);
22 } catch (const DBHandlerException& e) {
23 LogFile::error(e.what());
24 throw (e);
25 }
26}
27
28void MonitorDB::add(DBInterface& db, const std::string& table,
29 const std::string& vname, float val)
30{
31 try {
32 createTable(db, table);
33 db.execute("insert into %s (name, value_f) values ('%s', %f);",
34 table.c_str(), vname.c_str(), val);
35 } catch (const DBHandlerException& e) {
36 LogFile::error(e.what());
37 throw (e);
38 }
39}
40
41void MonitorDB::add(DBInterface& db, const std::string& table,
42 const std::string& vname, const std::string& val)
43{
44 try {
45 createTable(db, table);
46 db.execute("insert into %s (name, value_t) values ('%s', '%s');",
47 table.c_str(), vname.c_str(), val.c_str());
48 } catch (const DBHandlerException& e) {
49 LogFile::error(e.what());
50 throw (e);
51 }
52
53}
54
55NSMVarList MonitorDB::get(DBInterface& db, const std::string& tablename,
56 const std::string& vname)
57{
58 if (!db.isConnected()) db.connect();
59 db.execute("select *, extract(epoch from record_time) rtime "
60 "from %s p where p.name = '%s' order by id;",
61 tablename.c_str(), vname.c_str());
62 return readTable(db, vname);
63}
64
65NSMVarList MonitorDB::get(DBInterface& db, const std::string& tablename,
66 const std::string& vname, int max)
67{
68 if (!db.isConnected()) db.connect();
69 db.execute("select *, extract(epoch from record_time) rtime "
70 "from %s p where p.name = '%s' limit %d order by id;",
71 tablename.c_str(), vname.c_str(), max);
72 return readTable(db, vname);
73}
74
75NSMVarList MonitorDB::get(DBInterface& db, const std::string& tablename,
76 const std::string& vname, const Date& start,
77 const Date& end)
78{
79 if (!db.isConnected()) db.connect();
80 db.execute("select *, extract(epoch from record_time) rtime "
81 "from %s p where p.name = '%s' and record_time > '%s' and "
82 "record_time < '%s'order by id;", tablename.c_str(),
83 vname.c_str(), start.toString(), end.toString());
84 return readTable(db, vname);
85}
86
87NSMVarList MonitorDB::get(DBInterface& db, const std::string& tablename,
88 const std::string& vname, int max,
89 const Date& start, const Date& end)
90{
91 if (!db.isConnected()) db.connect();
92 db.execute("select *, extract(epoch from record_time) rtime "
93 "from %s p where p.name = '%s' and record_time > '%s' and "
94 "record_time < '%s' limit %d order by id;",
95 tablename.c_str(), vname.c_str(),
96 start.toString(), end.toString(), max);
97 return readTable(db, vname);
98}
99
100NSMVarList MonitorDB::readTable(DBInterface& db, const std::string& vname)
101{
102 DBRecordList record_v(db.loadRecords());
103 NSMVarList vars;
104 for (size_t i = 0; i < record_v.size(); i++) {
105 DBRecord& record(record_v[i]);
106 NSMVar var(vname);
107 if (record.hasField("value_b")) {
108 var = (int)record.getBool("value_b");
109 } else if (record.hasField("value_c")) {
110 var = (int)record.getInt("value_c");
111 } else if (record.hasField("value_s")) {
112 var = (int)record.getInt("value_s");
113 } else if (record.hasField("value_i")) {
114 var = record.getInt("value_i");
115 } else if (record.hasField("value_f")) {
116 var = record.getFloat("value_f");
117 } else if (record.hasField("value_d")) {
118 var = record.getFloat("value_d");
119 } else if (record.hasField("value_t")) {
120 var = record.get("value_t");
121 }
122 var.setId(record.getInt("id"));
123 var.setDate(record.getInt("rtime"));
124 LogFile::debug("%d=%s", record.getInt("rtime"), Date(var.getDate()).toString());
125 vars.push_back(var);
126 }
127 return vars;
128}
129
130void MonitorDB::createTable(DBInterface& db, const std::string& tablename)
131{
132 if (!db.isConnected()) db.connect();
133 if (!db.checkTable(tablename)) {
134 db.execute("create table %s \n"
135 "(name varchar(64), \n"
136 "id bigserial, \n"
137 "record_time timestamp with time zone default current_timestamp, \n"
138 "value_b boolean default NULL, \n"
139 "value_c char default NULL, \n"
140 "value_s smallint default NULL, \n"
141 "value_i int default NULL, \n"
142 "value_l bigint default NULL, \n"
143 "value_f float default NULL, \n"
144 "value_d double precision default NULL, \n"
145 "value_t text default NULL \n"
146 "); ", tablename.c_str());
147 db.execute("create index %s_id_index on %s(id);",
148 tablename.c_str(), tablename.c_str());
149 }
150}
Abstract base class for different kinds of events.