FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AjaxAdminScheduler.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011-2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2014 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\UI\Ajax;
10 
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16 
18 {
19  const NAME = "ajax_admin_scheduler";
21  private $dbManager;
22 
23  function __construct()
24  {
25  parent::__construct(self::NAME, array(
26  self::TITLE => _("URL"),
27  self::PERMISSION => Auth::PERM_WRITE
28  ));
29 
30  $this->dbManager = $this->getObject('db.manager');
31  }
32 
37  public function handle(Request $request)
38  {
39  $V = '';
40  $operation = $request->get('operation');
41  $vars['jobOptions'] = $this->jobListOption($operation);
42  $vars['operation'] = $operation;
43  $vars['priorityList'] = $this->priorityListOption($request->get('fromRest'));
44  $content = $this->renderer->load('ajax-admin-scheduler.html.twig')->render($vars);
45  $restRes = [
46  'jobList' => $vars['jobOptions'] ?? [],
47  'priorityList' => $vars['priorityList'] ?? [],
48  'verboseList' => [],
49  'agentList' => [],
50  ];
51 
52  if ('pause' == $operation || 'restart' == $operation ||
53  'status' == $operation || 'priority' == $operation) {
54  $V = $content;
55  } else if ('verbose' == $operation) {
56  $verbose_list_option = $this->verboseListOption($request->get('fromRest'));
57  $text2 = _("Select a verbosity level");
58  $V = $content .
59  "<br>$text2: <select name='level_list' id='level_list'>$verbose_list_option</select>";
60  $restRes['verboseList'] = $verbose_list_option;
61  } else if ('agents' == $operation) {
63  $dbManager = $this->getObject('db.manager');
64  $dbManager->prepare($stmt = __METHOD__ . '.getAgents',
65  'SELECT MAX(agent_pk) agent_id, agent_name FROM agent WHERE agent_enabled GROUP BY agent_name');
66  $res = $dbManager->execute($stmt);
67  $V = '<ul>';
68  while ($row = $dbManager->fetchArray($res)) {
69  $restRes['agentList'][] = $row['agent_name'];
70  $V .= "<li>$row[agent_name]</li>";
71  }
72  $V .= '</ul>';
73  $dbManager->freeResult($res);
74  }
75 
76  if ($request->get('fromRest')) {
77  return $restRes;
78  }
79  return new Response($V, Response::HTTP_OK, array('content-type'=>'text/htm')); // not 'text/html' while console-logging
80  }
81 
87  function jobListOption($type)
88  {
89  if (empty($type)) {
90  return array();
91  }
92 
93  $job_array = array();
94  if ('status' == $type || 'verbose' == $type || 'priority' == $type) {
95  $job_array = GetRunnableJobList();
96  if ('priority' != $type) {
97  $job_array[0] = "scheduler";
98  }
99  }
100  if ('pause' == $type) {
101  $job_array = GetJobList("Started");
102  }
103  if ('restart' == $type) {
104  $job_array = GetJobList("Paused");
105  }
106  return $job_array;
107  }
108 
113  function verboseListOption($fromRest = false)
114  {
115  $verbose_list_option = "";
116  $min = 1;
117  $max = 3;
118  $restRes = [];
119  for ($i = $min; $i <= $max; $i++) {
120  $bitmask= (1<<$i) - 1;
121  $verbose_list_option .= "<option value='$bitmask'>$i</option>";
122  $restRes[] = $bitmask;
123  }
124  if ($fromRest) {
125  return $restRes;
126  }
127  return $verbose_list_option;
128  }
129 
134  function priorityListOption($fromRest = false)
135  {
136  $min = -20;
137  $max = 20;
138  $resRes = [];
139  $priority_list = array();
140  for ($i = $min; $i <= $max; $i++) {
141  $priority_list[$i]=$i;
142  $resRes[] = $i;
143  }
144  if ($fromRest) {
145  return $resRes;
146  }
147  return $priority_list;
148  }
149 }
150 
151 register_plugin(new AjaxAdminScheduler());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
jobListOption($type)
get the job list for the specified operation
verboseListOption($fromRest=false)
get the verbose list: if the value of verbose is 1, set verbose as 1
priorityListOption($fromRest=false)
get the priority list for setting, -20..20
GetJobList($status)
Gets the list of jobqueue records with the requested $status.
Definition: common-job.php:244
GetRunnableJobList()
Get runnable job list, the process is below:
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16