Fix diff not starting from last viewed snapshot (#2744)

pull/2856/head
MoshiMoshi0 3 weeks ago
parent 5dea5e1def
commit a023fca8f6

@ -247,12 +247,9 @@ class model(watch_base):
bump = self.history bump = self.history
return self.__newest_history_key return self.__newest_history_key
# Given an arbitrary timestamp, find the closest next key # Given an arbitrary timestamp, find the best history key for the [diff] button so it can preset a smarter from_version
# For example, last_viewed = 1000 so it should return the next 1001 timestamp
#
# used for the [diff] button so it can preset a smarter from_version
@property @property
def get_next_snapshot_key_to_last_viewed(self): def get_from_version_based_on_last_viewed(self):
"""Unfortunately for now timestamp is stored as string key""" """Unfortunately for now timestamp is stored as string key"""
keys = list(self.history.keys()) keys = list(self.history.keys())
@ -260,24 +257,23 @@ class model(watch_base):
return None return None
last_viewed = int(self.get('last_viewed')) last_viewed = int(self.get('last_viewed'))
prev_k = keys[0]
sorted_keys = sorted(keys, key=lambda x: int(x)) sorted_keys = sorted(keys, key=lambda x: int(x))
sorted_keys.reverse() sorted_keys.reverse()
# When the 'last viewed' timestamp is greater than the newest snapshot, return second last # When the 'last viewed' timestamp is greater than or equal the newest snapshot, return second newest
if last_viewed > int(sorted_keys[0]): if last_viewed >= int(sorted_keys[0]):
return sorted_keys[1] return sorted_keys[1]
for k in sorted_keys: # When the 'last viewed' timestamp is less than or equal the oldest snapshot, return oldest
if int(k) < last_viewed: if last_viewed <= int(sorted_keys[-1]):
if prev_k == sorted_keys[0]: return sorted_keys[-1]
# Return the second last one so we dont recommend the same version compares itself
return sorted_keys[1]
return prev_k for newer, older in list(zip(sorted_keys[0:], sorted_keys[1:])):
prev_k = k if last_viewed < int(newer) and last_viewed >= int(older):
return older
return keys[0] # Unreachable, return oldest
return sorted_keys[-1]
def get_history_snapshot(self, timestamp): def get_history_snapshot(self, timestamp):
import brotli import brotli

@ -191,7 +191,7 @@
{% if watch.history_n >= 2 %} {% if watch.history_n >= 2 %}
{% if is_unviewed %} {% if is_unviewed %}
<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> <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>
{% else %} {% else %}
<a href="{{ url_for('diff_history_page', uuid=watch.uuid)}}" target="{{watch.uuid}}" class="pure-button pure-button-primary diff-link">History</a> <a href="{{ url_for('diff_history_page', uuid=watch.uuid)}}" target="{{watch.uuid}}" class="pure-button pure-button-primary diff-link">History</a>
{% endif %} {% endif %}

@ -16,7 +16,6 @@ class TestDiffBuilder(unittest.TestCase):
watch = Watch.model(datastore_path='/tmp', default={}) watch = Watch.model(datastore_path='/tmp', default={})
watch.ensure_data_dir_exists() watch.ensure_data_dir_exists()
watch['last_viewed'] = 110
# Contents from the browser are always returned from the browser/requests/etc as str, str is basically UTF-16 in python # Contents from the browser are always returned from the browser/requests/etc as str, str is basically UTF-16 in python
watch.save_history_text(contents="hello world", timestamp=100, snapshot_id=str(uuid_builder.uuid4())) watch.save_history_text(contents="hello world", timestamp=100, snapshot_id=str(uuid_builder.uuid4()))
@ -26,30 +25,37 @@ class TestDiffBuilder(unittest.TestCase):
watch.save_history_text(contents="hello world", timestamp=115, snapshot_id=str(uuid_builder.uuid4())) watch.save_history_text(contents="hello world", timestamp=115, snapshot_id=str(uuid_builder.uuid4()))
watch.save_history_text(contents="hello world", timestamp=117, snapshot_id=str(uuid_builder.uuid4())) watch.save_history_text(contents="hello world", timestamp=117, snapshot_id=str(uuid_builder.uuid4()))
p = watch.get_next_snapshot_key_to_last_viewed p = watch.get_from_version_based_on_last_viewed
assert p == "112", "Correct last-viewed timestamp was detected" assert p == "100", "Correct 'last viewed' timestamp was detected"
watch['last_viewed'] = 110
p = watch.get_from_version_based_on_last_viewed
assert p == "109", "Correct 'last viewed' timestamp was detected"
# When there is only one step of difference from the end of the list, it should return second-last change
watch['last_viewed'] = 116 watch['last_viewed'] = 116
p = watch.get_next_snapshot_key_to_last_viewed p = watch.get_from_version_based_on_last_viewed
assert p == "115", "Correct 'second last' last-viewed timestamp was detected when using the last timestamp" assert p == "115", "Correct 'last viewed' timestamp was detected"
watch['last_viewed'] = 99 watch['last_viewed'] = 99
p = watch.get_next_snapshot_key_to_last_viewed p = watch.get_from_version_based_on_last_viewed
assert p == "100" assert p == "100", "When the 'last viewed' timestamp is less than the oldest snapshot, return oldest"
watch['last_viewed'] = 200 watch['last_viewed'] = 200
p = watch.get_next_snapshot_key_to_last_viewed p = watch.get_from_version_based_on_last_viewed
assert p == "115", "When the 'last viewed' timestamp is greater than the newest snapshot, return second last " assert p == "115", "When the 'last viewed' timestamp is greater than the newest snapshot, return second newest"
watch['last_viewed'] = 109 watch['last_viewed'] = 109
p = watch.get_next_snapshot_key_to_last_viewed p = watch.get_from_version_based_on_last_viewed
assert p == "109", "Correct when its the same time" assert p == "109", "Correct when its the same time"
# new empty one # new empty one
watch = Watch.model(datastore_path='/tmp', default={}) watch = Watch.model(datastore_path='/tmp', default={})
p = watch.get_next_snapshot_key_to_last_viewed p = watch.get_from_version_based_on_last_viewed
assert p == None, "None when no history available" assert p == None, "None when no history available"
watch.save_history_text(contents="hello world", timestamp=100, snapshot_id=str(uuid_builder.uuid4()))
p = watch.get_from_version_based_on_last_viewed
assert p == "100", "Correct with only one history snapshot"
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

Loading…
Cancel
Save