Refactor: Better Post Metrics data fetching

This commit is contained in:
Donatas Kirda 2024-05-15 00:41:28 +03:00
parent 196166897e
commit a6f3c6c695
Signed by: bloodwiing
GPG Key ID: 63020D8D3F4A164F
8 changed files with 154 additions and 77 deletions

View File

@ -14,6 +14,8 @@
* @type {import("$types/base").CommentTreeNode[] | null}
*/
export let commentTree = null;
$: isPreview = commentTree == null;
</script>
<style lang="scss">

View File

@ -24,12 +24,22 @@
posts.forEach((p, index) => {
hidden[index] = p == post;
});
goto(`/posts?glance=${currentPreview.id}`, {
replaceState: true,
noScroll: true
});
}
function dismiss() {
currentPreview = null;
hidden = Array(posts.length).fill(false);
// goto(`/posts`, {
// replaceState: true,
// noScroll: true
// });
}
function expand() {

View File

@ -3,15 +3,32 @@ import { getUser, getUsersCachedByRef } from './user';
/**
* @typedef {import('$types/base').Post} Post
* @typedef {import('$types/base').PostMetrics} PostMetrics
*/
/**
* @param {import('postgres').Row} row
* @returns {PostMetrics}
*/
function parsePostMetricsFromRow(row) {
return {
commentCount: BigInt(row['comment_count']),
userCount: BigInt(row['user_count']),
latestActivity: row['latest_activity'],
engagement: BigInt(row['engagement']),
age: row['age'],
relevancy: row['relevancy'],
};
}
/**
* @param {import('$types/base').User | null} author
* @param {import('$types/base').Category} category
* @param {import('postgres').Row} row
* @param {boolean} withMetrics
* @returns {Post}
*/
function parsePostFromRow(author, category, row) {
function parsePostFromRow(author, category, row, withMetrics = false) {
return {
id: row['id'],
author: author,
@ -22,7 +39,8 @@ function parsePostFromRow(author, category, row) {
rating: {
likes: BigInt(row['likes']),
dislikes: BigInt(row['dislikes']),
}
},
metrics: withMetrics ? parsePostMetricsFromRow(row) : null,
};
}
@ -31,19 +49,15 @@ function parsePostFromRow(author, category, row) {
* @param {import('$types/base').Category | undefined} category
* @param {number} limit
* @param {number} offset
* @param {boolean} withMetrics
* @returns {Promise<Post[]>}
*/
export async function getPosts(sql, category = undefined, limit = 10, offset = 0) {
let filter;
if (category === undefined) {
filter = sql``;
} else {
filter = sql`WHERE category_id = ${ category.id }`;
}
export async function getPosts(sql, category = undefined, limit = 10, offset = 0, withMetrics = false) {
const filter = category === undefined ? sql`` : sql`WHERE category_id = ${ category.id }`;
const metrics = !withMetrics ? sql`` : sql`, comment_count, user_count, latest_activity, engagement, age, relevancy`;
const query = sql`
SELECT id, author_id, name, category_id, latest_content, created_date, likes, dislikes
SELECT id, author_id, name, category_id, latest_content, created_date, likes, dislikes ${ metrics }
FROM doki8902.message_post
${ filter }
FETCH FIRST ${ limit } ROWS ONLY
@ -60,7 +74,8 @@ export async function getPosts(sql, category = undefined, limit = 10, offset = 0
return posts.map(row => parsePostFromRow(
users.get(row['author_id']) || null,
/** @type {import('$types/base').Category} */ (categories.get(row['category_id'])),
row
row,
withMetrics
));
}

View File

@ -1,46 +1,89 @@
/**
* @typedef {import('$types/base').PostMetrics} PostMetrics
*/
// /**
// * @typedef {import('$types/base').PostMetrics} PostMetrics
// */
import { sql } from '$lib/db.server';
// 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
// * @param {import('postgres').Row} row
// * @returns {PostMetrics}
// */
// function parsePostMetricsFromRowAndPost(post, row) {
// return {
// postId: post.id,
// 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 }`;
// /**
// * @param {import('postgres').Row} row
// * @returns {PostMetrics}
// */
// function parsePostMetricsFromRow(row) {
// return {
// postId: row['post_id'],
// commentCount: BigInt(row['comment_count']),
// userCount: BigInt(row['user_count']),
// latestActivity: row['latest_activity'],
// rating: {
// likes: row['likes'],
// dislikes: row['dislikes']
// },
// score: BigInt(row['score']),
// engagement: BigInt(row['engagement']),
// age: row['age'],
// relevancy: row['relevancy']
// };
// }
const result = await query;
// /**
// * @param {import('$types/base').Post} post
// * @returns {Promise<PostMetrics | import('$types/error').Error>}
// */
// export async function getPostMetricsForPost(post) {
// const query = sql`
// SELECT comment_count, user_count, latest_activity, score, engagement, age, relevancy
// FROM doki8902.post_metrics
// WHERE post_id = ${ post.id }`;
if (result.length == 0) {
return {
error: true,
msg: `Could not find PostMetrics for Post ID ${post.id}`
};
}
// const result = await query;
return parsePostMetricsFromRow(post, result[0]);
}
// if (result.length == 0) {
// return {
// error: true,
// msg: `Could not find PostMetrics for Post ID ${post.id}`
// };
// }
// return parsePostMetricsFromRowAndPost(post, result[0]);
// }
// /**
// * @param {number} post_id
// * @returns {Promise<PostMetrics | import('$types/error').Error>}
// */
// export async function getPostMetricsForPostId(post_id) {
// const query = sql`
// SELECT post_id, comment_count, user_count, latest_activity, likes, dislikes, 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(result[0]);
// }

View File

@ -1,9 +1,20 @@
import { getPosts } from '$lib/server/db/post';
import { getPostMetricsForPostId } from '$lib/server/db/postmertics';
/** @type {import('./$types').PageServerLoad} */
export async function load({ locals }) {
let result = await getPosts(locals.sql);
export async function load({ locals, url }) {
const result = await getPosts(locals.sql, undefined, 10, 0, true);
// const glance = url.searchParams.get('glance');
// const glanceID = glance ? parseInt(glance) : null
// let postMetrics = null;
// if (glanceID) {
// postMetrics = await getPostMetricsForPostId(glanceID);
// }
console.log(result);
return {
posts: result

View File

@ -2,7 +2,9 @@
import Postlist from "$comp/postlist.svelte";
/**
* @type {{posts: import("$types/base").Post[]}}
* @type {{
* posts: import("$types/base").Post[]
* }}
*/
export let data;
</script>

View File

@ -1,6 +1,5 @@
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";
@ -16,8 +15,6 @@ 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

@ -3,19 +3,28 @@ export type Result<T> = Map<number, T>;
export type User = {
id: number,
name: string,
joinDate: Date
joinDate: Date,
};
export type Rating = {
likes: bigint,
dislikes: bigint
dislikes: bigint,
}
export type Category = {
id: number,
name: string
name: string,
}
export type PostMetrics = {
commentCount: bigint,
userCount: bigint,
latestActivity: Date,
engagement: bigint,
age: number,
relevancy: number,
};
export type Post = {
id: number,
author: User | null,
@ -23,7 +32,8 @@ export type Post = {
category: Category,
content: string,
postDate: Date,
rating: Rating
rating: Rating,
metrics: PostMetrics | null,
};
export type Comment = {
@ -32,23 +42,10 @@ export type Comment = {
content: string,
commentDate: Date,
rating: Rating,
parentCommentId: number
parentCommentId: number,
};
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
children: (CommentTreeNode | number)[],
};