FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AjaxAllJobStatus.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2020 Siemens AG
4  * Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 namespace Fossology\UI\Ajax;
20 
28 
34 {
35 
38  const NAME = "ajax_all_job_status";
39 
42  private $dbManager;
43 
46  private $showJobDao;
47 
48  function __construct()
49  {
50  parent::__construct(self::NAME,
51  array(
52  self::PERMISSION => Auth::PERM_NONE,
53  self::REQUIRES_LOGIN => false
54  ));
55 
56  $this->dbManager = $this->getObject('db.manager');
57  $this->showJobDao = $this->getObject('dao.show_jobs');
58  }
59 
64  protected function handle(Request $request)
65  {
66  $results = $this->showJobDao->getJobsForAll();
67  $uniqueTypes = array_unique(array_column($results, 'job'));
68  $data = array();
69 
70  foreach ($uniqueTypes as $type) {
71  $data[$type] = [];
72  $data[$type]['running'] = 0;
73  $data[$type]['pending'] = 0;
74  $data[$type]['eta'] = 0;
75  foreach ($results as $row) {
76  if ($row['job'] != $type || empty($row['status'])) {
77  continue;
78  }
79  $data[$type][$row['status']] ++;
80  $newEta = $this->showJobDao->getEstimatedTime($row['jq_job_fk'],
81  $row['job'], 0, $row['upload_fk'], 1);
82  if (! empty($newEta)) {
83  $data[$type]['eta'] = ($newEta > $data[$type]['eta']) ? $newEta:$data[$type]['eta'];
84  }
85  }
86  }
87 
88  $returnData = array();
89  foreach ($data as $agent => $row) {
90  $dataRow = [
91  "name" => $agent,
92  "running" => $row["running"],
93  "pending" => $row["pending"]
94  ];
95  if ($row['eta'] == 0) {
96  $dataRow['eta'] = "N/A";
97  } else {
98  $dataRow['eta'] = intval($row["eta"] / 3600) .
99  gmdate(":i:s", $row["eta"]);
100  }
101  $returnData[] = $dataRow;
102  }
103  $output = "";
104  $error_msg = "";
105  $schedStatus = "Running";
106  if (! fo_communicate_with_scheduler("status", $output, $error_msg)
107  && strstr($error_msg, "Connection refused") !== false) {
108  $schedStatus = "Stopped";
109  }
110  return new JsonResponse(["data" => $returnData, "scheduler" => $schedStatus]);
111  }
112 }
113 
114 register_plugin(new AjaxAllJobStatus());
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28