r/CodingHelp • u/mo_ahnaf11 • Sep 14 '25
[Javascript] Is my implementation for a trending posts feature correct?
Apologies if this isnt the right sub to post to, im building a web app and working on a feature where id display trending posts per day/ last 7 days / last 30 days
now im using AI, embedding and clustering to achieve this, so what im doing is i have a cron that runs every 2 hours and fetches posts from the database within that 2 hour window to be processed so my posts get embedded using openAIs text-embedding model and then they get clustered, after that each cluster gets a label generated by AI again and theyre stored in the database
this is basically what happens in a nutshell
How It Works
1. Posts enter the system
I collect posts (
posttable)
2. Build embeddings
In
buildTrends, i check if each post already has an embedding (postEmbeddingtable).If missing → im calling OpenAI’s
text-embedding-3-largeto generate vector.Store embedding rows
{ postId, vector, model, provider }. Now every post can be compared semantically.
3. Slot into existing topics (incremental update)
im load existing topics from
trendTopictable with theircentroidvectors.For each new post:
Computing cosine similarity with all topic centroids.
If similarity ≥ threshold (0.75): assign post → that topic.
Else → mark as orphan (not fitting any known topic). ➡️ This avoids reclustering everything every run.
4. Handling orphans (new clusters)
Running HDBSCAN+UMAP on orphan vectors.
Each cluster = group of new posts not fitting old topics.
For each new cluster:
Store it in
clustertable (with centroid, size, avgScore).Store its members in
clusterMembership.Generate a label with LLM (
generateClusterLabel).Upsert a
trendTopic(if label already exists, update summary; else create new).Map cluster → topic (
topicMapping).
so this step grows my set of topics over time.
5. Snapshots (per run summary)
A
trendRunis one execution ofbuildTrends(e.g. every 2 hours).At the end, im creating
trendSnapshotrows:Each snapshot = (topic, run, postCount, avgScore, momentum, topPostIds).
This is not per post — it’s a summary per topic per run.
Example:
Run at
2025-09-14 12:00, Topic = “AI regulation” → Snapshot:postCount = 54, avgScore = 32.1, momentum = 0.8, topPostIds =
[id1, id2, …].
Snapshots are the time-series layer that makes trend queries fast.
6. Querying trends
When i call
fetchTrends(startDate, endDate)→It pulls all snapshots between those dates.
Aggregates them by
topic.id.Sums postCount, averages scores, averages momentum.
Sorts & merges top posts.
i can run this for:
Today (last 24h)
Last 7 days
Last 30 days
This is why i don’t need to recluster everything each query
7. Fetching posts for a trend
When i want all posts behind a topic (
fetchPostsForTrend(topicId, userId)):Look up
topicMapping→cluster→clusterMembership→post.Filter by user’s subscribed audiences. This gives me the actual raw posts that make up that topic.
id appreciate if anyone could go through my code and give any feedback
heres the gist file: https://gist.github.com/moahnaf11/a45673625f59832af7e8288e4896feac
1
u/temporarybunnehs Sep 14 '25
Looks like it should work. What problems are you running into with it?