Commit 2e10210a authored by Giacomo Strangolino's avatar Giacomo Strangolino
Browse files

introduced historical view

parent e8a7accf
...@@ -11,134 +11,117 @@ ...@@ -11,134 +11,117 @@
AlarmHistoryTree::AlarmHistoryTree(QWidget *parent) : QTreeView(parent) AlarmHistoryTree::AlarmHistoryTree(QWidget *parent) : QTreeView(parent)
{ {
db = QSqlDatabase::addDatabase("QMYSQL"); db = QSqlDatabase::addDatabase("QMYSQL");
QStringList args = qApp->arguments(); QStringList args = qApp->arguments();
QString dbHost = "log"; QString dbHost = "fcslog-replica-01";
if(args.contains("--log-database-host") && args.size() > args.indexOf("--log-database-host")) if(args.contains("--log-database-host") && args.size() > args.indexOf("--log-database-host")) {
{ dbHost = args.at(args.indexOf("--log-database-host") + 1);
dbHost = args.at(args.indexOf("--log-database-host") + 1); }
//qDebug()<<"Database host: "<<dbHost;
}
db.setHostName(dbHost); db.setHostName(dbHost);
db.setDatabaseName("alarm"); db.setDatabaseName("hdbpp");
db.setUserName("alarm-client"); db.setUserName("hdbppro");
db.setPassword(""); db.setPassword("hdbppro");
if (!db.open()) if (!db.open()) {
{ QString sqlErr = db.lastError().text();
QString sqlErr = db.lastError().text(); QMessageBox::critical(0, "Error", QString("Can't establish connection with db \"alarm\" on host log:\n%1").arg(sqlErr));
QMessageBox::critical(0, "Error", QString("Can't establish connection with db \"alarm\" on host log:\n%1").arg(sqlErr)); }
model = new MySqlModel();
} model->setHeaderData(0, Qt::Horizontal, "Timestamp");
model = new MySqlModel(); setModel(model);
model->setHeaderData(0, Qt::Horizontal, "Timestamp");
setModel(model);
} }
/*
void AlarmHistoryTree::updateView(QString &s)
{
}
*/
void AlarmHistoryTree::applyFilter(AlarmFilter f) void AlarmHistoryTree::applyFilter(AlarmFilter f)
{ {
int i, j; int i, j;
QList<QVariant> rowEls; QList<QVariant> rowEls;
if(_filter_enabled) if(_filter_enabled)
removeFilter(); removeFilter();
int rowCount = model->rowCount(); int rowCount = model->rowCount();
int colCount = model->columnCount(); int colCount = model->columnCount();
QModelIndex root = rootIndex(); QModelIndex root = rootIndex();
for(i = 0; i < rowCount; i++) for(i = 0; i < rowCount; i++)
{ {
rowEls.clear(); rowEls.clear();
for(j = 0; j < colCount; j++) for(j = 0; j < colCount; j++)
rowEls << model->data(model->index(i, j), Qt::DisplayRole); rowEls << model->data(model->index(i, j), Qt::DisplayRole);
if(f.ack_enable) if(f.ack_enable)
{ {
QString sack = f.ack ? "ACK" : "NACK"; QString sack = f.ack ? "ACK" : "NACK";
if(rowEls.size() > ACK && rowEls[ACK] != sack) if(rowEls.size() > ACK && rowEls[ACK] != sack)
setRowHidden(i, root, true); setRowHidden(i, root, true);
} }
if(f.lev) if(f.lev)
{ {
/* must not have been hidden previously */ /* must not have been hidden previously */
if(rowEls.size() > LEVEL && rowEls[LEVEL] != f.level && !isRowHidden(i, root)) if(rowEls.size() > LEVEL && rowEls[LEVEL] != f.level && !isRowHidden(i, root))
setRowHidden(i, root, true); setRowHidden(i, root, true);
} }
if(f.stat) if(f.stat)
{ {
/* must not have been hidden previously */ /* must not have been hidden previously */
if(rowEls.size() > STATUS && rowEls[STATUS] != f.status && !isRowHidden(i, root)) if(rowEls.size() > STATUS && rowEls[STATUS] != f.status && !isRowHidden(i, root))
setRowHidden(i, root, true); setRowHidden(i, root, true);
} }
if(f.grp) if(f.grp)
{ {
/* must not have been hidden previously */ /* must not have been hidden previously */
for(int k = 0; k < f.groups.size(); k++) for(int k = 0; k < f.groups.size(); k++)
{ {
if(rowEls.size() > GROUP && rowEls[GROUP].canConvert(QVariant::String) && !rowEls[GROUP].toString(). if(rowEls.size() > GROUP && rowEls[GROUP].canConvert(QVariant::String) && !rowEls[GROUP].toString().
contains(f.groups[k]) && !isRowHidden(i, root)) contains(f.groups[k]) && !isRowHidden(i, root))
{ {
setRowHidden(i, root, true); setRowHidden(i, root, true);
break; break;
} }
} }
} }
} }
_filter_enabled = true; _filter_enabled = true;
} }
void AlarmHistoryTree::removeFilter() void AlarmHistoryTree::removeFilter() {
{ int i;
int i; QModelIndex root = rootIndex();
for(i = 0; i < model->rowCount(); i++) {
QModelIndex root = rootIndex(); if(isRowHidden(i, root))
setRowHidden(i, root, false);
for(i = 0; i < model->rowCount(); i++) }
{ _filter_enabled = false;
if(isRowHidden(i, root))
setRowHidden(i, root, false);
}
_filter_enabled = false;
// headerItem()->setIcon(ACK, QIcon(ICON_PATH + ""));
// headerItem()->setIcon(LEVEL, QIcon(ICON_PATH + ""));
// headerItem()->setIcon(STATUS, QIcon(ICON_PATH + ""));
// headerItem()->setIcon(GROUP, QIcon(ICON_PATH + ""));
} }
void AlarmHistoryTree::executeQuery(QString &q) void AlarmHistoryTree::executeQuery(QString &q)
{ {
if (!db.isOpen()) if (!db.isOpen())
{ {
if (!db.open()) if (!db.open())
{
QMessageBox::critical(0, "Error", "Can't establish connection with db Alarm on host log");
return;
}
}
model->setQuery(q);
QSqlError error = model->lastError();
if (error.type() != QSqlError::NoError)
{ {
QMessageBox::critical(0, "Database Error", QString("Error reading from Db:\n") + error.text() + "\nreconnecting"); QMessageBox::critical(0, "Error", "Can't establish connection with db Alarm on host log");
db.close(); return;
if (!db.open()) }
{ }
QMessageBox::critical(0, "Error", "Can't establish connection with db FastInterlock on host log"); model->setQuery(q);
return; QSqlError error = model->lastError();
} if (error.type() != QSqlError::NoError)
{
QMessageBox::critical(0, "Database Error", QString("Error reading from Db:\n") + error.text() + "\nreconnecting");
db.close();
if (!db.open())
{
QMessageBox::critical(0, "Error", "Can't establish connection with db FastInterlock on host log");
return;
} }
for(int i = 0; i < header()->count(); i++) }
{ for(int i = 0; i < header()->count(); i++)
resizeColumnToContents(i); {
header()->setSortIndicator(i, Qt::AscendingOrder); resizeColumnToContents(i);
} header()->setSortIndicator(i, Qt::AscendingOrder);
}
} }
...@@ -149,6 +149,14 @@ Alarm::Alarm(CumbiaPool *cu_p, QWidget *parent) : QWidget(parent) ...@@ -149,6 +149,14 @@ Alarm::Alarm(CumbiaPool *cu_p, QWidget *parent) : QWidget(parent)
//connect(ui->pBTestSoundOff, SIGNAL(clicked()), this, SLOT(stopSound())); //connect(ui->pBTestSoundOff, SIGNAL(clicked()), this, SLOT(stopSound()));
/* sound: beep on new alarm */ /* sound: beep on new alarm */
connect(ui->treeWidgetAlarms, SIGNAL(newAlarmDetected()), this, SLOT(beep())); connect(ui->treeWidgetAlarms, SIGNAL(newAlarmDetected()), this, SLOT(beep()));
// database
ui->dateStart->setDate(QDate::currentDate());
ui->dateStop->setDate(QDate::currentDate());
ui->timeStart->setTime(QTime::currentTime());
ui->timeStop->setTime(QTime::currentTime());
connect(ui->pBApplyHistory, SIGNAL(clicked()), this, SLOT(queryDB()));
connect(ui->comboPeriod, SIGNAL(currentIndexChanged(int)), this, SLOT(enableFrameHistoryInterval(int)));
} }
Alarm::~Alarm() { Alarm::~Alarm() {
...@@ -308,6 +316,8 @@ void Alarm::onItemDoubleClicked(const QModelIndex &mi) { ...@@ -308,6 +316,8 @@ void Alarm::onItemDoubleClicked(const QModelIndex &mi) {
} }
void Alarm::tabChanged(int t) { void Alarm::tabChanged(int t) {
if(t == 2)
queryDB();
if(t > 1) { if(t > 1) {
ui->pBAckAll->setHidden(true); ui->pBAckAll->setHidden(true);
ui->pBConfHistory->setHidden(false); ui->pBConfHistory->setHidden(false);
...@@ -344,3 +354,62 @@ void Alarm::closeEvent(QCloseEvent *e) ...@@ -344,3 +354,62 @@ void Alarm::closeEvent(QCloseEvent *e)
e->ignore(); e->ignore();
} }
void Alarm::queryDB()
{
QDateTime startSearch;
QDateTime now = QDateTime::currentDateTime();
QString query;
QDateTime dts, dte;
switch(ui->comboPeriod->currentIndex()) {
case 0:
startSearch = now.addDays(-1);
break;
case 1:
startSearch = now.addDays(-7);
break;
case 2:
startSearch = now.addMonths(-1);
break;
case 3:
dts.setDate(ui->dateStart->date());
dts.setTime(ui->timeStart->time());
dte.setDate(ui->dateStop->date());
dte.setTime(ui->timeStop->time());
break;
}
if(startSearch.isValid()) {
query = QString("SELECT asd.data_time,ac.att_name, "
"COALESCE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(asd.value_r,'0','NORMAL'),'1','ALARM UNACK'),'2','ALARM ACKED'),'3','RETURNED NORMAL'),'4','SHELVED'),'6','DISABLED'),aed.error_desc) as value_r "
"FROM att_scalar_devenum_ro asd "
"JOIN att_conf ac ON asd.att_conf_id=ac.att_conf_id "
"LEFT OUTER JOIN att_error_desc aed ON aed.att_error_desc_id=asd.att_error_desc_id "
"WHERE ac.att_name in (SELECT att_name FROM att_conf WHERE att_name LIKE '%alarm/test/0/%' OR att_name LIKE '%alarm/fermi/test/%') "
"AND asd.data_time > '%1' ORDER BY asd.data_time DESC;").arg(startSearch.toString("yyyy-MM-dd hh:mm:ss.zzz"));
}
else if(dts.isValid() && dte.isValid() && dts < dte) {
query = QString("SELECT asd.data_time,ac.att_name, "
"COALESCE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(asd.value_r,'0','NORMAL'),'1','ALARM UNACK'),'2','ALARM ACKED'),'3','RETURNED NORMAL'),'4','SHELVED'),'6','DISABLED'),aed.error_desc) as value_r "
"FROM att_scalar_devenum_ro asd "
"JOIN att_conf ac ON asd.att_conf_id=ac.att_conf_id "
"LEFT OUTER JOIN att_error_desc aed ON aed.att_error_desc_id=asd.att_error_desc_id "
"WHERE ac.att_name in (SELECT att_name FROM att_conf WHERE att_name LIKE '%alarm/test/0/%' OR att_name LIKE '%alarm/fermi/test/%') "
"AND asd.data_time > '%1' AND asd.data_time < '%2' ORDER BY asd.data_time DESC;").arg(dts.toString("yyyy-MM-dd hh:mm:ss.zzz")).arg(dte.toString("yyyy-MM-dd hh:mm:ss.zzz"));
}
else if(dts.isValid() && dte.isValid() && dts >= dte)
QMessageBox::information(this, "Invalid date / time range", "Stop date/time is ahead of start date/time");
if(query.length() > 0)
ui->twHistory->executeQuery(query);
printf("executing query \"%s\"\n", qstoc(query));
if(ui->cBDynamic->isChecked()) {
applyFilter();
}
}
void Alarm::enableFrameHistoryInterval(int i) {
ui->widgetHistoryConf->setEnabled(i == 3);
}
...@@ -53,6 +53,7 @@ public: ...@@ -53,6 +53,7 @@ public:
public slots: public slots:
void queryDB();
protected slots: protected slots:
void refresh(const CuData &da); void refresh(const CuData &da);
void onDevList(const CuData& da); void onDevList(const CuData& da);
...@@ -79,6 +80,8 @@ protected slots: ...@@ -79,6 +80,8 @@ protected slots:
protected: protected:
void closeEvent(QCloseEvent *); void closeEvent(QCloseEvent *);
private slots:
void enableFrameHistoryInterval(int i);
private: private:
void insertItem(const QStringList &s, int index); void insertItem(const QStringList &s, int index);
void tangoStringListCommand(const QString &device_name, const QStringList &names, const char *cmdname); void tangoStringListCommand(const QString &device_name, const QStringList &names, const char *cmdname);
......
...@@ -73,6 +73,178 @@ ...@@ -73,6 +73,178 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>History</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="AlarmHistoryTree" name="twHistory"/>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Configure History Parameters</string>
</property>
<property name="checkable">
<bool>true</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>
</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="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>
</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="1" column="3">
<widget class="QPushButton" name="pBApplyHistory">
<property name="text">
<string>Apply</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tabTest"> <widget class="QWidget" name="tabTest">
<attribute name="title"> <attribute name="title">
<string>Test</string> <string>Test</string>
...@@ -513,6 +685,11 @@ ...@@ -513,6 +685,11 @@
<extends>QListView</extends> <extends>QListView</extends>
<header>alarminfow.h</header> <header>alarminfow.h</header>
</customwidget> </customwidget>
<customwidget>
<class>AlarmHistoryTree</class>
<extends>QTreeView</extends>
<header>AlarmHistoryTree.h</header>
</customwidget>
</customwidgets> </customwidgets>
<resources/> <resources/>
<connections/> <connections/>
......
...@@ -32,15 +32,15 @@ MySqlModel::MySqlModel(QObject *parent) : QSqlQueryModel(parent) ...@@ -32,15 +32,15 @@ MySqlModel::MySqlModel(QObject *parent) : QSqlQueryModel(parent)
QVariant MySqlModel::data(const QModelIndex &index, int role) const QVariant MySqlModel::data(const QModelIndex &index, int role) const
{ {
QVariant value = QSqlQueryModel::data(index, role); QVariant value = QSqlQueryModel::data(index, role);
if (value.isValid() && role == Qt::DisplayRole) { // if (value.isValid() && role == Qt::DisplayRole) {
if (index.column() == 0) // if (index.column() == 0)
{ // {
QDateTime data; // QDateTime data;
double v = value.toDouble(); // double v = value.toDouble();
data.setTime_t((int) v); // data.setTime_t((int) v);
data = data.addMSecs(1000 *(v - (int) v)); // data = data.addMSecs(1000 *(v - (int) v));
return data.toString("d MMM yyyy hh:mm:ss.zzz"); // return data.toString("d MMM yyyy hh:mm:ss.zzz");
} // }
} // }
return value; return value;
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment