diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-11-09 18:56:23 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-11-09 18:56:23 +0100 |
commit | feca811e0e0afcd67e9153522d40ae58bb468b2f (patch) | |
tree | af4ba172ff8877e6e9f0dfb44285afffd84ffdfd | |
parent | 91e5543757dad266f041f46d11bf973966368a30 (diff) |
Modified time branch to store only first and last time seen.
-rw-r--r-- | config.c | 6 | ||||
-rw-r--r-- | export_xml.c | 4 | ||||
-rw-r--r-- | parsing.c | 171 | ||||
-rw-r--r-- | regexset.c | 11 | ||||
-rw-r--r-- | user.c | 51 | ||||
-rw-r--r-- | user.h | 14 |
6 files changed, 55 insertions, 202 deletions
@@ -65,11 +65,15 @@ int cfg_init() { && config_setting_lookup_string(re_setting, "kick", &kick) && config_setting_lookup_string(re_setting, "nick_changed", &nick_changed) && config_setting_lookup_string(re_setting, "log_opened", &log_opened) - && config_setting_lookup_string(re_setting, "day_changed", &day_changed) )) { fprintf(stderr, "Regex set #%d missing one or more keys.\n", i+1); return 0; } + + if(!config_setting_lookup_string(re_setting, "day_changed", &day_changed)) { + day_changed = NULL; + } + if(!config_setting_lookup_string(re_setting, "log_date_format", &log_date_format)) log_date_format = NULL; if(!config_setting_lookup_string(re_setting, "day_date_format", &day_date_format)) diff --git a/export_xml.c b/export_xml.c index 4f77bbc..91f5af0 100644 --- a/export_xml.c +++ b/export_xml.c @@ -87,10 +87,6 @@ int export_xml(struct channel_t *channel, struct user_t *users) { xmlTextWriterWriteFormatString(writer, "%lu", user->seen_last); xmlTextWriterEndElement(writer); - xmlTextWriterStartElement(writer, (const xmlChar*)"time_total"); - xmlTextWriterWriteFormatString(writer, "%lu", user->time_total); - xmlTextWriterEndElement(writer); - /* Add lines for this user. */ xmlTextWriterStartElement(writer, (const xmlChar*)"lines"); for(int h = 0; h < 24; h++) { @@ -18,7 +18,7 @@ #define TIME_BUFFER_SIZE 0xf #define DATE_BUFFER_SIZE 0x20 -static pthread_mutex_t user_mutex, user_time_mutex, word_mutex, channel_mutex, time_mutex; +static pthread_mutex_t user_mutex, word_mutex, channel_mutex, time_mutex; static struct user_t *last_user = NULL; static int in_monolog = 0, monolog_len = 0; @@ -99,11 +99,6 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * wchar_t wtext[TEXT_BUFFER_SIZE]; mbstowcs(wtext, text, TEXT_BUFFER_SIZE); - pthread_mutex_lock(&user_mutex); - user->characters += wcslen(wtext); - user->lines[time_i]++; - pthread_mutex_unlock(&user_mutex); - pthread_mutex_lock(&time_mutex); now = now_global; pthread_mutex_unlock(&time_mutex); @@ -112,21 +107,15 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * now.tm_min = min; time_t now_ut = mktime(&now); - pthread_mutex_lock(&user_time_mutex); - struct user_time_t *user_time = user_time_get(nick, 0); - if(user_time->seen_first == 0) { - user_time->seen_first = now_ut; - } - /* Set if user is not yet seen. */ - if(user_time->last_join == 0) { - user_time->last_join = now_ut; - } - /* Add to online total. */ - if(user_time->seen_last > 0) { - user_time->time_total += now_ut - user_time->seen_last; + pthread_mutex_lock(&user_mutex); + user->characters += wcslen(wtext); + user->lines[time_i]++; + + if(user->seen_first == 0) { + user->seen_first = now_ut; } - user_time->seen_last = now_ut; - pthread_mutex_unlock(&user_time_mutex); + user->seen_last = now_ut; + pthread_mutex_unlock(&user_mutex); pthread_mutex_lock(&channel_mutex); channel->hours[time_i]++; @@ -175,14 +164,14 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * now.tm_min = min; time_t now_ut = mktime(&now); - pthread_mutex_lock(&user_time_mutex); - struct user_time_t *user_time = user_time_get(nick, 0); + pthread_mutex_lock(&user_mutex); + struct user_t *user = user_get(nick); - if(user_time->seen_first == 0) { - user_time->seen_first = now_ut; + if(user->seen_first == 0) { + user->seen_first = now_ut; } - user_time->last_join = user_time->seen_last = now_ut; - pthread_mutex_unlock(&user_time_mutex); + user->seen_last = now_ut; + pthread_mutex_unlock(&user_mutex); continue; } @@ -206,24 +195,14 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * now.tm_min = min; time_t now_ut = mktime(&now); - pthread_mutex_lock(&user_time_mutex); - struct user_time_t *user_time = user_time_get(nick, 1); - - if(user_time->real_user) { - struct user_time_t *temp = user_time->real_user; - user_time->real_user = NULL; - user_time = temp; - } + pthread_mutex_lock(&user_mutex); + struct user_t *user = user_get(nick); - if(user_time->seen_first == 0) { - user_time->seen_first = now_ut; + if(user->seen_first == 0) { + user->seen_first = now_ut; } - /* Add to online total. */ - if(user_time->seen_last > 0) { - user_time->time_total += now_ut - user_time->seen_last; - } - user_time->seen_last = now_ut; - pthread_mutex_unlock(&user_time_mutex); + user->seen_last = now_ut; + pthread_mutex_unlock(&user_mutex); continue; } @@ -247,32 +226,21 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * now.tm_min = min; time_t now_ut = mktime(&now); - pthread_mutex_lock(&user_time_mutex); - struct user_time_t *user_time = user_time_get(nick, 1); - - if(user_time->real_user) { - struct user_time_t *temp = user_time->real_user; - user_time->real_user = NULL; - user_time = temp; - } + pthread_mutex_lock(&user_mutex); + struct user_t *user = user_get(nick); - if(user_time->seen_first == 0) { - user_time->seen_first = now_ut; + if(user->seen_first == 0) { + user->seen_first = now_ut; } - /* Add to online total. */ - if(user_time->seen_last > 0) { - user_time->time_total += now_ut - user_time->seen_last; - } - user_time->seen_last = now_ut; - pthread_mutex_unlock(&user_time_mutex); + user->seen_last = now_ut; + pthread_mutex_unlock(&user_mutex); continue; } rc = pcre_exec(rs->nick_changed, rs->nick_changed_e, line, strlen(line), 0, 0, ovector, 30); if(rc > 0) { - char oldnick[NICK_BUFFER_SIZE], newnick[NICK_BUFFER_SIZE], hour_s[TIME_BUFFER_SIZE], min_s[TIME_BUFFER_SIZE]; - pcre_copy_named_substring(rs->nick_changed, line, ovector, rc, "old_nick", oldnick, NICK_BUFFER_SIZE); + char newnick[NICK_BUFFER_SIZE], hour_s[TIME_BUFFER_SIZE], min_s[TIME_BUFFER_SIZE]; pcre_copy_named_substring(rs->nick_changed, line, ovector, rc, "new_nick", newnick, NICK_BUFFER_SIZE); pcre_copy_named_substring(rs->nick_changed, line, ovector, rc, "hour", hour_s, TIME_BUFFER_SIZE); pcre_copy_named_substring(rs->nick_changed, line, ovector, rc, "minute", min_s, TIME_BUFFER_SIZE); @@ -289,24 +257,14 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * now.tm_min = min; time_t now_ut = mktime(&now); - pthread_mutex_lock(&user_time_mutex); - struct user_time_t *from_user = user_time_get(oldnick, 1); - struct user_time_t *to_user = user_time_get(newnick, 1); + pthread_mutex_lock(&user_mutex); + struct user_t *user = user_get(newnick); - if(from_user->real_user) { - to_user->real_user = from_user->real_user; - from_user->real_user = NULL; - } else { - to_user->real_user = from_user; + if(user->seen_first == 0) { + user->seen_first = now_ut; } - if(to_user->real_user) { - to_user->time_total += now_ut - to_user->seen_last; - to_user->real_user->seen_last = now_ut; - } else { - from_user->time_total += now_ut - from_user->seen_last; - from_user->seen_last = now_ut; - } - pthread_mutex_unlock(&user_time_mutex); + user->seen_last = now_ut; + pthread_mutex_unlock(&user_mutex); continue; } @@ -321,7 +279,7 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * pcre_copy_named_substring(rs->log_opened, line, ovector, rc, "date", date, DATE_BUFFER_SIZE); if(!strptime(date, log_date_format, &now)) { - printf("log fail: %s\n", date); + fprintf(stderr, "log fail: %s\n", date); continue; } @@ -332,7 +290,8 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * continue; } - rc = pcre_exec(rs->day_changed, rs->day_changed_e, line, strlen(line), 0, 0, ovector, 30); + /* day_changed is optional */ + rc = rs->day_changed ? pcre_exec(rs->day_changed, rs->day_changed_e, line, strlen(line), 0, 0, ovector, 30) : 0; if(rc > 0) { char date[DATE_BUFFER_SIZE]; @@ -342,7 +301,7 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * pcre_copy_named_substring(rs->day_changed, line, ovector, rc, "date", date, DATE_BUFFER_SIZE); if(!strptime(date, day_date_format, &now)) { - printf("day fail: %s\n", date); + fprintf(stderr, "day fail: %s\n", date); continue; } @@ -378,19 +337,12 @@ static void process_file(FILE *f, struct channel_t *channel, struct regexset_t * *victim_user = user_get(victim); user->kicks++; victim_user->kicked++; - pthread_mutex_unlock(&user_mutex); - pthread_mutex_lock(&user_time_mutex); - struct user_time_t *user_time = user_time_get(nick, 0); - if(user_time->seen_first == 0) { - user_time->seen_first = now_ut; - } - /* Add to online total. */ - if(user_time->seen_last > 0) { - user_time->time_total += now_ut - user_time->seen_last; + if(user->seen_first == 0) { + user->seen_first = now_ut; } - user_time->seen_last = now_ut; - pthread_mutex_unlock(&user_time_mutex); + user->seen_last = now_ut; + pthread_mutex_unlock(&user_mutex); continue; } @@ -410,49 +362,14 @@ static void *thread_func(void *arg) { return NULL; } -static void merge_user_time_data(struct user_time_t *user_time) { - struct user_t *user; - - user = user_get(user_time->nick); - - if(user->seen_first == 0 || user_time->seen_first < user->seen_first) { - user->seen_first = user_time->seen_first; - } - - if(user_time->seen_last > user->seen_last) { - user->seen_last = user_time->seen_last; - } - - printf("adding %lu to %s\n", user_time->time_total, user->nick); - user->time_total += user_time->time_total; -} - -static void merge_time_data() { - for(int i = 0; i < USERS_MAX; i++) { - struct user_time_t *user_time; - - user_time = &users_time[i]; - if(user_time->nick) - merge_user_time_data(user_time); - user_time = user_time->next; - - while(user_time) { - merge_user_time_data(user_time); - user_time = user_time->next; - } - } -} - void process(int thread_n) { pthread_mutex_init(&user_mutex, NULL); - pthread_mutex_init(&user_time_mutex, NULL); pthread_mutex_init(&word_mutex, NULL); pthread_mutex_init(&channel_mutex, NULL); pthread_mutex_init(&time_mutex, NULL); /* Parsing stuff goes here. */ for(int chan_i = 0; chan_i < channel_get_count(); chan_i++) { user_init(); - user_time_init(); word_init(); struct channel_t *channel = channel_get(chan_i); printf("Channel %s\n", channel->name); @@ -488,16 +405,12 @@ void process(int thread_n) { file = file->next; } - merge_time_data(); - export_xml(channel, users); user_free(); - user_time_free(); word_free(); } pthread_mutex_destroy(&time_mutex); pthread_mutex_destroy(&user_mutex); - pthread_mutex_destroy(&user_time_mutex); pthread_mutex_destroy(&word_mutex); pthread_mutex_destroy(&channel_mutex); } @@ -89,12 +89,13 @@ struct regexset_t *rs_add(const char *text, const char *join, const char *part, } rs->log_opened_e = pcre_study(rs->log_opened, 0, &error); - rs->day_changed = pcre_compile(day_changed, 0, &error, &erroffset, rs_pcre_tables); - if(rs->day_changed == NULL) { - re_error("day_changed", day_changed, error, erroffset); - goto free_log_opened; + /* day_changed is optional */ + if(day_changed) { + rs->day_changed = pcre_compile(day_changed, 0, &error, &erroffset, rs_pcre_tables); + if(rs->day_changed) { + rs->day_changed_e = pcre_study(rs->day_changed, 0, &error); + } } - rs->day_changed_e = pcre_study(rs->day_changed, 0, &error); /* These are managed by libconfig and must never be free'd. */ rs->log_date_format = log_date_format; @@ -7,8 +7,6 @@ struct user_t *users; -struct user_time_t *users_time; - void user_init() { users = malloc(sizeof(struct user_t) * USERS_MAX); memset(users, 0, sizeof(struct user_t) * USERS_MAX); @@ -35,7 +33,7 @@ struct user_t *user_get(char *nick) { user->nick = strdup(nick); memset(user->lines, 0, 24*4 * sizeof(unsigned long)); user->words = user->characters = user->kicks = user->kicked = user->monolog_lines = user->monologs = 0; - user->seen_first = user->seen_last = user->last_join = user->time_total = 0; + user->seen_first = user->seen_last = 0; user->next = NULL; char *_nick = nick_get(nick); user->real_user = strcmp(nick, _nick) ? user_get(_nick) : NULL; @@ -59,50 +57,3 @@ void user_free() { } free(users); } - -void user_time_init() { - users_time = malloc(sizeof(struct user_time_t) * USERS_MAX); - memset(users_time, 0, sizeof(struct user_time_t) * USERS_MAX); -} - -struct user_time_t *user_time_get(char *nick, int ignore_real) { - unsigned long hash = sdbm(nick); - int index = hash % USERS_MAX; - - struct user_time_t *user = &users_time[index]; - /* If hash doesn't match and there exists another user, fetch it. */ - while(user->hash != hash && user->next) user = user->next; - /* If hash still doesn't match and the user exists, add a new user. */ - if(user->hash != hash && user->nick) { - struct user_time_t *temp_user = malloc(sizeof(struct user_time_t)); - user->next = temp_user; - user = temp_user; - /* Initialize nick to NULL so the user can be correctly added. */ - user->nick = NULL; - } - /* Add the new user data to the current pointer if none was found. */ - if(!user->nick) { - user->hash = hash; - user->nick = strdup(nick); - user->seen_first = user->seen_last = user->last_join = user->time_total = 0; - user->real_user = user->next = NULL; - } - - return (!ignore_real && user->real_user ? user->real_user : user); -} - -void user_time_free() { - struct user_time_t *user; - for(int i = 0; i < USERS_MAX; i++) { - if(users_time[i].nick) - free(users_time[i].nick); - user = users_time[i].next; - while(user) { - struct user_time_t *temp = user->next; - free(user->nick); - free(user); - user = temp; - } - } - free(users_time); -} @@ -10,26 +10,14 @@ struct user_t { char *nick; unsigned long lines[24*4]; unsigned long long words, characters, kicks, kicked, monolog_lines, monologs; - time_t seen_first, seen_last, last_join, time_total; + time_t seen_first, seen_last; struct user_t *real_user, *next; }; -struct user_time_t { - unsigned long hash; - char *nick; - time_t seen_first, seen_last, last_join, time_total; - struct user_time_t *real_user, *next; -}; - void user_init(); struct user_t *user_get(char *nick); void user_free(); -void user_time_init(); -struct user_time_t *user_time_get(char *nick, int ignore_real); -void user_time_free(); - extern struct user_t *users; -extern struct user_time_t *users_time; #endif |