Belle II Software  release-05-01-25
DAQLogDB.cc
1 #include "daq/slc/database/DAQLogDB.h"
2 
3 #include <daq/slc/system/LogFile.h>
4 
5 #include <daq/slc/base/StringUtil.h>
6 #include <daq/slc/database/DBHandlerException.h>
7 
8 #include <iostream>
9 #include <sstream>
10 #include <cstdio>
11 #include <stdexcept>
12 
13 using namespace Belle2;
14 
15 bool DAQLogDB::createLog(DBInterface& db, const std::string& tablename,
16  const DAQLogMessage& log)
17 {
18  std::stringstream ss;
19  try {
20  if (!db.isConnected()) db.connect();
21  } catch (const DBHandlerException& e) {
22  LogFile::error(e.what());
23  return false;
24  }
25  try {
26  std::string tablename_date = tablename + "_" + Date().toString("%Y");
27  if (!db.checkTable("daqlog")) {
28  db.execute("create table daqlog \n"
29  "(name text not null, \n"
30  "id bigserial, lastupdate timestamp, \n"
31  "UNIQUE(name));");
32  db.execute("create index daqlog_id_index on daqlog(id);",
33  tablename.c_str(), tablename.c_str());
34  }
35  if (!db.checkTable(tablename_date)) {
36  try {
37  db.execute("insert into daqlog (name, lastupdate) values "
38  "('%s', current_timestamp);", tablename_date.c_str());
39  db.execute("create table %s \n"
40  "(node int not null, \n"
41  "priority int not null, \n"
42  "id bigserial, \n"
43  "date timestamp with time zone, \n"
44  "message text not null); ", tablename_date.c_str());
45  } catch (const std::exception& e) {
46  db.execute("update daqlog set lastupdate = current_timestamp where name = '%s';",
47  tablename_date.c_str());
48  }
49  db.execute("create index %s_id_index on %s(id);",
50  tablename_date.c_str(), tablename_date.c_str());
51  }
52  db.execute("select id,category from log_node where name = '" + log.getNodeName() + "';");
53  DBRecordList record(db.loadRecords());
54  int id = 1;
55  //int cid = 1;
56  if (record.size() > 0) {
57  id = record[0].getInt("id");
58  //cid = record[0].getInt("category");
59  } else {
60  db.execute("insert into log_node (name, category) values ('" + log.getNodeName() +
61  "', " + StringUtil::form("%d", log.getCategory()) + ") returning id,category;");
62  DBRecordList record(db.loadRecords());
63  id = record[0].getInt("id");
64  //cid = record[0].getInt("category");
65  }
66  db.execute("insert into %s (node, priority, date, message) values "
67  "(%d, %d, to_timestamp(%d), '%s');",
68  tablename_date.c_str(), id, log.getPriority(),
69  log.getDateInt(), log.getMessage().c_str());
70  } catch (const DBHandlerException& e) {
71  LogFile::error(e.what());
72  return false;
73  }
74  return true;
75 }
76 
77 DAQLogMessageList DAQLogDB::getLogs(DBInterface& db, const std::string& tablename,
78  const std::string& nodename, int max)
79 {
80  std::string tablename_date = tablename + "_" + Date().toString("%Y");
81  DAQLogMessageList logs;
82  try {
83  if (!db.isConnected()) db.connect();
84  if (nodename.size() > 0) {
85  if (max > 0) {
86  db.execute("select n.name, extract(epoch from l.date) date, l.priority, l.message"
87  " from %s as l, log_node as n, log_priority as p where l.node=n.id and p.id=l.priority and n.name = '%s' order by l.id desc limit %d;",
88  tablename_date.c_str(), nodename.c_str(), max);
89  } else {
90  db.execute("select n.name, extract(epoch from l.date) date, l.priority, l.message"
91  " from %s as l, log_node as n, log_priority as p where l.node=n.id and p.id=l.priority and n.name = '%s' order by l.id desc;",
92  tablename_date.c_str(), nodename.c_str());
93  }
94  } else {
95  if (max > 0) {
96  db.execute("select n.name, extract(epoch from l.date) date, l.priority, l.message"
97  " from %s as l, log_node as n, log_priority as p where l.node=n.id and p.id=l.priority order by l.id desc limit %d;",
98  tablename_date.c_str(), max);
99  } else {
100  db.execute("select n.name, extract(epoch from l.date) date, l.priority, l.message"
101  " from %s as l, log_node as n, log_priority as p where l.node=n.id and p.id=l.priority order by l.id desc;",
102  tablename_date.c_str());
103  }
104  }
105  DBRecordList record_v(db.loadRecords());
106  for (size_t i = 0; i < record_v.size(); i++) {
107  DBRecord& record(record_v[i]);
108  logs.push_back(DAQLogMessage(record.get("n.name"),
109  (LogFile::Priority)(record.getInt("priority")),
110  record.get("l.message"), Date(record.getInt("date"))));
111  }
112  } catch (const DBHandlerException& e) {
113  LogFile::error(e.what());
114  }
115  return logs;
116 }
117 
118 DAQLogMessageList DAQLogDB::getLogs(DBInterface& db, const std::string& tablename,
119  const std::string& nodename, const std::string& begin_date,
120  const std::string& end_date, int max, int priority)
121 {
122  std::string tablename_date = tablename + "_" + Date().toString("%Y");
123  DAQLogMessageList logs;
124  try {
125  if (!db.isConnected()) db.connect();
126  std::stringstream ss;
127  ss << "select n.name, extract(epoch from l.date) date, l.priority, l.message"
128  << " from " << tablename_date << " as l, log_node as n where l.node=n.id ";
129  if (nodename.size() > 0) {
130  ss << "and n.name = '" << nodename << "' ";
131  }
132  if (begin_date.size() > 0) {
133  ss << "and l.date >= (timestamp '" << begin_date << "') ";
134  }
135  if (end_date.size() > 0) {
136  ss << "and l.date <= (timestamp '" << end_date << "') ";
137  }
138  if (priority > 0) {
139  ss << "and l.priority >= " << priority << " ";
140  }
141  ss << "order by l.id desc ";
142  if (max > 0) ss << "limit " << max;
143  db.execute(ss.str().c_str());
144  DBRecordList record_v(db.loadRecords());
145  for (size_t i = 0; i < record_v.size(); i++) {
146  DBRecord& record(record_v[i]);
147  logs.push_back(DAQLogMessage(record.get("name"),
148  (LogFile::Priority)(record.getInt("priority")),
149  record.get("message"), Date(record.getInt("date"))));
150  }
151  } catch (const DBHandlerException& e) {
152  LogFile::error(e.what());
153  }
154  return logs;
155 }
Belle2::DBHandlerException
Definition: DBHandlerException.h:12
prepareAsicCrosstalkSimDB.e
e
aux.
Definition: prepareAsicCrosstalkSimDB.py:53
Belle2::DAQLogMessage
Definition: DAQLogMessage.h:16
Belle2::DBRecord
Definition: DBRecord.h:16
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Date
Definition: Date.h:12
Belle2::DBInterface
Definition: DBInterface.h:19