14 namespace Fossology\UI\Api\Controllers;
29 use Psr\Http\Message\ServerRequestInterface;
30 use Slim\Psr7\Request;
74 if ($request->hasHeader(
'limit')) {
75 $limit = $request->getHeaderLine(
'limit');
76 $page = $request->getHeaderLine(
'page');
80 if ((! is_numeric($limit) || $limit < 0) ||
81 (! is_numeric($page) || $page < 1)) {
83 "Limit and page cannot be smaller than 1 and has to be numeric!");
98 public function getJobs($request, $response, $args)
100 $query = $request->getQueryParams();
101 $userId = $this->restHelper->getUserId();
104 if ($request->hasHeader(
'limit')) {
105 $limit = $request->getHeaderLine(
'limit');
106 $page = $request->getHeaderLine(
'page');
110 if ((! is_numeric($limit) || $limit < 0) ||
111 (! is_numeric($page) || $page < 1)) {
113 "Limit and page cannot be smaller than 1 and has to be numeric!");
118 if (isset($args[
'id'])) {
119 $id = intval($args[
'id']);
120 if (! $this->dbHelper->doesIdExist(
"job",
"job_pk", $id)) {
130 if (array_key_exists(self::UPLOAD_PARAM, $query)) {
133 $response, $limit, $page);
151 $folder = $request->getHeaderLine(
"folderId");
152 $upload = $request->getHeaderLine(
"uploadId");
153 if (is_numeric($folder) && is_numeric($upload) && $folder > 0 && $upload > 0) {
155 if (empty($scanOptionsJSON)) {
159 $info = $uploadHelper->handleScheduleAnalysis($upload, $folder,
161 return $response->withJson($info->getArray(), $info->getCode());
164 "Folder id and upload id should be integers!");
179 $userId = $this->restHelper->getUserId();
180 $userName = $this->restHelper->getUserDao()->getUserName($userId);
183 $jobId = intval($args[
'id']);
184 if (! $this->dbHelper->doesIdExist(
"job",
"job_pk", $jobId)) {
189 $canDeleteJob = $this->restHelper->getJobDao()->hasActionPermissionsOnJob($jobId, $userId, $this->restHelper->getGroupId());
190 if (! $canDeleteJob) {
192 "You don't have permission to delete this job.");
195 $queueId = $args[
'queue'];
198 $JobQueue = $this->restHelper->getShowJobDao()->getJobInfo([$jobId])[$jobId][
"jobqueue"];
200 if (!array_key_exists($queueId, $JobQueue)) {
202 "Job queue " . $queueId .
" doesn't exist in Job " . $jobId);
206 $dependentJobs[] = $queueId;
208 foreach ($JobQueue as $job) {
209 if (in_array($queueId, $job[
"depends"])) {
210 $dependentJobs[] = $job[
"jq_pk"];
215 foreach ($dependentJobs as $job) {
216 $Msg =
"\"" . _(
"Killed by") .
" " . $userName .
"\"";
217 $command =
"kill $job $Msg";
221 "Failed to kill job $jobId");
224 $returnVal =
new Info(200,
"Job deleted successfully", InfoType::INFO);
225 return $response->withJson($returnVal->getArray(), $returnVal->getCode());
240 list($jobs, $count) = $this->dbHelper->getUserJobs($id, $uid, $limit, $page);
242 foreach ($jobs as $job) {
244 $finalJobs[] = $job->getArray();
247 $finalJobs = $finalJobs[0];
249 return $response->withHeader(
"X-Total-Pages", $count)->withJson($finalJobs, 200);
263 list($jobs, $count) = $this->dbHelper->getJobs($id, $limit, $page);
265 foreach ($jobs as $job) {
267 $finalJobs[] = $job->getArray();
270 $finalJobs = $finalJobs[0];
272 return $response->withHeader(
"X-Total-Pages", $count)->withJson($finalJobs, 200);
287 if (! $this->dbHelper->doesIdExist(
"upload",
"upload_pk", $uploadId)) {
291 list($jobs, $count) = $this->dbHelper->getJobs(
null, $limit, $page, $uploadId);
293 foreach ($jobs as $job) {
295 $finalJobs[] = $job->getArray();
297 return $response->withHeader(
"X-Total-Pages", $count)->withJson($finalJobs, 200);
307 $jobDao = $this->restHelper->getJobDao();
312 if (empty($job->getUploadId())) {
313 $sql =
"SELECT jq_pk, jq_end_bits from jobqueue WHERE jq_job_fk = $1;";
314 $statement = __METHOD__ .
".getJqpk";
315 $rows = $this->dbHelper->getDbManager()->getRows($sql, [$job->getId()],
317 if (count($rows) > 0) {
318 $jobqueue[$rows[0][
'jq_pk']] = $rows[0][
'jq_end_bits'];
321 $jobqueue = $jobDao->getAllJobStatus($job->getUploadId(),
322 $job->getUserId(), $job->getGroupId());
326 $job->getUploadId()));
328 $job->setStatus($this->
getJobStatus(array_keys($jobqueue)));
340 $showJobDao = $this->restHelper->getShowJobDao();
341 $eta = $showJobDao->getEstimatedTime($jobId,
'', 0, $uploadId);
342 $eta = explode(
":", $eta);
343 if (count($eta) > 1) {
344 $eta = (intval($eta[0]) * 3600) + (intval($eta[1]) * 60) + intval($eta[2]);
359 $showJobDao = $this->restHelper->getShowJobDao();
362 foreach ($jobqueue as $jobId) {
363 $jobInfo = $showJobDao->getDataForASingleJob($jobId);
364 $endtext = $jobInfo[
'jq_endtext'];
375 if (empty($jobInfo[
'jq_endtime'])) {
383 $jobStatusString =
"";
384 if ($jobStatus & self::JOB_FAILED) {
386 $jobStatusString =
"Failed";
387 }
else if ($jobStatus & self::JOB_STARTED) {
389 $jobStatusString =
"Processing";
390 }
else if ($jobStatus & self::JOB_QUEUED) {
391 $jobStatusString =
"Queued";
394 $jobStatusString =
"Completed";
396 return $jobStatusString;
408 public function getJobsHistory($request, $response, $args)
410 $query = $request->getQueryParams();
411 if (!array_key_exists(self::UPLOAD_PARAM, $query)) {
414 $upload_fk = intval($query[self::UPLOAD_PARAM]);
422 $dbManager = $this->dbHelper->getDbManager();
425 $query =
"SELECT job_pk FROM job WHERE job_upload_fk=$1;";
426 $statement = __METHOD__.
".getJobs";
427 $result = $dbManager->getRows($query, [$upload_fk], $statement);
430 $allJobPks = array_column($result,
'job_pk');
436 $showJobsDao = $this->container->get(
'dao.show_jobs');
438 $jobsInfo = $showJobsDao->getJobInfo($allJobPks);
439 usort($jobsInfo, [$this,
"compareJobsInfo"]);
445 $ajaxShowJobs = $this->restHelper->getPlugin(
'ajaxShowJobs');
448 $showJobData = $ajaxShowJobs->getShowJobsForEachJob($jobsInfo);
451 $allJobsHistory = array();
452 foreach ($showJobData as $jobValObj) {
453 $finalJobqueue = array();
454 foreach ($jobValObj[
'job'][
'jobQueue'] as $jqVal) {
456 if ($jqVal[
'depends'][0] !=
null) {
457 $depends = $jqVal[
'depends'];
460 if (!empty($jqVal[
'download'])) {
462 "text" => $jqVal[
"download"],
467 $jobQueue =
new JobQueue($jqVal[
'jq_pk'], $jqVal[
'jq_type'],
468 $jqVal[
'jq_starttime'], $jqVal[
'jq_endtime'], $jqVal[
'jq_endtext'],
469 $jqVal[
'jq_itemsprocessed'], $jqVal[
'jq_log'], $depends,
470 $jqVal[
'itemsPerSec'], $jqVal[
'canDoActions'], $jqVal[
'isInProgress'],
471 $jqVal[
'isReady'], $download);
472 $finalJobqueue[] = $jobQueue->getArray();
474 $job =
new ShowJob($jobValObj[
'job'][
'jobId'],
475 $jobValObj[
'job'][
'jobName'], $finalJobqueue,
476 $jobValObj[
'upload'][
'uploadId']);
477 $allJobsHistory[] = $job->getArray();
479 return $response->withJson($allJobsHistory, 200);
490 return $JobsInfo2[
"job"][
"job_pk"] - $JobsInfo1[
"job"][
"job_pk"];
501 public function getJobStatistics($request, $response, $args)
505 $statisticsPlugin = $this->restHelper->getPlugin(
'dashboard-statistics');
506 $res = $statisticsPlugin->CountAllJobs(
true);
507 return $response->withJson($res, 200);
519 public function getAllServerJobsStatus($request, $response, $args)
523 $allJobStatusPlugin = $this->restHelper->getPlugin(
'ajax_all_job_status');
524 $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
525 $res = $allJobStatusPlugin->handle($symfonyRequest);
526 return $response->withJson(json_decode($res->getContent(),
true), 200);
537 public function getSchedulerJobOptionsByOperation($request, $response, $args)
540 $operation = $args[
'operationName'];
542 $adminSchedulerPlugin = $this->restHelper->getPlugin(
'admin_scheduler');
544 if (!in_array($operation, array_keys($adminSchedulerPlugin->operation_array))) {
545 $allowedOperations = implode(
', ', array_keys($adminSchedulerPlugin->operation_array));
546 throw new HttpBadRequestException(
"Operation '$operation' not allowed." .
547 " Allowed operations are: $allowedOperations");
551 $schedulerPlugin = $this->restHelper->getPlugin(
'ajax_admin_scheduler');
552 $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
553 $symfonyRequest->request->set(
'operation', $operation);
554 $symfonyRequest->request->set(
'fromRest',
true);
555 $res = $schedulerPlugin->handle($symfonyRequest);
556 return $response->withJson($res, 200);
568 public function handleRunSchedulerOption($request, $response, $args)
572 $query = $request->getQueryParams();
574 $operation = $body[
'operation'];
576 $adminSchedulerPlugin = $this->restHelper->getPlugin(
'admin_scheduler');
578 if (!in_array($operation, array_keys($adminSchedulerPlugin->operation_array))) {
579 $allowedOperations = implode(
', ', array_keys($adminSchedulerPlugin->operation_array));
580 throw new HttpBadRequestException(
"Operation '$operation' not allowed." .
581 " Allowed operations are: $allowedOperations");
585 $schedulerPlugin = $this->restHelper->getPlugin(
'ajax_admin_scheduler');
586 $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
587 $symfonyRequest->request->set(
'operation', $operation);
588 $symfonyRequest->request->set(
'fromRest',
true);
589 $data = $schedulerPlugin->handle($symfonyRequest);
591 if ($operation ==
'status' || $operation ==
'verbose') {
592 if (!isset($query[
'job']) || !in_array($query[
'job'], $data[
'jobList'])) {
593 $allowedJobs = implode(
', ', $data[
'jobList']);
594 throw new HttpBadRequestException(
"Job '{$query['job']}' not " .
595 "allowed. Allowed jobs are: $allowedJobs");
597 if (($operation ==
'verbose') && (!isset($query[
'level']) || !in_array($query[
'level'], $data[
'verboseList']))) {
598 $allowedLevels = implode(
', ', $data[
'verboseList']);
599 throw new HttpBadRequestException(
"Level '{$query['level']}' not " .
600 "allowed. Allowed levels are: $allowedLevels");
602 } elseif ($operation ==
'priority' && (!isset($query[
'priority']) || !in_array($query[
'priority'], $data[
'priorityList']))) {
603 $allowedPriorities = implode(
', ', $data[
'priorityList']);
604 throw new HttpBadRequestException(
"Priority '{$query['priority']}' not " .
605 "allowed. Allowed priorities are: $allowedPriorities");
608 if ($operation ==
'status') {
609 $query[
'priority'] =
null;
610 $query[
'level'] =
null;
611 }
else if ($operation ==
'priority') {
612 $query[
'job'] =
null;
613 $query[
'level'] =
null;
614 }
else if ($operation ==
'verbose') {
615 $query[
'priority'] =
null;
617 $query[
'job'] =
null;
618 $query[
'priority'] =
null;
619 $query[
'level'] =
null;
622 $response_from_scheduler = $adminSchedulerPlugin->OperationSubmit(
623 $operation, array_search($query[
'job'], $data[
'jobList']),
624 $query[
'priority'], $query[
'level']);
625 $operation_text = $adminSchedulerPlugin->GetOperationText($operation);
629 if (!empty($adminSchedulerPlugin->error_info)) {
631 $status_msg .=
"$operation_text $text.";
632 throw new HttpInternalServerErrorException($status_msg . $report);
634 $text = _(
"successfully");
635 $status_msg .=
"$operation_text $text.";
636 if (! empty($response_from_scheduler)) {
637 $report .= $response_from_scheduler;
640 $info =
new Info(200, $status_msg. $report, InfoType::INFO);
641 return $response->withJson($info->getArray(), $info->getCode());
Controller for Job model.
getAllResults($id, $response, $limit, $page)
createJob($request, $response, $args)
getJobs($request, $response, $args)
getFilteredResults($uploadId, $response, $limit, $page)
deleteJob($request, $response, $args)
getAllUserResults($id, $uid, $response, $limit, $page)
compareJobsInfo($JobsInfo1, $JobsInfo2)
Sort compare function to order $JobsInfo by job_pk.
updateEtaAndStatus(&$job)
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.
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.