Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks for getting items sorted by progress #84

Merged
merged 6 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions deploy/lib/watch_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,18 @@ def _create_tables(self):
sort_key=Attribute(name="state", type=AttributeType.STRING),
index_name="state"
)
self.watch_history_table.add_global_secondary_index(
partition_key=Attribute(name="item_id", type=AttributeType.STRING),
index_name="item_id"
)
self.watch_history_table.add_global_secondary_index(
partition_key=Attribute(name="ep_progress",
type=AttributeType.STRING),
self.watch_history_table.add_local_secondary_index(
sort_key=Attribute(name="ep_progress", type=AttributeType.STRING),
index_name="ep_progress"
)
self.watch_history_table.add_global_secondary_index(
partition_key=Attribute(name="special_progress",
type=AttributeType.STRING),
self.watch_history_table.add_local_secondary_index(
sort_key=Attribute(name="special_progress", type=AttributeType.STRING),
index_name="special_progress"
)
self.watch_history_table.add_global_secondary_index(
partition_key=Attribute(name="item_id", type=AttributeType.STRING),
index_name="item_id"
)

self.episodes_table = Table(
self,
Expand Down
26 changes: 17 additions & 9 deletions src/lambdas/api/watch_history_by_collection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def handle(event, context):
collection_name = event["pathParameters"].get("collection_name")

method = event["requestContext"]["http"]["method"]
query_params = event.get("queryStringParameters")
query_params = event.get("queryStringParameters", {})

if collection_name not in schema.COLLECTION_NAMES:
err = f"Invalid collection name, " \
Expand All @@ -37,7 +37,6 @@ def handle(event, context):
}

if method == "GET":
query_params = event.get("queryStringParameters", {})
return _get(username, collection_name, auth_header, query_params, auth_header)
elif method == "POST":
body = event.get("body")
Expand Down Expand Up @@ -161,13 +160,22 @@ def _post_collection_item(username, collection_name, body, token):
del body["api_id"]
del body["api_name"]

if "ep_count" in res:
body["ep_count"] = res.get("ep_count")
body["special_count"] = res.get("special_count")
body["ep_progress"] = 0
body["special_progress"] = 0
body["watched_eps"] = 0
body["watched_special"] = 0
try:
current_item = watch_history_db.get_item(
username,
collection_name,
item_id,
include_deleted=True
)
except watch_history_db.NotFoundError:
current_item = {}

body["ep_count"] = res.get("ep_count", 0)
body["special_count"] = res.get("special_count", 0)
body["ep_progress"] = current_item.get("ep_progress", 0)
body["special_progress"] = current_item.get("special_progress", 0)
body["watched_eps"] = current_item.get("watched_eps", 0)
body["watched_special"] = current_item.get("watched_special", 0)

watch_history_db.add_item(username, collection_name, item_id, body)
return {
Expand Down
4 changes: 2 additions & 2 deletions src/lambdas/subscribers/show_updates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ def handle(event, context):
ep_progress = 0
else:
ep_progress = item["watched_eps"] / item["ep_count"]
item["ep_progress"] = str(round(ep_progress * 100, 2))
item["ep_progress"] = round(ep_progress * 100, 2)

item["special_count"] = show["special_count"]
if item["special_count"] == 0:
special_progress = 0
else:
special_progress = item["watched_specials"] / item["special_count"]
item["special_progress"] = str(round(special_progress * 100, 2))
item["special_progress"] = round(special_progress * 100, 2)

watch_history_db.put_item(item)
2 changes: 1 addition & 1 deletion src/layers/databases/python/watch_history_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def change_watched_eps(username, collection_name, item_id, change, special=False
ep_progress = 0
else:
ep_progress = (item[f"watched_{field_name}s"] + (change)) / item[f"{field_name}_count"]
ep_progress = str(round(ep_progress * 100, 2))
ep_progress = round(ep_progress * 100, 2)

_get_table().update_item(
Key={
Expand Down
9 changes: 6 additions & 3 deletions test/unittest/test_watch_history_by_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ class TestPost:

@patch("api.watch_history_by_collection.watch_history_db.add_item")
@patch("api.watch_history_by_collection.anime_api.post_anime")
def test_success(self, mocked_post_anime, mocked_post):
@patch("api.watch_history_by_collection.watch_history_db.get_item")
def test_success(self, mocked_get_item, mocked_post_anime, mocked_post):
mocked_post_anime.return_value = {
"id": "123"
}
Expand Down Expand Up @@ -269,7 +270,8 @@ def test_empty_body(self, mocked_post):

@patch("api.watch_history_by_collection.watch_history_db.add_item")
@patch("api.watch_history_by_collection.shows_api.post_show")
def test_show_success(self, mocked_post_show, mocked_post):
@patch("api.watch_history_by_collection.watch_history_db.get_item")
def test_show_success(self, mocked_get_item, mocked_post_show, mocked_post):
mocked_post_show.return_value = {
"id": "123"
}
Expand All @@ -286,7 +288,8 @@ def test_show_success(self, mocked_post_show, mocked_post):

@patch("api.watch_history_by_collection.watch_history_db.add_item")
@patch("api.watch_history_by_collection.movie_api.post_movie")
def test_movie_success(self, mocked_post_movie, mocked_post):
@patch("api.watch_history_by_collection.watch_history_db.get_item")
def test_movie_success(self, mocked_get_item, mocked_post_movie, mocked_post):
mocked_post_movie.return_value = {
"id": "123"
}
Expand Down
6 changes: 3 additions & 3 deletions test/unittest/test_watch_history_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def test_change_watched_eps(mocked_watch_history_db):

assert mock_func.update_values == {
'ExpressionAttributeNames': {'#p': 'ep_progress', '#w': 'watched_eps'},
'ExpressionAttributeValues': {':i': 1, ':p': '20.0'},
'ExpressionAttributeValues': {':i': 1, ':p': 20.0},
'Key': {'item_id': '123123', 'username': 'TEST_USERNAME'},
'UpdateExpression': 'SET #w=#w+:i, #p=:p'
}
Expand All @@ -370,7 +370,7 @@ def test_change_watched_eps_removal(mocked_watch_history_db):

assert mock_func.update_values == {
'ExpressionAttributeNames': {'#p': 'ep_progress', '#w': 'watched_eps'},
'ExpressionAttributeValues': {':i': -1, ':p': '0.0'},
'ExpressionAttributeValues': {':i': -1, ':p': 0.0},
'Key': {'item_id': '123123', 'username': 'TEST_USERNAME'},
'UpdateExpression': 'SET #w=#w+:i, #p=:p'
}
Expand All @@ -393,7 +393,7 @@ def test_change_watched_specials(mocked_watch_history_db):

assert mock_func.update_values == {
'ExpressionAttributeNames': {'#p': 'special_progress', '#w': 'watched_specials'},
'ExpressionAttributeValues': {':i': 1, ':p': '15.0'},
'ExpressionAttributeValues': {':i': 1, ':p': 15.0},
'Key': {'item_id': '123123', 'username': 'TEST_USERNAME'},
'UpdateExpression': 'SET #w=#w+:i, #p=:p'
}