| 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_view(view) |
| 64 |
{ |
| 65 |
const int row = layout->rowCount(); |
| 66 |
const QColor nodeColor = view->hostInfoManager()->hostColor(hostid); |
| 67 |
|
| 68 |
auto labelBox = new NodeInfoFrame(parent, nodeColor); |
| 69 |
layout->setContentsMargins({5, 5, 5, 5}); |
| 70 |
layout->addWidget(labelBox, row, 0); |
| 71 |
labelBox->show(); |
| 72 |
labelBox->setMinimumWidth(75); |
| 73 |
m_widgets.append(labelBox); |
| 74 |
|
| 75 |
auto labelLayout = new QVBoxLayout(labelBox); |
| 76 |
labelLayout->setContentsMargins({10, 10, 10, 10}); |
| 77 |
labelLayout->setSpacing(5); |
| 78 |
|
| 79 |
QLabel *l; |
| 80 |
|
| 81 |
l = new QLabel(view->nameForHost(hostid), labelBox); |
| 82 |
l->setAlignment(Qt::AlignCenter); |
| 83 |
l->show(); |
| 84 |
labelLayout->addWidget(l); |
| 85 |
|
| 86 |
m_speedLabel = new QLabel(labelBox); |
| 87 |
m_speedLabel->setToolTip(QApplication::tr("Average job time for a file sent by this client / total number of jobs sent.")); |
| 88 |
m_speedLabel->setAlignment(Qt::AlignCenter); |
| 89 |
m_speedLabel->show(); |
| 90 |
labelLayout->addWidget(m_speedLabel); |
| 91 |
|
| 92 |
const int maxJobs = view->hostInfoManager()->maxJobs(hostid); |
| 93 |
|
| 94 |
m_jobHandlers.resize(maxJobs); |
| 95 |
|
| 96 |
for (int i = 0; i < maxJobs; i++) { |
| 97 |
m_jobHandlers[i].stateWidget = new QFrame(labelBox); |
| 98 |
m_jobHandlers[i].stateWidget->setFrameStyle(QFrame::Box | QFrame::Plain); |
| 99 |
m_jobHandlers[i].stateWidget->setLineWidth(2); |
| 100 |
m_jobHandlers[i].stateWidget->setFixedHeight(15); |
| 101 |
QPalette palette = m_jobHandlers[i].stateWidget->palette(); |
| 102 |
palette.setColor(m_jobHandlers[i].stateWidget->foregroundRole(), Qt::black); |
| 103 |
m_jobHandlers[i].stateWidget->setPalette(palette); |
| 104 |
m_jobHandlers[i].stateWidget->show(); |
| 105 |
labelLayout->addWidget(m_jobHandlers[i].stateWidget); |
| 106 |
} |
| 107 |
|
| 108 |
auto detailsBox = new NodeInfoFrame(parent, nodeColor); |
| 109 |
layout->setContentsMargins({5, 5, 5, 5}); |
| 110 |
layout->addWidget(detailsBox, row, 1); |
| 111 |
detailsBox->show(); |
| 112 |
m_widgets.append(detailsBox); |
| 113 |
|
| 114 |
auto grid = new QGridLayout(detailsBox); |
| 115 |
grid->setContentsMargins({10, 10, 10, 10}); |
| 116 |
grid->setSpacing(5); |
| 117 |
|
| 118 |
m_jobsLabel = addLine(QApplication::tr("Jobs:"), detailsBox, grid, Qt::AlignBottom, QStringLiteral("0")); |
| 119 |
m_jobsLabel->setToolTip(QApplication::tr("Total number of jobs processed by this server / average duration of each job.")); |
| 120 |
|
| 121 |
for (int i = 0; i < maxJobs; i++) { |
| 122 |
if (maxJobs > 1) { |
| 123 |
auto spacer = new QSpacerItem(1, 8, QSizePolicy::Expanding); |
| 124 |
const int row = grid->rowCount(); |
| 125 |
grid->addItem(spacer, row, 0, 1, grid->columnCount() - 1); |
| 126 |
} |
| 127 |
m_jobHandlers[i].sourceLabel = addLine(QApplication::tr("Source:"), detailsBox, grid); |
| 128 |
m_jobHandlers[i].stateLabel = addLine(QApplication::tr("State:"), detailsBox, grid); |
| 129 |
} |
| 130 |
|
| 131 |
grid->setColumnStretch(grid->columnCount() - 1, 1); |
| 132 |
grid->setRowStretch(0, 1); |
| 133 |
grid->setRowStretch(grid->rowCount(), 1); |
| 134 |
} |
| 135 |
|
| 136 |
SummaryViewItem::~SummaryViewItem() |
| 137 |
{ |
| 138 |
qDeleteAll(m_widgets); |
| 139 |
m_widgets.clear(); |
| 140 |
} |
| 141 |
|
| 142 |
void SummaryViewItem::updateStats() |
| 143 |
{ |
| 144 |
const double avgDuration = m_finishedJobCount > 0 ? m_totalJobsLength / m_finishedJobCount : 0.0f; |
| 145 |
m_jobsLabel->setText(QApplication::tr("%1 (Ø duration: %2 ms)").arg( |
| 146 |
QString::number(m_jobCount), |
| 147 |
QString::number(avgDuration, 'f', 0) |
| 148 |
)); |
| 149 |
|
| 150 |
const double avgTime = m_requestedJobCount > 0 ? m_totalRequestedJobsLength / m_requestedJobCount : 0.0f; |
| 151 |
if (qIsNull(avgTime)) { |
| 152 |
m_speedLabel->setText(QString()); |
| 153 |
} else { |
| 154 |
m_speedLabel->setText(QApplication::tr("Ø job time: %1 ms\nrequested jobs count: %2").arg( |
| 155 |
QString::number(avgTime, 'f', 0), |
| 156 |
QString::number(m_requestedJobCount) |
| 157 |
)); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
void SummaryViewItem::updateClient(const Job &job) |
| 162 |
{ |
| 163 |
if (job.state == Job::Finished) { |
| 164 |
m_totalRequestedJobsLength += job.real_msec; |
| 165 |
m_requestedJobCount++; |
| 166 |
updateStats(); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
void SummaryViewItem::update(const Job &job) |
| 171 |
{ |
| 172 |
switch (job.state) { |
| 173 |
case Job::Compiling: |
| 174 |
{ |
| 175 |
m_jobCount++; |
| 176 |
updateStats(); |
| 177 |
|
| 178 |
QVector<JobHandler>::Iterator it = m_jobHandlers.begin(); |
| 179 |
while (it != m_jobHandlers.end() && !(*it).currentFile.isNull()) |
| 180 |
++it; |
| 181 |
|
| 182 |
if (it != m_jobHandlers.end()) { |
| 183 |
const QColor nodeColor = m_view->hostInfoManager()->hostColor(job.client); |
| 184 |
QPalette palette = (*it).stateWidget->palette(); |
| 185 |
palette.setColor((*it).stateWidget->foregroundRole(), nodeColor); |
| 186 |
(*it).stateWidget->setPalette(palette); |
| 187 |
const QString fileName = job.fileName.section(QLatin1Char('/'), -1); |
| 188 |
const QString hostName = m_view->nameForHost(job.client); |
| 189 |
(*it).sourceLabel->setText(QStringLiteral("%1 (%2)").arg(fileName).arg(hostName)); |
| 190 |
(*it).stateLabel->setText(job.stateAsString()); |
| 191 |
(*it).currentFile = job.fileName; |
| 192 |
} |
| 193 |
break; |
| 194 |
} |
| 195 |
case Job::Finished: |
| 196 |
case Job::Failed: |
| 197 |
{ |
| 198 |
QVector<JobHandler>::Iterator it = m_jobHandlers.begin(); |
| 199 |
while (it != m_jobHandlers.end() && (*it).currentFile != job.fileName) |
| 200 |
++it; |
| 201 |
|
| 202 |
if (it != m_jobHandlers.end()) { |
| 203 |
QPalette palette = (*it).stateWidget->palette(); |
| 204 |
palette.setColor((*it).stateWidget->foregroundRole(), Qt::black); |
| 205 |
(*it).stateWidget->setPalette(palette); |
| 206 |
(*it).stateWidget->repaint(); |
| 207 |
(*it).sourceLabel->clear(); |
| 208 |
(*it).stateLabel->setText(job.stateAsString()); |
| 209 |
(*it).currentFile = QString(); |
| 210 |
if (job.state == Job::Finished) { |
| 211 |
m_totalJobsLength += job.real_msec; |
| 212 |
m_finishedJobCount++; |
| 213 |
updateStats(); |
| 214 |
} |
| 215 |
} |
| 216 |
break; |
| 217 |
} |
| 218 |
default: |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
QLabel *SummaryViewItem::addLine(const QString &caption, QWidget *parent, |
| 224 |
QGridLayout *grid, Qt::Alignment flags, |
| 225 |
const QString &status) |
| 226 |
{ |
| 227 |
auto label = new QLabel(caption, parent); |
| 228 |
label->setAlignment(Qt::AlignRight | flags); |
| 229 |
const int row = grid->rowCount(); |
| 230 |
grid->addWidget(label, row, 0); |
| 231 |
auto statusLabel = new QLabel(status, parent); |
| 232 |
//statusLabel->setAlignment(Qt::AlignLeft | flags); |
| 233 |
grid->addWidget(statusLabel, row, 1); |
| 234 |
label->show(); |
| 235 |
statusLabel->show(); |
| 236 |
|
| 237 |
return statusLabel; |
| 238 |
} |
| 239 |
|
| 240 |
SummaryViewScrollArea::SummaryViewScrollArea(QWidget *parent) |
| 241 |
: QScrollArea(parent) |
| 242 |
{ |
| 243 |
} |
| 244 |
|
| 245 |
void SummaryViewScrollArea::resizeEvent(QResizeEvent *e) |
| 246 |
{ |
| 247 |
Q_ASSERT(widget()); |
| 248 |
|
| 249 |
QSize s = e->size(); |
| 250 |
|
| 251 |
setMinimumWidth(widget()->minimumSizeHint().width() + verticalScrollBar()->width()); |
| 252 |
widget()->setMinimumWidth(s.width()); |
| 253 |
|
| 254 |
if (widget()->height() <= s.height()) { |
| 255 |
widget()->setMinimumHeight(s.height()); |
| 256 |
} else { |
| 257 |
widget()->setMinimumHeight(widget()->sizeHint().height()); |
| 258 |
} |
| 259 |
|
| 260 |
QScrollArea::resizeEvent(e); |
| 261 |
} |
| 262 |
|
| 263 |
//////////////////////////////////////////////////////////////////////////////// |
| 264 |
// SummaryView implementation |
| 265 |
//////////////////////////////////////////////////////////////////////////////// |
| 266 |
|
| 267 |
SummaryView::SummaryView(QObject *parent) |
| 268 |
: StatusView(parent) |
| 269 |
, m_widget(new SummaryViewScrollArea) |
| 270 |
{ |
| 271 |
m_base = new QWidget; |
| 272 |
m_widget->setWidget(m_base); |
| 273 |
|
| 274 |
m_layout = new QGridLayout(m_base); |
| 275 |
m_layout->setColumnStretch(1, 1); |
| 276 |
m_layout->setSpacing(5); |
| 277 |
m_layout->setContentsMargins({5, 5, 5, 5}); |
| 278 |
|
| 279 |
m_widget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 280 |
m_widget->setMinimumHeight(150); |
| 281 |
createKnownHosts(); |
| 282 |
} |
| 283 |
|
| 284 |
SummaryView::~SummaryView() |
| 285 |
{ |
| 286 |
} |
| 287 |
|
| 288 |
QWidget *SummaryView::widget() const |
| 289 |
{ |
| 290 |
return m_widget.data(); |
| 291 |
} |
| 292 |
|
| 293 |
void SummaryView::update(const Job &job) |
| 294 |
{ |
| 295 |
if (!job.server) |
| 296 |
return; |
| 297 |
|
| 298 |
SummaryViewItem *i = m_items[job.server]; |
| 299 |
if (i) |
| 300 |
i->update(job); |
| 301 |
|
| 302 |
i= m_items[job.client]; |
| 303 |
if (i) |
| 304 |
i->updateClient(job); |
| 305 |
} |
| 306 |
|
| 307 |
void SummaryView::createKnownHosts() |
| 308 |
{ |
| 309 |
if (!hostInfoManager()) { |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
qDeleteAll(m_items); |
| 314 |
m_items.clear(); |
| 315 |
|
| 316 |
const HostInfoManager::HostMap hosts(hostInfoManager()->hostMap()); |
| 317 |
|
| 318 |
foreach(int hostid, hosts.keys()) { |
| 319 |
checkNode(hostid); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
void SummaryView::setMonitor(Monitor *monitor) |
| 324 |
{ |
| 325 |
StatusView::setMonitor(monitor); |
| 326 |
|
| 327 |
if (monitor) |
| 328 |
createKnownHosts(); |
| 329 |
} |
| 330 |
|
| 331 |
void SummaryView::checkNode(unsigned int hostid) |
| 332 |
{ |
| 333 |
HostInfo *hostInfo = hostInfoManager()->find(hostid); |
| 334 |
|
| 335 |
if (!hostInfo) { |
| 336 |
delete m_items[hostid]; |
| 337 |
m_items.remove(hostid); |
| 338 |
} else if (!m_items[hostid]) { |
| 339 |
auto i = new SummaryViewItem(hostid, m_base, this, m_layout); |
| 340 |
m_items.insert(hostid, i); |
| 341 |
m_widget->widget()->setMinimumHeight(m_widget->widget()->sizeHint().height()); |
| 342 |
} |
| 343 |
} |
| 344 |
void SummaryView::removeNode(unsigned int hostid) |
| 345 |
{ |
| 346 |
delete m_items[hostid]; |
| 347 |
m_items.remove(hostid); |
| 348 |
} |