FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AgentDao.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2012 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2014 Siemens AG
5  Author: Johannes Najjar
6 
7  SPDX-License-Identifier: GPL-2.0-only
8 */
9 
10 namespace Fossology\Lib\Dao;
11 
14 use Monolog\Logger;
15 
20 class AgentDao
21 {
22  const ARS_TABLE_SUFFIX = "_ars";
23 
25  private $dbManager;
26 
31  function __construct(DbManager $dbManager, Logger $logger)
32  {
33  $this->dbManager = $dbManager;
34  $this->logger = $logger;
35  }
36 
37  public function arsTableExists($agentName)
38  {
39  return $this->dbManager->existsTable($this->getArsTableName($agentName));
40  }
41 
42  public function createArsTable($agentName)
43  {
44  $tableName = $this->getArsTableName($agentName);
45  $this->dbManager->queryOnce("CREATE TABLE ".$tableName."() INHERITS(ars_master);
46  ALTER TABLE ONLY ".$tableName." ADD CONSTRAINT ".$tableName."_agent_fk_fkc FOREIGN KEY (agent_fk) REFERENCES agent(agent_pk);
47  ALTER TABLE ONLY ".$tableName." ADD CONSTRAINT ".$tableName."_upload_fk_fkc FOREIGN KEY (upload_fk) REFERENCES upload(upload_pk) ON DELETE CASCADE", __METHOD__);
48  }
49 
50  public function writeArsRecord($agentName,$agentId,$uploadId,$arsId=0,$success=false,$status="")
51  {
52  $arsTableName = $this->getArsTableName($agentName);
53 
54  if ($arsId) {
55  $successDb = $this->dbManager->booleanToDb($success);
56  $parms = array($successDb, $arsId);
57 
58  $stmt = __METHOD__.".$arsTableName";
59 
60  if (!empty($status)) {
61  $stmt .= ".status";
62  $parms[] = $status;
63  $statusClause = ", ars_status = $".count($parms);
64  } else {
65  $statusClause = "";
66  }
67 
68  $this->dbManager->getSingleRow(
69  "UPDATE $arsTableName
70  SET ars_success=$1,
71  ars_endtime=now() $statusClause
72  WHERE ars_pk = $2",
73  $parms, $stmt);
74  } else {
75  $row = $this->dbManager->getSingleRow(
76  "INSERT INTO $arsTableName(agent_fk,upload_fk)
77  VALUES ($1,$2) RETURNING ars_pk",
78  array($agentId, $uploadId),
79  __METHOD__.".update.".$arsTableName);
80  if ($row !== false) {
81  return $row['ars_pk'];
82  }
83  }
84 
85  return -1;
86  }
87 
88  public function getCurrentAgentId($agentName, $agentDesc="", $agentRev="")
89  {
90  $row = $this->dbManager->getSingleRow(
91  "SELECT agent_pk FROM agent WHERE agent_name = $1 order by agent_ts desc limit 1",
92  array($agentName), __METHOD__."select"
93  );
94 
95  if ($row === false) {
96  $row = $this->dbManager->getSingleRow(
97  "INSERT INTO agent(agent_name,agent_desc,agent_rev) VALUES ($1,$2,$3) RETURNING agent_pk",
98  array($agentName, $agentDesc, $agentRev), __METHOD__."insert"
99  );
100  return false !== $row ? intval($row['agent_pk']) : -1;
101  }
102 
103  return intval($row['agent_pk']);
104  }
105 
123  public function agentARSList($tableName, $uploadId, $limit = 1, $agentId = 0, $agentSuccess = true)
124  {
125  //based on common-agents.php AgentARSList
126  if (!$this->dbManager->existsTable($tableName)) {
127  return false;
128  }
129 
130  $arguments = array($uploadId);
131  $statementName = __METHOD__ . $tableName;
132  $sql = "SELECT * FROM $tableName, agent WHERE agent_pk=agent_fk AND upload_fk=$1 AND agent_enabled";
133  if ($agentId) {
134  $arguments[] = $agentId;
135  $sql .= ' AND agent_fk=$'.count($arguments);
136  $statementName .= ".agent";
137  }
138  if ($agentSuccess) {
139  $sql .= " AND ars_success";
140  $statementName .= ".suc";
141  }
142  $sql .= " ORDER BY agent_ts DESC";
143  if ($limit > 0) {
144  $arguments[] = $limit;
145  $sql .= ' limit $'.count($arguments);
146  $statementName .= ".lim";
147  }
148  $this->dbManager->prepare($statementName,$sql);
149  $result = $this->dbManager->execute($statementName, $arguments);
150  $resultArray = $this->dbManager->fetchAll($result);
151  $this->dbManager->freeResult($result);
152  return $resultArray;
153  }
154 
162  public function getRunningAgentIds($uploadId, $agentName)
163  {
164  $arsTableName = $this->getArsTableName($agentName);
165  $listOfAllJobs = $this->agentARSList($arsTableName, $uploadId, 0, 0, false);
166 
167  $listOfRunningAgents = array();
168 
169  if ($listOfAllJobs !== false) {
170  foreach ($listOfAllJobs as $job) {
171  if ($job ['ars_success'] === $this->dbManager->booleanToDb(true)) {
172  continue;
173  }
174  $listOfRunningAgents[] = intval($job['agent_fk']);
175  }
176  }
177  return $listOfRunningAgents;
178  }
179 
180  public function getLatestAgentResultForUpload($uploadId, $agentNames)
181  {
182  $latestScannerProxy = new \Fossology\Lib\Proxy\LatestScannerProxy($uploadId, $agentNames, "latest_scanner$uploadId");
183 
184  return $latestScannerProxy->getNameToIdMap();
185  }
186 
191  public function getCurrentAgentRef($agentName)
192  {
193  $row = $this->dbManager->getSingleRow("SELECT agent_pk, agent_name, agent_rev from agent WHERE agent_enabled AND agent_name=$1 "
194  . "ORDER BY agent_pk DESC LIMIT 1", array($agentName));
195  return $this->createAgentRef($row);
196  }
197 
203  public function getSuccessfulAgentRuns($agentName, $uploadId)
204  {
205  $stmt = __METHOD__ . ".getAgent.$agentName";
206  $this->dbManager->prepare($stmt,
207  $sql = "SELECT agent_pk,agent_rev,agent_name FROM agent LEFT JOIN " . $this->getArsTableName($agentName) . " ON agent_fk=agent_pk "
208  . "WHERE agent_name=$2 AND agent_enabled AND upload_fk=$1 AND ars_success "
209  . "ORDER BY agent_pk DESC");
210  $res = $this->dbManager->execute($stmt, array($uploadId, $agentName));
211  $agents = array();
212  while ($row = $this->dbManager->fetchArray($res)) {
213  $agents[] = $this->createAgentRef($row);
214  }
215  $this->dbManager->freeResult($res);
216  return $agents;
217  }
218 
224  public function getSuccessfulAgentEntries($scannerName, $uploadId)
225  {
226  $stmt = __METHOD__ . ".getAgent.$scannerName";
227  $this->dbManager->prepare($stmt,
228  $sql = "SELECT agent_pk AS agent_id,agent_rev,agent_name "
229  . "FROM agent LEFT JOIN $scannerName" . self::ARS_TABLE_SUFFIX . " ON agent_fk=agent_pk "
230  . "WHERE agent_name=$2 AND agent_enabled AND upload_fk=$1 AND ars_success "
231  . "ORDER BY agent_id DESC");
232  $res = $this->dbManager->execute($stmt, array($uploadId, $scannerName));
233  $agents = $this->dbManager->fetchAll($res);
234  $this->dbManager->freeResult($res);
235  return $agents;
236  }
237 
242  private function createAgentRef($row)
243  {
244  return new AgentRef(intval($row['agent_pk']), $row['agent_name'], $row['agent_rev']);
245  }
246 
251  private function getArsTableName($agentName)
252  {
253  return $agentName . self::ARS_TABLE_SUFFIX;
254  }
255 
260  public function renewCurrentAgent($agentName)
261  {
262  $this->dbManager->begin();
263  $row = $this->dbManager->getSingleRow("SELECT agent_pk, agent_name, agent_rev, agent_desc FROM agent "
264  . "WHERE agent_enabled AND agent_name=$1 ORDER BY agent_pk DESC LIMIT 1", array($agentName),__METHOD__.'.get');
265  $this->dbManager->getSingleRow("UPDATE agent SET agent_rev=agent_rev||'.'||substr(md5(agent_ts::text),0,6) "
266  ."WHERE agent_pk=$1",array($row['agent_pk']),__METHOD__.'.upd');
267  unset($row['agent_pk']);
268  $this->dbManager->insertTableRow('agent',$row);
269  $this->dbManager->commit();
270  return true;
271  }
272 
277  public function getAgentName($agentId)
278  {
279  $row = $this->dbManager->getSingleRow("SELECT agent_name FROM agent WHERE agent_enabled AND agent_pk=$1", array($agentId));
280  return ($row===false)?false:$row['agent_name'];
281  }
282 
287  public function getAgentRev($agentId)
288  {
289  $row = $this->dbManager->getSingleRow("SELECT agent_rev FROM agent WHERE agent_enabled AND agent_pk=$1", array($agentId));
290  return ($row===false)?false:$row['agent_rev'];
291  }
292 }
getArsTableName($agentName)
Definition: AgentDao.php:251
__construct(DbManager $dbManager, Logger $logger)
Definition: AgentDao.php:31
getCurrentAgentRef($agentName)
Definition: AgentDao.php:191
agentARSList($tableName, $uploadId, $limit=1, $agentId=0, $agentSuccess=true)
The purpose of this function is to return an array of _ars records for an agent so that the latest ag...
Definition: AgentDao.php:123
renewCurrentAgent($agentName)
Definition: AgentDao.php:260
getSuccessfulAgentRuns($agentName, $uploadId)
Definition: AgentDao.php:203
getSuccessfulAgentEntries($scannerName, $uploadId)
Definition: AgentDao.php:224
getRunningAgentIds($uploadId, $agentName)
Returns the list of running or failed agent_pk s. Before latest successful run.
Definition: AgentDao.php:162
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16