9 namespace Fossology\Lib\Dao;
24 private $maxJobsPerPage = 10;
26 private $nhours = 336;
31 $this->uploadDao = $uploadDao;
32 $this->logger =
new Logger(self::class);
42 return '{' . implode(
',', array_map(
'intval', $ints)) .
'}';
58 $upload_pks = $this->uploadDao->filterAccessibleUploads($upload_pks,
Auth::getGroupId());
60 if (empty($upload_pks)) {
64 $upload_pks = array_values($upload_pks);
68 $countRow = $this->
dbManager->getSingleRow(
69 "SELECT count(*) AS cnt FROM job WHERE job_upload_fk = ANY($1)",
71 __METHOD__ .
".countJobs"
73 $totalJobCount = intval($countRow[
'cnt']);
74 $totalPages = floor($totalJobCount / $this->maxJobsPerPage);
77 $offset = empty($page) ? 0 : $page * $this->maxJobsPerPage;
78 $statementName = __METHOD__ .
".paginatedJobs";
80 "SELECT job_pk FROM job WHERE job_upload_fk = ANY($1) " .
81 "ORDER BY job_pk DESC LIMIT $2 OFFSET $3");
82 $result = $this->
dbManager->execute($statementName,
83 array($uploadList, $this->maxJobsPerPage, $offset));
85 while ($row = $this->
dbManager->fetchArray($result)) {
86 $jobArray[] = $row[
'job_pk'];
90 return array($jobArray, $totalPages);
100 $statementName = __METHOD__.
"forjob_name";
103 "SELECT job_name FROM job WHERE job_upload_fk= $1 ORDER BY job_pk ASC",
107 return (empty($row[
'job_name']) ? $uploadId : $row[
'job_name']);
118 public function myJobs($allusers, $page = 0)
121 $offset = empty($page) ? 0 : ($page * $this->maxJobsPerPage) - 1;
123 $allusers_str = ($allusers == 0) ?
"job_user_fk='" .
Auth::getUserId() .
126 $statementName = __METHOD__ .
"." . $allusers_str;
127 $this->
dbManager->prepare($statementName,
128 "SELECT job_pk, job_upload_fk FROM job " .
"WHERE $allusers_str " .
129 "job_queued >= (now() - interval '" . $this->nhours .
" hours') " .
130 "ORDER BY job_queued DESC");
131 $result = $this->
dbManager->execute($statementName);
134 $uploadIds = array();
135 while ($row = $this->
dbManager->fetchArray($result)) {
137 if (!empty($row[
'job_upload_fk'])) {
138 $uploadIds[] = intval($row[
'job_upload_fk']);
143 $accessibleUploads = $this->uploadDao->filterAccessibleUploads(array_unique($uploadIds),
Auth::getGroupId());
145 foreach ($rows as $row) {
146 if (!empty($row[
'job_upload_fk'])) {
147 if (!in_array(intval($row[
'job_upload_fk']), $accessibleUploads)) {
151 $jobArray[] = $row[
'job_pk'];
155 $totalPages = floor(count($jobArray) / $this->maxJobsPerPage);
158 $pageJobs = array_slice($jobArray, $offset, $this->maxJobsPerPage);
160 return array($pageJobs, $totalPages);
190 if (empty($job_pks)) {
201 $this->
attachUploadData($jobData, $uploadRows, $uploadtreeRows, $deletedMap);
205 foreach ($jobData as $job_pk => $data) {
206 if (!isset($data[
'jobqueue'])) {
207 unset($jobData[$job_pk]);
224 "SELECT * FROM job WHERE job_pk = ANY($1::int[])");
225 $result = $this->
dbManager->execute($stmt, array($jobPkArray));
226 foreach ($this->
dbManager->fetchAll($result) as $row) {
227 $jobData[$row[
'job_pk']][
'job'] = $row;
242 "SELECT u.*, j.job_pk, j.job_upload_fk AS jb_upload_fk
244 LEFT JOIN upload u ON u.upload_pk = j.job_upload_fk
245 WHERE j.job_pk = ANY($1::int[])
246 AND j.job_upload_fk IS NOT NULL");
247 $result = $this->
dbManager->execute($stmt, array($jobPkArray));
249 $uploadRows = array();
250 $deletedMap = array();
251 foreach ($this->
dbManager->fetchAll($result) as $row) {
252 $jobPk = $row[
'job_pk'];
253 $uploadFk = intval($row[
'jb_upload_fk']);
254 unset($row[
'job_pk'], $row[
'jb_upload_fk']);
256 if (!empty($row[
'upload_pk'])) {
257 $uploadRows[$row[
'upload_pk']] = $row;
259 $deletedMap[$jobPk] = $uploadFk;
263 return array($uploadRows, $deletedMap);
273 $uploadsByTablename = array();
274 foreach ($uploadRows as $rec) {
275 $tablename = $rec[
'uploadtree_tablename'];
276 $uploadsByTablename[$tablename][] = intval($rec[
'upload_pk']);
280 foreach ($uploadsByTablename as $tablename => $pks) {
281 if (!$this->
dbManager->existsTable($tablename)) {
285 $stmt = __METHOD__ .
".$tablename";
287 "SELECT * FROM $tablename WHERE upload_fk = ANY($1::int[]) AND parent IS NULL");
288 $result = $this->
dbManager->execute($stmt, array($pkArray));
289 foreach ($this->
dbManager->fetchAll($result) as $row) {
290 $rows[$row[
'upload_fk']] = $row;
307 foreach ($jobData as $job_pk => &$data) {
308 $upload_pk = $data[
'job'][
'job_upload_fk'] ??
null;
309 if (empty($upload_pk)) {
312 if (isset($uploadRows[$upload_pk])) {
313 $data[
'upload'] = $uploadRows[$upload_pk];
314 $data[
'uploadtree'] = $uploadtreeRows[$upload_pk] ??
null;
315 } elseif (isset($deletedMap[$job_pk])) {
316 $uploadFk = $deletedMap[$job_pk];
318 $data[
'upload'] = array(
319 'upload_filename' =>
"Deleted Upload: " . $uploadFk .
"(" . $jobName .
")",
320 'upload_pk' => $uploadFk,
336 "SELECT jq.*, jd.jdep_jq_depends_fk
338 LEFT OUTER JOIN jobdepends jd ON jq.jq_pk = jd.jdep_jq_fk
339 WHERE jq.jq_job_fk = ANY($1::int[])
340 ORDER BY jq.jq_pk ASC");
341 $result = $this->
dbManager->execute($stmt, array($jobPkArray));
343 foreach ($this->
dbManager->fetchAll($result) as $jobQueueRec) {
344 $job_pk = $jobQueueRec[
'jq_job_fk'];
345 $jq_pk = $jobQueueRec[
'jq_pk'];
347 if (!isset($jobData[$job_pk])) {
351 if (isset($jobData[$job_pk][
'jobqueue'][$jq_pk])) {
352 $jobData[$job_pk][
'jobqueue'][$jq_pk][
'depends'][] = $jobQueueRec[
'jdep_jq_depends_fk'];
354 $jobQueueRec[
'depends'] = array($jobQueueRec[
'jdep_jq_depends_fk']);
355 $jobData[$job_pk][
'jobqueue'][$jq_pk] = $jobQueueRec;
369 return ($numSecs > 0) ? $itemsprocessed/$numSecs : 0;
380 public function getEstimatedTime($job_pk, $jq_Type=
'', $filesPerSec=0, $uploadId=0, $timeInSec=0)
382 if (!empty($uploadId)) {
383 $itemCount = $this->
dbManager->getSingleRow(
384 "SELECT jq_itemsprocessed FROM jobqueue INNER JOIN job ON jq_job_fk=job_pk "
385 .
" WHERE jq_type LIKE 'ununpack' AND jq_end_bits ='1' AND job_upload_fk=$1",
387 __METHOD__.
'.ununpack_might_be_in_other_job'
390 $itemCount = $this->
dbManager->getSingleRow(
391 "SELECT jq_itemsprocessed FROM jobqueue WHERE jq_type LIKE 'ununpack' AND jq_end_bits ='1' AND jq_job_fk =$1",
393 __METHOD__.
'.ununpack_must_be_in_this_job'
397 if (!empty($itemCount[
'jq_itemsprocessed']) && $jq_Type !==
'decider') {
399 $selectCol =
"jq_type, jq_endtime, jq_starttime, jq_itemsprocessed";
400 if (empty($jq_Type)) {
401 $removeType =
"jq_type NOT LIKE 'ununpack' AND jq_type NOT LIKE 'reportgen' AND jq_type NOT LIKE 'decider' AND jq_type NOT LIKE 'softwareHeritage' AND";
403 $statementName = __METHOD__.
"$selectCol.$removeType";
404 $this->
dbManager->prepare($statementName,
405 "SELECT $selectCol FROM jobqueue WHERE $removeType jq_job_fk =$1 ORDER BY jq_type DESC");
407 $statementName = __METHOD__.
"$selectCol.$jq_Type";
408 $this->
dbManager->prepare($statementName,
409 "SELECT $selectCol FROM jobqueue WHERE jq_type LIKE '$jq_Type' AND jq_job_fk =$1");
411 $result = $this->
dbManager->execute($statementName, array($job_pk));
412 $estimatedArray = array();
414 while ($row = $this->
dbManager->fetchArray($result)) {
415 $timeOfCompletion = 0;
416 if (empty($row[
'jq_endtime']) && !empty($row[
'jq_starttime'])) {
417 if (empty($filesPerSec)) {
418 $burnTime = time() - strtotime($row[
'jq_starttime']);
422 if (!empty($filesPerSec)) {
423 $timeOfCompletion = ($itemCount[
'jq_itemsprocessed'] - $row[
'jq_itemsprocessed']) / $filesPerSec;
425 $estimatedArray[] = $timeOfCompletion;
428 if (empty($estimatedArray)) {
431 $estimatedTime = round(
max($estimatedArray));
432 if (!empty($timeInSec)) {
433 return intval(!empty($estimatedTime) ? $estimatedTime : 0);
435 return intval($estimatedTime/3600).gmdate(
":i:s", $estimatedTime);
447 $statementName = __METHOD__.
"getDataForASingleJob";
448 $this->
dbManager->prepare($statementName,
449 "SELECT *, jq_endtime-jq_starttime as elapsed FROM jobqueue LEFT JOIN job ON job.job_pk = jobqueue.jq_job_fk WHERE jobqueue.jq_pk =$1");
450 $result = $this->
dbManager->execute($statementName, array($jq_pk));
451 $row = $this->
dbManager->fetchArray($result);
462 $statementName = __METHOD__.
"forjq_pk";
464 "SELECT jq_end_bits FROM jobqueue WHERE jq_pk = $1",
468 if ($row[
'jq_end_bits'] == 1 || $row[
'jq_end_bits'] == 2) {
482 $statementName = __METHOD__.
"forjqTypeAndjobId";
484 "SELECT jq_itemsprocessed, job.job_upload_fk FROM jobqueue JOIN job ON jobqueue.jq_job_fk = job.job_pk WHERE jq_type = $1 AND jq_end_bits = 0 AND jq_job_fk IN (SELECT job_pk FROM job WHERE job_upload_fk = (SELECT job_upload_fk FROM job WHERE job_pk = $2 LIMIT 1)) LIMIT 1",
485 array($jqType, $jobId),
488 if (!empty($row[
'jq_itemsprocessed'])) {
489 return array($row[
'jq_itemsprocessed'], $row[
'job_upload_fk']);
501 $sql =
"SELECT jq_type AS job, jq_job_fk, job_upload_fk AS upload_fk, " .
502 "CASE WHEN (jq_endtext IS NULL AND jq_end_bits = 0) THEN 'pending' " .
503 "WHEN (jq_endtext = ANY('{Started,Restarted,Paused}')) THEN 'running' " .
504 "ELSE '' END AS status " .
505 "FROM jobqueue INNER JOIN job " .
506 "ON jq_job_fk = job_pk " .
507 "AND job_queued >= (now() - interval '" . $this->nhours .
" hours') " .
508 "WHERE jq_endtime IS NULL;";
509 $statement = __METHOD__ .
".getAllUnFinishedJobs";
510 return $this->
dbManager->getRows($sql, [], $statement);
519 $statementName = __METHOD__ .
"forJobIdCheck";
521 "SELECT 1 FROM reportgen WHERE job_fk = $1",
Contains the constants and helpers for authentication of user.
static getUserId()
Get the current user's id.
static getGroupId()
Get the current user's group id.
fetchUploadData($jobPkArray)
Fetch both existing uploads and jobs referencing deleted uploads.
uploads2Jobs($upload_pks, $page=0)
Find all the jobs for a given set of uploads.
getJobName($uploadId)
Return job name. Used for deleted jobs.
toPgIntArray(array $ints)
Convert an array of integers to a Postgres array literal string.
isJobIdPresentInReportGen($jobId)
fetchJobRecords($jobPkArray)
Fetch job records for the given job_pk's.
getItemsProcessedForDecider($jqType, $jobId)
fetchUploadtreeRoots($uploadRows)
Fetch root records from uploadtree tables for the given upload records.
myJobs($allusers, $page=0)
Find all of my jobs submitted within the last n hours.
attachJobQueueData(&$jobData, $jobPkArray)
Attach jobqueue data to job data for the given job_pk's.
getEstimatedTime($job_pk, $jq_Type='', $filesPerSec=0, $uploadId=0, $timeInSec=0)
Returns Estimated time using jobid.
getDataForASingleJob($jq_pk)
Return total Job data with time elapsed.
getNumItemsPerSec($itemsprocessed, $numSecs)
Returns Number of files/items processed per sec.
getJobInfo($job_pks)
Get job queue data from db.
attachUploadData(&$jobData, $uploadRows, $uploadtreeRows, $deletedMap)
Attach upload and uploadtree data to job data. Also handles deleted uploads using the pre-fetched del...
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
fo_dbManager * dbManager
fo_dbManager object
list_t type structure used to keep various lists. (e.g. there are multiple lists).