Skip to content

Commit

Permalink
Added count progress and records per second metric.
Browse files Browse the repository at this point in the history
  • Loading branch information
allinurl committed Apr 13, 2014
1 parent 25a64a6 commit 0c232b2
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 32 deletions.
35 changes: 27 additions & 8 deletions goaccess.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int active_gdns = 0;
static GDash *dash;
static GHolder *holder;
static GLog *logger;
static GSpinner *parsing_spinner;
GSpinner *parsing_spinner;

/* *INDENT-OFF* */
struct option long_opts[] = {
Expand Down Expand Up @@ -988,7 +988,8 @@ main (int argc, char *argv[])
ht_host_serve_usecs =
g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) g_free,
g_free);
/* the following tables contain s structure as their value, thus we

/* The following tables contain s structure as their value, thus we
use a special iterator to free its value */
ht_requests =
g_hash_table_new_full (g_str_hash, g_str_equal,
Expand Down Expand Up @@ -1017,14 +1018,21 @@ main (int argc, char *argv[])
setlocale (LC_CTYPE, loc_ctype);
else
setlocale (LC_CTYPE, "");
setlocale (LC_NUMERIC, "");

#ifdef HAVE_LIBGEOIP
/* Geolocation data */
geo_location_data = GeoIP_new (conf.geo_db);
#endif

if (conf.output_html)
logger = init_log ();
parsing_spinner = new_gspinner ();
parsing_spinner->process = &logger->process;

if (conf.output_html) {
ui_spinner_create (parsing_spinner);
goto out;
}

initscr ();
clear ();
Expand All @@ -1038,37 +1046,42 @@ main (int argc, char *argv[])
}
init_colors ();

/* init standard screen */
attron (COLOR_PAIR (COL_WHITE));
getmaxyx (stdscr, row, col);
if (row < MIN_HEIGHT || col < MIN_WIDTH)
error_handler (__PRETTY_FUNCTION__, __FILE__, __LINE__,
"Minimum screen size - 0 columns by 7 lines");

/* init header screen */
header_win = newwin (5, col, 0, 0);
keypad (header_win, TRUE);
if (header_win == NULL)
error_handler (__PRETTY_FUNCTION__, __FILE__, __LINE__,
"Unable to allocate memory for header_win.");

/* init main screen */
main_win = newwin (row - 7, col, 6, 0);
keypad (main_win, TRUE);
if (main_win == NULL)
error_handler (__PRETTY_FUNCTION__, __FILE__, __LINE__,
"Unable to allocate memory for main_win.");

parsing_spinner = new_gspinner ();
out:
set_curses_spinner (parsing_spinner);

logger = init_log ();
/* configuration dialog */
if (isatty (STDIN_FILENO) && (conf.log_format == NULL || conf.load_conf_dlg)
&& !conf.output_html) {
refresh ();
quit = verify_format (logger, parsing_spinner);
} else if (!conf.output_html) {
draw_header (stdscr, "Parsing...", "%s", row - 1, 0, col, HIGHLIGHT);
}
/* straight parsing */
else if (!conf.output_html) {
ui_spinner_create (parsing_spinner);
}

out:

/* main processing event */
time (&start_proc);
if (!quit && parse_log (&logger, NULL, -1))
Expand All @@ -1078,16 +1091,22 @@ main (int argc, char *argv[])

gdns_init ();
logger->offset = logger->process;

/* STDOUT */
if (conf.output_html) {
/* no valid entries to process from the log */
if ((logger->process == 0) || (logger->process == logger->invalid))
goto done;

allocate_holder ();
/* csv */
if (conf.output_format != NULL && strcmp ("csv", conf.output_format) == 0)
output_csv (logger, holder);
/* json */
else if (conf.output_format != NULL
&& strcmp ("json", conf.output_format) == 0)
output_json (logger, holder);
/* HTML */
else
output_html (logger, holder);
goto done;
Expand Down
3 changes: 3 additions & 0 deletions goaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#ifndef GOACCESS_H_INCLUDED
#define GOACCESS_H_INCLUDED

#include "ui.h"

extern GSpinner *parsing_spinner;
extern int active_gdns; /* kill dns pthread flag */
void render_screens (void);

Expand Down
4 changes: 4 additions & 0 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

#include "browsers.h"
#include "commons.h"
#include "goaccess.h"
#include "error.h"
#include "opesys.h"
#include "settings.h"
Expand Down Expand Up @@ -917,10 +918,13 @@ process_log (GLog * logger, char *line, int test)
return 0;
}

/* ignore comments */
if (*line == '#' || *line == '\n')
return 0;

pthread_mutex_lock (&parsing_spinner->mutex);
logger->process++;
pthread_mutex_unlock (&parsing_spinner->mutex);

glog = init_log_item (logger);
if (parse_format (glog, conf.log_format, conf.date_format, line) == 1) {
Expand Down
77 changes: 57 additions & 20 deletions ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,21 +645,44 @@ load_agent_list (WINDOW * main_win, char *addr)
static void
ui_spinner (void *ptr_data)
{
GSpinner *spinner = (GSpinner *) ptr_data;
GSpinner *sp = (GSpinner *) ptr_data;

static char const spin_chars[] = "/-\\|";
char buf[SPIN_LBL];
long long tdiff = 0, psec = 0;
int i = 0;
time_t begin;

time (&begin);
while (1) {
pthread_mutex_lock (&spinner->mutex);
if (spinner->state == SPN_END)
pthread_mutex_lock (&sp->mutex);
if (sp->state == SPN_END)
break;
wattron (spinner->win, COLOR_PAIR (spinner->color));
mvwaddch (spinner->win, spinner->y, spinner->x, spin_chars[i++ & 3]);
wattroff (spinner->win, COLOR_PAIR (spinner->color));
wrefresh (spinner->win);
pthread_mutex_unlock (&spinner->mutex);

tdiff = (long long) (time (NULL) - begin);
psec = tdiff >= 1 ? *(sp->process) / tdiff : 0;
snprintf (buf, sizeof buf, SPIN_FMT, sp->label, *(sp->process), psec);

/* CURSES */
if (sp->curses) {
/* label + metrics */
draw_header (sp->win, buf, " %s", sp->y, sp->x, sp->w, sp->color);

/* caret */
wattron (sp->win, COLOR_PAIR (sp->color));
mvwaddch (sp->win, sp->y, sp->spin_x, spin_chars[i++ & 3]);
wattroff (sp->win, COLOR_PAIR (sp->color));
wrefresh (sp->win);
}
/* STDOUT */
else {
fprintf (stderr, "%s\r", buf);
}

pthread_mutex_unlock (&sp->mutex);
usleep (100000);
}
free (spinner);
free (sp);
}

/* create spinner's thread */
Expand All @@ -671,20 +694,34 @@ ui_spinner_create (GSpinner * spinner)
}

/* allocate memory and initialize data */
GSpinner *
new_gspinner (void)
void
set_curses_spinner (GSpinner * spinner)
{
int y, x;
GSpinner *spinner;
if (spinner == NULL)
return;

getmaxyx (stdscr, y, x);
spinner = xcalloc (1, sizeof (GSpinner));

spinner->color = HIGHLIGHT;
spinner->state = SPN_RUN;
spinner->curses = 1;
spinner->win = stdscr;
spinner->x = x - 2;
spinner->x = 0;
spinner->w = x;
spinner->spin_x = x - 2;
spinner->y = y - 1;
}

/* allocate memory and initialize data */
GSpinner *
new_gspinner (void)
{
GSpinner *spinner;

spinner = xcalloc (1, sizeof (GSpinner));
spinner->label = "Parsing...";
spinner->state = SPN_RUN;
spinner->curses = 0;

if (pthread_mutex_init (&(spinner->mutex), NULL))
error_handler (__PRETTY_FUNCTION__, __FILE__, __LINE__,
Expand Down Expand Up @@ -858,8 +895,8 @@ verify_format (GLog * logger, GSpinner * spinner)
/* test log against selected settings */
if (test_format (logger)) {
invalid = 1;
draw_header (win, "No valid hits. 'y' to continue anyway.",
"%s", 3, 2, CONF_MENU_W, WHITE_RED);
draw_header (win, "No valid hits. 'y' to continue anyway.", "%s",
3, 2, CONF_MENU_W, WHITE_RED);
free (conf.date_format);
free (conf.log_format);

Expand All @@ -869,13 +906,13 @@ verify_format (GLog * logger, GSpinner * spinner)
/* valid data, reset logger & start parsing */
else {
reset_struct (logger);
draw_header (win, "Parsing...", "%s", 3, 2, CONF_MENU_W,
BLACK_CYAN);

/* start spinner thread */
spinner->win = win;
spinner->y = 3;
spinner->x = w - 4;
spinner->x = 2;
spinner->spin_x = CONF_MENU_W;
spinner->w = CONF_MENU_W;
spinner->color = BLACK_CYAN;
ui_spinner_create (spinner);

Expand Down
18 changes: 14 additions & 4 deletions ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
#define T_LOG_PATH "Log File"
#define T_UNIQUE_VIS "Unique Visitors"

/* spinner label format */
#define SPIN_LBL 50
#define SPIN_FMT "%s [%'d] [%'lld/s]"

/* modules */
#define VISIT_HEAD "Unique visitors per day - Including spiders"
#define VISIT_DESC "Hits having the same IP, date and agent are a unique visit."
Expand Down Expand Up @@ -213,12 +217,17 @@ typedef struct GAgents_

typedef struct GSpinner_
{
pthread_t thread;
const char *label;
int color;
int curses;
int spin_x;
int w;
int x;
int y;
pthread_mutex_t mutex;
pthread_t thread;
unsigned int *process;
WINDOW *win;
int y;
int x;
int color;
enum
{
SPN_RUN,
Expand All @@ -230,6 +239,7 @@ typedef struct GSpinner_
char *get_browser_type (char *line);
char *input_string (WINDOW * win, int pos_y, int pos_x, size_t max_width, const char *str, int enable_case, int *toggle_case);
GSpinner *new_gspinner (void);
void set_curses_spinner (GSpinner *spinner);
int split_agent_str (char *ptr_value, GAgents * agents, int max);
int verify_format (GLog * logger, GSpinner * spinner);
void close_win (WINDOW * w);
Expand Down

0 comments on commit 0c232b2

Please sign in to comment.