FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AjaxAllJobStatus.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2020 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 namespace Fossology\UI\Ajax;
9 
13 use Symfony\Component\HttpFoundation\Request;
14 use Symfony\Component\HttpFoundation\Response;
15 use Symfony\Component\HttpFoundation\JsonResponse;
17 
23 {
24 
27  const NAME = "ajax_all_job_status";
28 
31  private $dbManager;
32 
35  private $showJobDao;
36 
37  function __construct()
38  {
39  parent::__construct(self::NAME,
40  array(
41  self::PERMISSION => Auth::PERM_READ,
42  self::REQUIRES_LOGIN => false
43  ));
44 
45  $this->dbManager = $this->getObject('db.manager');
46  $this->showJobDao = $this->getObject('dao.show_jobs');
47  }
48 
53  public function handle(Request $request)
54  {
55  $results = $this->showJobDao->getJobsForAll();
56  $uniqueTypes = array_unique(array_column($results, 'job'));
57  $data = array();
58 
59  foreach ($uniqueTypes as $type) {
60  $data[$type] = [];
61  $data[$type]['running'] = 0;
62  $data[$type]['pending'] = 0;
63  $data[$type]['eta'] = 0;
64  foreach ($results as $row) {
65  if ($row['job'] != $type || empty($row['status'])) {
66  continue;
67  }
68  $data[$type][$row['status']] ++;
69  $newEta = $this->showJobDao->getEstimatedTime($row['jq_job_fk'],
70  $row['job'], 0, $row['upload_fk'], 1);
71  if (! empty($newEta)) {
72  $data[$type]['eta'] = ($newEta > $data[$type]['eta']) ? $newEta:$data[$type]['eta'];
73  }
74  }
75  }
76 
77  $returnData = array();
78  foreach ($data as $agent => $row) {
79  $dataRow = [
80  "name" => $agent,
81  "running" => $row["running"],
82  "pending" => $row["pending"]
83  ];
84  if ($row['eta'] == 0) {
85  $dataRow['eta'] = "N/A";
86  } else {
87  $dataRow['eta'] = intval($row["eta"] / 3600) .
88  gmdate(":i:s", $row["eta"]);
89  }
90  $returnData[] = $dataRow;
91  }
92  $output = "";
93  $error_msg = "";
94  $schedStatus = "Running";
95  $systemLoad = get_system_load_average();
96  if (! fo_communicate_with_scheduler("status", $output, $error_msg)
97  && strstr($error_msg, "Connection refused") !== false) {
98  $schedStatus = "Stopped";
99  }
100  return new JsonResponse(["data" => $returnData, "scheduler" => $schedStatus, "systemLoad" => $systemLoad]);
101  }
102 }
103 
104 register_plugin(new AjaxAllJobStatus());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.
get_system_load_average()
Get system load average.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16