From 595c52f611aff9131c97cf4f088b76608cecb683 Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 19 Jul 2022 17:04:37 +0930 Subject: [PATCH] bkpr: add timestamp filters to event lists --- plugins/bkpr/recorder.c | 43 ++++++++++++++++++++++++++++++++++++++--- plugins/bkpr/recorder.h | 37 ++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/plugins/bkpr/recorder.c b/plugins/bkpr/recorder.c index f5bbc4bbe..cb4ac7b0f 100644 --- a/plugins/bkpr/recorder.c +++ b/plugins/bkpr/recorder.c @@ -102,7 +102,10 @@ static struct channel_event *stmt2channel_event(const tal_t *ctx, struct db_stmt return e; } -struct chain_event **list_chain_events(const tal_t *ctx, struct db *db) +struct chain_event **list_chain_events_timebox(const tal_t *ctx, + struct db *db, + u64 start_time, + u64 end_time) { struct db_stmt *stmt; @@ -125,11 +128,20 @@ struct chain_event **list_chain_events(const tal_t *ctx, struct db *db) " FROM chain_events e" " LEFT OUTER JOIN accounts a" " ON e.account_id = a.id" + " WHERE e.timestamp > ?" + " AND e.timestamp <= ?" " ORDER BY e.timestamp, e.id;")); + db_bind_u64(stmt, 0, start_time); + db_bind_u64(stmt, 1, end_time); return find_chain_events(ctx, take(stmt)); } +struct chain_event **list_chain_events(const tal_t *ctx, struct db *db) +{ + return list_chain_events_timebox(ctx, db, 0, SQLITE_MAX_UINT); +} + struct chain_event **account_get_chain_events(const tal_t *ctx, struct db *db, struct account *acct) @@ -701,7 +713,11 @@ char *account_get_balance(const tal_t *ctx, return NULL; } -struct channel_event **list_channel_events(const tal_t *ctx, struct db *db) +struct channel_event **list_channel_events_timebox(const tal_t *ctx, + struct db *db, + u64 start_time, + u64 end_time) + { struct db_stmt *stmt; struct channel_event **results; @@ -721,8 +737,12 @@ struct channel_event **list_channel_events(const tal_t *ctx, struct db *db) " FROM channel_events e" " LEFT OUTER JOIN accounts a" " ON a.id = e.account_id" + " WHERE e.timestamp > ?" + " AND e.timestamp <= ?" " ORDER BY e.timestamp, e.id;")); + db_bind_u64(stmt, 0, start_time); + db_bind_u64(stmt, 1, end_time); db_query_prepared(stmt); results = tal_arr(ctx, struct channel_event *, 0); @@ -735,6 +755,12 @@ struct channel_event **list_channel_events(const tal_t *ctx, struct db *db) return results; } +struct channel_event **list_channel_events(const tal_t *ctx, struct db *db) +{ + return list_channel_events_timebox(ctx, db, 0, SQLITE_MAX_UINT); +} + + struct channel_event **account_get_channel_events(const tal_t *ctx, struct db *db, struct account *acct) @@ -827,7 +853,8 @@ struct onchain_fee **account_get_chain_fees(const tal_t *ctx, struct db *db, return results; } -struct onchain_fee **list_chain_fees(const tal_t *ctx, struct db *db) +struct onchain_fee **list_chain_fees_timebox(const tal_t *ctx, struct db *db, + u64 start_time, u64 end_time) { struct db_stmt *stmt; struct onchain_fee **results; @@ -844,11 +871,16 @@ struct onchain_fee **list_chain_fees(const tal_t *ctx, struct db *db) " FROM onchain_fees of" " LEFT OUTER JOIN accounts a" " ON a.id = of.account_id" + " WHERE timestamp > ?" + " AND timestamp <= ?" " ORDER BY " " of.timestamp" ", of.account_id" ", of.txid" ", of.update_count")); + + db_bind_u64(stmt, 0, start_time); + db_bind_u64(stmt, 1, end_time); db_query_prepared(stmt); results = tal_arr(ctx, struct onchain_fee *, 0); @@ -861,6 +893,11 @@ struct onchain_fee **list_chain_fees(const tal_t *ctx, struct db *db) return results; } +struct onchain_fee **list_chain_fees(const tal_t *ctx, struct db *db) +{ + return list_chain_fees_timebox(ctx, db, 0, SQLITE_MAX_UINT); +} + static struct account *stmt2account(const tal_t *ctx, struct db_stmt *stmt) { struct account *a = tal(ctx, struct account); diff --git a/plugins/bkpr/recorder.h b/plugins/bkpr/recorder.h index 5eb623a6c..f54c74c45 100644 --- a/plugins/bkpr/recorder.h +++ b/plugins/bkpr/recorder.h @@ -14,6 +14,7 @@ struct onchain_fee; #define EXTERNAL_ACCT "external" #define WALLET_ACCT WALLET +#define SQLITE_MAX_UINT 0x7FFFFFFFFFFFFFFF struct acct_balance { char *currency; @@ -53,14 +54,38 @@ struct channel_event **account_get_channel_events(const tal_t *ctx, /* Get all channel events, ordered by timestamp */ struct channel_event **list_channel_events(const tal_t *ctx, struct db *db); +/* Get all channel events, order by timestamp. + * + * @ctx - context to allocate from + * @db - database to query + * @start_time - UNIX timestamp to query after (exclusive) + * @end_time - UNIX timestamp to query until (inclusive) + */ +struct channel_event **list_channel_events_timebox(const tal_t *ctx, + struct db *db, + u64 start_time, + u64 end_time); + /* Get all chain events for this account */ struct chain_event **account_get_chain_events(const tal_t *ctx, struct db *db, struct account *acct); -/* Get all chain events, ordered by timestamp */ +/* Get all chain events, order by timestamp. */ struct chain_event **list_chain_events(const tal_t *ctx, struct db *db); +/* Get all chain events, order by timestamp. + * + * @ctx - context to allocate from + * @db - database to query + * @start_time - UNIX timestamp to query after (exclusive) + * @end_time - UNIX timestamp to query until (inclusive) + */ +struct chain_event **list_chain_events_timebox(const tal_t *ctx, + struct db *db, + u64 start_time, + u64 end_time); + /* Calculate the balances for an account * * @calc_sum - compute the total balance. error if negative @@ -93,6 +118,16 @@ bool find_txo_chain(const tal_t *ctx, /* List all chain fees, for all accounts */ struct onchain_fee **list_chain_fees(const tal_t *ctx, struct db *db); +/* Get all chain fees, order by timestamp. + * + * @ctx - context to allocate from + * @db - database to query + * @start_time - UNIX timestamp to query after (exclusive) + * @end_time - UNIX timestamp to query until (inclusive) + */ +struct onchain_fee **list_chain_fees_timebox(const tal_t *ctx, struct db *db, + u64 start_time, u64 end_time); + /* Returns a list of sums of the fees we've recorded for every txid * for the given account */ struct fee_sum **find_account_onchain_fees(const tal_t *ctx,