Add active outreach funnel dashboard
This commit is contained in:
1 parent
deaf86c364
commit
f55ae7ddf8
15 files changed
+519
-1
No files matched your search
@@ -0,0 +1,91 @@
|
||||
import { Router } from 'express';
|
||||
import type { CandidateStatus } from '../../database/candidate-queue-repository.js';
|
||||
import type { WebContext } from '../context.js';
|
||||
|
||||
const ACTIVE_STATUSES = ['discovered', 'screening', 'ready', 'sending', 'retryable'] as const;
|
||||
type ActiveCandidateStatus = Exclude<CandidateStatus, 'sent' | 'rejected'>;
|
||||
|
||||
interface OutreachCandidateView {
|
||||
postId: string;
|
||||
subreddit: string;
|
||||
author: string;
|
||||
title: string;
|
||||
postUrl: string;
|
||||
sourceType: 'image' | 'chat';
|
||||
status: ActiveCandidateStatus;
|
||||
priority: number;
|
||||
postCreatedAt: number;
|
||||
discoveredAt: number;
|
||||
updatedAt: number;
|
||||
availableAt: number;
|
||||
attemptCount: number;
|
||||
hasPreparedMessage: boolean;
|
||||
note: string | null;
|
||||
owner: { accountId: string; redditUsername: string } | null;
|
||||
}
|
||||
|
||||
function redditPostUrl(permalink: string): string {
|
||||
try {
|
||||
return new URL(permalink, 'https://www.reddit.com').toString();
|
||||
} catch {
|
||||
return 'https://www.reddit.com';
|
||||
}
|
||||
}
|
||||
|
||||
export function createOutreachRouter(ctx: WebContext): Router {
|
||||
const router = Router();
|
||||
|
||||
router.get('/outreach', (_req, res) => {
|
||||
const queue = ctx.services.candidateQueue;
|
||||
if (!queue) {
|
||||
res.status(503).json({ error: 'Outreach queue not initialized' });
|
||||
return;
|
||||
}
|
||||
|
||||
const stats = queue.getStats();
|
||||
const records = queue.listActive();
|
||||
const accountById = new Map((ctx.services.redditAccounts?.list() ?? [])
|
||||
.map((account) => [account.accountId, account.redditUsername] as const));
|
||||
const candidates: OutreachCandidateView[] = records.map((candidate) => ({
|
||||
postId: candidate.postId,
|
||||
subreddit: candidate.subreddit,
|
||||
author: candidate.author,
|
||||
title: candidate.post.title,
|
||||
postUrl: redditPostUrl(candidate.post.permalink),
|
||||
sourceType: candidate.sourceType,
|
||||
status: candidate.status as ActiveCandidateStatus,
|
||||
priority: candidate.priority,
|
||||
postCreatedAt: candidate.post.created_utc * 1_000,
|
||||
discoveredAt: candidate.discoveredAt,
|
||||
updatedAt: candidate.updatedAt,
|
||||
availableAt: candidate.availableAt,
|
||||
attemptCount: candidate.attemptCount,
|
||||
hasPreparedMessage: candidate.prepared !== null,
|
||||
note: candidate.lastError,
|
||||
owner: candidate.assignedAccountId
|
||||
? {
|
||||
accountId: candidate.assignedAccountId,
|
||||
redditUsername: accountById.get(candidate.assignedAccountId) ?? candidate.assignedAccountId,
|
||||
}
|
||||
: null,
|
||||
}));
|
||||
const stages = {
|
||||
discovered: stats.byStatus.discovered,
|
||||
screening: stats.byStatus.screening,
|
||||
ready: stats.byStatus.ready,
|
||||
sending: stats.byStatus.sending,
|
||||
retryable: stats.byStatus.retryable,
|
||||
};
|
||||
const response = {
|
||||
generatedAt: Date.now(),
|
||||
activeCount: ACTIVE_STATUSES.reduce((total, status) => total + stats.byStatus[status], 0),
|
||||
stages,
|
||||
outcomes: { sent: stats.byStatus.sent, rejected: stats.byStatus.rejected },
|
||||
oldestActiveAt: queue.getOldestActiveAt(),
|
||||
candidates,
|
||||
};
|
||||
res.json(response);
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
Reference in new issue
Block a user