v1.0.0 - Version 1.0.0)
This commit is contained in:
84
src/api.c
84
src/api.c
@@ -1503,6 +1503,32 @@ char* generate_config_text(void) {
|
||||
return config_text;
|
||||
}
|
||||
|
||||
// Helper function to format numbers with commas
|
||||
void format_number_with_commas(long long num, char* buffer, size_t buffer_size) {
|
||||
char temp[32];
|
||||
int len = snprintf(temp, sizeof(temp), "%lld", num);
|
||||
int comma_count = (len - 1) / 3;
|
||||
int result_len = len + comma_count;
|
||||
|
||||
if (result_len >= (int)buffer_size) {
|
||||
buffer[0] = '\0';
|
||||
return;
|
||||
}
|
||||
|
||||
int j = result_len - 1;
|
||||
int k = len - 1;
|
||||
int comma_pos = 0;
|
||||
|
||||
while (k >= 0) {
|
||||
buffer[j--] = temp[k--];
|
||||
comma_pos++;
|
||||
if (comma_pos % 3 == 0 && k >= 0) {
|
||||
buffer[j--] = ',';
|
||||
}
|
||||
}
|
||||
buffer[result_len] = '\0';
|
||||
}
|
||||
|
||||
// Generate human-readable stats text
|
||||
char* generate_stats_text(void) {
|
||||
char* stats_json = generate_stats_json();
|
||||
@@ -1575,17 +1601,17 @@ char* generate_stats_text(void) {
|
||||
// Database Overview section
|
||||
offset += snprintf(stats_text + offset, 16384 - offset,
|
||||
|
||||
"Database Size %.2f MB\n"
|
||||
"Total Events %lld\n"
|
||||
"Active Subscriptions %d\n"
|
||||
"Oldest Event %s\n"
|
||||
"Newest Event %s\n"
|
||||
"\n",
|
||||
db_mb, db_bytes, total, active_subs, oldest_str, newest_str);
|
||||
"Database Size %.2f MB (%lld bytes)\n"
|
||||
"Total Events %lld\n"
|
||||
"Active Subscriptions %d\n"
|
||||
"Oldest Event %s\n"
|
||||
"Newest Event %s\n"
|
||||
"\n",
|
||||
db_mb, db_bytes, total, active_subs, oldest_str, newest_str);
|
||||
|
||||
// Event Kind Distribution section
|
||||
offset += snprintf(stats_text + offset, 16384 - offset,
|
||||
"Event Kind Distribution:\n");
|
||||
"Event_Kind_Distribution:\n");
|
||||
|
||||
if (event_kinds && cJSON_IsArray(event_kinds)) {
|
||||
cJSON* kind_item = NULL;
|
||||
@@ -1595,11 +1621,45 @@ char* generate_stats_text(void) {
|
||||
cJSON* percentage = cJSON_GetObjectItem(kind_item, "percentage");
|
||||
|
||||
if (kind && count && percentage) {
|
||||
// Format event kind (right-justified, minimum 5 chars wide with underscores)
|
||||
char kind_str[16];
|
||||
int kind_val = (int)cJSON_GetNumberValue(kind);
|
||||
char temp_kind[16];
|
||||
snprintf(temp_kind, sizeof(temp_kind), "%d", kind_val);
|
||||
|
||||
// Calculate padding needed (minimum 5 chars, but more if kind is longer)
|
||||
int kind_len = strlen(temp_kind);
|
||||
int min_width = 5;
|
||||
int total_width = (kind_len > min_width) ? kind_len : min_width;
|
||||
int padding = total_width - kind_len;
|
||||
|
||||
// Create padded string with underscores
|
||||
memset(kind_str, '_', padding);
|
||||
strcpy(kind_str + padding, temp_kind);
|
||||
|
||||
// Format count with commas (right-justified, 11 chars wide with underscores)
|
||||
char count_str[16];
|
||||
format_number_with_commas((long long)cJSON_GetNumberValue(count), count_str, sizeof(count_str));
|
||||
char count_formatted[16];
|
||||
snprintf(count_formatted, sizeof(count_formatted), "%11s", count_str);
|
||||
// Replace spaces with underscores for alignment
|
||||
for (int i = 0; count_formatted[i]; i++) {
|
||||
if (count_formatted[i] == ' ') count_formatted[i] = '_';
|
||||
}
|
||||
|
||||
// Format percentage (right-justified, 7 chars wide including %, with underscores)
|
||||
double pct = cJSON_GetNumberValue(percentage);
|
||||
char pct_str[8];
|
||||
snprintf(pct_str, sizeof(pct_str), "%.1f%%", pct);
|
||||
// Pad with underscores to make 7 chars total (right-justified)
|
||||
char pct_formatted[8];
|
||||
int pct_len = strlen(pct_str);
|
||||
int underscores_needed = 7 - pct_len;
|
||||
memset(pct_formatted, '_', underscores_needed);
|
||||
strcpy(pct_formatted + underscores_needed, pct_str);
|
||||
|
||||
offset += snprintf(stats_text + offset, 16384 - offset,
|
||||
"%lld\t%lld\t%.1f%%\n",
|
||||
(long long)cJSON_GetNumberValue(kind),
|
||||
(long long)cJSON_GetNumberValue(count),
|
||||
cJSON_GetNumberValue(percentage));
|
||||
"%s%s_%s\n", kind_str, count_formatted, pct_formatted);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user