-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: retrieving monthly data (#364)
- Loading branch information
1 parent
0e7ddbb
commit 5256a98
Showing
1 changed file
with
15 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |