Skip to content

Commit 05f54f0

Browse files
authored
UI - Fix diff not starting from last viewed snapshot (#2744) (#2856)
1 parent 6adf105 commit 05f54f0

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed

changedetectionio/model/Watch.py

+14-19
Original file line numberDiff line numberDiff line change
@@ -247,37 +247,32 @@ def newest_history_key(self):
247247
bump = self.history
248248
return self.__newest_history_key
249249

250-
# Given an arbitrary timestamp, find the closest next key
251-
# For example, last_viewed = 1000 so it should return the next 1001 timestamp
252-
#
253-
# used for the [diff] button so it can preset a smarter from_version
250+
# Given an arbitrary timestamp, find the best history key for the [diff] button so it can preset a smarter from_version
254251
@property
255-
def get_next_snapshot_key_to_last_viewed(self):
252+
def get_from_version_based_on_last_viewed(self):
256253

257254
"""Unfortunately for now timestamp is stored as string key"""
258255
keys = list(self.history.keys())
259256
if not keys:
260257
return None
258+
if len(keys) == 1:
259+
return keys[0]
261260

262261
last_viewed = int(self.get('last_viewed'))
263-
prev_k = keys[0]
264262
sorted_keys = sorted(keys, key=lambda x: int(x))
265263
sorted_keys.reverse()
266264

267-
# When the 'last viewed' timestamp is greater than the newest snapshot, return second last
268-
if last_viewed > int(sorted_keys[0]):
265+
# When the 'last viewed' timestamp is greater than or equal the newest snapshot, return second newest
266+
if last_viewed >= int(sorted_keys[0]):
269267
return sorted_keys[1]
270-
271-
for k in sorted_keys:
272-
if int(k) < last_viewed:
273-
if prev_k == sorted_keys[0]:
274-
# Return the second last one so we dont recommend the same version compares itself
275-
return sorted_keys[1]
276-
277-
return prev_k
278-
prev_k = k
279-
280-
return keys[0]
268+
269+
# When the 'last viewed' timestamp is between snapshots, return the older snapshot
270+
for newer, older in list(zip(sorted_keys[0:], sorted_keys[1:])):
271+
if last_viewed < int(newer) and last_viewed >= int(older):
272+
return older
273+
274+
# When the 'last viewed' timestamp is less than the oldest snapshot, return oldest
275+
return sorted_keys[-1]
281276

282277
def get_history_snapshot(self, timestamp):
283278
import brotli

changedetectionio/templates/watch-overview.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@
191191
{% if watch.history_n >= 2 %}
192192

193193
{% if is_unviewed %}
194-
<a href="{{ url_for('diff_history_page', uuid=watch.uuid, from_version=watch.get_next_snapshot_key_to_last_viewed) }}" target="{{watch.uuid}}" class="pure-button pure-button-primary diff-link">History</a>
194+
<a href="{{ url_for('diff_history_page', uuid=watch.uuid, from_version=watch.get_from_version_based_on_last_viewed) }}" target="{{watch.uuid}}" class="pure-button pure-button-primary diff-link">History</a>
195195
{% else %}
196196
<a href="{{ url_for('diff_history_page', uuid=watch.uuid)}}" target="{{watch.uuid}}" class="pure-button pure-button-primary diff-link">History</a>
197197
{% endif %}

changedetectionio/tests/unit/test_watch_model.py

+22-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def test_watch_get_suggested_from_diff_timestamp(self):
1616
watch = Watch.model(datastore_path='/tmp', default={})
1717
watch.ensure_data_dir_exists()
1818

19-
watch['last_viewed'] = 110
2019

2120
# Contents from the browser are always returned from the browser/requests/etc as str, str is basically UTF-16 in python
2221
watch.save_history_text(contents="hello world", timestamp=100, snapshot_id=str(uuid_builder.uuid4()))
@@ -25,31 +24,42 @@ def test_watch_get_suggested_from_diff_timestamp(self):
2524
watch.save_history_text(contents="hello world", timestamp=112, snapshot_id=str(uuid_builder.uuid4()))
2625
watch.save_history_text(contents="hello world", timestamp=115, snapshot_id=str(uuid_builder.uuid4()))
2726
watch.save_history_text(contents="hello world", timestamp=117, snapshot_id=str(uuid_builder.uuid4()))
27+
28+
p = watch.get_from_version_based_on_last_viewed
29+
assert p == "100", "Correct 'last viewed' timestamp was detected"
2830

29-
p = watch.get_next_snapshot_key_to_last_viewed
30-
assert p == "112", "Correct last-viewed timestamp was detected"
31+
watch['last_viewed'] = 110
32+
p = watch.get_from_version_based_on_last_viewed
33+
assert p == "109", "Correct 'last viewed' timestamp was detected"
3134

32-
# When there is only one step of difference from the end of the list, it should return second-last change
3335
watch['last_viewed'] = 116
34-
p = watch.get_next_snapshot_key_to_last_viewed
35-
assert p == "115", "Correct 'second last' last-viewed timestamp was detected when using the last timestamp"
36+
p = watch.get_from_version_based_on_last_viewed
37+
assert p == "115", "Correct 'last viewed' timestamp was detected"
3638

3739
watch['last_viewed'] = 99
38-
p = watch.get_next_snapshot_key_to_last_viewed
39-
assert p == "100"
40+
p = watch.get_from_version_based_on_last_viewed
41+
assert p == "100", "When the 'last viewed' timestamp is less than the oldest snapshot, return oldest"
4042

4143
watch['last_viewed'] = 200
42-
p = watch.get_next_snapshot_key_to_last_viewed
43-
assert p == "115", "When the 'last viewed' timestamp is greater than the newest snapshot, return second last "
44+
p = watch.get_from_version_based_on_last_viewed
45+
assert p == "115", "When the 'last viewed' timestamp is greater than the newest snapshot, return second newest"
4446

4547
watch['last_viewed'] = 109
46-
p = watch.get_next_snapshot_key_to_last_viewed
48+
p = watch.get_from_version_based_on_last_viewed
4749
assert p == "109", "Correct when its the same time"
4850

4951
# new empty one
5052
watch = Watch.model(datastore_path='/tmp', default={})
51-
p = watch.get_next_snapshot_key_to_last_viewed
53+
p = watch.get_from_version_based_on_last_viewed
5254
assert p == None, "None when no history available"
5355

56+
watch.save_history_text(contents="hello world", timestamp=100, snapshot_id=str(uuid_builder.uuid4()))
57+
p = watch.get_from_version_based_on_last_viewed
58+
assert p == "100", "Correct with only one history snapshot"
59+
60+
watch['last_viewed'] = 200
61+
p = watch.get_from_version_based_on_last_viewed
62+
assert p == "100", "Correct with only one history snapshot"
63+
5464
if __name__ == '__main__':
5565
unittest.main()

0 commit comments

Comments
 (0)