Skip to content

Commit 5ce1ca2

Browse files
authored
Merge pull request #2706 from pi-hole/new/earliest_timestamp
Get earliest query timestamp from database
2 parents 616837b + 7d5d355 commit 5ce1ca2

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

src/api/docs/content/specs/info.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,20 @@ components:
994994
example: "pihole"
995995
queries:
996996
type: integer
997-
description: Number of queries in long-term database
997+
description: Number of queries in in-memory database
998998
example: 536956
999+
earliest_timestamp:
1000+
type: number
1001+
description: Unix timestamp of the earliest query in the in-memory database (Defaults to 0.0 if no queries are stored in memory)
1002+
example: 1611742120.5
1003+
queries_disk:
1004+
type: integer
1005+
description: Number of queries in on-disk database
1006+
example: 1234567
1007+
earliest_timestamp_disk:
1008+
type: number
1009+
description: Unix timestamp of the earliest query in the on-disk database (Defaults to 0.0 if no queries are stored on disk)
1010+
example: 1608450520.3
9991011
sqlite_version:
10001012
type: string
10011013
description: Version of embedded SQLite3 engine

src/api/info.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,17 @@ int api_info_database(struct ftl_conn *api)
146146
JSON_ADD_ITEM_TO_OBJECT(owner, "group", group);
147147
JSON_ADD_ITEM_TO_OBJECT(json, "owner", owner);
148148

149-
// Add number of queries in on-disk database
150-
const int queries_in_database = get_number_of_queries_in_DB(NULL, "query_storage");
149+
// Add number of queries and earliest timestamp in in-memory database
150+
double earliest_timestamp_mem = 0.0;
151+
const int queries_in_database = get_number_of_queries_in_DB(NULL, "query_storage", &earliest_timestamp_mem);
151152
JSON_ADD_NUMBER_TO_OBJECT(json, "queries", queries_in_database);
153+
JSON_ADD_NUMBER_TO_OBJECT(json, "earliest_timestamp", earliest_timestamp_mem);
154+
155+
// Add number of queries and earliest timestamp in on-disk database
156+
double earliest_timestamp_disk = 0.0;
157+
const int queries_in_disk_database = get_number_of_queries_in_DB(NULL, "disk.query_storage", &earliest_timestamp_disk);
158+
JSON_ADD_NUMBER_TO_OBJECT(json, "queries_disk", queries_in_disk_database);
159+
JSON_ADD_NUMBER_TO_OBJECT(json, "earliest_timestamp_disk", earliest_timestamp_disk);
152160

153161
// Add SQLite library version
154162
JSON_REF_STR_IN_OBJECT(json, "sqlite_version", get_sqlite3_version());

src/database/query-table.c

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ static bool get_memdb_size(sqlite3 *db, size_t *memsize, int *queries)
368368
*memsize = page_count * page_size;
369369

370370
// Get number of queries in the memory table
371-
if((*queries = get_number_of_queries_in_DB(db, "query_storage")) == DB_FAILED)
371+
if((*queries = get_number_of_queries_in_DB(db, "query_storage", NULL)) == DB_FAILED)
372372
return false;
373373

374374
return true;
@@ -506,21 +506,29 @@ bool detach_database(sqlite3* db, const char **message, const char *alias)
506506

507507
// Get number of queries either in the temp or in the on-diks database
508508
// This routine is used by the API routines.
509-
int get_number_of_queries_in_DB(sqlite3 *db, const char *tablename)
509+
int get_number_of_queries_in_DB(sqlite3 *db, const char *tablename, double *earliest_timestamp)
510510
{
511511
int rc = 0, num = 0;
512512
sqlite3_stmt *stmt = NULL;
513513

514-
// Count number of rows
515-
const size_t buflen = 42 + strlen(tablename);
516-
char *querystr = calloc(buflen, sizeof(char));
517-
snprintf(querystr, buflen, "SELECT COUNT(*) FROM %s", tablename);
518-
519514
// The database pointer may be NULL, meaning we want the memdb
520515
if(db == NULL)
521516
db = get_memdb();
522517

523-
// PRAGMA page_size
518+
// Build query string based on whether we need the earliest timestamp too
519+
const size_t buflen = 37 + strlen(tablename);
520+
char *querystr = calloc(buflen, sizeof(char));
521+
if(earliest_timestamp != NULL)
522+
{
523+
// Get both count and earliest timestamp
524+
snprintf(querystr, buflen, "SELECT COUNT(*), MIN(timestamp) FROM %s", tablename);
525+
}
526+
else
527+
{
528+
// Get only count
529+
snprintf(querystr, buflen, "SELECT COUNT(*) FROM %s", tablename);
530+
}
531+
524532
rc = sqlite3_prepare_v2(db, querystr, -1, &stmt, NULL);
525533
if( rc != SQLITE_OK )
526534
{
@@ -532,7 +540,13 @@ int get_number_of_queries_in_DB(sqlite3 *db, const char *tablename)
532540
}
533541
rc = sqlite3_step(stmt);
534542
if( rc == SQLITE_ROW )
543+
{
544+
// Get count from first column
535545
num = sqlite3_column_int(stmt, 0);
546+
// Get timestamp from second column if requested
547+
if(earliest_timestamp != NULL && sqlite3_column_type(stmt, 1) != SQLITE_NULL)
548+
*earliest_timestamp = sqlite3_column_double(stmt, 1);
549+
}
536550
sqlite3_finalize(stmt);
537551
free(querystr);
538552

@@ -617,8 +631,8 @@ bool import_queries_from_disk(void)
617631
}
618632

619633
// Get number of queries on disk before detaching
620-
disk_db_num = get_number_of_queries_in_DB(memdb, "disk.query_storage");
621-
mem_db_num = get_number_of_queries_in_DB(memdb, "query_storage");
634+
disk_db_num = get_number_of_queries_in_DB(memdb, "disk.query_storage", NULL);
635+
mem_db_num = get_number_of_queries_in_DB(memdb, "query_storage", NULL);
622636

623637
log_info("Imported %u queries from the on-disk database (it has %u rows)", mem_db_num, disk_db_num);
624638

@@ -710,7 +724,7 @@ bool export_queries_to_disk(const bool final)
710724
}
711725

712726
// Update number of queries in the disk database
713-
disk_db_num = get_number_of_queries_in_DB(memdb, "disk.query_storage");
727+
disk_db_num = get_number_of_queries_in_DB(memdb, "disk.query_storage", NULL);
714728
}
715729

716730
// Export linking tables and current AUTOINCREMENT values to the disk database
@@ -778,7 +792,7 @@ bool delete_old_queries_from_db(const bool use_memdb, const double mintime)
778792
mintime, sqlite3_errstr(rc));
779793

780794
// Update number of queries in in-memory database
781-
const int new_num = get_number_of_queries_in_DB(NULL, "query_storage");
795+
const int new_num = get_number_of_queries_in_DB(NULL, "query_storage", NULL);
782796
log_debug(DEBUG_GC, "delete_old_queries_from_db(): Deleted %i (%u) queries, new number of queries in memory: %i",
783797
sqlite3_changes(db), (mem_db_num - new_num), new_num);
784798
mem_db_num = new_num;
@@ -1738,7 +1752,7 @@ bool queries_to_database(void)
17381752
}
17391753

17401754
// Update number of queries in in-memory database
1741-
mem_db_num = get_number_of_queries_in_DB(NULL, "query_storage");
1755+
mem_db_num = get_number_of_queries_in_DB(NULL, "query_storage", NULL);
17421756

17431757
if(config.debug.database.v.b && updated + added > 0)
17441758
{

src/database/query-table.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void close_memory_database(void);
113113
bool import_queries_from_disk(void);
114114
bool attach_database(sqlite3* db, const char **message, const char *path, const char *alias);
115115
bool detach_database(sqlite3* db, const char **message, const char *alias);
116-
int get_number_of_queries_in_DB(sqlite3 *db, const char *tablename);
116+
int get_number_of_queries_in_DB(sqlite3 *db, const char *tablename, double *earliest_timestamp);
117117
bool export_queries_to_disk(const bool final);
118118
bool delete_old_queries_from_db(const bool use_memdb, const double mintime);
119119
bool add_additional_info_column(sqlite3 *db);

0 commit comments

Comments
 (0)