Add: Post Metrics

This commit is contained in:
2024-05-14 14:01:21 +03:00
parent eb7271a0c3
commit 5eb5f2d5ff
3 changed files with 62 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
/**
* @typedef {import('$types/base').PostMetrics} PostMetrics
*/
import { sql } from '$lib/db.server';
/**
* @param {import('$types/base').Post} post
* @param {import('postgres').Row} row
* @returns {PostMetrics}
*/
function parsePostMetricsFromRow(post, row) {
return {
post: post,
commentCount: BigInt(row['comment_count']),
userCount: BigInt(row['user_count']),
latestActivity: row['latest_activity'],
rating: post.rating,
score: BigInt(row['score']),
engagement: BigInt(row['engagement']),
age: row['age'],
relevancy: row['relevancy']
};
}
/**
* @param {import('$types/base').Post} post
* @returns {Promise<PostMetrics | import('$types/error').Error>}
*/
export async function getPostMetrics(post) {
const query = sql`
SELECT comment_count, user_count, latest_activity, score, engagement, age, relevancy
FROM doki8902.post_metrics
WHERE post_id = ${ post.id }`;
const result = await query;
if (result.length == 0) {
return {
error: true,
msg: `Could not find PostMetrics for Post ID ${post.id}`
};
}
return parsePostMetricsFromRow(post, result[0]);
}