Fix RFV all-period scaling
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 50s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 50s
This commit is contained in:
@@ -94,10 +94,50 @@ const scoreTertile = (value, values, higherIsBetter = true) => {
|
||||
return Math.min(3, Math.max(1, Math.floor(percentile * 3) + 1));
|
||||
};
|
||||
|
||||
const buildTertileScorer = (values, higherIsBetter = true) => {
|
||||
const numericValues = values.map(toNumber).filter(Number.isFinite);
|
||||
if (!numericValues.length) return () => 1;
|
||||
if (numericValues.length === 1) return () => 3;
|
||||
|
||||
const min = Math.min(...numericValues);
|
||||
const max = Math.max(...numericValues);
|
||||
if (min === max) return () => higherIsBetter ? 2 : 3;
|
||||
|
||||
const sorted = [...numericValues].sort((a, b) => higherIsBetter ? a - b : b - a);
|
||||
const scoresByValue = new Map();
|
||||
|
||||
sorted.forEach((candidate, index) => {
|
||||
if (scoresByValue.has(candidate)) return;
|
||||
|
||||
const percentile = index / (sorted.length - 1);
|
||||
scoresByValue.set(candidate, Math.min(3, Math.max(1, Math.floor(percentile * 3) + 1)));
|
||||
});
|
||||
|
||||
return (value) => scoresByValue.get(toNumber(value)) || 1;
|
||||
};
|
||||
|
||||
const getRfmSegment = (recencyScore, valueScore) => {
|
||||
return RFM_SEGMENTS[`${recencyScore}-${valueScore}`] || RFM_SEGMENTS['1-1'];
|
||||
};
|
||||
|
||||
const getDateDiffDays = (endDate, startDate) => {
|
||||
const normalizedEnd = normalizeDateParam(endDate);
|
||||
const normalizedStart = normalizeDateParam(startDate);
|
||||
|
||||
if (!normalizedEnd || !normalizedStart) return 0;
|
||||
|
||||
const end = new Date(`${normalizedEnd}T00:00:00.000Z`);
|
||||
const start = new Date(`${normalizedStart}T00:00:00.000Z`);
|
||||
return Math.max(0, Math.floor((end.getTime() - start.getTime()) / 86400000));
|
||||
};
|
||||
|
||||
const getDateOnly = (value) => {
|
||||
if (!value) return null;
|
||||
if (value instanceof Date) return value.toISOString().slice(0, 10);
|
||||
const match = String(value).match(/^(\d{4}-\d{2}-\d{2})/);
|
||||
return match ? match[1] : null;
|
||||
};
|
||||
|
||||
const buildRfmSegments = (clients) => {
|
||||
return Object.values(RFM_SEGMENTS).map(segment => {
|
||||
const segmentClients = clients.filter(client => client.segmentKey === segment.key);
|
||||
@@ -113,16 +153,16 @@ const buildRfmSegments = (clients) => {
|
||||
};
|
||||
|
||||
const buildRfmClients = (baseClients) => {
|
||||
const recencyValues = baseClients.map(client => client.recencyDays);
|
||||
const frequencyValues = baseClients.map(client => client.rfmFrequency ?? client.frequency);
|
||||
const monetaryValues = baseClients.map(client => client.rfmMonetary ?? client.monetary);
|
||||
const recencyScoreFor = buildTertileScorer(baseClients.map(client => client.recencyDays), false);
|
||||
const frequencyScoreFor = buildTertileScorer(baseClients.map(client => client.rfmFrequency ?? client.frequency), true);
|
||||
const monetaryScoreFor = buildTertileScorer(baseClients.map(client => client.rfmMonetary ?? client.monetary), true);
|
||||
|
||||
return baseClients.map(client => {
|
||||
const frequencyForScore = client.rfmFrequency ?? client.frequency;
|
||||
const monetaryForScore = client.rfmMonetary ?? client.monetary;
|
||||
const recencyScore = scoreTertile(client.recencyDays, recencyValues, false);
|
||||
const frequencyScore = scoreTertile(frequencyForScore, frequencyValues, true);
|
||||
const monetaryScore = scoreTertile(monetaryForScore, monetaryValues, true);
|
||||
const recencyScore = recencyScoreFor(client.recencyDays);
|
||||
const frequencyScore = frequencyScoreFor(frequencyForScore);
|
||||
const monetaryScore = monetaryScoreFor(monetaryForScore);
|
||||
const valueScore = Math.min(3, Math.max(1, Math.round((frequencyScore + monetaryScore) / 2)));
|
||||
const segment = getRfmSegment(recencyScore, valueScore);
|
||||
|
||||
@@ -267,6 +307,35 @@ const getRfmAnalytics = async (range = {}) => {
|
||||
const normalizedEnd = normalizeDateParam(range.end);
|
||||
const periodRecencyReferenceDate = normalizedEnd ? `$${params.length}::date` : 'CURRENT_DATE';
|
||||
const usePeriodAsHistory = !normalizedStart || normalizedStart <= '2000-01-01';
|
||||
|
||||
if (usePeriodAsHistory) {
|
||||
const clientRows = await getClientAnalytics({ end: normalizedEnd });
|
||||
const recencyEnd = normalizedEnd || new Date().toISOString().slice(0, 10);
|
||||
const clients = buildRfmClients(clientRows.map(row => ({
|
||||
customerKey: row.customerKey,
|
||||
name: row.name,
|
||||
phone: row.phone || '',
|
||||
monetary: row.totalSpent,
|
||||
frequency: row.orderCount,
|
||||
quantityPurchased: row.quantityPurchased,
|
||||
lastPurchaseDate: row.lastPurchaseDate,
|
||||
recencyDays: getDateDiffDays(recencyEnd, getDateOnly(row.lastPurchaseDate))
|
||||
})));
|
||||
|
||||
return {
|
||||
range: {
|
||||
start: normalizeDateParam(range.start),
|
||||
end: normalizeDateParam(range.end)
|
||||
},
|
||||
clients,
|
||||
segments: buildRfmSegments(clients),
|
||||
matrix: {
|
||||
recencyScores: [3, 2, 1],
|
||||
valueScores: [1, 2, 3]
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const client = await pool.connect();
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user