feat(network): Ability to change speed unit text (B/s) (#2068)
New config option `speed-unit = B/s` will be used to suffix the upload and download speeds. * mod::network: udspeed-unit to set network speed unit suffix * Changed udspeed-unit to speed-unit
This commit is contained in:
parent
52eee95bf8
commit
50d8a1ba06
@ -82,14 +82,14 @@ namespace net {
|
|||||||
|
|
||||||
string ip() const;
|
string ip() const;
|
||||||
string ip6() const;
|
string ip6() const;
|
||||||
string downspeed(int minwidth = 3) const;
|
string downspeed(int minwidth = 3, const string& unit = "B/s") const;
|
||||||
string upspeed(int minwidth = 3) const;
|
string upspeed(int minwidth = 3, const string& unit = "B/s") const;
|
||||||
void set_unknown_up(bool unknown = true);
|
void set_unknown_up(bool unknown = true);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void check_tuntap_or_bridge();
|
void check_tuntap_or_bridge();
|
||||||
bool test_interface() const;
|
bool test_interface() const;
|
||||||
string format_speedrate(float bytes_diff, int minwidth) const;
|
string format_speedrate(float bytes_diff, int minwidth, const string& unit) const;
|
||||||
void query_ip6();
|
void query_ip6();
|
||||||
|
|
||||||
const logger& m_log;
|
const logger& m_log;
|
||||||
|
@ -54,6 +54,7 @@ namespace modules {
|
|||||||
int m_udspeed_minwidth{0};
|
int m_udspeed_minwidth{0};
|
||||||
bool m_accumulate{false};
|
bool m_accumulate{false};
|
||||||
bool m_unknown_up{false};
|
bool m_unknown_up{false};
|
||||||
|
string m_udspeed_unit{"B/s"};
|
||||||
};
|
};
|
||||||
} // namespace modules
|
} // namespace modules
|
||||||
|
|
||||||
|
@ -153,17 +153,17 @@ namespace net {
|
|||||||
/**
|
/**
|
||||||
* Get download speed rate
|
* Get download speed rate
|
||||||
*/
|
*/
|
||||||
string network::downspeed(int minwidth) const {
|
string network::downspeed(int minwidth, const string& unit) const {
|
||||||
float bytes_diff = m_status.current.received - m_status.previous.received;
|
float bytes_diff = m_status.current.received - m_status.previous.received;
|
||||||
return format_speedrate(bytes_diff, minwidth);
|
return format_speedrate(bytes_diff, minwidth, unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get upload speed rate
|
* Get upload speed rate
|
||||||
*/
|
*/
|
||||||
string network::upspeed(int minwidth) const {
|
string network::upspeed(int minwidth, const string& unit) const {
|
||||||
float bytes_diff = m_status.current.transmitted - m_status.previous.transmitted;
|
float bytes_diff = m_status.current.transmitted - m_status.previous.transmitted;
|
||||||
return format_speedrate(bytes_diff, minwidth);
|
return format_speedrate(bytes_diff, minwidth, unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -223,14 +223,14 @@ namespace net {
|
|||||||
/**
|
/**
|
||||||
* Format up- and download speed
|
* Format up- and download speed
|
||||||
*/
|
*/
|
||||||
string network::format_speedrate(float bytes_diff, int minwidth) const {
|
string network::format_speedrate(float bytes_diff, int minwidth, const string& unit) const {
|
||||||
// Get time difference in seconds as a float
|
// Get time difference in seconds as a float
|
||||||
const std::chrono::duration<float> duration = m_status.current.time - m_status.previous.time;
|
const std::chrono::duration<float> duration = m_status.current.time - m_status.previous.time;
|
||||||
float time_diff = duration.count();
|
float time_diff = duration.count();
|
||||||
float speedrate = bytes_diff / time_diff;
|
float speedrate = bytes_diff / time_diff;
|
||||||
|
|
||||||
vector<string> suffixes{"GB", "MB"};
|
vector<string> suffixes{"G", "M"};
|
||||||
string suffix{"KB"};
|
string suffix{"K"};
|
||||||
|
|
||||||
while ((speedrate /= 1000) > 999) {
|
while ((speedrate /= 1000) > 999) {
|
||||||
suffix = suffixes.back();
|
suffix = suffixes.back();
|
||||||
@ -238,7 +238,7 @@ namespace net {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return sstream() << std::setw(minwidth) << std::setfill(' ') << std::setprecision(0) << std::fixed << speedrate
|
return sstream() << std::setw(minwidth) << std::setfill(' ') << std::setprecision(0) << std::fixed << speedrate
|
||||||
<< " " << suffix << "/s";
|
<< " " << suffix << unit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
|
@ -21,6 +21,7 @@ namespace modules {
|
|||||||
m_accumulate = m_conf.get(name(), "accumulate-stats", m_accumulate);
|
m_accumulate = m_conf.get(name(), "accumulate-stats", m_accumulate);
|
||||||
m_interval = m_conf.get<decltype(m_interval)>(name(), "interval", 1s);
|
m_interval = m_conf.get<decltype(m_interval)>(name(), "interval", 1s);
|
||||||
m_unknown_up = m_conf.get<bool>(name(), "unknown-as-up", false);
|
m_unknown_up = m_conf.get<bool>(name(), "unknown-as-up", false);
|
||||||
|
m_udspeed_unit = m_conf.get<string>(name(), "speed-unit", m_udspeed_unit);
|
||||||
|
|
||||||
m_conf.warn_deprecated(name(), "udspeed-minwidth", "%downspeed:min:max% and %upspeed:min:max%");
|
m_conf.warn_deprecated(name(), "udspeed-minwidth", "%downspeed:min:max% and %upspeed:min:max%");
|
||||||
|
|
||||||
@ -109,8 +110,8 @@ namespace modules {
|
|||||||
m_counter = 0;
|
m_counter = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto upspeed = network->upspeed(m_udspeed_minwidth);
|
auto upspeed = network->upspeed(m_udspeed_minwidth, m_udspeed_unit);
|
||||||
auto downspeed = network->downspeed(m_udspeed_minwidth);
|
auto downspeed = network->downspeed(m_udspeed_minwidth, m_udspeed_unit);
|
||||||
|
|
||||||
// Update label contents
|
// Update label contents
|
||||||
const auto replace_tokens = [&](label_t& label) {
|
const auto replace_tokens = [&](label_t& label) {
|
||||||
|
Loading…
Reference in New Issue
Block a user