summaryrefslogtreecommitdiff
path: root/src/plugins/ctfvisualizer/ctfstatisticsmodel.cpp
blob: f9b16569034edcfbfe72eebb1017aa4f308d60ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company,
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "ctfstatisticsmodel.h"

#include "ctfvisualizerconstants.h"
#include "ctfvisualizertr.h"

#include <tracing/timelineformattime.h>

namespace CtfVisualizer {
namespace Internal {

using json = nlohmann::json;
using namespace Constants;

CtfStatisticsModel::CtfStatisticsModel(QObject *parent)
    : QAbstractTableModel(parent)
{

}

void CtfStatisticsModel::beginLoading()
{
    beginResetModel();
    m_data.clear();
}

void CtfStatisticsModel::addEvent(const QString &title, qint64 durationInNs)
{
    EventData &data = m_data[title];
    ++data.count;
    if (durationInNs >= 0) {
        data.totalDuration += durationInNs;
        data.minDuration = std::min(data.minDuration, durationInNs);
        data.maxDuration = std::max(data.maxDuration, durationInNs);
    }
}

void CtfStatisticsModel::setMeasurementDuration(qint64 timeInNs)
{
    m_measurementDurationInNs = timeInNs;
}

void CtfStatisticsModel::endLoading()
{
    endResetModel();
}

int CtfStatisticsModel::rowCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : m_data.size();
}

int CtfStatisticsModel::columnCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : Column::COUNT;
}

QVariant CtfStatisticsModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    auto it = m_data.cbegin();
    std::advance(it, index.row());
    const QString &title = it.key();

    switch (role) {
    case Qt::TextAlignmentRole:
        switch (index.column()) {
        case Column::Title:
            return Qt::AlignLeft;
        case Column::Count:
        case Column::TotalDuration:
        case Column::RelativeDuration:
        case Column::MinDuration:
        case Column::AvgDuration:
        case Column::MaxDuration:
            return Qt::AlignRight;
        default:
            Q_UNREACHABLE();
            return QVariant();
        }
    case SortRole:
        switch (index.column()) {
        case Column::Title:
            return title;
        case Column::Count:
            return m_data.value(title).count;
        case Column::TotalDuration:
            return m_data.value(title).totalDuration;
        case Column::RelativeDuration:
            return m_data.value(title).totalDuration;
        case Column::MinDuration:
        {
            auto minDuration = m_data.value(title).minDuration;
            return minDuration != std::numeric_limits<qint64>::max() ? minDuration : 0;
        }
        case Column::AvgDuration:
        {
            auto data = m_data.value(title);
            if (data.totalDuration > 0 && data.count > 0)
                return double(data.totalDuration) / data.count;
            return 0;
        }
        case Column::MaxDuration:
            return m_data.value(title).maxDuration;
        default:
            return QVariant();
        }
    case Qt::DisplayRole:
        switch (index.column()) {
        case Column::Title:
            return title;
        case Column::Count:
            return m_data.value(title).count;
        case Column::TotalDuration:
        {
            auto totalDuration = m_data.value(title).totalDuration;
            if (totalDuration > 0)
                return Timeline::formatTime(totalDuration);
            else
                return "-";
        }
        case Column::RelativeDuration:
        {
            auto totalDuration = m_data.value(title).totalDuration;
            if (m_measurementDurationInNs > 0 && totalDuration > 0) {
                const double percent = (totalDuration / double(m_measurementDurationInNs)) * 100;
                return QString("%1 %").arg(percent, 0, 'f', 2);
            } else {
                return "-";
            }
        }
        case Column::MinDuration:
        {
            auto minDuration = m_data.value(title).minDuration;
            if (minDuration != std::numeric_limits<qint64>::max())
                return Timeline::formatTime(minDuration);
            else
                return "-";
        }
        case Column::AvgDuration:
        {
            auto data = m_data.value(title);
            if (data.totalDuration > 0 && data.count > 0)
                return Timeline::formatTime(data.totalDuration / data.count);
            return "-";
        }
        case Column::MaxDuration:
        {
            auto maxDuration = m_data.value(title).maxDuration;
            if (maxDuration > 0)
                return Timeline::formatTime(maxDuration);
            else
                return "-";
        }
        }
    }

    return QVariant();
}

QVariant CtfStatisticsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
        return QAbstractTableModel::headerData(section, orientation, role);

    switch (section) {
    case Column::Title:
        return Tr::tr("Title");
    case Column::Count:
        return Tr::tr("Count");
    case Column::TotalDuration:
        return Tr::tr("Total Time");
    case Column::RelativeDuration:
        return Tr::tr("Percentage");
    case Column::MinDuration:
        return Tr::tr("Minimum Time");
    case Column::AvgDuration:
        return Tr::tr("Average Time");
    case Column::MaxDuration:
        return Tr::tr("Maximum Time");
    default:
        return "";
    }
}

}  // Internal
}  // CtfVisualizer