feat(analytics): Implement Analytics Dashboard and Token Management

This commit is contained in:
Michael committed 2026-01-20 13:49:24 +01:00
1 parent b38f082d6e
commit f8ddb44e38
822 files changed
+132545 -51

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
import cron from 'node-cron';
import { TokenManager } from '../services/token_manager';
export function startTokenCron() {
// Run every day at midnight (00:00)
cron.schedule('0 0 * * *', async () => {
console.log('[Cron] Running daily token check...');
await TokenManager.checkAndRefreshTokens();
});
console.log('[Cron] Token refresh job scheduled (0 0 * * *).');
}
+4
View File
@@ -10,6 +10,7 @@ import { registerRoutes } from "./routes";
import { setupVite, serveStatic, log } from "./vite";
import { schedulerService } from "./services/scheduler";
import { ensureAdminUserExists } from "./init-admin";
import { startTokenCron } from "./cron";
const app = express();
const PgSession = connectPgSimple(session);
@@ -183,5 +184,8 @@ app.use((req, res, next) => {
// Démarrer le scheduler pour les publications programmées
schedulerService.start();
// Start Token Refresh Cron
startTokenCron();
});
})();
+4
View File
@@ -10,6 +10,7 @@ import { openRouterService } from "./services/openrouter";
import { cloudinaryService } from "./services/cloudinary";
import { insertPostSchema, insertScheduledPostSchema, insertSocialPageSchema, insertAiGenerationSchema, insertCloudinaryConfigSchema, updateCloudinaryConfigSchema, insertOpenrouterConfigSchema, updateOpenrouterConfigSchema, insertUserSchema, postMedia, type SocialPage } from "@shared/schema";
import type { User, InsertUser, ScheduledPost } from "@shared/schema";
import { analyticsRouter } from "./routes/analytics";
// Types MIME autorisés pour les uploads
const ALLOWED_MIME_TYPES = [
@@ -55,6 +56,9 @@ function requireAdmin(req: Request, res: Response, next: NextFunction) {
export async function registerRoutes(app: Express): Promise<Server> {
// Analytics Routes
app.use("/api/analytics", requireAuth, analyticsRouter);
// Routes d'authentification
app.post("/api/auth/login", (req, res, next) => {
passport.authenticate("local", (err: any, user: User | false, info: any) => {
+78
View File
@@ -0,0 +1,78 @@
import { Router } from 'express';
import { db } from '../index';
import { AnalyticsService } from '../services/analytics';
import { TokenManager } from '../services/token_manager';
import { postAnalytics, pageAnalyticsHistory, socialPages } from '../../shared/schema';
import { eq, desc } from 'drizzle-orm';
const router = Router();
// Get Post Analytics
router.get('/posts/:postId', async (req, res) => {
try {
const { postId } = req.params;
const analytics = await db.query.postAnalytics.findFirst({
where: eq(postAnalytics.postId, postId)
});
res.json(analytics || {});
} catch (error) {
res.status(500).json({ error: 'Failed to fetch post analytics' });
}
});
// Refresh Post Analytics
router.post('/posts/:postId/refresh', async (req, res) => {
try {
const { postId } = req.params;
await AnalyticsService.syncPostAnalytics(postId);
const updated = await db.query.postAnalytics.findFirst({
where: eq(postAnalytics.postId, postId)
});
res.json(updated);
} catch (error) {
res.status(500).json({ error: 'Failed to refresh post analytics' });
}
});
// Get Page Analytics History
router.get('/pages/:pageId/history', async (req, res) => {
try {
const { pageId } = req.params;
const history = await db.query.pageAnalyticsHistory.findMany({
where: eq(pageAnalyticsHistory.pageId, pageId),
orderBy: [desc(pageAnalyticsHistory.date)],
limit: 30 // Last 30 entries
});
res.json(history);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch page history' });
}
});
// Refresh Page Analytics
router.post('/pages/:pageId/refresh', async (req, res) => {
try {
const { pageId } = req.params;
await AnalyticsService.syncPageAnalytics(pageId);
const page = await db.query.socialPages.findFirst({
where: eq(socialPages.id, pageId)
});
res.json(page);
} catch (error) {
res.status(500).json({ error: 'Failed to refresh page analytics' });
}
});
// Manual Token Check Trigger
router.post('/tokens/check', async (req, res) => {
try {
// This runs in background, but we can await it for manual trigger
await TokenManager.checkAndRefreshTokens();
res.json({ success: true, message: 'Token check completed' });
} catch (error) {
res.status(500).json({ error: 'Token check failed' });
}
});
export const analyticsRouter = router;
+196
View File
@@ -0,0 +1,196 @@
import { db } from '../index';
import {
posts,
scheduledPosts,
postAnalytics,
pageAnalyticsHistory,
socialPages,
tokenStatusEnum
} from '../../shared/schema';
import { eq, and, isNotNull, desc } from 'drizzle-orm';
import { GraphAPIClient } from '../utils/graph_client';
import { TokenManager } from './token_manager';
export class AnalyticsService {
/**
* Fetches latest metrics for a specific post from Facebook/Instagram
* and updates the local postAnalytics table.
* Handles aggregation if a single Post content was published to multiple destinations.
*/
static async syncPostAnalytics(postId: string) {
console.log(`[AnalyticsService] Syncing post ${postId}`);
// Find all published instances of this post with an external ID
const publishedInstances = await db.query.scheduledPosts.findMany({
where: and(
eq(scheduledPosts.postId, postId),
isNotNull(scheduledPosts.externalPostId)
),
with: {
page: true
}
});
if (publishedInstances.length === 0) {
console.log(`[AnalyticsService] No published external posts found for ${postId}`);
return;
}
let totalImpressions = 0;
let totalReach = 0;
let totalEngagement = 0;
let totalReactions = 0;
let totalComments = 0;
let totalShares = 0;
let totalClicks = 0;
const rawDataList: any[] = [];
for (const instance of publishedInstances) {
if (!instance.externalPostId || !instance.page) continue;
try {
// We need the page access token
let accessToken = instance.page.accessToken;
// Decrypt if needed
if (accessToken.includes(':')) {
try { accessToken = TokenManager.decrypt(accessToken); } catch { }
}
// Determine fields based on platform (assuming Facbeook for now as per context)
// providing a generic list of fields
const fields = 'insights.metric(post_impressions,post_impressions_unique,post_engaged_users,post_clicks),shares,comments.summary(true),reactions.summary(true)';
const result = await GraphAPIClient.get<any>(instance.externalPostId, {
accessToken,
params: { fields }
});
rawDataList.push({ id: instance.externalPostId, data: result });
// Parse Metrics
// Note: Graph API structure for insights is complex (data array).
// For simplicity in this implementation, we will try to extract what we can.
// This is a simplified mapper.
const insights = result.insights?.data || [];
const findMetric = (name: string) => {
const m = insights.find((i: any) => i.name === name);
return m ? (m.values[0]?.value || 0) : 0;
};
const impressions = findMetric('post_impressions');
const reach = findMetric('post_impressions_unique');
const engagement = findMetric('post_engaged_users');
const clicks = findMetric('post_clicks'); // might be 'post_clicks_by_type'
const reactionCount = result.reactions?.summary?.total_count || 0;
const commentCount = result.comments?.summary?.total_count || 0;
const shareCount = result.shares?.count || 0;
totalImpressions += impressions;
totalReach += reach;
totalEngagement += engagement;
totalClicks += clicks; // Aggregation might be complex for unique values, but sum is okay for now
totalReactions += reactionCount;
totalComments += commentCount;
totalShares += shareCount;
} catch (error) {
console.error(`[AnalyticsService] Failed to fetch for instance ${instance.id}:`, error);
}
}
// Update DB
// Check if exists
const existing = await db.query.postAnalytics.findFirst({
where: eq(postAnalytics.postId, postId)
});
if (existing) {
await db.update(postAnalytics)
.set({
fetchedAt: new Date(),
impressions: totalImpressions,
reach: totalReach,
engagement: totalEngagement,
reactions: totalReactions,
comments: totalComments,
shares: totalShares,
clicks: totalClicks,
rawData: rawDataList
})
.where(eq(postAnalytics.id, existing.id));
} else {
await db.insert(postAnalytics).values({
postId,
fetchedAt: new Date(),
impressions: totalImpressions,
reach: totalReach,
engagement: totalEngagement,
reactions: totalReactions,
comments: totalComments,
shares: totalShares,
clicks: totalClicks,
rawData: rawDataList
});
}
}
/**
* Syncs page-level analytics (Followers, Reach, etc.)
*/
static async syncPageAnalytics(pageId: string) {
console.log(`[AnalyticsService] Syncing page ${pageId}`);
const page = await db.query.socialPages.findFirst({
where: eq(socialPages.id, pageId)
});
if (!page) return;
try {
let accessToken = page.accessToken;
if (accessToken.includes(':')) {
try { accessToken = TokenManager.decrypt(accessToken); } catch { }
}
// Fetch Page Insights
// common metrics: page_impressions, page_post_engagements, page_fans
const fields = 'insights.metric(page_impressions,page_post_engagements).period(day),fan_count';
const result = await GraphAPIClient.get<any>(page.pageId, {
accessToken,
params: { fields }
});
const insights = result.insights?.data || [];
const findMetric = (name: string) => {
const m = insights.find((i: any) => i.name === name);
// period(day) returns values array. We take the latest (yesterday usually)
return m ? (m.values[m.values.length - 1]?.value || 0) : 0;
};
const followers = result.fan_count || page.followersCount || 0;
const pageReach = findMetric('page_impressions'); // Proxy for reach if unique not available
const pageViews = 0; // page_views_total requirement specific permission
// Update Page
await db.update(socialPages)
.set({ followersCount: followers })
.where(eq(socialPages.id, pageId));
// Insert History
await db.insert(pageAnalyticsHistory).values({
pageId,
date: new Date(),
followersCount: followers,
pageReach,
pageViews
});
} catch (error) {
console.error(`[AnalyticsService] Failed to sync page ${page.pageName}:`, error);
}
}
}
+121
View File
@@ -0,0 +1,121 @@
import crypto from 'crypto';
import { db } from '../index';
import { socialPages, tokenStatusEnum } from '../../shared/schema';
import { eq, and, lte } from 'drizzle-orm';
import { GraphAPIClient } from '../utils/graph_client';
// Constants
const ALGORITHM = 'aes-256-cbc';
const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY || 'default-dev-key-must-be-changed-in-prod-32chars'; // 32 chars
const IV_LENGTH = 16;
/**
* Service responsible for managing Facebook Page Token lifecycle
* - Encryption at rest
* - Automated refreshing of expiring tokens
*/
export class TokenManager {
/**
* Encrypts a text using AES-256-CBC
*/
static encrypt(text: string): string {
const key = crypto.scryptSync(ENCRYPTION_KEY, 'salt', 32);
const iv = crypto.randomBytes(IV_LENGTH);
const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}
/**
* Decrypts a text using AES-256-CBC
*/
static decrypt(text: string): string {
const textParts = text.split(':');
const iv = Buffer.from(textParts.shift()!, 'hex');
const encryptedText = Buffer.from(textParts.join(':'), 'hex');
const key = crypto.scryptSync(ENCRYPTION_KEY, 'salt', 32);
const decipher = crypto.createDecipheriv(ALGORITHM, key, iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
/**
* Checks all active pages for expiring tokens and refreshes them if needed.
*/
static async checkAndRefreshTokens() {
console.log('[TokenManager] Starting daily token check...');
try {
// Fetch active pages
const pages = await db.query.socialPages.findMany({
where: eq(socialPages.isActive, "true")
});
for (const page of pages) {
try {
await this.processPageToken(page);
} catch (error) {
console.error(`[TokenManager] Error processing page ${page.pageName} (${page.id}):`, error);
await db.update(socialPages)
.set({
tokenStatus: "error",
lastTokenCheck: new Date()
})
.where(eq(socialPages.id, page.id));
}
}
console.log('[TokenManager] Finished token check.');
} catch (e) {
console.error('[TokenManager] Critical error in checkAndRefreshTokens:', e);
}
}
private static async processPageToken(page: any) {
let accessToken = page.accessToken;
// Attempt to decrypt. If fails, assume it's plain text (migration phase)
try {
if (accessToken.includes(':')) {
accessToken = this.decrypt(accessToken);
}
} catch (e) {
// Not encrypted or format error, proceed with original
}
console.log(`[TokenManager] Checking page: ${page.pageName}`);
const isValid = await this.verifyToken(accessToken);
if (isValid) {
await db.update(socialPages)
.set({
tokenStatus: "valid",
lastTokenCheck: new Date()
})
.where(eq(socialPages.id, page.id));
} else {
await db.update(socialPages)
.set({
tokenStatus: "expired",
lastTokenCheck: new Date()
})
.where(eq(socialPages.id, page.id));
console.warn(`[TokenManager] Token expired for ${page.pageName}`);
}
}
private static async verifyToken(token: string): Promise<boolean> {
try {
// Simple validation check: Get 'me' (page profile)
await GraphAPIClient.get('me', { accessToken: token });
return true;
} catch (error) {
console.error('[TokenManager] Token verification failed:', error);
return false;
}
}
}
+94
View File
@@ -0,0 +1,94 @@
export class FacebookApiError extends Error {
constructor(
public message: string,
public status: number,
public code: number,
public fbTraceId: string
) {
super(message);
this.name = 'FacebookApiError';
}
}
interface GraphRequestConfig {
params?: Record<string, string | number | boolean>;
accessToken?: string;
}
const GRAPH_API_VERSION = 'v19.0';
const BASE_URL = `https://graph.facebook.com/${GRAPH_API_VERSION}`;
export class GraphAPIClient {
static async get<T>(endpoint: string, config: GraphRequestConfig): Promise<T> {
const url = this.buildUrl(endpoint, config);
console.log(`[GraphAPI] GET ${endpoint}`);
// In node-fetch v2 (which is typically used in CJS/TS mixes unless v3 ESM), basic fetch
// If project uses v3, imports might differ. Assuming standard fetch is available or polyfilled.
// socialflow package.json has "@types/node-fetch": "^2.6.13" but also "node-fetch" isn't explicitly in dependencies?
// Wait, package.json has "node-fetch" missing in dependencies list!
// But it has "@types/node-fetch".
// "canvas" is there. "express" is there.
// I need to check if fetch is globally available (Node 18+) or if I need to use `axios` or add `node-fetch`.
// Package.json has: "type": "module". Node 20.16.11 is used in parsing.
// Node 18+ has native fetch. I should use `global.fetch` or just `fetch`.
// Explicit import `import fetch from 'node-fetch'` might fail if not installed.
// I will use native fetch.
// Remove import above if using native.
const response = await fetch(url.toString());
return this.handleResponse<T>(response);
}
static async post<T>(endpoint: string, body: any, config: GraphRequestConfig): Promise<T> {
const url = this.buildUrl(endpoint, config);
console.log(`[GraphAPI] POST ${endpoint}`);
const response = await fetch(url.toString(), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
return this.handleResponse<T>(response);
}
private static buildUrl(endpoint: string, config: GraphRequestConfig): URL {
const url = new URL(`${BASE_URL}${endpoint.startsWith('/') ? endpoint : '/' + endpoint}`);
if (config.accessToken) {
url.searchParams.append('access_token', config.accessToken);
}
if (config.params) {
Object.entries(config.params).forEach(([key, value]) => {
url.searchParams.append(key, String(value));
});
}
return url;
}
private static async handleResponse<T>(response: Response): Promise<T> {
const data: any = await response.json();
if (!response.ok) {
// Parse FB Error structure
const error = data.error || {};
throw new FacebookApiError(
error.message || 'Unknown Facebook API Error',
response.status,
error.code || 0,
error.fbtrace_id || ''
);
}
return data as T;
}
}