Re #30 - Delete history watch snapshots (#31)

Re #30 - Delete history watch snapshots  Scrub - Optionally delete history snapshots newer than timestamp
pull/32/head
dgtlmoon 4 years ago committed by GitHub
parent 294256d5c3
commit 934d8c6211
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -174,19 +174,16 @@ def changedetection_app(config=None, datastore_o=None):
if request.method == 'POST': if request.method == 'POST':
confirmtext = request.form.get('confirmtext') confirmtext = request.form.get('confirmtext')
limit_timestamp = int(request.form.get('limit_date'))
if confirmtext == 'scrub': if confirmtext == 'scrub':
for txt_file_path in Path(app.config['datastore_path']).rglob('*.txt'):
os.unlink(txt_file_path)
for uuid, watch in datastore.data['watching'].items(): for uuid, watch in datastore.data['watching'].items():
watch['last_checked'] = 0 if len(str(limit_timestamp)) == 10:
watch['last_changed'] = 0 datastore.scrub_watch(uuid, limit_timestamp = limit_timestamp)
watch['previous_md5'] = None else:
watch['history'] = {} datastore.scrub_watch(uuid)
datastore.needs_write = True
messages.append({'class': 'ok', 'message': 'Cleaned all version history.'}) messages.append({'class': 'ok', 'message': 'Cleaned all version history.'})
else: else:
messages.append({'class': 'error', 'message': 'Wrong confirm text.'}) messages.append({'class': 'error', 'message': 'Wrong confirm text.'})

@ -183,12 +183,28 @@ class ChangeDetectionStore:
tags.sort() tags.sort()
return tags return tags
def unlink_history_file(self, path):
try:
os.unlink(path)
except (FileNotFoundError, IOError):
pass
# Delete a single watch by UUID
def delete(self, uuid): def delete(self, uuid):
with self.lock: with self.lock:
if uuid == 'all': if uuid == 'all':
self.__data['watching'] = {} self.__data['watching'] = {}
# GitHub #30 also delete history records
for uuid in self.data['watching']:
for path in self.data['watching'][uuid]['history'].values():
self.unlink_history_file(path)
else: else:
del (self.__data['watching'][uuid]) for path in self.data['watching'][uuid]['history'].values():
self.unlink_history_file(path)
del self.data['watching'][uuid]
self.needs_write = True self.needs_write = True
@ -205,6 +221,47 @@ class ChangeDetectionStore:
# Probably their should be dict... # Probably their should be dict...
return self.data['watching'][uuid].get(val) return self.data['watching'][uuid].get(val)
# Remove a watchs data but keep the entry (URL etc)
def scrub_watch(self, uuid, limit_timestamp = False):
import hashlib
del_timestamps = []
for timestamp, path in self.data['watching'][uuid]['history'].items():
if not limit_timestamp or (limit_timestamp is not False and int(timestamp) > limit_timestamp):
self.unlink_history_file(path)
del_timestamps.append(timestamp)
if not limit_timestamp:
self.data['watching'][uuid]['last_checked'] = 0
self.data['watching'][uuid]['last_changed'] = 0
self.data['watching'][uuid]['previous_md5'] = 0
for timestamp in del_timestamps:
del self.data['watching'][uuid]['history'][str(timestamp)]
# If there was a limitstamp, we need to reset some meta data about the entry
# This has to happen after we remove the others from the list
if limit_timestamp:
newest_key = self.get_newest_history_key(uuid)
if newest_key:
self.data['watching'][uuid]['last_checked'] = int(newest_key)
# @todo should be the original value if it was less than newest key
self.data['watching'][uuid]['last_changed'] = int(newest_key)
try:
with open(self.data['watching'][uuid]['history'][str(newest_key)], "rb") as fp:
content = fp.read()
self.data['watching'][uuid]['previous_md5'] = hashlib.md5(content).hexdigest()
except (FileNotFoundError, IOError):
self.data['watching'][uuid]['previous_md5'] = False
pass
self.needs_write = True
def add_watch(self, url, tag): def add_watch(self, url, tag):
with self.lock: with self.lock:
# @todo use a common generic version of this # @todo use a common generic version of this

@ -17,14 +17,19 @@
<div class="pure-control-group"> <div class="pure-control-group">
<br/> <br/>
<label for="confirmtext">Confirm</label><br/> <label for="confirmtext">Confirm text</label><br/>
<input type="text" id="confirmtext" required="" name="confirmtext" value="" size="10"/> <input type="text" id="confirmtext" required="" name="confirmtext" value="" size="10"/>
<br/> </div>
</div> <div class="pure-control-group">
<br/>
<label for="confirmtext">Limit delete history including and after date</label><br/>
<input type="text" id="limit_date" required="" name="limit_date" value="" size="10"/>
<br/>
</div>
<div class="pure-control-group"> <div class="pure-control-group">
<button type="submit" class="pure-button pure-button-primary">Scrub!</button> <button type="submit" class="pure-button pure-button-primary">Scrub!</button>
</div> </div>

@ -22,7 +22,7 @@
<div class="pure-control-group"> <div class="pure-control-group">
<a href="/" class="pure-button button-small button-cancel">Back</a> <a href="/" class="pure-button button-small button-cancel">Back</a>
<a href="/scrub" class="pure-button button-small button-cancel">Reset all version data</a> <a href="/scrub" class="pure-button button-small button-cancel">Delete history version data</a>
</div> </div>

Loading…
Cancel
Save