Add aggregate AI tokens and move column
This commit is contained in:
@@ -51,6 +51,44 @@ class ResultsModel extends ListModel
|
||||
return $items;
|
||||
}
|
||||
|
||||
public function getAiUsageTotals(): array
|
||||
{
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
$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];
|
||||
|
||||
foreach ($answersRows as $answersJson) {
|
||||
$answers = json_decode((string) $answersJson, true);
|
||||
if (!is_array($answers)) {
|
||||
continue;
|
||||
}
|
||||
foreach ($answers as $answer) {
|
||||
if (!is_array($answer)) {
|
||||
continue;
|
||||
}
|
||||
$usages = $answer['ai_usage_history'] ?? (isset($answer['ai_usage']) ? [$answer['ai_usage']] : []);
|
||||
if (!is_array($usages)) {
|
||||
continue;
|
||||
}
|
||||
foreach ($usages as $usage) {
|
||||
if (!is_array($usage)) {
|
||||
continue;
|
||||
}
|
||||
$totals['input_tokens'] += (int) ($usage['input_tokens'] ?? 0);
|
||||
$totals['output_tokens'] += (int) ($usage['output_tokens'] ?? 0);
|
||||
$totals['total_tokens'] += (int) ($usage['total_tokens'] ?? 0);
|
||||
if (is_numeric($usage['cost_usd'] ?? null)) {
|
||||
$totals['cost_usd'] += (float) $usage['cost_usd'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $totals;
|
||||
}
|
||||
|
||||
protected function populateState($ordering = 'r.created', $direction = 'desc')
|
||||
{
|
||||
parent::populateState($ordering, $direction);
|
||||
|
||||
@@ -15,6 +15,7 @@ class HtmlView extends BaseHtmlView
|
||||
protected $pagination;
|
||||
protected $state;
|
||||
protected $sets;
|
||||
protected $aiUsageTotals;
|
||||
|
||||
public function display($tpl = null)
|
||||
{
|
||||
@@ -22,6 +23,7 @@ class HtmlView extends BaseHtmlView
|
||||
$this->pagination = $this->get('Pagination');
|
||||
$this->state = $this->get('State');
|
||||
$this->sets = $this->get('Sets');
|
||||
$this->aiUsageTotals = $this->get('AiUsageTotals');
|
||||
ToolbarHelper::title(Text::_('COM_TESTQUESTIONS_RESULTS'));
|
||||
ToolbarHelper::link('index.php?option=com_testquestions&view=sets', 'COM_TESTQUESTIONS_SETS', 'back');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user