fix: use correct PostgreSQL interval units in date_trunc

This commit is contained in:
Carlos
2025-05-24 16:32:30 -04:00
parent 1d195363eb
commit 3c0956b518

View File

@ -27,12 +27,19 @@ async function getModelExpenses({ startDate, endDate, granularity = 'monthly' }
yearly: '%Y'
}[granularity] || '%Y-%m'; // Default to monthly if invalid granularity
// Map granularity to PostgreSQL interval units
const intervalUnit = {
daily: 'day',
monthly: 'month',
yearly: 'year'
}[granularity] || 'month'; // Default to month if invalid granularity
// Get all prompts within the date range
const prompts = await Prompt.findAll({
where,
attributes: [
'model',
[sequelize.fn('date_trunc', granularity, sequelize.col('created_at')), 'time_period'],
[sequelize.fn('date_trunc', intervalUnit, sequelize.col('created_at')), 'time_period'],
[sequelize.fn('COUNT', sequelize.col('id')), 'total_requests'],
[sequelize.fn('SUM', sequelize.col('prompt_tokens')), 'total_prompt_tokens'],
[sequelize.fn('SUM', sequelize.col('response_tokens')), 'total_response_tokens'],