mirror of
https://github.com/R0m1k3/Socialflow.git
synced 2026-10-11 17:26:45 +02:00
fix(analytics): upsert history one data point per day per page, add gte/lt imports
This commit is contained in:
1 parent
8b13748f13
commit
3859170d35
1 file changed
+36
-9
@@ -7,7 +7,7 @@ import {
|
||||
socialPages,
|
||||
tokenStatusEnum
|
||||
} from '../../shared/schema';
|
||||
import { eq, and, isNotNull, desc } from 'drizzle-orm';
|
||||
import { eq, and, isNotNull, desc, gte, lt } from 'drizzle-orm';
|
||||
import { GraphAPIClient } from '../utils/graph_client';
|
||||
import { TokenManager } from './token_manager';
|
||||
|
||||
@@ -232,16 +232,43 @@ export class AnalyticsService {
|
||||
})
|
||||
.where(eq(socialPages.id, pageId));
|
||||
|
||||
// 4. Insert History snapshot
|
||||
await db.insert(pageAnalyticsHistory).values({
|
||||
pageId,
|
||||
date: new Date(),
|
||||
followersCount: followers,
|
||||
pageReach,
|
||||
pageViews,
|
||||
pageEngagement,
|
||||
// 4. Upsert History snapshot — one entry per day per page
|
||||
const today = new Date();
|
||||
today.setHours(0, 0, 0, 0);
|
||||
const tomorrow = new Date(today);
|
||||
tomorrow.setDate(tomorrow.getDate() + 1);
|
||||
|
||||
const existingEntry = await db.query.pageAnalyticsHistory.findFirst({
|
||||
where: and(
|
||||
eq(pageAnalyticsHistory.pageId, pageId),
|
||||
gte(pageAnalyticsHistory.date, today),
|
||||
lt(pageAnalyticsHistory.date, tomorrow)
|
||||
)
|
||||
});
|
||||
|
||||
if (existingEntry) {
|
||||
// Update today's existing entry with latest values
|
||||
await db.update(pageAnalyticsHistory)
|
||||
.set({
|
||||
followersCount: followers,
|
||||
pageReach,
|
||||
pageViews,
|
||||
pageEngagement,
|
||||
date: new Date(),
|
||||
})
|
||||
.where(eq(pageAnalyticsHistory.id, existingEntry.id));
|
||||
} else {
|
||||
// Insert new entry for today
|
||||
await db.insert(pageAnalyticsHistory).values({
|
||||
pageId,
|
||||
date: new Date(),
|
||||
followersCount: followers,
|
||||
pageReach,
|
||||
pageViews,
|
||||
pageEngagement,
|
||||
});
|
||||
}
|
||||
|
||||
console.log(`[AnalyticsService] Synced page ${pageId}: ${followers} followers, ${pageReach} reach, ${pageEngagement} engagement, ${pageViews} views`);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user