Skip to content

Commit

Permalink
database: add max_reassign_logentries parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Morg42 committed Oct 19, 2024
1 parent 8df8228 commit ca3c8e8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
22 changes: 16 additions & 6 deletions database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Database(SmartPlugin):
"""

ALLOW_MULTIINSTANCE = True
PLUGIN_VERSION = '1.6.12'
PLUGIN_VERSION = '1.6.13'

# SQL queries: {item} = item table name, {log} = log table name
# time, item_id, val_str, val_num, val_bool, changed
Expand Down Expand Up @@ -104,6 +104,7 @@ def __init__(self, sh, *args, **kwargs):
self._precision = self.get_parameter_value('precision')
self.count_logentries = self.get_parameter_value('count_logentries')
self.max_delete_logentries = self.get_parameter_value('max_delete_logentries')
self.max_reassign_logentries = self.get_parameter_value('max_reassign_logentries')
self._default_maxage = float(self.get_parameter_value('default_maxage'))

self._copy_database = self.get_parameter_value('copy_database')
Expand Down Expand Up @@ -976,15 +977,24 @@ def reassign_orphaned_id(self, orphan_id, to):
:type orphan_id: int
:type to: int
"""
log_info = self.logger.warning # info
log_debug = self.logger.error # debug
try:
self.logger.debug(f'reassigning orphaned data from (old) id {orphan_id} to (new) id {to}')
log_info(f'reassigning orphaned data from (old) id {orphan_id} to (new) id {to}')
cur = self._db_maint.cursor()
self._execute(self._prepare("UPDATE {log} SET item_id = :newid WHERE item_id = :orphanid;"), {'newid': to, 'orphanid': orphan_id}, cur=cur)
count = self.readLogCount(orphan_id, cur=cur)
log_debug(f'found {count} entries to reassign, reassigning {self.max_reassign_logentries} at once')

while count > 0:
log_debug(f'reassigning {min(count, self.max_reassign_logentries)} log entries')
self._execute(self._prepare("UPDATE {log} SET item_id = :newid WHERE item_id = :orphanid LIMIT :limit;"), {'newid': to, 'orphanid': orphan_id, 'limit': self.max_reassign_logentries}, cur=cur)
count -= self.max_reassign_logentries

self._execute(self._prepare("DELETE FROM {item} WHERE id = :orphanid LIMIT 1;"), {'orphanid': orphan_id}, cur=cur)
self.logger.info(f'reassigned orphaned id {orphan_id} to new id {to}')
log_info(f'reassigned orphaned id {orphan_id} to new id {to}')
cur.close()
self._db_maint.commit()
self.logger.debug('rebuilding orphan list')
log_debug('rebuilding orphan list')
self.build_orphanlist()
except Exception as e:
self.logger.error(f'error on reassigning id {orphan_id} to {to}: {e}')
Expand Down Expand Up @@ -1207,7 +1217,7 @@ def _expression(self, func):
expression['finalizer'] = func[:func.index(":")]
func = func[func.index(":") + 1:]
if func == 'count' or func.startswith('count'):
parts = re.match('(count)((<>|!=|<|=|>)(\d+))?', func)
parts = re.match(r'(count)((<>|!=|<|=|>)(\d+))?', func)
func = 'count'
if parts and parts.group(3) is not None:
expression['params']['op'] = parts.group(3)
Expand Down
10 changes: 9 additions & 1 deletion database/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugin:
keywords: database
support: https://knx-user-forum.de/forum/supportforen/smarthome-py/1021844-neues-database-plugin

version: 1.6.12 # Plugin version
version: 1.6.13 # Plugin version
sh_minversion: '1.9.3.2' # minimum shNG version to use this plugin
# sh_maxversion: # maximum shNG version to use this plugin (leave empty if latest)
multi_instance: True # plugin supports multi instance
Expand Down Expand Up @@ -72,6 +72,14 @@ parameters:
de: "Maximal auf einmal zu löschende Anzahl an Log Einträgen mit dem database_maxage Attribut, reduziert die Belastung der Datenbank bei alten Datenbeständen"
en: "Maximum number of Logentries to delete at once with database_maxage attribute, reduces load on database with old datasets"

max_reassign_logentries:
type: int
default: 20 # 000
valid_min: 10 # 00
description:
de: "Maximal auf einmal neu zuzuweisende Anzahl an Log Einträgen, reduziert die Belastung der Datenbank bei großen Datenbeständen"
en: "Maximum number of Logentries to reassign at once, reduces load on database with large datasets"

default_maxage:
type: int
default: 0
Expand Down

0 comments on commit ca3c8e8

Please sign in to comment.