Belle II Software development
MonitorDB Class Reference

Static Public Member Functions

static void add (DBInterface &db, const std::string &tablename, const std::string &vname, int val)
 
static void add (DBInterface &db, const std::string &tablename, const std::string &vname, float val)
 
static void add (DBInterface &db, const std::string &tablename, const std::string &vname, const std::string &val)
 
static NSMVarList get (DBInterface &db, const std::string &tablename, const std::string &vname)
 
static NSMVarList get (DBInterface &db, const std::string &tablename, const std::string &vname, int max)
 
static NSMVarList get (DBInterface &db, const std::string &tablename, const std::string &vname, const Date &start, const Date &end)
 
static NSMVarList get (DBInterface &db, const std::string &tablename, const std::string &vname, int max, const Date &start, const Date &end)
 

Static Private Member Functions

static NSMVarList readTable (DBInterface &db, const std::string &vname)
 
static void createTable (DBInterface &db, const std::string &tablename)
 

Detailed Description

Definition at line 23 of file MonitorDB.h.

Member Function Documentation

◆ add() [1/3]

void add ( DBInterface db,
const std::string &  tablename,
const std::string &  vname,
const std::string &  val 
)
static

Definition at line 41 of file MonitorDB.cc.

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}

◆ add() [2/3]

void add ( DBInterface db,
const std::string &  tablename,
const std::string &  vname,
float  val 
)
static

Definition at line 28 of file MonitorDB.cc.

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}

◆ add() [3/3]

void add ( DBInterface db,
const std::string &  tablename,
const std::string &  vname,
int  val 
)
static

Definition at line 15 of file MonitorDB.cc.

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}

◆ createTable()

void createTable ( DBInterface db,
const std::string &  tablename 
)
staticprivate

Definition at line 130 of file MonitorDB.cc.

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}

◆ get() [1/4]

NSMVarList get ( DBInterface db,
const std::string &  tablename,
const std::string &  vname 
)
static

Definition at line 55 of file MonitorDB.cc.

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}

◆ get() [2/4]

NSMVarList get ( DBInterface db,
const std::string &  tablename,
const std::string &  vname,
const Date start,
const Date end 
)
static

Definition at line 75 of file MonitorDB.cc.

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}

◆ get() [3/4]

NSMVarList get ( DBInterface db,
const std::string &  tablename,
const std::string &  vname,
int  max 
)
static

Definition at line 65 of file MonitorDB.cc.

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}

◆ get() [4/4]

NSMVarList get ( DBInterface db,
const std::string &  tablename,
const std::string &  vname,
int  max,
const Date start,
const Date end 
)
static

Definition at line 87 of file MonitorDB.cc.

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}

◆ readTable()

NSMVarList readTable ( DBInterface db,
const std::string &  vname 
)
staticprivate

Definition at line 100 of file MonitorDB.cc.

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}

The documentation for this class was generated from the following files: