- fixed totally bogus rates
- tweaked bar graph scale - added basic kb handler - split out sent/recv lines
This commit is contained in:
79
ui.c
79
ui.c
@@ -19,7 +19,8 @@
|
||||
|
||||
#define HISTORY_DIVISIONS 3
|
||||
|
||||
int history_divs[HISTORY_DIVISIONS] = {3, 20, 40};
|
||||
/* 3, 15 and 60 seconds */
|
||||
int history_divs[HISTORY_DIVISIONS] = {1, 5, 20};
|
||||
|
||||
|
||||
typedef struct host_pair_line_tag {
|
||||
@@ -41,12 +42,12 @@ int screen_line_compare(void* a, void* b) {
|
||||
return(aa->recv[0] + aa->sent[0] < bb->recv[0] + bb->sent[0]);
|
||||
}
|
||||
|
||||
void readable_size(float n, char* buf, int bsize) {
|
||||
if(n >= 102400) {
|
||||
snprintf(buf, bsize, " %4.1fM", n / (1024 * 1024));
|
||||
void readable_size(float n, char* buf, int bsize, int ksize) {
|
||||
if(n >= 100000) {
|
||||
snprintf(buf, bsize, " %4.1fM", n / (ksize * ksize));
|
||||
}
|
||||
else if(n >= 1024) {
|
||||
snprintf(buf, bsize, " %4.1fK", n / 1024);
|
||||
else if(n >= 1000) {
|
||||
snprintf(buf, bsize, " %4.1fK", n / ksize);
|
||||
}
|
||||
else {
|
||||
snprintf(buf, bsize, " %4.0fb", n );
|
||||
@@ -55,14 +56,14 @@ void readable_size(float n, char* buf, int bsize) {
|
||||
|
||||
/* Maximum and minimum rate which we plot on the bar chart. */
|
||||
static int min_rate = 1; /* 1 byte/s */
|
||||
static int max_rate = (10000000 / 8); /* 10 Mbit/s */
|
||||
static int max_rate = (10000000); /* 10 Mbit/s */
|
||||
|
||||
static int get_bar_length(const int rate) {
|
||||
float l;
|
||||
if (rate <= 0)
|
||||
return 0;
|
||||
l = (log(rate) - log(min_rate)) / (log(max_rate) - log(min_rate));
|
||||
return l * COLS;
|
||||
return (l * COLS);
|
||||
}
|
||||
|
||||
static void draw_bar_scale(void) {
|
||||
@@ -72,7 +73,7 @@ static void draw_bar_scale(void) {
|
||||
for (i = min_rate; i <= max_rate; i *= 10) {
|
||||
char s[40], *p;
|
||||
int x;
|
||||
readable_size(i, s, sizeof s);
|
||||
readable_size(i, s, sizeof s, 1000);
|
||||
p = s + strspn(s, " ");
|
||||
x = get_bar_length(i);
|
||||
mvaddch(1, x, ACS_BTEE);
|
||||
@@ -100,7 +101,8 @@ void ui_print() {
|
||||
screen_list.compare = &screen_line_compare;
|
||||
sorted_list_initialise(&screen_list);
|
||||
|
||||
erase();
|
||||
clear();
|
||||
//erase();
|
||||
draw_bar_scale();
|
||||
|
||||
while(hash_next_item(history, &n) == HASH_STATUS_OK) {
|
||||
@@ -133,41 +135,52 @@ void ui_print() {
|
||||
int x = 0, j, L, t;
|
||||
host_pair_line* screen_line = (host_pair_line*)nn->data;
|
||||
|
||||
if(history_len < history_divs[j]) {
|
||||
t = history_len * RESOLUTION;
|
||||
} else {
|
||||
t = history_divs[j] * RESOLUTION;
|
||||
L = (COLS - 8 * HISTORY_DIVISIONS - 4) / 2;
|
||||
if(L > sizeof hostname) {
|
||||
L = sizeof hostname;
|
||||
}
|
||||
|
||||
L = COLS - 12 * HISTORY_DIVISIONS;
|
||||
|
||||
resolve(&screen_line->ap->src, hostname, HOSTNAME_LENGTH);
|
||||
sprintf(line, "%s", hostname);
|
||||
resolve(&screen_line->ap->src, hostname, L);
|
||||
sprintf(line, "%-*s", L, hostname);
|
||||
mvaddstr(y, x, line);
|
||||
x += L / 2 - 2;
|
||||
x += L;
|
||||
|
||||
resolve(&screen_line->ap->dst, hostname, HOSTNAME_LENGTH);
|
||||
sprintf(line, " => %*s", L / 2 - 2, hostname);
|
||||
mvaddstr(y, x, " => ");
|
||||
mvaddstr(y+1, x, " <= ");
|
||||
|
||||
x += 4;
|
||||
resolve(&screen_line->ap->dst, hostname, L);
|
||||
sprintf(line, "%-*s", L, hostname);
|
||||
mvaddstr(y, x, line);
|
||||
x = L;
|
||||
x += L;
|
||||
|
||||
for(j = 0; j < HISTORY_DIVISIONS; j++) {
|
||||
readable_size(screen_line->sent[j] / t, line, 10);
|
||||
if(history_len < history_divs[j]) {
|
||||
t = history_len * RESOLUTION;
|
||||
} else {
|
||||
t = history_divs[j] * RESOLUTION;
|
||||
}
|
||||
readable_size(8 * screen_line->sent[j] / t, line, 10, 1024);
|
||||
mvaddstr(y, x, line);
|
||||
x += strlen(line);
|
||||
|
||||
readable_size(screen_line->recv[j] / t, line, 10);
|
||||
mvaddstr(y, x, line);
|
||||
x += strlen(line);
|
||||
readable_size(8 * screen_line->recv[j] / t, line, 10, 1024);
|
||||
mvaddstr(y+1, x, line);
|
||||
x += 8;
|
||||
|
||||
}
|
||||
|
||||
/* Do some sort of primitive bar graph thing. */
|
||||
mvchgat(y, 0, -1, A_NORMAL, 0, NULL);
|
||||
L = get_bar_length(screen_line->recv[0] / t);
|
||||
L = get_bar_length(8 * screen_line->sent[0] / history_divs[0]);
|
||||
if (L > 0)
|
||||
mvchgat(y, 0, L, A_REVERSE, 0, NULL);
|
||||
|
||||
mvchgat(y+1, 0, -1, A_NORMAL, 0, NULL);
|
||||
L = get_bar_length(8 * screen_line->recv[0] / history_divs[0]);
|
||||
if (L > 0)
|
||||
mvchgat(y+1, 0, L, A_REVERSE, 0, NULL);
|
||||
|
||||
y++;
|
||||
y+=2;
|
||||
free(screen_line);
|
||||
}
|
||||
refresh();
|
||||
@@ -185,17 +198,17 @@ void ui_loop() {
|
||||
(void) nonl(); /* tell curses not to do NL->CR/NL on output */
|
||||
(void) cbreak(); /* take input chars one at a time, no wait for \n */
|
||||
(void) noecho(); /* don't echo input */
|
||||
halfdelay(2);
|
||||
|
||||
erase();
|
||||
|
||||
pthread_mutex_init(&tick_wait_mutex, NULL);
|
||||
pthread_cond_init(&tick_wait_cond, NULL);
|
||||
while(foad == 0) {
|
||||
struct timespec t;
|
||||
t.tv_sec = time(NULL) + 1;
|
||||
if(getch() == 'q') {
|
||||
foad = 1;
|
||||
|
||||
pthread_cond_timedwait(&tick_wait_cond, &tick_wait_mutex, &t);
|
||||
//fprintf(stderr,"timeout tick\n");
|
||||
}
|
||||
tick();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user