Skip to content

Commit

Permalink
feat: retrieving monthly data (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonNotJson authored Oct 21, 2023
1 parent 0e7ddbb commit 5256a98
Showing 1 changed file with 15 additions and 67 deletions.
82 changes: 15 additions & 67 deletions src/lambda/test-get-single-thread/index.py
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()

0 comments on commit 5256a98

Please sign in to comment.