From bf05c9515cae41524e7b998d23f344fafa71b61b Mon Sep 17 00:00:00 2001 From: Wojtek Date: Fri, 16 Oct 2026 19:48:45 +0200 Subject: [PATCH] Split AI token counters by provider --- README.md | 2 +- .../sql/updates/mysql/1.43.11.sql | 1 + .../src/Model/ResultsModel.php | 23 +++++++++++++- .../tmpl/results/default.php | 31 +++++++++++++------ com_testquestions.xml | 2 +- 5 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 administrator/components/com_testquestions/sql/updates/mysql/1.43.11.sql diff --git a/README.md b/README.md index 59763cb..d082be6 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,6 @@ Szkielet komponentu Joomla 6 do gromadzenia pytań testowych. ## 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**. diff --git a/administrator/components/com_testquestions/sql/updates/mysql/1.43.11.sql b/administrator/components/com_testquestions/sql/updates/mysql/1.43.11.sql new file mode 100644 index 0000000..a282b6c --- /dev/null +++ b/administrator/components/com_testquestions/sql/updates/mysql/1.43.11.sql @@ -0,0 +1 @@ +-- No database changes required for version 1.43.11. diff --git a/administrator/components/com_testquestions/src/Model/ResultsModel.php b/administrator/components/com_testquestions/src/Model/ResultsModel.php index 9d97583..819ca43 100644 --- a/administrator/components/com_testquestions/src/Model/ResultsModel.php +++ b/administrator/components/com_testquestions/src/Model/ResultsModel.php @@ -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']; + } + } } } } diff --git a/administrator/components/com_testquestions/tmpl/results/default.php b/administrator/components/com_testquestions/tmpl/results/default.php index 7ee38d7..881dc3e 100644 --- a/administrator/components/com_testquestions/tmpl/results/default.php +++ b/administrator/components/com_testquestions/tmpl/results/default.php @@ -34,13 +34,22 @@ use Joomla\CMS\Router\Route;
- : - aiUsageTotals['total_tokens'], 0, ',', ' '); ?> escape(\Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_AI_USAGE_TOTAL')); ?> - (aiUsageTotals['input_tokens'], 0, ',', ' '); ?> in / - aiUsageTotals['output_tokens'], 0, ',', ' '); ?> out) - aiUsageTotals['cost_usd'] > 0) : ?> - · aiUsageTotals['cost_usd'], 8, ',', ' '); ?> USD - + escape(\Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_RESULTS_AI_TOKENS_TOTAL')); ?>: + + OpenAI: + aiUsageTotals['providers']['openai']['total_tokens'], 0, ',', ' '); ?> tokenów + (aiUsageTotals['providers']['openai']['input_tokens'], 0, ',', ' '); ?> wej. / + aiUsageTotals['providers']['openai']['output_tokens'], 0, ',', ' '); ?> wyj.) + aiUsageTotals['providers']['openai']['cost_usd'] > 0) : ?> · aiUsageTotals['providers']['openai']['cost_usd'], 8, ',', ' '); ?> USD + + + Grok: + aiUsageTotals['providers']['grok']['total_tokens'], 0, ',', ' '); ?> tokenów + (aiUsageTotals['providers']['grok']['input_tokens'], 0, ',', ' '); ?> wej. / + aiUsageTotals['providers']['grok']['output_tokens'], 0, ',', ' '); ?> wyj.) + aiUsageTotals['providers']['grok']['cost_usd'] > 0) : ?> · aiUsageTotals['providers']['grok']['cost_usd'], 8, ',', ' '); ?> USD + + Łącznie: aiUsageTotals['total_tokens'], 0, ',', ' '); ?> tokenów
@@ -70,10 +79,12 @@ use Joomla\CMS\Router\Route;
created, 'Y-m-d H:i'); ?> ai_total_tokens > 0) : ?> - ai_total_tokens, 0, ',', ' '); ?> escape(\Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_AI_USAGE_TOTAL')); ?> - ai_input_tokens, 0, ',', ' '); ?> in / - ai_output_tokens, 0, ',', ' '); ?> out + OpenAI: ai_tokens_by_provider['openai'], 0, ',', ' '); ?> · + Grok: ai_tokens_by_provider['grok'], 0, ',', ' '); ?> + + + ai_total_tokens, 0, ',', ' '); ?> łącznie ai_cost_usd > 0) : ?> · ai_cost_usd, 8, ',', ' '); ?> USD diff --git a/com_testquestions.xml b/com_testquestions.xml index 1f89367..c4db9a4 100644 --- a/com_testquestions.xml +++ b/com_testquestions.xml @@ -4,7 +4,7 @@ 2026-10-14 Komponent GNU General Public License version 2 or later - 1.43.10 + 1.43.11 COM_TESTQUESTIONS_DESCRIPTION com_testquestions Komponent\Component\Testquestions