Split AI token counters by provider

This commit is contained in:
Wojtek
2026-10-16 19:48:45 +02:00
parent 08220dc6e8
commit bf05c9515c
5 changed files with 46 additions and 13 deletions
+1 -1
View File
@@ -30,6 +30,6 @@ Szkielet komponentu Joomla 6 do gromadzenia pytań testowych.
## Instalacja ## Instalacja
W Joomla wybierz **System → Install → Install from Folder** i wskaż ten katalog. Manifestem instalacyjnym jest `com_testquestions.xml` (wersja 1.43.10). W Joomla wybierz **System → Install → Install from Folder** i wskaż ten katalog. Manifestem instalacyjnym jest `com_testquestions.xml` (wersja 1.43.11).
Po instalacji komponent jest dostępny w panelu administratora jako **Pytania testowe**. Po instalacji komponent jest dostępny w panelu administratora jako **Pytania testowe**.
@@ -0,0 +1 @@
-- No database changes required for version 1.43.11.
@@ -18,6 +18,7 @@ class ResultsModel extends ListModel
$item->ai_output_tokens = 0; $item->ai_output_tokens = 0;
$item->ai_total_tokens = 0; $item->ai_total_tokens = 0;
$item->ai_cost_usd = 0.0; $item->ai_cost_usd = 0.0;
$item->ai_tokens_by_provider = ['openai' => 0, 'grok' => 0];
$answers = json_decode((string) ($item->answers ?? ''), true); $answers = json_decode((string) ($item->answers ?? ''), true);
if (!is_array($answers)) { if (!is_array($answers)) {
@@ -41,6 +42,10 @@ class ResultsModel extends ListModel
$item->ai_input_tokens += (int) ($usage['input_tokens'] ?? 0); $item->ai_input_tokens += (int) ($usage['input_tokens'] ?? 0);
$item->ai_output_tokens += (int) ($usage['output_tokens'] ?? 0); $item->ai_output_tokens += (int) ($usage['output_tokens'] ?? 0);
$item->ai_total_tokens += (int) ($usage['total_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)) { if (is_numeric($usage['cost_usd'] ?? null)) {
$item->ai_cost_usd += (float) $usage['cost_usd']; $item->ai_cost_usd += (float) $usage['cost_usd'];
} }
@@ -57,7 +62,14 @@ class ResultsModel extends ListModel
$query = clone $this->getListQuery(); $query = clone $this->getListQuery();
$query->clear('select')->clear('order')->select($db->quoteName('r.answers')); $query->clear('select')->clear('order')->select($db->quoteName('r.answers'));
$answersRows = $db->setQuery($query)->loadColumn(); $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) { foreach ($answersRows as $answersJson) {
$answers = json_decode((string) $answersJson, true); $answers = json_decode((string) $answersJson, true);
@@ -82,6 +94,15 @@ class ResultsModel extends ListModel
if (is_numeric($usage['cost_usd'] ?? null)) { if (is_numeric($usage['cost_usd'] ?? null)) {
$totals['cost_usd'] += (float) $usage['cost_usd']; $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'];
}
}
} }
} }
} }
@@ -34,13 +34,22 @@ use Joomla\CMS\Router\Route;
<div class="col-md-3 align-self-end"><button type="submit" class="btn btn-primary"><?php echo \Joomla\CMS\Language\Text::_('JSEARCH_FILTER_SUBMIT'); ?></button></div> <div class="col-md-3 align-self-end"><button type="submit" class="btn btn-primary"><?php echo \Joomla\CMS\Language\Text::_('JSEARCH_FILTER_SUBMIT'); ?></button></div>
</div> </div>
<div class="alert alert-info" role="status"> <div class="alert alert-info" role="status">
<strong><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_RESULTS_AI_TOKENS_TOTAL'); ?>:</strong> <strong><?php echo $this->escape(\Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_RESULTS_AI_TOKENS_TOTAL')); ?>:</strong>
<?php echo number_format((int) $this->aiUsageTotals['total_tokens'], 0, ',', ' '); ?> <?php echo $this->escape(\Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_AI_USAGE_TOTAL')); ?> <span class="d-block">
(<?php echo number_format((int) $this->aiUsageTotals['input_tokens'], 0, ',', ' '); ?> in / <strong>OpenAI:</strong>
<?php echo number_format((int) $this->aiUsageTotals['output_tokens'], 0, ',', ' '); ?> out) <?php echo number_format((int) $this->aiUsageTotals['providers']['openai']['total_tokens'], 0, ',', ' '); ?> tokenów
<?php if ($this->aiUsageTotals['cost_usd'] > 0) : ?> (<?php echo number_format((int) $this->aiUsageTotals['providers']['openai']['input_tokens'], 0, ',', ' '); ?> wej. /
· <?php echo number_format((float) $this->aiUsageTotals['cost_usd'], 8, ',', ' '); ?> USD <?php echo number_format((int) $this->aiUsageTotals['providers']['openai']['output_tokens'], 0, ',', ' '); ?> wyj.)
<?php endif; ?> <?php if ($this->aiUsageTotals['providers']['openai']['cost_usd'] > 0) : ?> · <?php echo number_format((float) $this->aiUsageTotals['providers']['openai']['cost_usd'], 8, ',', ' '); ?> USD<?php endif; ?>
</span>
<span class="d-block">
<strong>Grok:</strong>
<?php echo number_format((int) $this->aiUsageTotals['providers']['grok']['total_tokens'], 0, ',', ' '); ?> tokenów
(<?php echo number_format((int) $this->aiUsageTotals['providers']['grok']['input_tokens'], 0, ',', ' '); ?> wej. /
<?php echo number_format((int) $this->aiUsageTotals['providers']['grok']['output_tokens'], 0, ',', ' '); ?> wyj.)
<?php if ($this->aiUsageTotals['providers']['grok']['cost_usd'] > 0) : ?> · <?php echo number_format((float) $this->aiUsageTotals['providers']['grok']['cost_usd'], 8, ',', ' '); ?> USD<?php endif; ?>
</span>
<span class="d-block"><strong>Łącznie:</strong> <?php echo number_format((int) $this->aiUsageTotals['total_tokens'], 0, ',', ' '); ?> tokenów</span>
</div> </div>
<table class="table table-striped"> <table class="table table-striped">
<thead><tr> <thead><tr>
@@ -70,10 +79,12 @@ use Joomla\CMS\Router\Route;
<td><?php echo HTMLHelper::_('date', $item->created, 'Y-m-d H:i'); ?></td> <td><?php echo HTMLHelper::_('date', $item->created, 'Y-m-d H:i'); ?></td>
<td> <td>
<?php if ($item->ai_total_tokens > 0) : ?> <?php if ($item->ai_total_tokens > 0) : ?>
<?php echo number_format((int) $item->ai_total_tokens, 0, ',', ' '); ?> <?php echo $this->escape(\Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_AI_USAGE_TOTAL')); ?>
<small class="d-block text-muted"> <small class="d-block text-muted">
<?php echo number_format((int) $item->ai_input_tokens, 0, ',', ' '); ?> in / OpenAI: <?php echo number_format((int) $item->ai_tokens_by_provider['openai'], 0, ',', ' '); ?> ·
<?php echo number_format((int) $item->ai_output_tokens, 0, ',', ' '); ?> out Grok: <?php echo number_format((int) $item->ai_tokens_by_provider['grok'], 0, ',', ' '); ?>
</small>
<small class="d-block text-muted">
<?php echo number_format((int) $item->ai_total_tokens, 0, ',', ' '); ?> łącznie
<?php if ($item->ai_cost_usd > 0) : ?> · <?php echo number_format((float) $item->ai_cost_usd, 8, ',', ' '); ?> USD<?php endif; ?> <?php if ($item->ai_cost_usd > 0) : ?> · <?php echo number_format((float) $item->ai_cost_usd, 8, ',', ' '); ?> USD<?php endif; ?>
</small> </small>
<?php else : ?> <?php else : ?>
+1 -1
View File
@@ -4,7 +4,7 @@
<creationDate>2026-10-14</creationDate> <creationDate>2026-10-14</creationDate>
<author>Komponent</author> <author>Komponent</author>
<license>GNU General Public License version 2 or later</license> <license>GNU General Public License version 2 or later</license>
<version>1.43.10</version> <version>1.43.11</version>
<description>COM_TESTQUESTIONS_DESCRIPTION</description> <description>COM_TESTQUESTIONS_DESCRIPTION</description>
<element>com_testquestions</element> <element>com_testquestions</element>
<namespace path="src">Komponent\Component\Testquestions</namespace> <namespace path="src">Komponent\Component\Testquestions</namespace>