Add: Post Metrics
This commit is contained in:
parent
eb7271a0c3
commit
5eb5f2d5ff
46
src/lib/server/db/postmertics.js
Normal file
46
src/lib/server/db/postmertics.js
Normal 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]);
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
import { getCommentsForPost } from "$lib/server/db/comment";
|
import { getCommentsForPost } from "$lib/server/db/comment";
|
||||||
import { getPost } from "$lib/server/db/post";
|
import { getPost } from "$lib/server/db/post";
|
||||||
|
import { getPostMetrics } from "$lib/server/db/postmertics";
|
||||||
import { getIdFromName } from "$lib/util";
|
import { getIdFromName } from "$lib/util";
|
||||||
import { error } from "@sveltejs/kit";
|
import { error } from "@sveltejs/kit";
|
||||||
|
|
||||||
@ -15,6 +16,8 @@ export async function load({ params, locals }) {
|
|||||||
|
|
||||||
const comments = await getCommentsForPost(locals.sql, post_id);
|
const comments = await getCommentsForPost(locals.sql, post_id);
|
||||||
|
|
||||||
|
console.log(await getPostMetrics(post));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
post: post,
|
post: post,
|
||||||
comments: comments
|
comments: comments
|
||||||
|
|||||||
@ -39,3 +39,16 @@ export type CommentTreeNode = {
|
|||||||
parent: Comment,
|
parent: Comment,
|
||||||
children: (CommentTreeNode | number)[]
|
children: (CommentTreeNode | number)[]
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type PostMetrics = {
|
||||||
|
post: Post,
|
||||||
|
commentCount: bigint,
|
||||||
|
userCount: bigint,
|
||||||
|
latestActivity: Date,
|
||||||
|
rating: Rating,
|
||||||
|
score: bigint,
|
||||||
|
|
||||||
|
engagement: bigint,
|
||||||
|
age: number,
|
||||||
|
relevancy: number
|
||||||
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user