/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
backend/controllers/data/user/RequestController.php
221 строка
6 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace backend\controllers\data\user; use common\controllers\base\ParentCrudController; use common\helpers\TextHelper; use common\kdf\KTaskType; use common\models\Assistant; use common\models\BatchLog; use common\models\BatchLogMongo; use common\models\ModelAI; use common\models\Request; use common\models\RequestBatch; use common\models\RequestStat; use common\models\RequestToken; use common\models\ResponseBatch; use common\models\ResponseBatchError; use common\models\Task; use common\models\TaskAssistant; use common\models\TaskModel; use common\models\TextSegment; use common\models\Token; use Yii; use yii\helpers\Url; use yii\web\NotFoundHttpException; /** * Контроллер для работы с запросами * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2024 */ class RequestController extends ParentCrudController { /** * @inheritdoc */ protected $modelClassPath = 'backend\components\crud\user\request'; /** * @inheritdoc */ protected $classPrefix = 'Request'; /** * @inheritdoc */ protected $parentModelName = 'common\models\User'; /** * @inheritdoc */ protected $relationKey = ['user_id' => 'id']; /** * Просмотр Batch одного запроса * * @param $id * @return void * @throws NotFoundHttpException */ public function actionBatch($id) { Yii::$app->request->validateSig(); $r = Request::findOne($id); /** @var Task $task */ $task = $r->task; if ($task->type_id == KTaskType::T_ASSISTANT) { /** @var TaskAssistant $taskAssistant */ $taskAssistant = $task->getTaskAssistant() ->andWhere(['is_default' => 1]) ->one(); if (!$taskAssistant) { throw new NotFoundHttpException(Yii::t('app', 'No default dask')); } /** @var Assistant $assistant */ $assistant = $taskAssistant->assistant; /** @var ModelAI $model */ $modelAI = $assistant->model; $options = $taskAssistant->getOptions($r); } else { /** @var TaskModel $taskModel */ $taskModel = $task->getTaskModel() ->andWhere(['is_default' => 1]) ->one(); if (!$taskModel) { throw new NotFoundHttpException(Yii::t('app', 'No default dask')); } /** @var ModelAI $modelAI */ $modelAI = $taskModel->model; $options = $taskModel->getOptions($r); } $text_array = $r->getTextArray($modelAI->token_limit); $this->asJson([ 'model' => $modelAI->title, 'model_limit' => $modelAI->token_limit, 'options' => $options, 'items' => $text_array, ]); } /** * Просмотр лог информации * @param $id * @return string * @throws NotFoundHttpException * @throws \yii\base\InvalidConfigException */ public function actionLog($id) { Yii::$app->request->validateSig(); $request = Request::findOne($id); if (!$request) { throw new NotFoundHttpException(Yii::t('app', 'Request not found')); } $a = RequestBatch::getAlias(); $batch_list = RequestBatch::find() ->select([ 'batch_index', 'token_count', 'time_spent', 'attempt', 'error', 'reb.request_batch_id', ]) ->alias($a) ->leftJoin(['reb' => ResponseBatch::tableName()], 'reb.request_batch_id = rb.id') ->leftJoin(['rbe' => ResponseBatchError::tableName()], 'reb.id = rbe.response_batch_id') ->andWhere([ $a . '.request_id' => $id ]) ->orderBy([ 'batch_index' => SORT_ASC ]) ->asArray() ->all(); $debug = []; foreach ($batch_list as &$row) { $row['time_spent'] = sprintf('%.3f s', $row['time_spent']); if ($request->is_debug) { $debug[$row['request_batch_id']] = BatchLogMongo::find() ->andWhere([ 'request_batch_id' => (int)$row['request_batch_id'] ]) ->one(); } $token_list = RequestToken::find() ->select([ 't.created_at', 'input', 'output', 'total', ]) ->innerJoin(['t' => Token::tableName()], 't.id = token_id') ->andWhere([ 'request_batch_id' => $row['request_batch_id'] ]) ->orderBy([ 't.created_at' => SORT_ASC ]) ->asArray() ->all(); $row['token_list'] = $token_list; } $title = Yii::t('app', 'Request: ' . $request->id); $stat = RequestStat::findOne(['request_id' => $request->id]); $word_count = 0; if ($stat) { $word_count = $stat->word_count; } if (!$word_count) { $word_count = TextHelper::countWordsWithFilter($request->getText()); } $view = Yii::$app->getView(); $view->title = $title; $breadcrumbs[] = [ 'label' => Yii::t('app', 'Users'), 'url' => Url::to(['/data/user/index']) ]; $breadcrumbs[] = [ 'label' => Yii::t('app', 'Request'), 'url' => Url::to(['data/user/request/index', 'parent_id' => $request->user_id]) ]; $breadcrumbs[] = [ 'label' => $title, ]; $view->params['breadcrumbs'] = $breadcrumbs; return $this->render('log', [ 'request' => $request, 'batch_list' => $batch_list, 'debug_info' => $debug, 'word_count' => $word_count ]); } }