| 1 |
//------------------------------------------------------------------------------ |
| 2 |
// Author: Vitali Adamenka |
| 3 |
// Description: Source for describe ncurses statistics window. |
| 4 |
// Copyright (c) 2015 EPAM Systems. All Rights Reserved. |
| 5 |
//------------------------------------------------------------------------------ |
| 6 |
/* |
| 7 |
This file is part of Nfstrace. |
| 8 |
|
| 9 |
Nfstrace is free software: you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation, version 2 of the License. |
| 12 |
|
| 13 |
Nfstrace is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with Nfstrace. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
//------------------------------------------------------------------------------ |
| 22 |
#include <algorithm> |
| 23 |
#include <numeric> |
| 24 |
#include <stdexcept> |
| 25 |
|
| 26 |
#include <unistd.h> |
| 27 |
|
| 28 |
#include "nc_window_const.h" |
| 29 |
#include "statistics_window.h" |
| 30 |
//------------------------------------------------------------------------------ |
| 31 |
namespace STATISTICS |
| 32 |
{ |
| 33 |
const int PROTOCOLS_LINE = 1; |
| 34 |
const int FIRST_OPERATION_LINE = 3; |
| 35 |
const int DEFAULT_LINES = 10; |
| 36 |
const int DEFAULT_GROUP = 1; |
| 37 |
} |
| 38 |
|
| 39 |
void StatisticsWindow::destroy() |
| 40 |
{ |
| 41 |
if(_window == nullptr) |
| 42 |
{ |
| 43 |
return; |
| 44 |
} |
| 45 |
werase(_window); |
| 46 |
wclear(_window); |
| 47 |
delwin(_window); |
| 48 |
_window = nullptr; |
| 49 |
} |
| 50 |
|
| 51 |
bool StatisticsWindow::canWrite(unsigned int i) |
| 52 |
{ |
| •53 |
return (i >= _scrollOffset.at(_activeProtocol) + STATISTICS::FIRST_OPERATION_LINE && i - _scrollOffset.at(_activeProtocol) + BORDER_SIZE < static_cast<unsigned int>(getmaxy(_window) - 1)); |
| 54 |
} |
| 55 |
|
| 56 |
StatisticsWindow::StatisticsWindow(MainWindow& w, StatisticsContainers& c) |
| 57 |
: _window{nullptr} |
| 58 |
, _activeProtocol{nullptr} |
| 59 |
{ |
| 60 |
if(w._window == nullptr) |
| 61 |
{ |
| 62 |
throw std::runtime_error("Initialization of Header window failed."); |
| 63 |
} |
| 64 |
for(auto i : c) |
| 65 |
{ |
| 66 |
_allProtocols.push_back((i.first)->getProtocolName()); |
| 67 |
_scrollOffset.insert(std::make_pair<AbstractProtocol*, std::size_t>(reinterpret_cast<AbstractProtocol*>(i.first), 0)); |
| 68 |
}; |
| 69 |
_activeProtocol = (c.begin())->first; |
| 70 |
_statistic = c.at(_activeProtocol); |
| 71 |
resize(w); |
| 72 |
} |
| 73 |
|
| 74 |
StatisticsWindow::~StatisticsWindow() |
| 75 |
{ |
| 76 |
} |
| 77 |
|
| 78 |
void StatisticsWindow::scrollContent(int i) |
| 79 |
{ |
| 80 |
if(i > 0 && _scrollOffset.at(_activeProtocol) <= MAXSHIFT - SHIFTCU) |
| 81 |
{ |
| 82 |
_scrollOffset.at(_activeProtocol) += SHIFTCU; |
| 83 |
updateProtocol(_activeProtocol); |
| 84 |
} |
| 85 |
else if(i < 0 && _scrollOffset.at(_activeProtocol) >= SHIFTCU) |
| 86 |
{ |
| 87 |
_scrollOffset.at(_activeProtocol) -= SHIFTCU; |
| 88 |
updateProtocol(_activeProtocol); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
void StatisticsWindow::updateProtocol(AbstractProtocol* p) |
| 93 |
{ |
| 94 |
if(p != nullptr) |
| 95 |
_activeProtocol = p; |
| 96 |
if(_window == nullptr) |
| 97 |
{ |
| 98 |
return; |
| 99 |
} |
| 100 |
werase(_window); |
| 101 |
wborder(_window, ACS_VLINE, ACS_VLINE, ACS_HLINE, ACS_HLINE, ACS_ULCORNER, ACS_URCORNER, ACS_LLCORNER, ACS_LRCORNER); |
| 102 |
|
| 103 |
std::string tmp(" "); |
| 104 |
for_each(_allProtocols.begin(), _allProtocols.end(), [&](std::string& s) { |
| 105 |
if(!s.compare(_activeProtocol->getProtocolName())) |
| 106 |
{ |
| 107 |
tmp += std::string(" < ") + s + std::string(" > "); |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
tmp += std::string(" ") + s + std::string(" "); |
| 112 |
} |
| 113 |
|
| 114 |
}); |
| 115 |
mvwprintw(_window, STATISTICS::PROTOCOLS_LINE, FIRST_CHAR_POS, "%s", tmp.c_str()); |
| 116 |
|
| 117 |
unsigned int line = STATISTICS::FIRST_OPERATION_LINE; |
| 118 |
for(unsigned int i = STATISTICS::DEFAULT_GROUP; i <= _activeProtocol->getGroups(); i++) |
| 119 |
{ |
| 120 |
if(canWrite(line)) |
| 121 |
{ |
| 122 |
mvwprintw(_window, line - (_scrollOffset.at(p)), FIRST_CHAR_POS, "%s", "Total:"); |
| 123 |
} |
| 124 |
line++; |
| 125 |
for(unsigned int j = _activeProtocol->getGroupBegin(i); j < _activeProtocol->getGroupBegin(i + 1); j++) |
| 126 |
{ |
| 127 |
if(canWrite(line)) |
| 128 |
{ |
| 129 |
mvwprintw(_window, line - (_scrollOffset.at(p)), FIRST_CHAR_POS, "%s", p->printProcedure(j)); |
| 130 |
} |
| 131 |
line++; |
| 132 |
} |
| 133 |
line++; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void StatisticsWindow::update(const ProtocolStatistic& d) |
| 138 |
{ |
| 139 |
_statistic = d; |
| 140 |
if(_statistic.empty() || _window == nullptr) |
| 141 |
{ |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
unsigned int line = STATISTICS::FIRST_OPERATION_LINE; |
| 146 |
for(unsigned int i = STATISTICS::DEFAULT_GROUP; i <= _activeProtocol->getGroups(); i++) |
| 147 |
{ |
| 148 |
std::size_t m = 0; // sum of all counters |
| 149 |
|
| 150 |
for(std::size_t tmp = _activeProtocol->getGroupBegin(i); tmp < _activeProtocol->getGroupBegin(i + 1); tmp++) |
| 151 |
{ |
| 152 |
m += _statistic[tmp]; |
| 153 |
} |
| 154 |
if(canWrite(line)) |
| 155 |
{ |
| •156 |
mvwprintw(_window, line - (_scrollOffset.at(_activeProtocol)), FIRST_CHAR_POS + 25, "%zu", m); |
| 157 |
} |
| 158 |
line++; |
| 159 |
for(unsigned int j = _activeProtocol->getGroupBegin(i); j < _activeProtocol->getGroupBegin(i + 1); j++) |
| 160 |
{ |
| 161 |
if(canWrite(line)) |
| 162 |
{ |
| •163 |
mvwprintw(_window, line - _scrollOffset.at(_activeProtocol), COUNTERS_POS, "%lu ", static_cast<long unsigned int>(_statistic[j])); |
| 164 |
mvwprintw(_window, line - _scrollOffset.at(_activeProtocol), PERSENT_POS, "%-3.2f%% ", |
| 165 |
m > 0 ? static_cast<double>(_statistic[j]) / static_cast<double>(m) * 100.0 : 0.0); |
| 166 |
} |
| 167 |
line++; |
| 168 |
} |
| 169 |
line++; |
| 170 |
} |
| 171 |
wrefresh(_window); |
| 172 |
} |
| 173 |
|
| 174 |
void StatisticsWindow::resize(MainWindow& m) |
| 175 |
{ |
| 176 |
if(_window != nullptr) |
| 177 |
{ |
| 178 |
destroy(); |
| 179 |
} |
| 180 |
int tmp_size = STATISTICS::DEFAULT_LINES; |
| 181 |
if(_activeProtocol != nullptr) |
| 182 |
{ |
| 183 |
tmp_size = _activeProtocol->getAmount() + 2 * BORDER_SIZE + 2 * EMPTY_LINE + STATISTICS::PROTOCOLS_LINE + _activeProtocol->getGroups() * EMPTY_LINE * _activeProtocol->getGroups(); |
| 184 |
} |
| •185 |
if(m._window != nullptr && getmaxy(m._window) - 1 > GUI_HEADER_HEIGHT) |
| 186 |
{ |
| •187 |
_window = subwin(m._window, std::min(static_cast<int>(getmaxy(m._window) - 1 - GUI_HEADER_HEIGHT), tmp_size), |
| 188 |
std::min(static_cast<int>(getmaxx(m._window) - 1), GUI_LENGTH), GUI_HEADER_HEIGHT - BORDER_SIZE, 0); |
| 189 |
updateProtocol(_activeProtocol); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
void StatisticsWindow::setProtocol(AbstractProtocol* p) |
| 194 |
{ |
| 195 |
_activeProtocol = (p); |
| 196 |
} |
| 197 |
//------------------------------------------------------------------------------ |