This commit is contained in:
nBiqoz
2025-10-06 19:16:20 +02:00
parent 96dd721fcb
commit 0f2adca44a
23 changed files with 1569 additions and 248 deletions

View File

@@ -7,6 +7,22 @@ export async function GET() {
// Récupérer toutes les transactions
const transactions = await db.collection("transactions").find({}).toArray();
console.log(`Total transactions trouvées: ${transactions.length}`);
// Vérifier les champs de date disponibles dans les transactions
if (transactions.length > 0) {
const sampleTransaction = transactions[0];
console.log("Exemple de transaction:", {
_id: sampleTransaction._id,
createdAt: sampleTransaction.createdAt,
updatedAt: sampleTransaction.updatedAt,
date: sampleTransaction.date,
timestamp: sampleTransaction.timestamp,
rawAmount: sampleTransaction.rawAmount,
model: sampleTransaction.model
});
}
// Calculer les tokens par jour (7 derniers jours)
const dailyStats = [];
@@ -22,14 +38,44 @@ export async function GET() {
nextDate.setDate(nextDate.getDate() + 1);
const dayTransactions = transactions.filter(transaction => {
const transactionDate = new Date(transaction.createdAt);
// Essayer différents champs de date
let transactionDate = null;
if (transaction.createdAt) {
transactionDate = new Date(transaction.createdAt);
} else if (transaction.updatedAt) {
transactionDate = new Date(transaction.updatedAt);
} else if (transaction.date) {
transactionDate = new Date(transaction.date);
} else if (transaction.timestamp) {
transactionDate = new Date(transaction.timestamp);
} else if (transaction._id && transaction._id.getTimestamp) {
// Utiliser le timestamp de l'ObjectId MongoDB
transactionDate = transaction._id.getTimestamp();
}
if (!transactionDate || isNaN(transactionDate.getTime())) {
return false;
}
return transactionDate >= date && transactionDate < nextDate;
});
const totalTokens = dayTransactions.reduce((sum, transaction) => {
return sum + Math.abs(Number(transaction.rawAmount) || 0);
// Essayer différents champs pour les tokens
let tokens = 0;
if (transaction.rawAmount) {
tokens = Math.abs(Number(transaction.rawAmount) || 0);
} else if (transaction.amount) {
tokens = Math.abs(Number(transaction.amount) || 0);
} else if (transaction.tokens) {
tokens = Math.abs(Number(transaction.tokens) || 0);
}
return sum + tokens;
}, 0);
console.log(`${dayNames[date.getDay()]} (${date.toISOString().split('T')[0]}): ${dayTransactions.length} transactions, ${totalTokens} tokens`);
dailyStats.push({
name: dayNames[date.getDay()],
value: totalTokens
@@ -40,9 +86,19 @@ export async function GET() {
const modelStats = new Map<string, number>();
transactions.forEach(transaction => {
const model = transaction.model || "Inconnu";
const tokens = Math.abs(Number(transaction.rawAmount) || 0);
modelStats.set(model, (modelStats.get(model) || 0) + tokens);
const model = transaction.model || transaction.modelName || "Inconnu";
let tokens = 0;
if (transaction.rawAmount) {
tokens = Math.abs(Number(transaction.rawAmount) || 0);
} else if (transaction.amount) {
tokens = Math.abs(Number(transaction.amount) || 0);
} else if (transaction.tokens) {
tokens = Math.abs(Number(transaction.tokens) || 0);
}
if (tokens > 0) {
modelStats.set(model, (modelStats.get(model) || 0) + tokens);
}
});
// Convertir en array et trier par usage
@@ -50,6 +106,12 @@ export async function GET() {
.map(([name, value]) => ({ name, value }))
.sort((a, b) => b.value - a.value);
console.log("Statistiques calculées:", {
dailyStats,
totalModels: modelData.length,
topModel: modelData[0]
});
return NextResponse.json({
dailyTokens: dailyStats,
modelDistribution: modelData