Debian Codegraph @

src/views/summaryview.cc

raw
1 /*
2 This file is part of Icecream.
3
4 Copyright (c) 2004 Scott Wheeler <[email protected]>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "summaryview.h"
22
23 #include "hostinfo.h"
24 #include "job.h"
25
26 #include <qdebug.h>
27
28 #include <qlabel.h>
29 #include <qlayout.h>
30 #include <qpainter.h>
31 #include <QScrollBar>
32 #include <QApplication>
33
34 class NodeInfoFrame
35 : public QFrame
36 {
37 public:
38 NodeInfoFrame(QWidget *parent, const QColor &frameColor)
39 : QFrame(parent)
40 , m_frameColor(frameColor)
41 {
42 setObjectName(QStringLiteral("nodeInfoFrame"));
43 setStyleSheet(QStringLiteral("QFrame#nodeInfoFrame { border: 2px solid %1 }").arg(frameColor.name()));
44 }
45 private:
46 QColor m_frameColor;
47 };
48
49 class SummaryViewScrollArea
50 : public QScrollArea
51 {
52 public:
53 explicit SummaryViewScrollArea(QWidget *parent = nullptr);
54
55 void resizeEvent(QResizeEvent *) override;
56 };
57
58 ////////////////////////////////////////////////////////////////////////////////
59 // SummaryViewItem implementation
60 ////////////////////////////////////////////////////////////////////////////////
61
62 SummaryViewItem::SummaryViewItem(unsigned int hostid, QWidget *parent, SummaryView *view, QGridLayout *layout)
63 : m_jobCount(0)
64 , m_totalJobsLength(0.0f)
65 , m_finishedJobCount(0)
66 , m_totalRequestedJobsLength(0.0f)
67 , m_requestedJobCount(0)
68 , m_view(view)
69 {
70 const int row = layout->rowCount();
71 const QColor nodeColor = view->hostInfoManager()->hostColor(hostid);
72
73 auto labelBox = new NodeInfoFrame(parent, nodeColor);
74 layout->setMargin(5);
75 layout->addWidget(labelBox, row, 0);
76 labelBox->show();
77 labelBox->setMinimumWidth(75);
78 m_widgets.append(labelBox);
79
80 auto labelLayout = new QVBoxLayout(labelBox);
81 labelLayout->setMargin(10);
82 labelLayout->setSpacing(5);
83
84 QLabel *l;
85
86 l = new QLabel(view->nameForHost(hostid), labelBox);
87 l->setAlignment(Qt::AlignCenter);
88 l->show();
89 labelLayout->addWidget(l);
90
91 m_speedLabel = new QLabel(labelBox);
92 m_speedLabel->setToolTip(QApplication::tr("Average job time for a file sent by this client / total number of jobs sent."));
93 m_speedLabel->setAlignment(Qt::AlignCenter);
94 m_speedLabel->show();
95 labelLayout->addWidget(m_speedLabel);
96
97 const int maxJobs = view->hostInfoManager()->maxJobs(hostid);
98
99 m_jobHandlers.resize(maxJobs);
100
101 for (int i = 0; i < maxJobs; i++) {
102 m_jobHandlers[i].stateWidget = new QFrame(labelBox);
103 m_jobHandlers[i].stateWidget->setFrameStyle(QFrame::Box | QFrame::Plain);
104 m_jobHandlers[i].stateWidget->setLineWidth(2);
105 m_jobHandlers[i].stateWidget->setFixedHeight(15);
106 QPalette palette = m_jobHandlers[i].stateWidget->palette();
107 palette.setColor(m_jobHandlers[i].stateWidget->foregroundRole(), Qt::black);
108 m_jobHandlers[i].stateWidget->setPalette(palette);
109 m_jobHandlers[i].stateWidget->show();
110 labelLayout->addWidget(m_jobHandlers[i].stateWidget);
111 }
112
113 auto detailsBox = new NodeInfoFrame(parent, nodeColor);
114 layout->setMargin(5);
115 layout->addWidget(detailsBox, row, 1);
116 detailsBox->show();
117 m_widgets.append(detailsBox);
118
119 auto grid = new QGridLayout(detailsBox);
120 grid->setMargin(10);
121 grid->setSpacing(5);
122
123 m_jobsLabel = addLine(QApplication::tr("Jobs:"), detailsBox, grid, Qt::AlignBottom, QStringLiteral("0"));
124 m_jobsLabel->setToolTip(QApplication::tr("Total number of jobs processed by this server / average duration of each job."));
125
126 for (int i = 0; i < maxJobs; i++) {
127 if (maxJobs > 1) {
128 auto spacer = new QSpacerItem(1, 8, QSizePolicy::Expanding);
129 const int row = grid->rowCount();
130 grid->addItem(spacer, row, 0, 1, grid->columnCount() - 1);
131 }
132 m_jobHandlers[i].sourceLabel = addLine(QApplication::tr("Source:"), detailsBox, grid);
133 m_jobHandlers[i].stateLabel = addLine(QApplication::tr("State:"), detailsBox, grid);
134 }
135
136 grid->setColumnStretch(grid->columnCount() - 1, 1);
137 grid->setRowStretch(0, 1);
138 grid->setRowStretch(grid->rowCount(), 1);
139 }
140
141 SummaryViewItem::~SummaryViewItem()
142 {
143 qDeleteAll(m_widgets);
144 m_widgets.clear();
145 }
146
147 void SummaryViewItem::updateStats()
148 {
149 const double avgDuration = m_finishedJobCount > 0 ? m_totalJobsLength / m_finishedJobCount : 0.0f;
150 m_jobsLabel->setText(QApplication::tr("%1 (Ø duration: %2 ms)").arg(
151 QString::number(m_jobCount),
152 QString::number(avgDuration, 'f', 0)
153 ));
154
155 const double avgTime = m_requestedJobCount > 0 ? m_totalRequestedJobsLength / m_requestedJobCount : 0.0f;
156 if (qIsNull(avgTime)) {
157 m_speedLabel->setText(QString());
158 } else {
159 m_speedLabel->setText(QApplication::tr("Ø job time: %1 ms\nrequested jobs count: %2").arg(
160 QString::number(avgTime, 'f', 0),
161 QString::number(m_requestedJobCount)
162 ));
163 }
164 }
165
166 void SummaryViewItem::updateClient(const Job &job)
167 {
168 if (job.state == Job::Finished) {
169 m_totalRequestedJobsLength += job.real_msec;
170 m_requestedJobCount++;
171 updateStats();
172 }
173 }
174
175 void SummaryViewItem::update(const Job &job)
176 {
177 switch (job.state) {
178 case Job::Compiling:
179 {
180 m_jobCount++;
181 updateStats();
182
183 QVector<JobHandler>::Iterator it = m_jobHandlers.begin();
184 while (it != m_jobHandlers.end() && !(*it).currentFile.isNull())
185 ++it;
186
187 if (it != m_jobHandlers.end()) {
188 const QColor nodeColor = m_view->hostInfoManager()->hostColor(job.client);
189 QPalette palette = (*it).stateWidget->palette();
190 palette.setColor((*it).stateWidget->foregroundRole(), nodeColor);
191 (*it).stateWidget->setPalette(palette);
192 const QString fileName = job.fileName.section(QLatin1Char('/'), -1);
193 const QString hostName = m_view->nameForHost(job.client);
194 (*it).sourceLabel->setText(QStringLiteral("%1 (%2)").arg(fileName).arg(hostName));
195 (*it).stateLabel->setText(job.stateAsString());
196 (*it).currentFile = job.fileName;
197 }
198 break;
199 }
200 case Job::Finished:
201 case Job::Failed:
202 {
203 QVector<JobHandler>::Iterator it = m_jobHandlers.begin();
204 while (it != m_jobHandlers.end() && (*it).currentFile != job.fileName)
205 ++it;
206
207 if (it != m_jobHandlers.end()) {
208 QPalette palette = (*it).stateWidget->palette();
209 palette.setColor((*it).stateWidget->foregroundRole(), Qt::black);
210 (*it).stateWidget->setPalette(palette);
211 (*it).stateWidget->repaint();
212 (*it).sourceLabel->clear();
213 (*it).stateLabel->setText(job.stateAsString());
214 (*it).currentFile = QString();
215 if (job.state == Job::Finished) {
216 m_totalJobsLength += job.real_msec;
217 m_finishedJobCount++;
218 updateStats();
219 }
220 }
221 break;
222 }
223 default:
224 break;
225 }
226 }
227
228 QLabel *SummaryViewItem::addLine(const QString &caption, QWidget *parent,
229 QGridLayout *grid, Qt::Alignment flags,
230 const QString &status)
231 {
232 auto label = new QLabel(caption, parent);
233 label->setAlignment(Qt::AlignRight | flags);
234 const int row = grid->rowCount();
235 grid->addWidget(label, row, 0);
236 auto statusLabel = new QLabel(status, parent);
237 //statusLabel->setAlignment(Qt::AlignLeft | flags);
238 grid->addWidget(statusLabel, row, 1);
239 label->show();
240 statusLabel->show();
241
242 return statusLabel;
243 }
244
245 SummaryViewScrollArea::SummaryViewScrollArea(QWidget *parent)
246 : QScrollArea(parent)
247 {
248 }
249
250 void SummaryViewScrollArea::resizeEvent(QResizeEvent *e)
251 {
252 Q_ASSERT(widget());
253
254 QSize s = e->size();
255
256 setMinimumWidth(widget()->minimumSizeHint().width() + verticalScrollBar()->width());
257 widget()->setMinimumWidth(s.width());
258
259 if (widget()->height() <= s.height()) {
260 widget()->setMinimumHeight(s.height());
261 } else {
262 widget()->setMinimumHeight(widget()->sizeHint().height());
263 }
264
265 QScrollArea::resizeEvent(e);
266 }
267
268 ////////////////////////////////////////////////////////////////////////////////
269 // SummaryView implementation
270 ////////////////////////////////////////////////////////////////////////////////
271
272 SummaryView::SummaryView(QObject *parent)
273 : StatusView(parent)
274 , m_widget(new SummaryViewScrollArea)
275 {
276 m_base = new QWidget;
277 m_widget->setWidget(m_base);
278
279 m_layout = new QGridLayout(m_base);
280 m_layout->setColumnStretch(1, 1);
281 m_layout->setSpacing(5);
282 m_layout->setMargin(5);
283
284 m_widget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
285 m_widget->setMinimumHeight(150);
286 createKnownHosts();
287 }
288
289 SummaryView::~SummaryView()
290 {
291 }
292
293 QWidget *SummaryView::widget() const
294 {
295 return m_widget.data();
296 }
297
298 void SummaryView::update(const Job &job)
299 {
300 if (!job.server)
301 return;
302
303 SummaryViewItem *i = m_items[job.server];
304 if (i)
305 i->update(job);
306
307 i= m_items[job.client];
308 if (i)
309 i->updateClient(job);
310 }
311
312 void SummaryView::createKnownHosts()
313 {
314 if (!hostInfoManager()) {
315 return;
316 }
317
318 qDeleteAll(m_items);
319 m_items.clear();
320
321 const HostInfoManager::HostMap hosts(hostInfoManager()->hostMap());
322
323 foreach(int hostid, hosts.keys()) {
324 checkNode(hostid);
325 }
326 }
327
328 void SummaryView::setMonitor(Monitor *monitor)
329 {
330 StatusView::setMonitor(monitor);
331
332 if (monitor)
333 createKnownHosts();
334 }
335
336 void SummaryView::checkNode(unsigned int hostid)
337 {
338 HostInfo *hostInfo = hostInfoManager()->find(hostid);
339
340 if (!hostInfo) {
341 delete m_items[hostid];
342 m_items.remove(hostid);
343 } else if (!m_items[hostid]) {
344 auto i = new SummaryViewItem(hostid, m_base, this, m_layout);
345 m_items.insert(hostid, i);
346 m_widget->widget()->setMinimumHeight(m_widget->widget()->sizeHint().height());
347 }
348 }
349 void SummaryView::removeNode(unsigned int hostid)
350 {
351 delete m_items[hostid];
352 m_items.remove(hostid);
353 }