14 namespace Fossology\UI\Api\Controllers;
29 use Psr\Http\Message\ServerRequestInterface;
30 use Slim\Psr7\Request;
73 $queryParams = $request->getQueryParams();
74 $query = $apiVersion == ApiVersion::V2 ? $queryParams : array_map(
function($header) {
75 return implode(
",", $header);
76 }, $request->getHeaders());
78 $limit = isset($query[
'limit']) ? intval($query[
'limit']) : 0;
79 $page = isset($query[
'page']) ? intval($query[
'page']) : 1;
80 $sort = $queryParams[
'sort'] ??
"ASC";
81 $status = $queryParams[
'status'] ??
null;
83 if ($limit < 0 || $page < 1) {
87 return $this->
getAllResults(
null, $status, $request, $response, $sort, $limit, $page, $apiVersion);
99 public function getJobs($request, $response, $args)
102 $userId = $this->restHelper->getUserId();
104 $queryParams = $request->getQueryParams();
105 $query = $apiVersion == ApiVersion::V2 ? $queryParams : array_map(
function($header) {
106 return implode(
",", $header);
107 }, $request->getHeaders());
109 $limit = isset($query[
'limit']) ? intval($query[
'limit']) : 0;
110 $page = isset($query[
'page']) ? intval($query[
'page']) : 1;
111 $sort = $queryParams[
'sort'] ??
"ASC";
112 $status = $queryParams[
'status'] ??
null;
114 if ($limit < 0 || $page < 1) {
118 $id = isset($args[
'id']) ? intval($args[
'id']) :
null;
119 if ($id !==
null && !$this->dbHelper->doesIdExist(
"job",
"job_pk", $id)) {
125 return $this->
getAllResults($id, $status, $request, $response, $sort, $limit, $page, $apiVersion);
128 if (array_key_exists(self::UPLOAD_PARAM, $queryParams)) {
131 $status, $request, $response, $sort, $limit, $page, $apiVersion);
135 return $this->
getAllUserResults($userId, $status, $request, $response, $sort, $limit, $page, $apiVersion);
152 if ($apiVersion == ApiVersion::V2) {
153 $query = $request->getQueryParams();
154 $folder = $query[
"folderId"] ??
null;
155 $upload = $query[
"uploadId"] ??
null;
157 $folder = $request->hasHeader(
'folderId') ? $request->getHeaderLine(
'folderId') :
null;
158 $upload = $request->hasHeader(
'uploadId') ? $request->getHeaderLine(
'uploadId') :
null;
160 if (is_numeric($folder) && is_numeric($upload) && $folder > 0 && $upload > 0) {
162 if (empty($scanOptionsJSON)) {
166 $info = $uploadHelper->handleScheduleAnalysis($upload, $folder,
167 $scanOptionsJSON,
false, $apiVersion);
168 return $response->withJson($info->getArray(), $info->getCode());
171 "Folder id and upload id should be integers!");
186 $userId = $this->restHelper->getUserId();
187 $userName = $this->restHelper->getUserDao()->getUserName($userId);
190 $jobId = intval($args[
'id']);
191 if (! $this->dbHelper->doesIdExist(
"job",
"job_pk", $jobId)) {
196 $canDeleteJob = $this->restHelper->getJobDao()->hasActionPermissionsOnJob($jobId, $userId, $this->restHelper->getGroupId());
197 if (! $canDeleteJob) {
199 "You don't have permission to delete this job.");
202 $queueId = $args[
'queue'];
205 $JobQueue = $this->restHelper->getShowJobDao()->getJobInfo([$jobId])[$jobId][
"jobqueue"];
207 if (!array_key_exists($queueId, $JobQueue)) {
209 "Job queue " . $queueId .
" doesn't exist in Job " . $jobId);
213 $dependentJobs[] = $queueId;
215 foreach ($JobQueue as $job) {
216 if (in_array($queueId, $job[
"depends"])) {
217 $dependentJobs[] = $job[
"jq_pk"];
222 foreach ($dependentJobs as $job) {
223 $Msg =
"\"" . _(
"Killed by") .
" " . $userName .
"\"";
224 $command =
"kill $job $Msg";
228 "Failed to kill job $jobId");
231 $returnVal =
new Info(200,
"Job deleted successfully", InfoType::INFO);
232 return $response->withJson($returnVal->getArray(), $returnVal->getCode());
248 private function getAllUserResults($userId, $status, $request, $response, $sort, $limit, $page, $apiVersion)
250 list($jobs, $count) = $this->dbHelper->getUserJobs($userId, $status, $sort, $limit, $page);
252 foreach ($jobs as $job) {
254 if ($apiVersion == ApiVersion::V2) {
257 $finalJobs[] = $job->getArray($apiVersion);
259 return $response->withHeader(
"X-Total-Pages", $count)->withJson($finalJobs, 200);
275 private function getAllResults($id, $status, $request, $response, $sort, $limit, $page, $apiVersion)
277 list($jobs, $count) = $this->dbHelper->getJobs($id, $status, $sort, $limit, $page,
null);
279 foreach ($jobs as $job) {
281 if ($apiVersion == ApiVersion::V2) {
284 $finalJobs[] = $job->getArray($apiVersion);
287 $finalJobs = $finalJobs[0];
289 return $response->withHeader(
"X-Total-Pages", $count)->withJson($finalJobs, 200);
306 private function getFilteredResults($uploadId, $status, $request, $response, $sort, $limit, $page, $apiVersion)
308 if (! $this->dbHelper->doesIdExist(
"upload",
"upload_pk", $uploadId)) {
312 list($jobs, $count) = $this->dbHelper->getJobs(
null, $status, $sort, $limit, $page, $uploadId);
314 foreach ($jobs as $job) {
316 if ($apiVersion == ApiVersion::V2) {
319 $finalJobs[] = $job->getArray($apiVersion);
321 return $response->withHeader(
"X-Total-Pages", $count)->withJson($finalJobs, 200);
332 $job->getUploadId()));
344 $showJobDao = $this->restHelper->getShowJobDao();
345 $eta = $showJobDao->getEstimatedTime($jobId,
'', 0, $uploadId);
346 $eta = explode(
":", $eta);
347 if (count($eta) > 1) {
348 $eta = (intval($eta[0]) * 3600) + (intval($eta[1]) * 60) + intval($eta[2]);
364 public function getJobsHistory($request, $response, $args)
366 $query = $request->getQueryParams();
367 if (!array_key_exists(self::UPLOAD_PARAM, $query)) {
370 $upload_fk = intval($query[self::UPLOAD_PARAM]);
378 $dbManager = $this->dbHelper->getDbManager();
381 $query =
"SELECT job_pk FROM job WHERE job_upload_fk=$1;";
382 $statement = __METHOD__.
".getJobs";
383 $result = $dbManager->getRows($query, [$upload_fk], $statement);
386 $allJobPks = array_column($result,
'job_pk');
389 $showJobData = $this->getJobQueue($allJobPks);
392 $allJobsHistory = array();
393 foreach ($showJobData as $jobValObj) {
394 $finalJobqueue = array();
395 foreach ($jobValObj[
'job'][
'jobQueue'] as $jqVal) {
397 if ($jqVal[
'depends'][0] !=
null) {
398 $depends = $jqVal[
'depends'];
401 if (!empty($jqVal[
'download'])) {
403 "text" => $jqVal[
"download"],
408 $jobQueue =
new JobQueue($jqVal[
'jq_pk'], $jqVal[
'jq_type'],
409 $jqVal[
'jq_starttime'], $jqVal[
'jq_endtime'], $jqVal[
'jq_endtext'],
410 $jqVal[
'jq_itemsprocessed'], $jqVal[
'jq_log'], $depends,
411 $jqVal[
'itemsPerSec'], $jqVal[
'canDoActions'], $jqVal[
'isInProgress'],
412 $jqVal[
'isReady'], $download);
413 $finalJobqueue[] = $jobQueue->getArray();
415 $job =
new ShowJob($jobValObj[
'job'][
'jobId'],
416 $jobValObj[
'job'][
'jobName'], $finalJobqueue,
417 $jobValObj[
'upload'][
'uploadId']);
418 $allJobsHistory[] = $job->getArray();
420 return $response->withJson($allJobsHistory, 200);
429 private function getJobQueue($allJobPks)
431 $showJobsDao = $this->restHelper->getShowJobDao();
432 $jobsInfo = $showJobsDao->getJobInfo($allJobPks);
433 usort($jobsInfo, [$this,
"compareJobsInfo"]);
439 $ajaxShowJobs = $this->restHelper->getPlugin(
'ajaxShowJobs');
440 $showJobData = $ajaxShowJobs->getShowJobsForEachJob($jobsInfo,
true);
453 $jobQueue = $this->getJobQueue([$job->getId()]);
454 $finalJobqueue = array();
455 foreach ($jobQueue[0][
'job'][
'jobQueue'] as $jqVal) {
457 if ($jqVal[
'depends'][0] !=
null) {
458 $depends = $jqVal[
'depends'];
461 if (!empty($jqVal[
'download'])) {
463 "text" => $jqVal[
"download"],
468 $singleJobQueue =
new JobQueue($jqVal[
'jq_pk'], $jqVal[
'jq_type'],
469 $jqVal[
'jq_starttime'], $jqVal[
'jq_endtime'], $jqVal[
'jq_endtext'],
470 $jqVal[
'jq_itemsprocessed'], $jqVal[
'jq_log'], $depends,
471 $jqVal[
'itemsPerSec'], $jqVal[
'canDoActions'], $jqVal[
'isInProgress'],
472 $jqVal[
'isReady'], $download);
473 $finalJobqueue[] = $singleJobQueue->getArray();
475 $job->setJobQueue($finalJobqueue);
486 return $JobsInfo2[
"job"][
"job_pk"] - $JobsInfo1[
"job"][
"job_pk"];
497 public function getJobStatistics($request, $response, $args)
501 $statisticsPlugin = $this->restHelper->getPlugin(
'dashboard-statistics');
502 $res = $statisticsPlugin->CountAllJobs(
true);
503 return $response->withJson($res, 200);
515 public function getAllServerJobsStatus($request, $response, $args)
519 $allJobStatusPlugin = $this->restHelper->getPlugin(
'ajax_all_job_status');
520 $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
521 $res = $allJobStatusPlugin->handle($symfonyRequest);
522 return $response->withJson(json_decode($res->getContent(),
true), 200);
533 public function getSchedulerJobOptionsByOperation($request, $response, $args)
536 $operation = $args[
'operationName'];
538 $adminSchedulerPlugin = $this->restHelper->getPlugin(
'admin_scheduler');
540 if (!in_array($operation, array_keys($adminSchedulerPlugin->operation_array))) {
541 $allowedOperations = implode(
', ', array_keys($adminSchedulerPlugin->operation_array));
542 throw new HttpBadRequestException(
"Operation '$operation' not allowed." .
543 " Allowed operations are: $allowedOperations");
547 $schedulerPlugin = $this->restHelper->getPlugin(
'ajax_admin_scheduler');
548 $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
549 $symfonyRequest->request->set(
'operation', $operation);
550 $symfonyRequest->request->set(
'fromRest',
true);
551 $res = $schedulerPlugin->handle($symfonyRequest);
552 return $response->withJson($res, 200);
564 public function handleRunSchedulerOption($request, $response, $args)
568 $query = $request->getQueryParams();
570 $operation = $body[
'operation'];
572 $adminSchedulerPlugin = $this->restHelper->getPlugin(
'admin_scheduler');
574 if (!in_array($operation, array_keys($adminSchedulerPlugin->operation_array))) {
575 $allowedOperations = implode(
', ', array_keys($adminSchedulerPlugin->operation_array));
576 throw new HttpBadRequestException(
"Operation '$operation' not allowed." .
577 " Allowed operations are: $allowedOperations");
581 $schedulerPlugin = $this->restHelper->getPlugin(
'ajax_admin_scheduler');
582 $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
583 $symfonyRequest->request->set(
'operation', $operation);
584 $symfonyRequest->request->set(
'fromRest',
true);
585 $data = $schedulerPlugin->handle($symfonyRequest);
587 if ($operation ==
'status' || $operation ==
'verbose') {
588 if (!isset($query[
'job']) || !in_array($query[
'job'], $data[
'jobList'])) {
589 $allowedJobs = implode(
', ', $data[
'jobList']);
590 throw new HttpBadRequestException(
"Job '{$query['job']}' not " .
591 "allowed. Allowed jobs are: $allowedJobs");
593 if (($operation ==
'verbose') && (!isset($query[
'level']) || !in_array($query[
'level'], $data[
'verboseList']))) {
594 $allowedLevels = implode(
', ', $data[
'verboseList']);
595 throw new HttpBadRequestException(
"Level '{$query['level']}' not " .
596 "allowed. Allowed levels are: $allowedLevels");
598 } elseif ($operation ==
'priority' && (!isset($query[
'priority']) || !in_array($query[
'priority'], $data[
'priorityList']))) {
599 $allowedPriorities = implode(
', ', $data[
'priorityList']);
600 throw new HttpBadRequestException(
"Priority '{$query['priority']}' not " .
601 "allowed. Allowed priorities are: $allowedPriorities");
604 if ($operation ==
'status') {
605 $query[
'priority'] =
null;
606 $query[
'level'] =
null;
607 }
else if ($operation ==
'priority') {
608 $query[
'job'] =
null;
609 $query[
'level'] =
null;
610 }
else if ($operation ==
'verbose') {
611 $query[
'priority'] =
null;
613 $query[
'job'] =
null;
614 $query[
'priority'] =
null;
615 $query[
'level'] =
null;
618 $response_from_scheduler = $adminSchedulerPlugin->OperationSubmit(
619 $operation, array_search($query[
'job'], $data[
'jobList']),
620 $query[
'priority'], $query[
'level']);
621 $operation_text = $adminSchedulerPlugin->GetOperationText($operation);
625 if (!empty($adminSchedulerPlugin->error_info)) {
627 $status_msg .=
"$operation_text $text.";
628 throw new HttpInternalServerErrorException($status_msg . $report);
630 $text = _(
"successfully");
631 $status_msg .=
"$operation_text $text.";
632 if (! empty($response_from_scheduler)) {
633 $report .= $response_from_scheduler;
636 $info =
new Info(200, $status_msg. $report, InfoType::INFO);
637 return $response->withJson($info->getArray(), $info->getCode());
Controller for Job model.
createJob($request, $response, $args)
getJobs($request, $response, $args)
getFilteredResults($uploadId, $status, $request, $response, $sort, $limit, $page, $apiVersion)
getAllResults($id, $status, $request, $response, $sort, $limit, $page, $apiVersion)
deleteJob($request, $response, $args)
compareJobsInfo($JobsInfo1, $JobsInfo2)
Sort compare function to order $JobsInfo by job_pk.
getAllUserResults($userId, $status, $request, $response, $sort, $limit, $page, $apiVersion)
addJobQueue(&$job, $request=null)
getUploadEtaInSeconds($jobId, $uploadId)
getAllJobs($request, $response, $args)
static buildDownloadPath($request, $jobId)
Base controller for REST calls.
getParsedBody(ServerRequestInterface $request)
Parse request body as JSON and return associative PHP array.
Override Slim response for withJson function.
Handle new file uploads from Slim framework and move to FOSSology.
static getVersion(ServerRequestInterface $request)
Different type of infos provided by REST.
Info model to contain general error and return values.
Model class to hold JobQueue info.
Model class to hold ShowJob info.
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.