From 5256a986bc433aaf8c40329c62eb43829f4484f4 Mon Sep 17 00:00:00 2001 From: Jason Park <93040528+JasonNotJson@users.noreply.github.com> Date: Sun, 22 Oct 2023 02:35:35 +0900 Subject: [PATCH] feat: retrieving monthly data (#364) --- src/lambda/test-get-single-thread/index.py | 82 ++++------------------ 1 file changed, 15 insertions(+), 67 deletions(-) diff --git a/src/lambda/test-get-single-thread/index.py b/src/lambda/test-get-single-thread/index.py index bb7eb4c73..8d7c37286 100644 --- a/src/lambda/test-get-single-thread/index.py +++ b/src/lambda/test-get-single-thread/index.py @@ -1,82 +1,30 @@ from boto3.dynamodb.conditions import Key, Attr -from datetime import datetime +from datetime import datetime, timedelta from utils import JsonPayloadBuilder, table, resp_handler, s3_client, bucket, generate_url @resp_handler -def get_single_thread(board_id, thread_id, uid=""): - results = table.query( - KeyConditionExpression=Key("board_id").eq( - board_id) & Key("thread_id").eq(thread_id) - )["Items"] +def get_threads(): + # Calculate the timestamp for one month ago + one_month_ago = (datetime.utcnow() - timedelta(days=30)).timestamp() - if not results: - raise LookupError + thread_id_for_last_month = one_month_ago + \ + "_00000000-0000-0000-0000-000000000000" - item = results[0] + # Use the query method to fetch recent one month's data using the secondary index + response = table.query( + IndexName='UnivIDbyThreadIDIndex', + KeyConditionExpression=Key('thread_id').begins_with(1) & Key('thread_id').gt( + thread_id_for_last_month) + ) - if item["uid"] == uid: - table.update_item( - Key={ - "board_id": board_id, - "thread_id": thread_id, - }, - UpdateExpression="SET #v = #v + :incr, #nc = :newComment", - ConditionExpression="#uid = :uidValue", - ExpressionAttributeNames={ - '#v': 'views', - '#nc': 'new_comment', - '#uid': 'uid' - }, - ExpressionAttributeValues={ - ":incr": 1, - ":newComment": False, - ":uidValue": uid - } - ) - else: - # Increment the view count but do not update new_comment - table.update_item( - Key={ - "board_id": board_id, - "thread_id": thread_id, - }, - UpdateExpression="SET #v = #v + :incr", - ExpressionAttributeNames={ - '#v': 'views' - }, - ExpressionAttributeValues={ - ":incr": 1 - } - ) - - item["mod"] = False - if item["uid"] == uid: - item["mod"] = True - item['user_liked'] = uid in item.get('likes', []) - item['total_likes'] = len(item.get('likes', [])) - - if "obj_key" in item: - bucket_name = bucket - presigned_url = generate_url(bucket_name, item["obj_key"]) - if presigned_url: - item["url"] = presigned_url - - item.pop('uid', None) - item.pop('likes', None) - item.pop('obj_key', None) + items = response['Items'] body = JsonPayloadBuilder().add_status( - True).add_data(item).add_message('').compile() + True).add_data(items).add_message('').compile() return body def handler(event, context): - params = { - "board_id": event["queryStringParameters"]["board_id"], - "thread_id": event["queryStringParameters"]["thread_id"], - } - if "uid" in event["queryStringParameters"]: - params["uid"] = event["queryStringParameters"]["uid"] - return get_single_thread(**params) + return get_threads()