From 67624a252bccc0cc0bb250681d977cab65dfc8d5 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Mon, 24 Jul 2023 21:55:48 +0200 Subject: [PATCH] Remove DB records for expired offers option. --- basicswap/basicswap.py | 25 ++++++++++++++++++++++--- basicswap/basicswap_util.py | 2 ++ basicswap/db_util.py | 12 ++++++------ basicswap/templates/settings.html | 20 +++++++++++++++++--- basicswap/ui/page_debug.py | 1 + basicswap/ui/page_settings.py | 9 +++++---- doc/release-notes.md | 3 +++ tests/basicswap/test_xmr.py | 8 +++++--- 8 files changed, 61 insertions(+), 19 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index 32ce8e9..4b3c3b4 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -150,7 +150,9 @@ from .basicswap_util import ( VisibilityOverrideOptions, inactive_states, ) - +from basicswap.db_util import ( + remove_expired_data, +) PROTOCOL_VERSION_SECRET_HASH = 1 MINPROTO_VERSION_SECRET_HASH = 1 @@ -255,6 +257,8 @@ class BasicSwap(BaseApp): self._disabled_notification_types = self.settings.get('disabled_notification_types', []) self._keep_notifications = self.settings.get('keep_notifications', 50) self._show_notifications = self.settings.get('show_notifications', 10) + self._expire_db_records = self.settings.get('expire_db_records', False) + self._expire_db_records_after = self.settings.get('expire_db_records_after', 86400 * 7) # Seconds self._notifications_cache = {} self._is_encrypted = None self._is_locked = None @@ -4134,13 +4138,19 @@ class BasicSwap(BaseApp): if num_messages + num_removed > 0: self.log.info('Expired {} / {} messages.'.format(num_removed, num_messages)) - self.log.debug('TODO: Expire records from db') - finally: if rpc_conn: ci_part.close_rpc(rpc_conn) self.mxDB.release() + def expireDBRecords(self) -> None: + if self._is_locked is True: + self.log.debug('Not expiring database records while system locked') + return + if not self._expire_db_records: + return + remove_expired_data(self, self._expire_db_records_after) + def checkAcceptedBids(self) -> None: # Check for bids stuck as accepted (not yet in-progress) if self._is_locked is True: @@ -5969,6 +5979,7 @@ class BasicSwap(BaseApp): if now - self._last_checked_expired >= self.check_expired_seconds: self.expireMessages() + self.expireDBRecords() self.checkAcceptedBids() self._last_checked_expired = now @@ -6054,6 +6065,14 @@ class BasicSwap(BaseApp): settings_copy['debug_ui'] = new_value settings_changed = True + if 'expire_db_records' in data: + new_value = data['expire_db_records'] + ensure(type(new_value) == bool, 'New expire_db_records value not boolean') + if settings_copy.get('expire_db_records', False) != new_value: + self._expire_db_records = new_value + settings_copy['expire_db_records'] = new_value + settings_changed = True + if 'show_chart' in data: new_value = data['show_chart'] ensure(type(new_value) == bool, 'New show_chart value not boolean') diff --git a/basicswap/basicswap_util.py b/basicswap/basicswap_util.py index a9a7ed4..6fb2d66 100644 --- a/basicswap/basicswap_util.py +++ b/basicswap/basicswap_util.py @@ -306,6 +306,8 @@ def strBidState(state): return 'Request sent' if state == BidStates.BID_REQUEST_ACCEPTED: return 'Request accepted' + if state == BidStates.BID_STATE_UNKNOWN: + return 'Unknown bid state' return 'Unknown' + ' ' + str(state) diff --git a/basicswap/db_util.py b/basicswap/db_util.py index 73ad575..478f891 100644 --- a/basicswap/db_util.py +++ b/basicswap/db_util.py @@ -9,8 +9,7 @@ from .db import ( ) -def remove_expired_data(self): - self.log.warning('Removing expired data') +def remove_expired_data(self, time_offset: int = 0): now: int = self.getTime() try: session = self.openSession() @@ -18,11 +17,11 @@ def remove_expired_data(self): active_bids_insert = self.activeBidsQueryStr(now, '', 'b2') query_str = f''' SELECT o.offer_id FROM offers o - WHERE o.expire_at <= :now AND 0 = (SELECT COUNT(*) FROM bids b2 WHERE b2.offer_id = o.offer_id AND {active_bids_insert}) + WHERE o.expire_at <= :expired_at AND 0 = (SELECT COUNT(*) FROM bids b2 WHERE b2.offer_id = o.offer_id AND {active_bids_insert}) ''' num_offers = 0 num_bids = 0 - offer_rows = session.execute(query_str, {'now': now}) + offer_rows = session.execute(query_str, {'expired_at': now - time_offset}) for offer_row in offer_rows: num_offers += 1 bid_rows = session.execute('SELECT bids.bid_id FROM bids WHERE bids.offer_id = :offer_id', {'offer_id': offer_row[0]}) @@ -48,9 +47,10 @@ def remove_expired_data(self): session.execute('DELETE FROM sentoffers WHERE sentoffers.offer_id = :offer_id', {'offer_id': offer_row[0]}) session.execute('DELETE FROM actions WHERE actions.linked_id = :offer_id', {'offer_id': offer_row[0]}) session.execute('DELETE FROM offers WHERE offers.offer_id = :offer_id', {'offer_id': offer_row[0]}) - session.execute('DELETE FROM message_links WHERE linked_type = :type_ind AND linked_id = :linked_id', {'type_ind': int(Concepts.OFFER), 'offer_id': offer_row[0]}) + session.execute('DELETE FROM message_links WHERE linked_type = :type_ind AND linked_id = :offer_id', {'type_ind': int(Concepts.OFFER), 'offer_id': offer_row[0]}) - self.log.warning(f'Removed data for {num_offers} expired offers and {num_bids} bids.') + if num_offers > 0 or num_bids > 0: + self.log.info('Removed data for {} expired offer{} and {} bid{}.'.format(num_offers, 's' if num_offers != 1 else '', num_bids, 's' if num_bids != 1 else '')) finally: self.closeSession(session) diff --git a/basicswap/templates/settings.html b/basicswap/templates/settings.html index f729770..e460f86 100644 --- a/basicswap/templates/settings.html +++ b/basicswap/templates/settings.html @@ -308,7 +308,7 @@ - + Debug Mode UI
@@ -322,6 +322,20 @@
+ + Remove DB records for expired offers + +
+ + + + +
+ + @@ -445,7 +459,7 @@