9 namespace Fossology\Lib\Dao;
24 private $maxJobsPerPage = 10;
26 private $nhours = 672;
31 $this->uploadDao = $uploadDao;
32 $this->logger =
new Logger(self::class);
46 $jobCount = count($upload_pks);
52 $offset = empty($page) ? 0 : $page * $this->maxJobsPerPage;
53 $totalPages = floor($jobCount / $this->maxJobsPerPage);
56 $lastOffset = ($jobCount < $this->maxJobsPerPage) ? $offset+$jobCount : $this->maxJobsPerPage;
57 $statementName = __METHOD__.
"upload_pkforjob";
58 $this->
dbManager->prepare($statementName,
"SELECT job_pk FROM job WHERE job_upload_fk=$1 ORDER BY job_pk ASC");
59 for (; $offset < $lastOffset; $offset++) {
60 $upload_pk = $upload_pks[$offset];
62 $result = $this->
dbManager->execute($statementName, array($upload_pk));
63 while ($row = $this->
dbManager->fetchArray($result)) {
64 $jobArray[] = $row[
'job_pk'];
68 return array($jobArray, $totalPages);
78 $statementName = __METHOD__.
"forjob_name";
81 "SELECT job_name FROM job WHERE job_upload_fk= $1 ORDER BY job_pk ASC",
85 return (empty($row[
'job_name']) ? $uploadId : $row[
'job_name']);
96 public function myJobs($allusers, $page = 0)
99 $offset = empty($page) ? 0 : ($page * $this->maxJobsPerPage) - 1;
101 $allusers_str = ($allusers == 0) ?
"job_user_fk='" .
Auth::getUserId() .
104 $statementName = __METHOD__ .
".countJobs." . $allusers_str;
105 $sql =
"SELECT count(*) AS cnt FROM job WHERE $allusers_str " .
106 "job_queued >= (now() - interval '" . $this->nhours .
" hours');";
108 $countJobs = $this->
dbManager->getSingleRow($sql, [], $statementName)[
'cnt'];
109 $totalPages = floor($countJobs / $this->maxJobsPerPage);
111 $statementName = __METHOD__ .
"." . $allusers_str;
112 $this->
dbManager->prepare($statementName,
113 "SELECT job_pk, job_upload_fk FROM job " .
"WHERE $allusers_str " .
114 "job_queued >= (now() - interval '" . $this->nhours .
" hours') " .
115 "ORDER BY job_queued DESC OFFSET $1 LIMIT " . $this->maxJobsPerPage);
116 $result = $this->
dbManager->execute($statementName, [$offset]);
117 while ($row = $this->
dbManager->fetchArray($result)) {
118 if (! empty($row[
'job_upload_fk'])) {
119 $uploadIsAccessible = $this->uploadDao->isAccessible(
121 if (! $uploadIsAccessible) {
125 $jobArray[] = $row[
'job_pk'];
129 return array($jobArray, $totalPages);
161 foreach ($job_pks as $job_pk) {
163 $statementName = __METHOD__ .
"JobRec";
164 $jobRec = $this->
dbManager->getSingleRow(
165 "SELECT * FROM job WHERE job_pk= $1", array($job_pk),
167 $jobData[$job_pk][
"job"] = $jobRec;
168 if (! empty($jobRec[
"job_upload_fk"])) {
169 $upload_pk = $jobRec[
"job_upload_fk"];
171 $statementName = __METHOD__ .
"UploadRec";
172 $uploadRec = $this->
dbManager->getSingleRow(
173 "SELECT * FROM upload WHERE upload_pk= $1", array($upload_pk),
175 if (! empty($uploadRec)) {
176 $jobData[$job_pk][
"upload"] = $uploadRec;
178 $uploadtree_tablename = $uploadRec[
"uploadtree_tablename"];
179 $statementName = __METHOD__ .
"uploadtreeRec";
180 $uploadtreeRec = $this->
dbManager->getSingleRow(
181 "SELECT * FROM $uploadtree_tablename where upload_fk = $1 and parent is null",
182 array($upload_pk), $statementName);
183 $jobData[$job_pk][
"uploadtree"] = $uploadtreeRec;
185 $statementName = __METHOD__ .
"uploadRecord";
186 $uploadRec = $this->
dbManager->getSingleRow(
187 "SELECT * FROM upload right join job on upload_pk = job_upload_fk where job_upload_fk = $1",
188 array($upload_pk), $statementName);
193 $jobName = $this->
getJobName($uploadRec[
"job_upload_fk"]);
194 $uploadRec[
"upload_filename"] =
"Deleted Upload: " .
195 $uploadRec[
"job_upload_fk"] .
"(" . $jobName .
")";
196 $uploadRec[
"upload_pk"] = $uploadRec[
"job_upload_fk"];
197 $jobData[$job_pk][
"upload"] = $uploadRec;
201 $statementName = __METHOD__ .
"job_pkforjob";
202 $this->
dbManager->prepare($statementName,
203 "SELECT jq.*,jd.jdep_jq_depends_fk FROM jobqueue jq LEFT OUTER JOIN jobdepends jd ON jq.jq_pk=jd.jdep_jq_fk WHERE jq.jq_job_fk=$1 ORDER BY jq_pk ASC");
204 $result = $this->
dbManager->execute($statementName, array(
207 $rows = $this->
dbManager->fetchAll($result);
208 if (! empty($rows)) {
209 foreach ($rows as $jobQueueRec) {
210 $jq_pk = $jobQueueRec[
"jq_pk"];
211 if (array_key_exists($job_pk, $jobData) &&
212 array_key_exists(
'jobqueue', $jobData[$job_pk]) &&
213 array_key_exists($jq_pk, $jobData[$job_pk][
'jobqueue'])) {
214 $jobData[$job_pk][
'jobqueue'][$jq_pk][
"depends"][] = $jobQueueRec[
"jdep_jq_depends_fk"];
216 $jobQueueRec[
"depends"] = array($jobQueueRec[
"jdep_jq_depends_fk"]);
217 $jobData[$job_pk][
'jobqueue'][$jq_pk] = $jobQueueRec;
221 unset($jobData[$job_pk]);
236 return ($numSecs > 0) ? $itemsprocessed/$numSecs : 0;
247 public function getEstimatedTime($job_pk, $jq_Type=
'', $filesPerSec=0, $uploadId=0, $timeInSec=0)
249 if (!empty($uploadId)) {
250 $itemCount = $this->
dbManager->getSingleRow(
251 "SELECT jq_itemsprocessed FROM jobqueue INNER JOIN job ON jq_job_fk=job_pk "
252 .
" WHERE jq_type LIKE 'ununpack' AND jq_end_bits ='1' AND job_upload_fk=$1",
254 __METHOD__.
'.ununpack_might_be_in_other_job'
257 $itemCount = $this->
dbManager->getSingleRow(
258 "SELECT jq_itemsprocessed FROM jobqueue WHERE jq_type LIKE 'ununpack' AND jq_end_bits ='1' AND jq_job_fk =$1",
260 __METHOD__.
'.ununpack_must_be_in_this_job'
264 if (!empty($itemCount[
'jq_itemsprocessed']) && $jq_Type !==
'decider') {
266 $selectCol =
"jq_type, jq_endtime, jq_starttime, jq_itemsprocessed";
267 if (empty($jq_Type)) {
268 $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";
270 $statementName = __METHOD__.
"$selectCol.$removeType";
271 $this->
dbManager->prepare($statementName,
272 "SELECT $selectCol FROM jobqueue WHERE $removeType jq_job_fk =$1 ORDER BY jq_type DESC");
274 $statementName = __METHOD__.
"$selectCol.$jq_Type";
275 $this->
dbManager->prepare($statementName,
276 "SELECT $selectCol FROM jobqueue WHERE jq_type LIKE '$jq_Type' AND jq_job_fk =$1");
278 $result = $this->
dbManager->execute($statementName, array($job_pk));
279 $estimatedArray = array();
281 while ($row = $this->
dbManager->fetchArray($result)) {
282 $timeOfCompletion = 0;
283 if (empty($row[
'jq_endtime']) && !empty($row[
'jq_starttime'])) {
284 if (empty($filesPerSec)) {
285 $burnTime = time() - strtotime($row[
'jq_starttime']);
289 if (!empty($filesPerSec)) {
290 $timeOfCompletion = ($itemCount[
'jq_itemsprocessed'] - $row[
'jq_itemsprocessed']) / $filesPerSec;
292 $estimatedArray[] = $timeOfCompletion;
295 if (empty($estimatedArray)) {
298 $estimatedTime = round(
max($estimatedArray));
299 if (!empty($timeInSec)) {
300 return intval(!empty($estimatedTime) ? $estimatedTime : 0);
302 return intval($estimatedTime/3600).gmdate(
":i:s", $estimatedTime);
314 $statementName = __METHOD__.
"getDataForASingleJob";
315 $this->
dbManager->prepare($statementName,
316 "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");
317 $result = $this->
dbManager->execute($statementName, array($jq_pk));
318 $row = $this->
dbManager->fetchArray($result);
329 $statementName = __METHOD__.
"forjq_pk";
331 "SELECT jq_end_bits FROM jobqueue WHERE jq_pk = $1",
335 if ($row[
'jq_end_bits'] == 1 || $row[
'jq_end_bits'] == 2) {
349 $statementName = __METHOD__.
"forjqTypeAndjobId";
351 "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",
352 array($jqType, $jobId),
355 if (!empty($row[
'jq_itemsprocessed'])) {
356 return array($row[
'jq_itemsprocessed'], $row[
'job_upload_fk']);
368 $sql =
"SELECT jq_type AS job, jq_job_fk, job_upload_fk AS upload_fk, " .
369 "CASE WHEN (jq_endtext IS NULL AND jq_end_bits = 0) THEN 'pending' " .
370 "WHEN (jq_endtext = ANY('{Started,Restarted,Paused}')) THEN 'running' " .
371 "ELSE '' END AS status " .
372 "FROM jobqueue INNER JOIN job " .
373 "ON jq_job_fk = job_pk " .
374 "AND job_queued >= (now() - interval '" . $this->nhours .
" hours') " .
375 "WHERE jq_endtime IS NULL;";
376 $statement = __METHOD__ .
".getAllUnFinishedJobs";
377 return $this->
dbManager->getRows($sql, [], $statement);
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.
uploads2Jobs($upload_pks, $page=0)
Find all the jobs for a given set of uploads.
getJobName($uploadId)
Return job name. Used for deleted jobs.
getItemsProcessedForDecider($jqType, $jobId)
myJobs($allusers, $page=0)
Find all of my jobs submitted within the last n hours.
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.
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
fo_dbManager * dbManager
fo_dbManager object