Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

fix(overview): stop Syncing analytics from showing forever

spacesdrivef8c93534bc14
diff --git a/backend/routes/overview.js b/backend/routes/overview.js
index e555f73..f6c9941 100644
--- a/backend/routes/overview.js
+++ b/backend/routes/overview.js
@@ -1,8 +1,9 @@
import { Hono } from 'hono';
import { getAccounts, getTrackedPosts } from '../lib/db.js';
-import { getVideosCache, setVideosCache, getIGCache, setIGCache, getRedditCache } from '../lib/cache.js';
+import { getVideosCache, setVideosCache, getIGCache, setIGCache, getRedditCache, setRedditCache } from '../lib/cache.js';
import { fetchAllVideos } from '../services/youtube.js';
import { fetchInstagramMedia, summarizeContentTypes } from '../services/instagram.js';
+import { fetchRedditProfile, fetchRedditPosts, computeRedditAnalytics } from '../services/reddit.js';
const router = new Hono();
@@ -19,12 +20,27 @@ router.get('/overview', async (c) => {
]);
const warmTasks = [];
+ // Starts a background fetch and reports syncing: true only when one actually started, so the
+ // client never waits on an account that cannot be fetched (no Redis, no key, no cookie).
+ const warm = (id, task) => {
+ if (!redis || !task) return { id, cacheWarmed: false, syncing: false };
+ warmTasks.push(task().catch(() => {}));
+ return { id, cacheWarmed: false, syncing: true };
+ };
const cacheResults = await Promise.allSettled(
rawAccounts.map(async (a) => {
if (a.platform === 'reddit') {
const cached = await getRedditCache(redis, userId, a.id);
- if (!cached?.analytics) return { id: a.id, cacheWarmed: false };
+ if (!cached?.analytics) {
+ return warm(a.id, a.username && a.cookie && (async () => {
+ const [profile, posts] = await Promise.all([
+ fetchRedditProfile(a.username, a.cookie),
+ fetchRedditPosts(a.username, a.cookie, 100),
+ ]);
+ await setRedditCache(redis, userId, a.id, { profile, analytics: computeRedditAnalytics(profile, posts), posts });
+ }));
+ }
return {
id: a.id,
cacheWarmed: true,
@@ -36,17 +52,12 @@ router.get('/overview', async (c) => {
if (a.platform === 'instagram') {
const cached = await getIGCache(redis, userId, a.id);
- const media = cached?.media ?? [];
- if (!media.length) {
- if (redis && a.igUserId && a.accessToken) {
- warmTasks.push(
- fetchInstagramMedia(a.igUserId, a.accessToken, 500)
- .then(m => setIGCache(redis, userId, a.id, { media: m, fetchedAt: new Date().toISOString() }))
- .catch(() => {})
- );
- }
- return { id: a.id, cacheWarmed: false };
+ if (!cached?.media) {
+ return warm(a.id, a.igUserId && a.accessToken && (() =>
+ fetchInstagramMedia(a.igUserId, a.accessToken, 500)
+ .then(m => setIGCache(redis, userId, a.id, { media: m, fetchedAt: new Date().toISOString() }))));
}
+ const media = cached.media;
const contentTypes = summarizeContentTypes(media);
const types = Object.values(contentTypes);
return {
@@ -61,17 +72,12 @@ router.get('/overview', async (c) => {
// YouTube
const cached = await getVideosCache(redis, userId, a.id);
- const videos = cached?.videos ?? [];
- if (!videos.length) {
- if (redis && apiKey && a.uploadsPlaylistId) {
- warmTasks.push(
- fetchAllVideos(a.uploadsPlaylistId, apiKey, 10)
- .then(v => setVideosCache(redis, userId, a.id, { videos: v, fetchedAt: new Date().toISOString() }))
- .catch(() => {})
- );
- }
- return { id: a.id, cacheWarmed: false };
+ if (!cached?.videos) {
+ return warm(a.id, apiKey && a.uploadsPlaylistId && (() =>
+ fetchAllVideos(a.uploadsPlaylistId, apiKey, 10)
+ .then(v => setVideosCache(redis, userId, a.id, { videos: v, fetchedAt: new Date().toISOString() }))));
}
+ const videos = cached.videos;
return {
id: a.id,
cacheWarmed: true,
@@ -81,8 +87,7 @@ router.get('/overview', async (c) => {
})
);
- // Fire warming tasks without blocking the response.
- // The client will auto-refresh after a short delay when cacheWarmed: false is detected.
+ // Fire warming tasks without blocking the response; the client re-polls while any account reports syncing.
if (warmTasks.length > 0) {
const ctx = c.executionCtx;
if (ctx?.waitUntil) {
diff --git a/frontend/src/features/analytics/overview/Overview.jsx b/frontend/src/features/analytics/overview/Overview.jsx
index 1cc2d35..d8b6b0d 100644
--- a/frontend/src/features/analytics/overview/Overview.jsx
+++ b/frontend/src/features/analytics/overview/Overview.jsx
@@ -31,6 +31,7 @@ import { IG_CONTENT_TYPES, IG_CONTENT_TYPE_META } from '../../../utils/instagram
import { cn } from '@/lib/utils';
const CATEGORY_SERIES = SERIES.neutral;
+const SYNC_POLL_DELAYS = [4000, 8000, 16000];
function greeting(date = new Date()) {
const hour = date.getHours();
@@ -57,22 +58,23 @@ export default function Overview() {
.finally(() => setOverviewLoading(false));
}, []);
- // When the backend returns cacheWarmed: false for any account, it has fired a background
- // analytics fetch. Auto-refresh once after 6 seconds to pick up the newly warmed data.
- const isSyncing = !overviewLoading && accounts.some(a => {
- const entry = overviewData.analyticsCache[a.id];
- return entry?.cacheWarmed === false;
+ // The backend reports syncing: true for accounts whose analytics it started fetching in the
+ // background. Re-poll with backoff, and give up after a few tries so the label never hangs.
+ const [syncPolls, setSyncPolls] = useState(0);
+ const isSyncing = !overviewLoading && syncPolls < SYNC_POLL_DELAYS.length && accounts.some(a => {
+ return overviewData.analyticsCache[a.id]?.syncing === true;
});
useEffect(() => {
- if (!isSyncing) return;
+ if (!isSyncing) return undefined;
const timer = setTimeout(() => {
api.getOverview()
.then(data => { if (data && !data.error) setOverviewData(data); })
- .catch(() => {});
- }, 6000);
+ .catch(() => {})
+ .finally(() => setSyncPolls(n => n + 1));
+ }, SYNC_POLL_DELAYS[syncPolls]);
return () => clearTimeout(timer);
- }, [isSyncing]);
+ }, [isSyncing, syncPolls]);
const loading = accountsLoading || overviewLoading;