FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AgentDao.php
1 <?php
2 /*
3 Copyright (C) 2008-2012 Hewlett-Packard Development Company, L.P.
4 Copyright (C) 2014, Siemens AG
5 Author: Johannes Najjar
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as published by the Free Software Foundation.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
21 namespace Fossology\Lib\Dao;
22 
25 use Monolog\Logger;
26 
31 class AgentDao
32 {
33  const ARS_TABLE_SUFFIX = "_ars";
34 
36  private $dbManager;
37 
42  function __construct(DbManager $dbManager, Logger $logger)
43  {
44  $this->dbManager = $dbManager;
45  $this->logger = $logger;
46  }
47 
48  public function arsTableExists($agentName)
49  {
50  return $this->dbManager->existsTable($this->getArsTableName($agentName));
51  }
52 
53  public function createArsTable($agentName)
54  {
55  $tableName = $this->getArsTableName($agentName);
56 
57  $this->dbManager->queryOnce("CREATE TABLE ".$tableName."() INHERITS(ars_master);
58  ALTER TABLE ONLY ".$tableName." ADD CONSTRAINT ".$tableName."_agent_fk_fkc FOREIGN KEY (agent_fk) REFERENCES agent(agent_pk);
59  ALTER TABLE ONLY ".$tableName." ADD CONSTRAINT ".$tableName."_upload_fk_fkc FOREIGN KEY (upload_fk) REFERENCES upload(upload_pk) ON DELETE CASCADE", __METHOD__);
60  }
61 
62  public function writeArsRecord($agentName,$agentId,$uploadId,$arsId=0,$success=false,$status="")
63  {
64  $arsTableName = $this->getArsTableName($agentName);
65 
66  if ($arsId) {
67  $successDb = $this->dbManager->booleanToDb($success);
68  $parms = array($successDb, $arsId);
69 
70  $stmt = __METHOD__.".$arsTableName";
71 
72  if (!empty($status)) {
73  $stmt .= ".status";
74  $parms[] = $status;
75  $statusClause = ", ars_status = $".count($parms);
76  } else {
77  $statusClause = "";
78  }
79 
80  $this->dbManager->getSingleRow(
81  "UPDATE $arsTableName
82  SET ars_success=$1,
83  ars_endtime=now() $statusClause
84  WHERE ars_pk = $2",
85  $parms, $stmt);
86  } else {
87  $row = $this->dbManager->getSingleRow(
88  "INSERT INTO $arsTableName(agent_fk,upload_fk)
89  VALUES ($1,$2) RETURNING ars_pk",
90  array($agentId, $uploadId),
91  __METHOD__.".update.".$arsTableName);
92  if ($row !== false) {
93  return $row['ars_pk'];
94  }
95  }
96 
97  return -1;
98  }
99 
100  public function getCurrentAgentId($agentName, $agentDesc="", $agentRev="")
101  {
102  $row = $this->dbManager->getSingleRow(
103  "SELECT agent_pk FROM agent WHERE agent_name = $1 order by agent_ts desc limit 1",
104  array($agentName), __METHOD__."select"
105  );
106 
107  if ($row === false) {
108  $row = $this->dbManager->getSingleRow(
109  "INSERT INTO agent(agent_name,agent_desc,agent_rev) VALUES ($1,$2,$3) RETURNING agent_pk",
110  array($agentName, $agentDesc, $agentRev), __METHOD__."insert"
111  );
112  return false !== $row ? intval($row['agent_pk']) : -1;
113  }
114 
115  return intval($row['agent_pk']);
116  }
117 
135  public function agentARSList($tableName, $uploadId, $limit = 1, $agentId = 0, $agentSuccess = true)
136  {
137  //based on common-agents.php AgentARSList
138  if (!$this->dbManager->existsTable($tableName)) {
139  return false;
140  }
141 
142  $arguments = array($uploadId);
143  $statementName = __METHOD__ . $tableName;
144  $sql = "SELECT * FROM $tableName, agent WHERE agent_pk=agent_fk AND upload_fk=$1 AND agent_enabled";
145  if ($agentId) {
146  $arguments[] = $agentId;
147  $sql .= ' AND agent_fk=$'.count($arguments);
148  $statementName .= ".agent";
149  }
150  if ($agentSuccess) {
151  $sql .= " AND ars_success";
152  $statementName .= ".suc";
153  }
154  $sql .= " ORDER BY agent_ts DESC";
155  if ($limit > 0) {
156  $arguments[] = $limit;
157  $sql .= ' limit $'.count($arguments);
158  $statementName .= ".lim";
159  }
160  $this->dbManager->prepare($statementName,$sql);
161  $result = $this->dbManager->execute($statementName, $arguments);
162  $resultArray = $this->dbManager->fetchAll($result);
163  $this->dbManager->freeResult($result);
164  return $resultArray;
165  }
166 
167 
175  public function getRunningAgentIds($uploadId, $agentName)
176  {
177  $arsTableName = $this->getArsTableName($agentName);
178  $listOfAllJobs = $this->agentARSList($arsTableName, $uploadId, 0, 0, false);
179 
180  $listOfRunningAgents = array();
181 
182  if ($listOfAllJobs !== false) {
183  foreach ($listOfAllJobs as $job) {
184  if ($job ['ars_success'] === $this->dbManager->booleanToDb(true)) {
185  continue;
186  }
187  $listOfRunningAgents[] = intval($job['agent_fk']);
188  }
189  }
190  return $listOfRunningAgents;
191  }
192 
193  public function getLatestAgentResultForUpload($uploadId, $agentNames)
194  {
195  $latestScannerProxy = new \Fossology\Lib\Proxy\LatestScannerProxy($uploadId, $agentNames, "latest_scanner$uploadId");
196 
197  $agentLatestMap = $latestScannerProxy->getNameToIdMap();
198 
199  return $agentLatestMap;
200  }
201 
206  public function getCurrentAgentRef($agentName)
207  {
208  $row = $this->dbManager->getSingleRow("SELECT agent_pk, agent_name, agent_rev from agent WHERE agent_enabled AND agent_name=$1 "
209  . "ORDER BY agent_pk DESC LIMIT 1", array($agentName));
210  return $this->createAgentRef($row);
211  }
212 
218  public function getSuccessfulAgentRuns($agentName, $uploadId)
219  {
220  $stmt = __METHOD__ . ".getAgent.$agentName";
221  $this->dbManager->prepare($stmt,
222  $sql = "SELECT agent_pk,agent_rev,agent_name FROM agent LEFT JOIN " . $this->getArsTableName($agentName) . " ON agent_fk=agent_pk "
223  . "WHERE agent_name=$2 AND agent_enabled AND upload_fk=$1 AND ars_success "
224  . "ORDER BY agent_pk DESC");
225  $res = $this->dbManager->execute($stmt, array($uploadId, $agentName));
226  $agents = array();
227  while ($row = $this->dbManager->fetchArray($res)) {
228  $agents[] = $this->createAgentRef($row);
229  }
230  $this->dbManager->freeResult($res);
231  return $agents;
232  }
233 
239  public function getSuccessfulAgentEntries($scannerName, $uploadId)
240  {
241  $stmt = __METHOD__ . ".getAgent.$scannerName";
242  $this->dbManager->prepare($stmt,
243  $sql = "SELECT agent_pk AS agent_id,agent_rev,agent_name "
244  . "FROM agent LEFT JOIN $scannerName" . self::ARS_TABLE_SUFFIX . " ON agent_fk=agent_pk "
245  . "WHERE agent_name=$2 AND agent_enabled AND upload_fk=$1 AND ars_success "
246  . "ORDER BY agent_id DESC");
247  $res = $this->dbManager->execute($stmt, array($uploadId, $scannerName));
248  $agents = $this->dbManager->fetchAll($res);
249  $this->dbManager->freeResult($res);
250  return $agents;
251  }
252 
257  private function createAgentRef($row)
258  {
259  return new AgentRef(intval($row['agent_pk']), $row['agent_name'], $row['agent_rev']);
260  }
261 
266  private function getArsTableName($agentName)
267  {
268  return $agentName . self::ARS_TABLE_SUFFIX;
269  }
270 
275  public function renewCurrentAgent($agentName)
276  {
277  $this->dbManager->begin();
278  $row = $this->dbManager->getSingleRow("SELECT agent_pk, agent_name, agent_rev, agent_desc FROM agent "
279  . "WHERE agent_enabled AND agent_name=$1 ORDER BY agent_pk DESC LIMIT 1", array($agentName),__METHOD__.'.get');
280  $this->dbManager->getSingleRow("UPDATE agent SET agent_rev=agent_rev||'.'||substr(md5(agent_ts::text),0,6) "
281  ."WHERE agent_pk=$1",array($row['agent_pk']),__METHOD__.'.upd');
282  unset($row['agent_pk']);
283  $this->dbManager->insertTableRow('agent',$row);
284  $this->dbManager->commit();
285  return true;
286  }
287 
292  public function getAgentName($agentId)
293  {
294  $row = $this->dbManager->getSingleRow("SELECT agent_name FROM agent WHERE agent_enabled AND agent_pk=$1", array($agentId));
295  return ($row===false)?false:$row['agent_name'];
296  }
297 
302  public function getAgentRev($agentId)
303  {
304  $row = $this->dbManager->getSingleRow("SELECT agent_rev FROM agent WHERE agent_enabled AND agent_pk=$1", array($agentId));
305  return ($row===false)?false:$row['agent_rev'];
306  }
307 }
renewCurrentAgent($agentName)
Definition: AgentDao.php:275
getSuccessfulAgentRuns($agentName, $uploadId)
Definition: AgentDao.php:218
getSuccessfulAgentEntries($scannerName, $uploadId)
Definition: AgentDao.php:239
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:135
getRunningAgentIds($uploadId, $agentName)
Returns the list of running or failed agent_pk s. Before latest successful run.
Definition: AgentDao.php:175
__construct(DbManager $dbManager, Logger $logger)
Definition: AgentDao.php:42
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
getArsTableName($agentName)
Definition: AgentDao.php:266
getCurrentAgentRef($agentName)
Definition: AgentDao.php:206