From 3c0956b518f2162ad570487ca08f0e09eb919931 Mon Sep 17 00:00:00 2001 From: Carlos Date: Sat, 24 May 2025 16:32:30 -0400 Subject: [PATCH] fix: use correct PostgreSQL interval units in date_trunc --- src/api/controller/stats.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/api/controller/stats.js b/src/api/controller/stats.js index 46064b8..aae264e 100644 --- a/src/api/controller/stats.js +++ b/src/api/controller/stats.js @@ -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'],