commit 56952c8e629b016336399a700e304ba180e1669c
parent 464649828f88bb844caf573c4c82c47478f9c932
Author: William Casarin <jb55@jb55.com>
Date:   Tue, 14 Nov 2023 10:25:22 -0800
ndb/stat: include totals in stat output
Diffstat:
3 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/ndb.c b/ndb.c
@@ -24,16 +24,26 @@ static void print_stats(struct ndb_stat *stat)
 	const char *name;
 	struct ndb_stat_counts *c;
 
+	struct ndb_stat_counts total;
+	ndb_stat_counts_init(&total);
+
 	printf("name\tcount\tkey_bytes\tvalue_bytes\ttotal_bytes\n");
-	printf("dbs\n---\n");
+	printf("---\ndbs\n---\n");
 	for (i = 0; i < NDB_DBS; i++) {
 		name = ndb_db_name(i);
 
+		total.count += stat->dbs[i].count;
+		total.key_size += stat->dbs[i].key_size;
+		total.value_size += stat->dbs[i].value_size;
+
 		printf("%s\t", name);
 		print_stat_counts(&stat->dbs[i]);
 	}
 
-	printf("kinds\n-----\n");
+	printf("total\t");
+	print_stat_counts(&total);
+
+	printf("-----\nkinds\n-----\n");
 	for (i = 0; i < NDB_CKIND_COUNT; i++) {
 		c = &stat->common_kinds[i];
 		if (c->count == 0)
diff --git a/nostrdb.c b/nostrdb.c
@@ -2909,7 +2909,7 @@ int ndb_builder_new_tag(struct ndb_builder *builder)
 	return cursor_push_tag(&builder->note_cur, &tag);
 }
 
-static void ndb_stat_counts_init(struct ndb_stat_counts *counts)
+void ndb_stat_counts_init(struct ndb_stat_counts *counts)
 {
 	counts->count = 0;
 	counts->key_size = 0;
diff --git a/nostrdb.h b/nostrdb.h
@@ -271,6 +271,7 @@ int ndb_builder_push_tag_str(struct ndb_builder *builder, const char *str, int l
 
 // stats
 int ndb_stat(struct ndb *ndb, struct ndb_stat *stat);
+void ndb_stat_counts_init(struct ndb_stat_counts *counts);
 
 static inline struct ndb_str ndb_note_str(struct ndb_note *note,
 					  union ndb_packed_str *pstr)