fix: use correct PostgreSQL interval units in date_trunc
This commit is contained in:
@ -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'],
|
||||
|
Reference in New Issue
Block a user