Add: Post Metrics

This commit is contained in:
Donatas Kirda 2024-05-14 14:01:21 +03:00
parent eb7271a0c3
commit 5eb5f2d5ff
Signed by: bloodwiing
GPG Key ID: 63020D8D3F4A164F
3 changed files with 62 additions and 0 deletions

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]);
}

View File

@ -1,5 +1,6 @@
import { getCommentsForPost } from "$lib/server/db/comment";
import { getPost } from "$lib/server/db/post";
import { getPostMetrics } from "$lib/server/db/postmertics";
import { getIdFromName } from "$lib/util";
import { error } from "@sveltejs/kit";
@ -15,6 +16,8 @@ export async function load({ params, locals }) {
const comments = await getCommentsForPost(locals.sql, post_id);
console.log(await getPostMetrics(post));
return {
post: post,
comments: comments

View File

@ -39,3 +39,16 @@ export type CommentTreeNode = {
parent: Comment,
children: (CommentTreeNode | number)[]
};
export type PostMetrics = {
post: Post,
commentCount: bigint,
userCount: bigint,
latestActivity: Date,
rating: Rating,
score: bigint,
engagement: bigint,
age: number,
relevancy: number
};