AlarmHistoryTree.cpp 3.82 KB
Newer Older
Giacomo Strangolino's avatar
Giacomo Strangolino committed
1
2
3
4
5
6
7
8
9
#include <QSqlError>
#include <QTreeWidgetItem>
#include <QMessageBox>
#include "AlarmHistoryTree.h"
#include "mysqlmodel.h"
#include <QHeaderView>
#include <QtDebug>
#include <QApplication>
#include <QCoreApplication>
10
#include <cumacros.h>
Giacomo Strangolino's avatar
Giacomo Strangolino committed
11
12
13

AlarmHistoryTree::AlarmHistoryTree(QWidget *parent) : QTreeView(parent)
{
14
15
16
17
18
19
    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);
    }
Giacomo Strangolino's avatar
Giacomo Strangolino committed
20
    db.setHostName(dbHost);
21
22
23
24
25
26
27
    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));
    }
28
29
30
    m_sql_model = new MySqlModel();
    m_sql_model->setHeaderData(0, Qt::Horizontal, "Timestamp");
    setModel(m_sql_model);
Giacomo Strangolino's avatar
Giacomo Strangolino committed
31
32
33
34
}

void AlarmHistoryTree::applyFilter(AlarmFilter f)
{   
35
36
37
38
    int i, j;
    QList<QVariant> rowEls;
    if(_filter_enabled)
        removeFilter();
Giacomo Strangolino's avatar
Giacomo Strangolino committed
39

40
41
    int rowCount = m_sql_model->rowCount();
    int colCount = m_sql_model->columnCount();
42
    QModelIndex root = rootIndex();
Giacomo Strangolino's avatar
Giacomo Strangolino committed
43

44
45
46
47
    for(i = 0; i < rowCount; i++)
    {
        rowEls.clear();
        for(j = 0; j < colCount; j++)
48
            rowEls << m_sql_model->data(m_sql_model->index(i, j),  Qt::DisplayRole);
Giacomo Strangolino's avatar
Giacomo Strangolino committed
49

50
51
52
53
54
55
        if(f.ack_enable)
        {
            QString sack = f.ack ? "ACK" : "NACK";
            if(rowEls.size() > ACK && rowEls[ACK] != sack)
                setRowHidden(i, root, true);
        }
Giacomo Strangolino's avatar
Giacomo Strangolino committed
56

57
58
59
60
61
62
        if(f.lev)
        {
            /* must not have been hidden previously */
            if(rowEls.size() > LEVEL && rowEls[LEVEL] != f.level && !isRowHidden(i, root))
                setRowHidden(i, root, true);
        }
Giacomo Strangolino's avatar
Giacomo Strangolino committed
63

64
65
66
67
68
69
        if(f.stat)
        {
            /* must not have been hidden previously */
            if(rowEls.size() > STATUS && rowEls[STATUS] != f.status && !isRowHidden(i, root))
                setRowHidden(i, root, true);
        }
Giacomo Strangolino's avatar
Giacomo Strangolino committed
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        if(f.grp)
        {
            /* must not have been hidden previously */
            for(int k = 0; k < f.groups.size(); k++)
            {
                if(rowEls.size() > GROUP && rowEls[GROUP].canConvert(QVariant::String) && !rowEls[GROUP].toString().
                        contains(f.groups[k]) && !isRowHidden(i, root))
                {
                    setRowHidden(i, root, true);
                    break;
                }
            }
        }
    }
    _filter_enabled = true;
Giacomo Strangolino's avatar
Giacomo Strangolino committed
86
87
88

}

89
90
91
void AlarmHistoryTree::removeFilter() {
    int i;
    QModelIndex root = rootIndex();
92
    for(i = 0; i < m_sql_model->rowCount(); i++) {
93
94
95
96
        if(isRowHidden(i, root))
            setRowHidden(i, root, false);
    }
    _filter_enabled = false;
Giacomo Strangolino's avatar
Giacomo Strangolino committed
97
98
}

99
100
101
102
QAbstractItemModel *AlarmHistoryTree::model() const {
    return m_sql_model;
}

Giacomo Strangolino's avatar
Giacomo Strangolino committed
103
104
void AlarmHistoryTree::executeQuery(QString &q)
{
105
106
107
    if (!db.isOpen())
    {
        if (!db.open())
Giacomo Strangolino's avatar
Giacomo Strangolino committed
108
        {
109
110
111
112
            QMessageBox::critical(0, "Error", "Can't establish connection with db Alarm on host log");
            return;
        }
    }
113
114
    m_sql_model->setQuery(q);
    QSqlError error = m_sql_model->lastError();
115
116
117
118
119
120
121
122
    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;
Giacomo Strangolino's avatar
Giacomo Strangolino committed
123
        }
124
125
126
127
128
129
    }
    for(int i = 0; i < header()->count(); i++)
    {
        resizeColumnToContents(i);
        header()->setSortIndicator(i, Qt::AscendingOrder);
    }
Giacomo Strangolino's avatar
Giacomo Strangolino committed
130
131
}