Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.
feat(reddit): add view count to posts and analytics
spacesdriveedb3332ad710
diff --git a/backend/services/reddit.js b/backend/services/reddit.jsindex 850246a..adab366 100644--- a/backend/services/reddit.js+++ b/backend/services/reddit.js@@ -83,6 +83,7 @@ export async function fetchRedditPosts(username, cookie, limit = 100) {createdAt: p.created_utc ? new Date(p.created_utc * 1000).toISOString() : '',flair: p.link_flair_text ?? '',awardsCount: p.total_awards_received ?? 0,+ viewCount: typeof p.view_count === 'number' ? p.view_count : null,}));}@@ -91,6 +92,7 @@ export function computeRedditAnalytics(profile, posts) {fetchedPosts: 0, totalScore: 0, avgScore: 0, medianScore: 0,totalComments: 0, avgComments: 0, avgUpvoteRatio: 0,totalAwards: 0, avgAwards: 0,+ totalViews: null, avgViews: null, postsWithViews: 0,topPosts: [], worstPosts: [], subredditBreakdown: [],mediaTypeDistribution: {}, postsByDayOfWeek: [], postsByHour: [],monthlyBreakdown: [], engagementTimeline: [],@@ -108,6 +110,14 @@ export function computeRedditAnalytics(profile, posts) {const avgScore = Math.round(totalScore / posts.length);const avgComments = Math.round(totalComments / posts.length);const avgAwards = parseFloat((totalAwards / posts.length).toFixed(2));++ const postsWithViews = posts.filter(p => p.viewCount !== null).length;+ const totalViews = postsWithViews > 0+ ? posts.reduce((s, p) => s + (p.viewCount ?? 0), 0)+ : null;+ const avgViews = postsWithViews > 0 && totalViews !== null+ ? Math.round(totalViews / postsWithViews)+ : null;const avgUpvoteRatio = parseFloat((posts.reduce((s, p) => s + p.upvoteRatio, 0) / posts.length * 100).toFixed(1));@@ -235,6 +245,7 @@ export function computeRedditAnalytics(profile, posts) {totalScore, avgScore, medianScore,totalComments, avgComments, avgUpvoteRatio,totalAwards, avgAwards,+ totalViews, avgViews, postsWithViews,topPosts: byScore.slice(0, 10),worstPosts: byScore.slice(-5).reverse(),subredditBreakdown,diff --git a/frontend/src/features/analytics/reddit/RedditAnalytics.jsx b/frontend/src/features/analytics/reddit/RedditAnalytics.jsxindex 5bf7660..c800b40 100644--- a/frontend/src/features/analytics/reddit/RedditAnalytics.jsx+++ b/frontend/src/features/analytics/reddit/RedditAnalytics.jsx@@ -20,7 +20,7 @@ import { fmtNum, fmtNumFull, fmtDate, timeAgo } from '../../../utils/formatters'import {ArrowLeft, RefreshCw, TrendingUp, MessageSquare, Award, BarChart2,Calendar, Clock, Zap, Flame, Trophy, ExternalLink, FileText, Link2,- Image, Video, Star,+ Image, Video, Star, Eye,} from 'lucide-react';const REDDIT_COLORS = ['#FF4500', '#FF6534', '#FF8C00', '#FFA500', '#FFB347', '#FFC080'];@@ -183,10 +183,10 @@ export default function RedditAnalytics() {{/* KPI cards */}<div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-4">- <StatCard icon={<TrendingUp />} label="Posts Fetched" value={fmtNum(a.fetchedPosts)} subtitle={`${a.postsLast30Days ?? 0} in the last 30 days`} gradient="orange" />- <StatCard icon={<Award />} label="Total Score" value={fmtNum(a.totalScore)} subtitle={`Avg ${fmtNum(a.avgScore)} per post`} gradient="red" />- <StatCard icon={<MessageSquare />} label="Total Comments" value={fmtNum(a.totalComments)} subtitle={`Avg ${fmtNum(a.avgComments)} per post`} gradient="blue" />- <StatCard icon={<Star />} label="Awards Received" value={fmtNum(a.totalAwards ?? 0)} subtitle={`Avg ${a.avgAwards ?? 0} per post`} gradient="purple" />+ <StatCard icon={<TrendingUp />} label="Posts Fetched" value={fmtNum(a.fetchedPosts)} subtitle={`${a.postsLast30Days ?? 0} in the last 30 days`} gradient="orange" />+ <StatCard icon={<Eye />} label="Total Views" value={a.totalViews !== null ? fmtNum(a.totalViews) : 'N/A'} subtitle={a.totalViews !== null ? `Avg ${fmtNum(a.avgViews)} per post` : 'Reddit does not always expose views'} gradient="red" />+ <StatCard icon={<Award />} label="Total Score" value={fmtNum(a.totalScore)} subtitle={`Avg ${fmtNum(a.avgScore)} per post`} gradient="purple" />+ <StatCard icon={<MessageSquare />} label="Total Comments" value={fmtNum(a.totalComments)} subtitle={`Avg ${fmtNum(a.avgComments)} per post`} gradient="blue" /></div>{/* Score timeline + media type pie */}@@ -261,6 +261,8 @@ export default function RedditAnalytics() {{ label: 'Avg upvote ratio', value: `${a.avgUpvoteRatio ?? 0}%` },{ label: 'Total comments', value: fmtNum(a.totalComments) },{ label: 'Avg comments', value: fmtNum(a.avgComments) },+ { label: 'Awards received', value: fmtNum(a.totalAwards ?? 0) },+ { label: 'Avg awards / post', value: String(a.avgAwards ?? 0) },].map(item => (<div key={item.label} className="flex items-center justify-between"><span className="text-sm text-muted-foreground">{item.label}</span>@@ -378,6 +380,7 @@ export default function RedditAnalytics() {<TableHead className="w-8">#</TableHead><TableHead>Post</TableHead><TableHead className="text-right">Score</TableHead>+ <TableHead className="text-right">Views</TableHead><TableHead className="text-right">Comments</TableHead><TableHead className="text-right">Upvote %</TableHead><TableHead className="text-right">Posted</TableHead>@@ -402,6 +405,7 @@ export default function RedditAnalytics() {</div></TableCell><TableCell className="text-right text-sm font-semibold tabular-nums text-orange-500">{fmtNum(post.score)}</TableCell>+ <TableCell className="text-right text-sm tabular-nums text-muted-foreground">{post.viewCount !== null ? fmtNum(post.viewCount) : <span className="text-xs">N/A</span>}</TableCell><TableCell className="text-right text-sm tabular-nums">{fmtNum(post.numComments)}</TableCell><TableCell className="text-right text-sm tabular-nums">{Math.round((post.upvoteRatio ?? 0) * 100)}%</TableCell><TableCell className="text-right text-xs text-muted-foreground">{fmtDate(post.createdAt)}</TableCell>diff --git a/frontend/src/features/analytics/reddit/RedditPostsAll.jsx b/frontend/src/features/analytics/reddit/RedditPostsAll.jsxindex 3cb3fcd..dcf3d50 100644--- a/frontend/src/features/analytics/reddit/RedditPostsAll.jsx+++ b/frontend/src/features/analytics/reddit/RedditPostsAll.jsx@@ -14,7 +14,7 @@ import { api } from '../../../services/api';import { fmtNum, fmtDate } from '../../../utils/formatters';import {Search, ExternalLink, TrendingUp, MessageSquare, FileText,- Image, Video, Link2, Award, RefreshCw,+ Image, Video, Link2, Award, RefreshCw, Eye,} from 'lucide-react';const MEDIA_TYPE_LABELS = { text: 'Text', image: 'Image', video: 'Video', link: 'Link' };@@ -118,11 +118,16 @@ export default function RedditPostsAll() {});}, [posts, query, sort, accountFilter, subFilter]);- const totals = useMemo(() => ({- posts: filtered.length,- score: filtered.reduce((s, p) => s + p.score, 0),- comments: filtered.reduce((s, p) => s + p.numComments, 0),- }), [filtered]);+ const totals = useMemo(() => {+ const postsWithViews = filtered.filter(p => p.viewCount !== null);+ return {+ posts: filtered.length,+ score: filtered.reduce((s, p) => s + p.score, 0),+ comments: filtered.reduce((s, p) => s + p.numComments, 0),+ views: postsWithViews.length > 0 ? postsWithViews.reduce((s, p) => s + (p.viewCount ?? 0), 0) : null,+ postsWithViews: postsWithViews.length,+ };+ }, [filtered]);if (redditAccounts.length === 0) {return (@@ -172,9 +177,10 @@ export default function RedditPostsAll() {) : (<>{/* Summary stats */}- <div className="grid grid-cols-1 sm:grid-cols-3 gap-4">+ <div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-4"><StatCard icon={<TrendingUp />} label="Posts Shown" value={fmtNum(totals.posts)} subtitle={`${posts.length} total fetched`} gradient="orange" />- <StatCard icon={<Award />} label="Combined Score" value={fmtNum(totals.score)} subtitle={filtered.length > 0 ? `Avg ${fmtNum(Math.round(totals.score / filtered.length))} per post` : ''} gradient="red" />+ <StatCard icon={<Eye />} label="Total Views" value={totals.views !== null ? fmtNum(totals.views) : 'N/A'} subtitle={totals.views !== null ? `Across ${totals.postsWithViews} posts` : 'Reddit does not always expose views'} gradient="red" />+ <StatCard icon={<Award />} label="Combined Score" value={fmtNum(totals.score)} subtitle={filtered.length > 0 ? `Avg ${fmtNum(Math.round(totals.score / filtered.length))} per post` : ''} gradient="purple" /><StatCard icon={<MessageSquare />} label="Total Comments" value={fmtNum(totals.comments)} subtitle={filtered.length > 0 ? `Avg ${fmtNum(Math.round(totals.comments / filtered.length))} per post` : ''} gradient="blue" /></div>@@ -240,6 +246,7 @@ export default function RedditPostsAll() {{redditAccounts.length > 1 && <TableHead className="w-36">Account</TableHead>}<TableHead className="w-24">Type</TableHead><TableHead className="text-right w-24">Score</TableHead>+ <TableHead className="text-right w-24">Views</TableHead><TableHead className="text-right w-28">Comments</TableHead><TableHead className="text-right w-20">Upvote %</TableHead><TableHead className="text-right w-28">Date</TableHead>@@ -249,7 +256,7 @@ export default function RedditPostsAll() {<TableBody>{filtered.length === 0 ? (<TableRow>- <TableCell colSpan={redditAccounts.length > 1 ? 9 : 8} className="text-center text-muted-foreground py-12">+ <TableCell colSpan={redditAccounts.length > 1 ? 10 : 9} className="text-center text-muted-foreground py-12">No posts match the current filters</TableCell></TableRow>@@ -300,6 +307,9 @@ export default function RedditPostsAll() {<TableCell className="text-right text-sm font-semibold tabular-nums text-orange-500">{fmtNum(post.score)}</TableCell>+ <TableCell className="text-right text-sm tabular-nums text-muted-foreground">+ {post.viewCount !== null ? fmtNum(post.viewCount) : <span className="text-xs">N/A</span>}+ </TableCell><TableCell className="text-right text-sm tabular-nums">{fmtNum(post.numComments)}</TableCell><TableCell className="text-right text-sm tabular-nums">{Math.round((post.upvoteRatio ?? 0) * 100)}%