Commit 02dfd2ab authored by Giacomo Strangolino's avatar Giacomo Strangolino
Browse files

database parameters from free prop

parent df46fff3
......@@ -8,23 +8,10 @@
#include <QApplication>
#include <QCoreApplication>
#include <cumacros.h>
#include <cudata.h>
#include <qustring.h>
AlarmHistoryTree::AlarmHistoryTree(QWidget *parent) : QTreeView(parent)
{
db = QSqlDatabase::addDatabase("QMYSQL");
QStringList args = qApp->arguments();
QString dbHost = "fcslog-replica-01";
if(args.contains("--log-database-host") && args.size() > args.indexOf("--log-database-host")) {
dbHost = args.at(args.indexOf("--log-database-host") + 1);
}
db.setHostName(dbHost);
db.setDatabaseName("hdbpp");
db.setUserName("hdbppro");
db.setPassword("hdbppro");
if (!db.open()) {
QString sqlErr = db.lastError().text();
QMessageBox::critical(0, "Error", QString("Can't establish connection with db \"alarm\" on host log:\n%1").arg(sqlErr));
}
AlarmHistoryTree::AlarmHistoryTree(QWidget *parent) : QTreeView(parent) {
m_sql_model = new MySqlModel();
m_sql_model->setHeaderData(0, Qt::Horizontal, "Timestamp");
setModel(m_sql_model);
......@@ -50,7 +37,7 @@ void AlarmHistoryTree::applyFilter(AlarmFilter f)
printf("AlarmHistoryTree.applyFilter: status %s row status %s\n", qstoc(f.status), qstoc(rowEls[STATUS].toString()));
if(rowEls.size() > STATUS && rowEls[STATUS] != f.status && !isRowHidden(i, root))
setRowHidden(i, root, true);
}
}
}
_filter_enabled = true;
......@@ -72,10 +59,38 @@ QAbstractItemModel *AlarmHistoryTree::model() const {
void AlarmHistoryTree::onFilterNameChanged(const QString &f) {
for(int i = 0; i < m_sql_model->rowCount(); i++) {
setRowHidden(i, QModelIndex(), !f.isEmpty() && !m_sql_model->data(m_sql_model->index(i, NAME), Qt::DisplayRole).toString().contains(f));
setRowHidden(i, QModelIndex(), !f.isEmpty() && !m_sql_model->data(m_sql_model->index(i, NAME), Qt::DisplayRole).toString().contains(f));
}
}
void AlarmHistoryTree::onDbParamsReady(const CuData &p) {
QString m;
if(p.B("err")) {
m = QuString(p, "msg");
printf("AlarmHistoryTree::onDbParamsReady error ? %s\n", datos(p));
}
else {
db = QSqlDatabase::addDatabase("QMYSQL");
std::vector<std::string> pa = p["value"].toStringVector();
if(pa.size() != 5)
m = QString("HistoryDB property: expected 4 parameters (host, user, password, database, port), found %1").arg(pa.size());
else {
db.setHostName(pa[0].c_str());
db.setUserName(pa[1].c_str());
db.setPassword(pa[2].c_str());
db.setDatabaseName(pa[3].c_str());
db.setPort(QuString(pa[4]).toInt());
if (!db.open())
m = QString("Can't establish connection with db with params %s:\n%1").arg(p["value"].toString().c_str()).arg(db.lastError().text());
else
printf("AlarmHistoryTree::onDbParamsReady: successfully opened db\n");
}
}
if(!m.isEmpty())
QMessageBox::critical(0, "Error", m);
}
void AlarmHistoryTree::executeQuery(QString &q) {
if (!db.isOpen())
{
......
......@@ -8,6 +8,7 @@
#include <QSqlDatabase>
class MySqlModel;
class CuData;
class AlarmHistoryTree : public QTreeView
{
......@@ -22,6 +23,7 @@ class AlarmHistoryTree : public QTreeView
public slots:
void onFilterNameChanged(const QString& f);
void onDbParamsReady(const CuData& p);
protected:
......
......@@ -37,92 +37,11 @@ AlarmTreeWidget::AlarmTreeWidget(QWidget *parent) : QTreeWidget(parent)
connect(this, SIGNAL(itemEntered(QTreeWidgetItem*,int)), this, SLOT(slotItemEntered(QTreeWidgetItem*,int)));
}
void AlarmTreeWidget::applyFilter(AlarmFilter f)
{
QList<QTreeWidgetItem *> items;
int i;
if(_filter_enabled)
removeFilter();
items = findItems("*", Qt::MatchWildcard);
if(f.ack_enable) {
QString sack = f.ack ? "ACK" : "NACK";
for(i = 0; i < items.size(); i++)
if(items[i]->text(ACK) != sack)
items[i]->setHidden(true);
headerItem()->setIcon(ACK, QIcon(ICON_PATH + "filter.png"));
}
else
headerItem()->setIcon(ACK, QIcon(ICON_PATH + ""));
if(f.lev) {
for(i = 0; i < items.size(); i++) {
/* must not have been hidden previously */
if(!items[i]->isHidden()) {
if(items[i]->text(LEVEL) != f.level)
items[i]->setHidden(true);
}
}
headerItem()->setIcon(LEVEL, QIcon(ICON_PATH + "filter.png"));
}
else
headerItem()->setIcon(LEVEL, QIcon(ICON_PATH + ""));
if(f.stat) {
for(i = 0; i < items.size(); i++) {
/* must not have been hidden previously */
if(!items[i]->isHidden()) {
if(items[i]->text(STATUS) != f.status)
items[i]->setHidden(true);
}
}
headerItem()->setIcon(STATUS, QIcon(ICON_PATH + "filter.png"));
}
else
headerItem()->setIcon(STATUS, QIcon(ICON_PATH + ""));
if(f.grp) {
for(i = 0; i < items.size(); i++) {
/* must not have been hidden previously */
if(!items[i]->isHidden())
{
for(int j = 0; j < f.groups.size(); j++) {
if(!items[i]->text(DEVICE).contains(f.groups[j])) {
items[i]->setHidden(true);
break;
}
}
}
}
headerItem()->setIcon(DEVICE, QIcon(ICON_PATH + "filter.png"));
}
else
headerItem()->setIcon(DEVICE, QIcon(ICON_PATH + ""));
_filter_enabled = true;
}
void AlarmTreeWidget::setType(AlarmTreeWidget::Type t) {
m_type = t;
}
void AlarmTreeWidget::removeFilter()
{
QList<QTreeWidgetItem *> items;
int i;
items = findItems("*", Qt::MatchWildcard);
for(i = 0; i < items.size(); i++)
if(items[i]->isHidden())
items[i]->setHidden(false);
_filter_enabled = false;
headerItem()->setIcon(ACK, QIcon(ICON_PATH + ""));
headerItem()->setIcon(LEVEL, QIcon(ICON_PATH + ""));
headerItem()->setIcon(STATUS, QIcon(ICON_PATH + ""));
headerItem()->setIcon(DEVICE, QIcon(ICON_PATH + ""));
}
void AlarmTreeWidget::refresh(const CuData &v) {
double d = static_cast<int>(v["timestamp_us"].toDouble());
QString ts = QDateTime::fromSecsSinceEpoch(d).toString("HH:mm:ss");
......@@ -141,7 +60,7 @@ void AlarmTreeWidget::refresh(const CuData &v) {
// only AlarmType adds an entry for each error
if (err && m_type == AlarmType) {
data << m_gen_error_item(v);
perr("%s: %s\n", qstoc(src), qstoc(toolTip()));
// perr("%s: %s\n", qstoc(src), datos(v));
m_src_err.insert(src);
}
else {
......@@ -324,7 +243,7 @@ QString AlarmTreeWidget::m_gen_error_item(const CuData &da) const {
QString dev = QuString(da, "src"), msg = QuString(da, "msg");
dev = dev.section('/', 0, dev.count('/') - 1);
l << QString::number(dt.toSecsSinceEpoch()) << "-" << dev << "ERROR" << "NACK" << "high" << "-1" << "gr_handler"
<< msg.replace("\n", " ") << "NEW";
<< "Alarm server error: " + msg.replace("\n", " ") << "NEW";
return l.join("\t");
}
......
......@@ -19,9 +19,7 @@ public:
enum Columns { DATET, MICROSEC, ALARM, STATUS, ACK, LEVEL, SILENCED, DEVICE, MESSAGE, COLUMN_COUNT };
AlarmTreeWidget(QWidget *parent);
void applyFilter(AlarmFilter f);
void setType(Type t);
void removeFilter();
int findPos(time_t ts, const QString& pri, const QString &status);
int countAll() const;
int countAlarmed() const;
......
......@@ -88,6 +88,14 @@ Alarm::Alarm(CumbiaPool *cu_p, QWidget *parent) : QWidget(parent)
connect(db_dlist_w, SIGNAL(newData(CuData)), this, SLOT(onDevList(CuData)));
db_dlist_w->setSource(QString("tango://#AlarmHandler#%1").arg(devlist_prop));
// db params
QuWatcher *db_conn_p = new QuWatcher(this, cu_pool, m_ctrl_factory_pool); // device prop watcher
db_conn_p->setObjectName("db_conn_p");
db_conn_p->setSingleShot(true);
db_conn_p->setAutoDestroy(true);
connect(db_conn_p, SIGNAL(newData(CuData)), ui->twHistory, SLOT(onDbParamsReady(CuData)));
db_conn_p->setSource(QString("tango://#AlarmHandler#HistoryDb"));
// groups
QuWatcher *grpW = new QuWatcher(this, cu_pool, m_ctrl_factory_pool);
grpW->setObjectName("group_w");
......@@ -119,9 +127,6 @@ Alarm::Alarm(CumbiaPool *cu_p, QWidget *parent) : QWidget(parent)
sound = new Sound(soundFileName);
connect( ui->pBTestSoundOn,SIGNAL(clicked()), sound, SLOT(play()));
connect( ui->pBTestSoundOff,SIGNAL(clicked()), sound, SLOT(stop()));
ui->groupBoxFilter->setHidden(true);
connect(ui->pBFilter, SIGNAL(toggled(bool)), ui->groupBoxFilter, SLOT(setVisible(bool)));
connect(ui->cBStatus, SIGNAL(toggled(bool)), ui->comboStatus, SLOT(setEnabled(bool)));
ui->pbInfo->setEnabled(false);
......@@ -141,6 +146,8 @@ Alarm::Alarm(CumbiaPool *cu_p, QWidget *parent) : QWidget(parent)
/* sound: beep on new alarm */
connect(ui->treeWidgetAlarms, SIGNAL(newAlarmDetected()), this, SLOT(beep()));
ui->tabWidget->setCurrentIndex(0);
// database
ui->dateStart->setDate(QDate::currentDate());
ui->dateStop->setDate(QDate::currentDate());
......@@ -151,6 +158,9 @@ Alarm::Alarm(CumbiaPool *cu_p, QWidget *parent) : QWidget(parent)
connect(ui->leFilterName, SIGNAL(textChanged(QString)), this, SLOT(onFilterNameChanged(QString)));
connect(ui->comboStatus, SIGNAL(currentTextChanged(QString)), this, SLOT(onFilterStatusChanged(QString)));
connect(ui->cBStatus, SIGNAL(toggled(bool)), this, SLOT(onFilterStatusEnabled(bool)));
connect(ui->pbClearFilter, SIGNAL(clicked()), ui->leFilterName, SLOT(clear()));
m_filter = new AlarmFilter(ui->twHistory);
}
Alarm::~Alarm() {
......@@ -158,6 +168,7 @@ Alarm::~Alarm() {
sound->stop();
delete sound;
}
delete m_filter;
}
void Alarm::refresh(const CuData &da) {
......@@ -167,6 +178,8 @@ void Alarm::refresh(const CuData &da) {
QString tn = "%1 [%2]";
if(att == "alarm") ui->tabWidget->setTabText(0, tn.arg("Alarm").arg(tw->countAlarmed()));
else ui->tabWidget->setTabText(1, tn.arg("Disabled Alarms").arg(tw->countAll()));
// filter
m_filter->filter();
}
void Alarm::onDevList(const CuData &da) {
......@@ -269,13 +282,13 @@ void Alarm::onItemDoubleClicked(const QModelIndex &mi) {
}
void Alarm::onFilterNameChanged(const QString &n) {
AlarmFilter f;
f.filter(ui->twHistory, "Alarm", n);
n.isEmpty() ? m_filter->rem("Alarm") : m_filter->add("Alarm", n);
m_filter->filter();
}
void Alarm::onFilterStatusChanged(const QString &s) {
AlarmFilter f;
f.filter(ui->twHistory, "Status", s);
s.isEmpty() ? m_filter->rem("Status") : m_filter->add("Status", s);
m_filter->filter();
}
void Alarm::onFilterStatusEnabled(bool en) {
......@@ -283,8 +296,6 @@ void Alarm::onFilterStatusEnabled(bool en) {
}
void Alarm::tabChanged(int t) {
ui->pBFilter->setVisible(t == 2);
ui->groupBoxFilter->setEnabled(t ==2 );
if(t == 2) {
QApplication::setOverrideCursor(Qt::WaitCursor);
QTimer::singleShot(500, this, SLOT(queryDB()));
......
......@@ -92,6 +92,7 @@ private:
Ui::Alarm *ui;
Sound *sound;
bool filtering;
AlarmFilter *m_filter;
// cumbia
CumbiaPool *cu_pool;
......
......@@ -27,7 +27,7 @@
</property>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
<number>2</number>
</property>
<widget class="QWidget" name="tabAlarms">
<attribute name="title">
......@@ -89,156 +89,235 @@
<property name="checkable">
<bool>false</bool>
</property>
<layout class="QGridLayout" name="_2">
<property name="leftMargin">
<number>7</number>
</property>
<property name="topMargin">
<number>7</number>
</property>
<property name="rightMargin">
<number>7</number>
</property>
<property name="bottomMargin">
<number>7</number>
</property>
<property name="spacing">
<number>6</number>
</property>
<item row="0" column="0">
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Minimum</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>191</width>
<height>20</height>
</size>
</property>
</spacer>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="3">
<widget class="QPushButton" name="pBApplyHistory">
<property name="text">
<string>Update</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QWidget" name="widgetHistoryConf" native="true">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QGridLayout" name="_3">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<property name="spacing">
<number>6</number>
</property>
<item row="0" column="2">
<widget class="QTimeEdit" name="timeStart"/>
</item>
<item row="0" column="1">
<widget class="QDateEdit" name="dateStart">
<property name="minimumSize">
<size>
<width>80</width>
<height>0</height>
</size>
</property>
<property name="currentSection">
<enum>QDateTimeEdit::MonthSection</enum>
</property>
<property name="calendarPopup">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QDateEdit" name="dateStop">
<property name="calendarPopup">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>To:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QTimeEdit" name="timeStop"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>From:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="comboPeriod">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<item>
<property name="text">
<string>Last Day</string>
</property>
</item>
<item>
<property name="text">
<string>Last Week</string>
</property>
</item>
<item>
<property name="text">
<string>Last Month</string>
</property>
</item>
<item>
<property name="text">
<string>Interval</string>
</property>
</item>
</widget>
</item>
<item row="0" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Filter</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="1" column="1" colspan="3">
<widget class="Line" name="line_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="comboPeriod">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<property name="text">
<string>Last Day</string>
</property>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Name</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<property name="text">
<string>Last Week</string>
</property>
<widget class="QLineEdit" name="leFilterName"/>
</item>
<item>
<property name="text">
<string>Last Month</string>
</property>
<widget class="QPushButton" name="pbClearFilter">
<property name="text">
<string>Clear</string>
</property>
</widget>
</item>
<item>
<property name="text">
<string>Interval</string>
</property>
<widget class="QCheckBox" name="cBStatus">
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string>Status</string>
</property>
</widget>
</item>
</widget>
</item>
<item row="0" column="2" rowspan="2">
<widget class="QWidget" name="widgetHistoryConf" native="true">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QGridLayout" name="_3">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<property name="spacing">
<number>6</number>
</property>
<item row="0" column="2">
<widget class="QTimeEdit" name="timeStart"/>
</item>
<item row="0" column="1">
<widget class="QDateEdit" name="dateStart">
<property name="minimumSize">
<size>
<width>80</width>
<height>0</height>
</size>
</property>
<property name="currentSection">
<enum>QDateTimeEdit::MonthSection</enum>
<item>
<widget class="QComboBox" name="comboStatus">
<property name="enabled">
<bool>false</bool>
</property>
<item>
<property name="text">
<string>ALARM</string>
</property>
<property name="calendarPopup">
<bool>true</bool>
</item>