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 
46  $this->dbManager->queryOnce("CREATE TABLE ".$tableName."() INHERITS(ars_master);
47  ALTER TABLE ONLY ".$tableName." ADD CONSTRAINT ".$tableName."_agent_fk_fkc FOREIGN KEY (agent_fk) REFERENCES agent(agent_pk);
48  ALTER TABLE ONLY ".$tableName." ADD CONSTRAINT ".$tableName."_upload_fk_fkc FOREIGN KEY (upload_fk) REFERENCES upload(upload_pk) ON DELETE CASCADE", __METHOD__);
49  }
50 
51  public function writeArsRecord($agentName,$agentId,$uploadId,$arsId=0,$success=false,$status="")
52  {
53  $arsTableName = $this->getArsTableName($agentName);
54 
55  if ($arsId) {
56  $successDb = $this->dbManager->booleanToDb($success);
57  $parms = array($successDb, $arsId);
58 
59  $stmt = __METHOD__.".$arsTableName";
60 
61  if (!empty($status)) {
62  $stmt .= ".status";
63  $parms[] = $status;
64  $statusClause = ", ars_status = $".count($parms);
65  } else {
66  $statusClause = "";
67  }
68 
69  $this->dbManager->getSingleRow(
70  "UPDATE $arsTableName
71  SET ars_success=$1,
72  ars_endtime=now() $statusClause
73  WHERE ars_pk = $2",
74  $parms, $stmt);
75  } else {
76  $row = $this->dbManager->getSingleRow(
77  "INSERT INTO $arsTableName(agent_fk,upload_fk)
78  VALUES ($1,$2) RETURNING ars_pk",
79  array($agentId, $uploadId),
80  __METHOD__.".update.".$arsTableName);
81  if ($row !== false) {
82  return $row['ars_pk'];
83  }
84  }
85 
86  return -1;
87  }
88 
89  public function getCurrentAgentId($agentName, $agentDesc="", $agentRev="")
90  {
91  $row = $this->dbManager->getSingleRow(
92  "SELECT agent_pk FROM agent WHERE agent_name = $1 order by agent_ts desc limit 1",
93  array($agentName), __METHOD__."select"
94  );
95 
96  if ($row === false) {
97  $row = $this->dbManager->getSingleRow(
98  "INSERT INTO agent(agent_name,agent_desc,agent_rev) VALUES ($1,$2,$3) RETURNING agent_pk",
99  array($agentName, $agentDesc, $agentRev), __METHOD__."insert"
100  );
101  return false !== $row ? intval($row['agent_pk']) : -1;
102  }
103 
104  return intval($row['agent_pk']);
105  }
106 
124  public function agentARSList($tableName, $uploadId, $limit = 1, $agentId = 0, $agentSuccess = true)
125  {
126  //based on common-agents.php AgentARSList
127  if (!$this->dbManager->existsTable($tableName)) {
128  return false;
129  }
130 
131  $arguments = array($uploadId);
132  $statementName = __METHOD__ . $tableName;
133  $sql = "SELECT * FROM $tableName, agent WHERE agent_pk=agent_fk AND upload_fk=$1 AND agent_enabled";
134  if ($agentId) {
135  $arguments[] = $agentId;
136  $sql .= ' AND agent_fk=$'.count($arguments);
137  $statementName .= ".agent";
138  }
139  if ($agentSuccess) {
140  $sql .= " AND ars_success";
141  $statementName .= ".suc";
142  }
143  $sql .= " ORDER BY agent_ts DESC";
144  if ($limit > 0) {
145  $arguments[] = $limit;
146  $sql .= ' limit $'.count($arguments);
147  $statementName .= ".lim";
148  }
149  $this->dbManager->prepare($statementName,$sql);
150  $result = $this->dbManager->execute($statementName, $arguments);
151  $resultArray = $this->dbManager->fetchAll($result);
152  $this->dbManager->freeResult($result);
153  return $resultArray;
154  }
155 
156 
164  public function getRunningAgentIds($uploadId, $agentName)
165  {
166  $arsTableName = $this->getArsTableName($agentName);
167  $listOfAllJobs = $this->agentARSList($arsTableName, $uploadId, 0, 0, false);
168 
169  $listOfRunningAgents = array();
170 
171  if ($listOfAllJobs !== false) {
172  foreach ($listOfAllJobs as $job) {
173  if ($job ['ars_success'] === $this->dbManager->booleanToDb(true)) {
174  continue;
175  }
176  $listOfRunningAgents[] = intval($job['agent_fk']);
177  }
178  }
179  return $listOfRunningAgents;
180  }
181 
182  public function getLatestAgentResultForUpload($uploadId, $agentNames)
183  {
184  $latestScannerProxy = new \Fossology\Lib\Proxy\LatestScannerProxy($uploadId, $agentNames, "latest_scanner$uploadId");
185 
186  return $latestScannerProxy->getNameToIdMap();
187  }
188 
193  public function getCurrentAgentRef($agentName)
194  {
195  $row = $this->dbManager->getSingleRow("SELECT agent_pk, agent_name, agent_rev from agent WHERE agent_enabled AND agent_name=$1 "
196  . "ORDER BY agent_pk DESC LIMIT 1", array($agentName));
197  return $this->createAgentRef($row);
198  }
199 
205  public function getSuccessfulAgentRuns($agentName, $uploadId)
206  {
207  $stmt = __METHOD__ . ".getAgent.$agentName";
208  $this->dbManager->prepare($stmt,
209  $sql = "SELECT agent_pk,agent_rev,agent_name FROM agent LEFT JOIN " . $this->getArsTableName($agentName) . " ON agent_fk=agent_pk "
210  . "WHERE agent_name=$2 AND agent_enabled AND upload_fk=$1 AND ars_success "
211  . "ORDER BY agent_pk DESC");
212  $res = $this->dbManager->execute($stmt, array($uploadId, $agentName));
213  $agents = array();
214  while ($row = $this->dbManager->fetchArray($res)) {
215  $agents[] = $this->createAgentRef($row);
216  }
217  $this->dbManager->freeResult($res);
218  return $agents;
219  }
220 
226  public function getSuccessfulAgentEntries($scannerName, $uploadId)
227  {
228  $stmt = __METHOD__ . ".getAgent.$scannerName";
229  $this->dbManager->prepare($stmt,
230  $sql = "SELECT agent_pk AS agent_id,agent_rev,agent_name "
231  . "FROM agent LEFT JOIN $scannerName" . self::ARS_TABLE_SUFFIX . " ON agent_fk=agent_pk "
232  . "WHERE agent_name=$2 AND agent_enabled AND upload_fk=$1 AND ars_success "
233  . "ORDER BY agent_id DESC");
234  $res = $this->dbManager->execute($stmt, array($uploadId, $scannerName));
235  $agents = $this->dbManager->fetchAll($res);
236  $this->dbManager->freeResult($res);
237  return $agents;
238  }
239 
244  private function createAgentRef($row)
245  {
246  return new AgentRef(intval($row['agent_pk']), $row['agent_name'], $row['agent_rev']);
247  }
248 
253  private function getArsTableName($agentName)
254  {
255  return $agentName . self::ARS_TABLE_SUFFIX;
256  }
257 
262  public function renewCurrentAgent($agentName)
263  {
264  $this->dbManager->begin();
265  $row = $this->dbManager->getSingleRow("SELECT agent_pk, agent_name, agent_rev, agent_desc FROM agent "
266  . "WHERE agent_enabled AND agent_name=$1 ORDER BY agent_pk DESC LIMIT 1", array($agentName),__METHOD__.'.get');
267  $this->dbManager->getSingleRow("UPDATE agent SET agent_rev=agent_rev||'.'||substr(md5(agent_ts::text),0,6) "
268  ."WHERE agent_pk=$1",array($row['agent_pk']),__METHOD__.'.upd');
269  unset($row['agent_pk']);
270  $this->dbManager->insertTableRow('agent',$row);
271  $this->dbManager->commit();
272  return true;
273  }
274 
279  public function getAgentName($agentId)
280  {
281  $row = $this->dbManager->getSingleRow("SELECT agent_name FROM agent WHERE agent_enabled AND agent_pk=$1", array($agentId));
282  return ($row===false)?false:$row['agent_name'];
283  }
284 
289  public function getAgentRev($agentId)
290  {
291  $row = $this->dbManager->getSingleRow("SELECT agent_rev FROM agent WHERE agent_enabled AND agent_pk=$1", array($agentId));
292  return ($row===false)?false:$row['agent_rev'];
293  }
294 }
getArsTableName($agentName)
Definition: AgentDao.php:253
__construct(DbManager $dbManager, Logger $logger)
Definition: AgentDao.php:31
getCurrentAgentRef($agentName)
Definition: AgentDao.php:193
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:124
renewCurrentAgent($agentName)
Definition: AgentDao.php:262
getSuccessfulAgentRuns($agentName, $uploadId)
Definition: AgentDao.php:205
getSuccessfulAgentEntries($scannerName, $uploadId)
Definition: AgentDao.php:226
getRunningAgentIds($uploadId, $agentName)
Returns the list of running or failed agent_pk s. Before latest successful run.
Definition: AgentDao.php:164
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16