Split AI token counters by provider
This commit is contained in:
@@ -18,6 +18,7 @@ class ResultsModel extends ListModel
|
||||
$item->ai_output_tokens = 0;
|
||||
$item->ai_total_tokens = 0;
|
||||
$item->ai_cost_usd = 0.0;
|
||||
$item->ai_tokens_by_provider = ['openai' => 0, 'grok' => 0];
|
||||
|
||||
$answers = json_decode((string) ($item->answers ?? ''), true);
|
||||
if (!is_array($answers)) {
|
||||
@@ -41,6 +42,10 @@ class ResultsModel extends ListModel
|
||||
$item->ai_input_tokens += (int) ($usage['input_tokens'] ?? 0);
|
||||
$item->ai_output_tokens += (int) ($usage['output_tokens'] ?? 0);
|
||||
$item->ai_total_tokens += (int) ($usage['total_tokens'] ?? 0);
|
||||
$provider = (string) ($usage['provider'] ?? '');
|
||||
if (isset($item->ai_tokens_by_provider[$provider])) {
|
||||
$item->ai_tokens_by_provider[$provider] += (int) ($usage['total_tokens'] ?? 0);
|
||||
}
|
||||
if (is_numeric($usage['cost_usd'] ?? null)) {
|
||||
$item->ai_cost_usd += (float) $usage['cost_usd'];
|
||||
}
|
||||
@@ -57,7 +62,14 @@ class ResultsModel extends ListModel
|
||||
$query = clone $this->getListQuery();
|
||||
$query->clear('select')->clear('order')->select($db->quoteName('r.answers'));
|
||||
$answersRows = $db->setQuery($query)->loadColumn();
|
||||
$totals = ['input_tokens' => 0, 'output_tokens' => 0, 'total_tokens' => 0, 'cost_usd' => 0.0];
|
||||
$emptyProviderTotals = ['input_tokens' => 0, 'output_tokens' => 0, 'total_tokens' => 0, 'cost_usd' => 0.0];
|
||||
$totals = [
|
||||
'input_tokens' => 0,
|
||||
'output_tokens' => 0,
|
||||
'total_tokens' => 0,
|
||||
'cost_usd' => 0.0,
|
||||
'providers' => ['openai' => $emptyProviderTotals, 'grok' => $emptyProviderTotals],
|
||||
];
|
||||
|
||||
foreach ($answersRows as $answersJson) {
|
||||
$answers = json_decode((string) $answersJson, true);
|
||||
@@ -82,6 +94,15 @@ class ResultsModel extends ListModel
|
||||
if (is_numeric($usage['cost_usd'] ?? null)) {
|
||||
$totals['cost_usd'] += (float) $usage['cost_usd'];
|
||||
}
|
||||
$provider = (string) ($usage['provider'] ?? '');
|
||||
if (isset($totals['providers'][$provider])) {
|
||||
$totals['providers'][$provider]['input_tokens'] += (int) ($usage['input_tokens'] ?? 0);
|
||||
$totals['providers'][$provider]['output_tokens'] += (int) ($usage['output_tokens'] ?? 0);
|
||||
$totals['providers'][$provider]['total_tokens'] += (int) ($usage['total_tokens'] ?? 0);
|
||||
if (is_numeric($usage['cost_usd'] ?? null)) {
|
||||
$totals['providers'][$provider]['cost_usd'] += (float) $usage['cost_usd'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user