AmendHub

Download:

jcs

/

subtext

/

amendments

/

255

session: Stop persisting tally, prune old session logs

Prune after 21 days of logs by default, every morning. The db was
getting quite large and each new login or recent call check was
taking a long time.
 
We're stable now so we don't have to persist the call log tally to
disk, just keep it in memory.

jcs made amendment 255 over 2 years ago
--- db.c Fri Sep 16 16:54:03 2022 +++ db.c Wed Sep 21 17:41:22 2022 @@ -75,6 +75,9 @@ struct struct_field config_fields[] = { { "Screen Blanker Runtime Seconds", CONFIG_TYPE_SHORT, offsetof(struct config, blanker_runtime_seconds), 1, USHRT_MAX }, + { "Session Log History Days", CONFIG_TYPE_SHORT, + offsetof(struct config, session_log_days), + 1, USHRT_MAX }, }; size_t nconfig_fields = nitems(config_fields); @@ -265,6 +268,7 @@ db_migrate(struct db *tdb, short is_new) tdb->config.max_login_seconds = 90; tdb->config.blanker_idle_seconds = (60 * 60); tdb->config.blanker_runtime_seconds = 30; + tdb->config.session_log_days = 21; db_config_save(tdb); /* create a default sysop user */ @@ -456,6 +460,18 @@ db_migrate(struct db *tdb, short is_new) if (ids != NULL) xfree(&ids); } + break; + } + case 10: { + /* 10->11, added session_log_days */ + struct config new_config = { 0 }; + + bile_read(tdb->bile, DB_CONFIG_RTYPE, 1, (char *)&new_config, + sizeof(new_config)); + new_config.session_log_days = 21; + + bile_write(tdb->bile, DB_CONFIG_RTYPE, 1, &new_config, + sizeof(new_config)); break; } } --- db.h Fri Sep 16 13:44:51 2022 +++ db.h Wed Sep 21 17:12:35 2022 @@ -23,7 +23,7 @@ #define DB_TYPE 'STDB' -#define DB_CUR_VERS 10 +#define DB_CUR_VERS 11 #define DB_TRUE 0x100 #define DB_FALSE 0x000 @@ -81,6 +81,7 @@ struct config { unsigned long trusted_proxy_ip; unsigned long modem_speed; unsigned short trusted_proxy_udp_port; + short session_log_days; }; extern struct struct_field config_fields[]; --- main.c Fri Sep 16 17:55:51 2022 +++ main.c Wed Sep 21 17:33:25 2022 @@ -53,7 +53,8 @@ main(void) struct focusable *found_focusable; unsigned long zone_size, stack_size, heap_size; short event_in, n, finder_action, finder_count; - char key; + char key, last_daily_job[9] = { 0 }, today[9]; + struct tm *date_tm; uthread_init(); @@ -97,8 +98,6 @@ main(void) _atexit(handle_exit); - session_load_tally(); - logger = logger_init(); logger_update_title(); @@ -133,6 +132,16 @@ main(void) xfree_verify(); #endif blanker_idle(); + + if (nsessions == 0) { + date_tm = localtime((time_t *)&Time); + strftime(today, sizeof(today), "%Y%m%d", date_tm); + if (strcmp(today, last_daily_job) != 0) { + session_purge_logs(db->config.session_log_days); + strlcpy(last_daily_job, today, sizeof(today)); + } + } + continue; } --- session.c Thu Sep 15 16:05:30 2022 +++ session.c Wed Sep 21 16:56:41 2022 @@ -55,24 +55,6 @@ void session_answer_page(struct session *s); void sysop_edit_settings(struct session *s); bool session_idled_out(struct session *session); -void -session_load_tally(void) -{ - char date[9]; - struct tm *date_tm; - - date_tm = localtime((time_t *)&Time); - strftime(date, sizeof(date), "%Y%m%d", date_tm); - if (bile_read(db->sessions_bile, SL_TALLY_RTYPE, 1, - (char *)&session_today_tally, - sizeof(session_today_tally)) != sizeof(session_today_tally) || - strcmp(date, session_today_tally.date) != 0) { - strlcpy(session_today_tally.date, date, - sizeof(session_today_tally.date)); - session_today_tally.calls = 0; - } -} - struct session * session_create(char *node, char *via, struct node_funcs *node_funcs) { @@ -181,8 +163,6 @@ session_run(struct uthread *uthread, void *arg) session_today_tally.calls = 0; } session_today_tally.calls++; - bile_write(db->sessions_bile, SL_TALLY_RTYPE, 1, - (char *)&session_today_tally, sizeof(session_today_tally)); if (s->terminal_type[0] == '\0') { /* per-node setup didn't fill this in, ask the user */ @@ -1498,4 +1478,47 @@ get_menu_option: session_flush(s); last_invalid = true; } +} + +void +session_purge_logs(short days) +{ + struct bile_object *obj; + struct session_log log; + size_t n, size = 0, count = 0, delta; + unsigned long secs, *ids = NULL; + struct tm *date_tm; + char date[20]; + + secs = (60UL * 60UL * 24UL * (unsigned long)days); + + logger_printf(logger, "[db] Purging session logs older than %d days", + days); + + for (n = 0; + (obj = bile_get_nth_of_type(db->sessions_bile, n, SL_LOG_RTYPE)); + n++) { + bile_read_object(db->sessions_bile, obj, &log, sizeof(log)); + + delta = Time - log.logged_on_at; + if (delta > secs) { + EXPAND_TO_FIT(ids, size, count * sizeof(unsigned long), + sizeof(unsigned long), 10 * sizeof(unsigned long)); + ids[count++] = obj->id; + } + + xfree(&obj); + } + + if (count) { + logger_printf(logger, "[db] Deleting %ld of %ld log entries", + count, n); + + for (n = 0; n < count; n++) { + bile_delete(db->sessions_bile, SL_LOG_RTYPE, ids[n]); + } + } + + if (ids) + xfree(&ids); } --- session.h Fri Sep 16 17:18:05 2022 +++ session.h Wed Sep 21 14:06:41 2022 @@ -118,7 +118,6 @@ struct session_menu_option { char title[50]; }; -void session_load_tally(void); struct session * session_create(char *node, char *via, struct node_funcs *node_funcs); void session_close(struct session *session); @@ -146,5 +145,6 @@ void session_recents(struct session *s); void session_who(struct session *s); char session_menu(struct session *s, char *title, char *prompt, const struct session_menu_option *opts, size_t nopts, bool show_opts); +void session_purge_logs(short days); #endif /* __SESSION_H__ */