Use recent data for purchase hour pattern
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m29s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m29s
This commit is contained in:
@@ -2,6 +2,7 @@ const crypto = require('node:crypto');
|
||||
const { pool } = require('../db');
|
||||
|
||||
const RFM_QUERY_TIMEOUT_MS = 15000;
|
||||
const CLIENT_PURCHASE_PATTERN_HOUR_LOOKBACK_DAYS = 60;
|
||||
const RECENT_MAX_DAYS = 7;
|
||||
const COOLING_MAX_DAYS = 15;
|
||||
const LOST_MIN_DAYS = 30;
|
||||
@@ -1005,7 +1006,8 @@ const getClientFilterOptions = async () => {
|
||||
};
|
||||
|
||||
const getClientPurchasePatternAnalytics = async () => {
|
||||
const result = await pool.query(`
|
||||
const [weekdayResult, hourResult] = await Promise.all([
|
||||
pool.query(`
|
||||
${CUSTOMER_IDENTITY_CTE},
|
||||
order_events AS (
|
||||
SELECT DISTINCT ON (
|
||||
@@ -1023,12 +1025,35 @@ const getClientPurchasePatternAnalytics = async () => {
|
||||
)
|
||||
SELECT
|
||||
EXTRACT(DOW FROM data_pedido_date)::int as weekday,
|
||||
COUNT(*)::int as order_count
|
||||
FROM order_events
|
||||
GROUP BY weekday
|
||||
ORDER BY weekday ASC;
|
||||
`),
|
||||
pool.query(`
|
||||
${CUSTOMER_IDENTITY_CTE},
|
||||
order_events AS (
|
||||
SELECT DISTINCT ON (
|
||||
${CUSTOMER_KEY_SQL},
|
||||
COALESCE(NULLIF(pedido_id, ''), data_pedido || '_' || valor_pedido::text)
|
||||
)
|
||||
created_at
|
||||
FROM identity_orders
|
||||
WHERE data_pedido_date IS NOT NULL
|
||||
AND created_at >= NOW() - ($1::int * INTERVAL '1 day')
|
||||
ORDER BY
|
||||
${CUSTOMER_KEY_SQL},
|
||||
COALESCE(NULLIF(pedido_id, ''), data_pedido || '_' || valor_pedido::text),
|
||||
created_at ASC NULLS LAST
|
||||
)
|
||||
SELECT
|
||||
EXTRACT(HOUR FROM created_at AT TIME ZONE 'America/Sao_Paulo')::int as hour,
|
||||
COUNT(*)::int as order_count
|
||||
FROM order_events
|
||||
GROUP BY weekday, hour
|
||||
ORDER BY weekday ASC, hour ASC;
|
||||
`);
|
||||
GROUP BY hour
|
||||
ORDER BY hour ASC;
|
||||
`, [CLIENT_PURCHASE_PATTERN_HOUR_LOOKBACK_DAYS])
|
||||
]);
|
||||
|
||||
const purchaseWeekdays = WEEKDAY_LABELS.map(label => ({ label, value: 0 }));
|
||||
const purchaseHours = Array.from({ length: 24 }, (_, hour) => ({
|
||||
@@ -1036,14 +1061,18 @@ const getClientPurchasePatternAnalytics = async () => {
|
||||
value: 0
|
||||
}));
|
||||
|
||||
result.rows.forEach(row => {
|
||||
weekdayResult.rows.forEach(row => {
|
||||
const weekday = row.weekday === null || row.weekday === undefined ? null : Number(row.weekday);
|
||||
const hour = row.hour === null || row.hour === undefined ? null : Number(row.hour);
|
||||
const orderCount = toNumber(row.order_count);
|
||||
|
||||
if (weekday !== null && Number.isInteger(weekday) && purchaseWeekdays[weekday]) {
|
||||
purchaseWeekdays[weekday].value += orderCount;
|
||||
}
|
||||
});
|
||||
|
||||
hourResult.rows.forEach(row => {
|
||||
const hour = row.hour === null || row.hour === undefined ? null : Number(row.hour);
|
||||
const orderCount = toNumber(row.order_count);
|
||||
|
||||
if (hour !== null && Number.isInteger(hour) && purchaseHours[hour]) {
|
||||
purchaseHours[hour].value += orderCount;
|
||||
@@ -1052,7 +1081,9 @@ const getClientPurchasePatternAnalytics = async () => {
|
||||
|
||||
return {
|
||||
purchaseWeekdays,
|
||||
purchaseHours
|
||||
purchaseHours,
|
||||
weekdayRangeLabel: 'Todo período',
|
||||
hourRangeLabel: `Últimos ${CLIENT_PURCHASE_PATTERN_HOUR_LOOKBACK_DAYS} dias`
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user