After persist the live activity data for welcome7

This commit is contained in:
Your Name 2026-10-22 12:49:15 +02:00
parent b56d810441
commit 4762a8160b
2 changed files with 91 additions and 10 deletions

View File

@ -186,6 +186,46 @@ export function mergeFeedItems<T extends { id: string; publicId?: string }>(
return out;
}
/**
* Withdrawals feed: treat real + synthetic the same — one de-duped pool,
* time-rotated so neither sticks permanently at the top.
*/
export function blendAndRotateFeedItems<T extends { id: string; publicId?: string }>(
real: T[],
synthetic: T[],
target: number,
now = Date.now()
): T[] {
const pool: T[] = [];
const seen = new Set<string>();
const push = (item: T) => {
const key = item.publicId ?? item.id;
if (seen.has(key)) return;
seen.add(key);
pool.push(item);
};
for (const r of real) push(r);
for (const s of synthetic) push(s);
if (pool.length === 0 || target <= 0) return [];
const slot = slotIndex(now);
const start = slot % pool.length;
const n = Math.min(target, pool.length);
const out: T[] = [];
for (let i = 0; i < n; i++) {
out.push(pool[(start + i) % pool.length]);
}
return out;
}
/** Marketing display amount: whole USDT-like units (same look as synthetic labels). */
export function formatWithdrawalMarketingAmount(raw: string | number): string {
const n = typeof raw === 'number' ? raw : Number(raw);
if (!Number.isFinite(n)) return '0';
const human = n / 1_000_000;
if (!Number.isFinite(human) || human <= 0) return '0';
return String(Math.max(1, Math.round(human)));
}
function mapRealPlaying(row: marketingRepository.RealActivityRow): MarketingActivityItem {
return {
id: row.id,
@ -202,7 +242,7 @@ function mapRealWithdrawal(row: marketingRepository.RealWithdrawalRow): Marketin
return {
id: row.id,
publicId: maskPublicId(row.user_id),
amountLabel: formatAmountRaw(row.amount),
amountLabel: formatWithdrawalMarketingAmount(row.amount),
statusLabel: 'Completed',
createdAt: row.created_at.toISOString(),
synthetic: false,
@ -267,21 +307,30 @@ export async function getWithdrawalFeed(): Promise<{ items: MarketingWithdrawalI
const ttlMs = settings.cacheTtlSeconds * 1000;
return cached('withdrawal-feed', ttlMs, async () => {
const pad = settings.maxSyntheticWithdrawals;
const realFetch = Math.max(8, pad);
const displayTarget = Math.max(pad > 0 ? pad : 8, 8);
const [rows, realActive, personas] = await Promise.all([
marketingRepository.listRecentRealWithdrawals(8, settings.withdrawalRealWindowHours),
marketingRepository.listRecentRealWithdrawals(
realFetch,
settings.withdrawalRealWindowHours
),
marketingRepository.countRecentDistinctPlayers(settings.activeWindowMinutes),
loadActivePersonasCached(ttlMs, Math.max(pad, settings.maxSyntheticEntries)),
]);
const real = rows.map(mapRealWithdrawal);
const useSynthetic = shouldUseSynthetic(realActive, settings);
if (!useSynthetic || pad <= 0) return { items: real };
const synthetic = buildSyntheticWithdrawalsFromPersonas(
personas,
pad,
settings.syntheticWithdrawalAmounts
);
const feedTarget = real.length + pad;
return { items: mergeFeedItems(real, synthetic, feedTarget) };
const synthetic =
useSynthetic && pad > 0
? buildSyntheticWithdrawalsFromPersonas(
personas,
pad,
settings.syntheticWithdrawalAmounts
)
: [];
// Same presentation for real + synthetic: shared rotating pool (no sticky real-first).
return {
items: blendAndRotateFeedItems(real, synthetic, displayTarget),
};
});
}

View File

@ -5,11 +5,13 @@ import {
utcDayBounds,
} from '../repositories/marketingRepository';
import {
blendAndRotateFeedItems,
buildSyntheticActivityFromPersonas,
buildSyntheticWithdrawalsFromPersonas,
clearMarketingCache,
computeActivePlayersCount,
formatAmountRaw,
formatWithdrawalMarketingAmount,
maskPublicId,
mergeFeedItems,
shouldUseSynthetic,
@ -60,6 +62,36 @@ describe('marketingService helpers', () => {
assert.equal(formatAmountRaw('50000000'), '50.00');
});
test('formatWithdrawalMarketingAmount rounds to whole units like synthetics', () => {
assert.equal(formatWithdrawalMarketingAmount('3200000'), '3');
assert.equal(formatWithdrawalMarketingAmount('120000000'), '120');
});
test('blendAndRotateFeedItems mixes real and synthetic without sticky real-first', () => {
const real = [
{ id: 'r1', publicId: 'aa***11' },
{ id: 'r2', publicId: 'bb***22' },
];
const synthetic = [
{ id: 's1', publicId: 'cc***33' },
{ id: 's2', publicId: 'dd***44' },
{ id: 's3', publicId: 'ee***55' },
];
// slotIndex = floor(now/10_000); pick times that shift start by 1 in a pool of 5.
const a = blendAndRotateFeedItems(real, synthetic, 4, 0);
const b = blendAndRotateFeedItems(real, synthetic, 4, 10_000);
assert.equal(a.length, 4);
assert.equal(b.length, 4);
assert.deepEqual(
a.map((x) => x.id),
['r1', 'r2', 's1', 's2']
);
assert.deepEqual(
b.map((x) => x.id),
['r2', 's1', 's2', 's3']
);
});
test('mergeFeedItems keeps real first then pads synthetic', () => {
const real = [
{ id: 'r1', publicId: 'aa***11' },