|
| 1 | +<?php |
| 2 | + |
| 3 | +use DBA\Aggregation; |
| 4 | +use DBA\Chunk; |
| 5 | +use DBA\Factory; |
| 6 | +use DBA\QueryFilter; |
| 7 | +use JetBrains\PhpStorm\NoReturn; |
| 8 | +use Middlewares\Utils\HttpErrorException; |
| 9 | +use Psr\Container\ContainerExceptionInterface; |
| 10 | +use Psr\Container\NotFoundExceptionInterface; |
| 11 | +use Psr\Http\Message\ResponseInterface as Response; |
| 12 | +use Psr\Http\Message\ServerRequestInterface as Request; |
| 13 | + |
| 14 | +require_once(dirname(__FILE__) . "/../common/AbstractHelperAPI.class.php"); |
| 15 | + |
| 16 | +class TaskExtraDetailsHelper extends AbstractHelperAPI { |
| 17 | + public static function getBaseUri(): string { |
| 18 | + return "/api/v2/helper/taskExtraDetails"; |
| 19 | + } |
| 20 | + |
| 21 | + public static function getAvailableMethods(): array { |
| 22 | + return ['GET']; |
| 23 | + } |
| 24 | + |
| 25 | + public function getRequiredPermissions(string $method): array { |
| 26 | + return []; |
| 27 | + } |
| 28 | + |
| 29 | + public function getFormFields(): array { |
| 30 | + return []; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @throws HttpErrorException |
| 35 | + */ |
| 36 | + public function handleGet(Request $request, Response $response): Response { |
| 37 | + $this->preCommon($request); |
| 38 | + |
| 39 | + $taskId = $request->getQueryParams()['task']; |
| 40 | + if ($taskId === null) { |
| 41 | + throw new HttpErrorException("No task query param has been provided"); |
| 42 | + } |
| 43 | + $taskId = intval($taskId); |
| 44 | + if ($taskId === 0) { |
| 45 | + throw new HttpErrorException("No valid integer provided as task"); |
| 46 | + } |
| 47 | + $task = Factory::getTaskFactory()->get($taskId); |
| 48 | + if ($task === null) { |
| 49 | + throw new HttpErrorException("No task found for provided task ID"); |
| 50 | + } |
| 51 | + |
| 52 | + $qF = new QueryFilter(Chunk::TASK_ID, $taskId, "="); |
| 53 | + $chunks = Factory::getChunkFactory()->filter([Factory::FILTER => $qF]); |
| 54 | + $currentSpeed = 0; |
| 55 | + $cProgress = 0; |
| 56 | + foreach ($chunks as $chunk) { |
| 57 | + $cProgress += $chunk->getCheckpoint() - $chunk->getSkip(); |
| 58 | + if (time() - max($chunk->getSolveTime(), $chunk->getDispatchTime()) < SConfig::getInstance()->getVal(DConfig::CHUNK_TIMEOUT) && $chunk->getProgress() < 10000) { |
| 59 | + $currentSpeed += $chunk->getSpeed(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + $timeChunks = $chunks; |
| 64 | + usort($timeChunks, "Util::compareChunksTime"); |
| 65 | + $timeSpent = 0; |
| 66 | + $current = 0; |
| 67 | + foreach ($timeChunks as $c) { |
| 68 | + if ($c->getDispatchTime() > $current) { |
| 69 | + $timeSpent += $c->getSolveTime() - $c->getDispatchTime(); |
| 70 | + $current = $c->getSolveTime(); |
| 71 | + } |
| 72 | + else if ($c->getSolveTime() > $current) { |
| 73 | + $timeSpent += $c->getSolveTime() - $current; |
| 74 | + $current = $c->getSolveTime(); |
| 75 | + } |
| 76 | + } |
| 77 | + $keyspace = $task->getKeyspace(); |
| 78 | + $estimatedTime = ($keyspace > 0 && $cProgress > 0) ? round($timeSpent / ($cProgress / $keyspace) - $timeSpent) : 0; |
| 79 | + $responseObject = [ |
| 80 | + "estimatedTime" => $estimatedTime, |
| 81 | + "timeSpent" => $timeSpent, |
| 82 | + "currentSpeed" => $currentSpeed, |
| 83 | + ]; |
| 84 | + |
| 85 | + return self::getMetaResponse($responseObject, $request, $response); |
| 86 | + } |
| 87 | + |
| 88 | + #[NoReturn] public function actionPost($data): object|array|null { |
| 89 | + assert(false, "TaskExtraDetails has no POST"); |
| 90 | + } |
| 91 | + |
| 92 | + static public function register($app): void { |
| 93 | + $baseUri = TaskExtraDetailsHelper::getBaseUri(); |
| 94 | + |
| 95 | + /* Allow CORS preflight requests */ |
| 96 | + $app->options($baseUri, function (Request $request, Response $response): Response { |
| 97 | + return $response; |
| 98 | + }); |
| 99 | + $app->get($baseUri, "TaskExtraDetailsHelper:handleGet"); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * getAccessGroups is different because it returns via another function |
| 104 | + */ |
| 105 | + public static function getResponse(): array|string|null { |
| 106 | + return null; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +TaskExtraDetailsHelper::register($app); |
0 commit comments