FOSSology  4.8.0-rc1
Open Source License Compliance by Open Source Software
admin-folder-size.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2023 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
10 
11 define("TITLE_SIZE_DASHBOARD", _("Folder and upload dashboard"));
12 
14 {
15 
17  private $dbManager;
18 
22  private $uploadDao;
23 
24  function __construct()
25  {
26  $this->Name = "size_dashboard";
27  $this->Title = TITLE_SIZE_DASHBOARD;
28  $this->MenuList = "Admin::Dashboards::Folder/Upload Proportions";
29  $this->Dependency = array();
30  $this->DBaccess = PLUGIN_DB_WRITE;
31  parent::__construct();
32  $this->dbManager = $GLOBALS['container']->get('db.manager');
33  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
34  }
35 
41  function getFolderAndUploadSize($folderId)
42  {
43  $sql = 'INNER JOIN upload ON upload.pfile_fk=pfile.pfile_pk '.
44  'INNER JOIN foldercontents ON upload.upload_pk=foldercontents.child_id '.
45  'INNER JOIN upload_clearing ON upload.upload_pk=upload_clearing.upload_fk '.
46  'WHERE parent_fk=$1';
47  $statementName = __METHOD__."GetFolderSize";
48  $folderSizesql = 'SELECT SUM(pfile_size) FROM (' .
49  'SELECT DISTINCT upload.upload_pk, pfile_size FROM pfile ' . $sql .
50  ') AS folder_uploads';
51  $row = $this->dbManager->getSingleRow($folderSizesql,array($folderId),$statementName);
52  $folderSize = HumanSize($row['sum']);
53 
54  $dispSql = "SELECT DISTINCT ON (upload.upload_pk) upload_pk, upload_filename, pfile_size, " .
55  "to_char(upload_ts, 'YYYY-MM-DD HH24:MI:SS') AS upload_ts, status_fk FROM pfile " . $sql . " ORDER BY upload.upload_pk";
56  $statementNameDisp = __METHOD__ . "GetEachUploadSize";
57  $results = $this->dbManager->getRows($dispSql, [$folderId], $statementNameDisp);
58 
59  $uploadIds = array_column($results, 'upload_pk');
60  $durationBatch = $this->uploadDao->getClearingDurationsBatch($uploadIds);
61 
62  $var = '';
63  foreach ($results as $result) {
64  $clearingDuration = $durationBatch[$result["upload_pk"]] ?? ['NA', 0];
65  $var .= "<tr><td align='left'>" . $result['upload_pk'] .
66  "</td><td align='left'>" . $result['upload_filename'] .
67  "</td><td align='left' data-order='{$result['pfile_size']}'>" .
68  HumanSize($result['pfile_size']) .
69  "</td><td align='left' data-order='{$clearingDuration[1]}'>$clearingDuration[0]</td>
70  <td align='left'>{$result['upload_ts']}</td><td align='left'>" . $this->ConvertStatusToString($result['status_fk']) .
71  "</td></tr>";
72  }
73  return [$var, $folderSize];
74  }
75 
81  private function generateExportData($folderId, $format)
82  {
83  $results = $this->dbManager->getRows(
84  "SELECT DISTINCT ON (upload.upload_pk) upload_pk, upload_filename, pfile_size, to_char(upload_ts, 'YYYY-MM-DD HH24:MI:SS') AS upload_ts, status_fk " .
85  "FROM pfile " .
86  "INNER JOIN upload ON upload.pfile_fk=pfile.pfile_pk " .
87  "INNER JOIN foldercontents ON upload.upload_pk=foldercontents.child_id " .
88  "INNER JOIN upload_clearing ON upload.upload_pk=upload_clearing.upload_fk " .
89  "WHERE parent_fk=$1 ORDER BY upload.upload_pk",
90  [$folderId],
91  __METHOD__ . "ExportData"
92  );
93 
94  $uploadIds = array_column($results, 'upload_pk');
95  $durationBatch = $this->uploadDao->getClearingDurationsBatch($uploadIds);
96 
97  $data = [];
98  foreach ($results as $row) {
99  $clearingDuration = $durationBatch[$row["upload_pk"]] ?? ['NA', 0];
100  $data[] = [
101  'uploadid' => $row['upload_pk'],
102  'name' => $row['upload_filename'],
103  'size' => $row['pfile_size'],
104  'duration' => $clearingDuration[1],
105  'date' => $row['upload_ts'],
106  'status' => $this->ConvertStatusToString($row['status_fk'])
107  ];
108  }
109 
110  switch ($format) {
111  case 'csv':
112  $this->outputCSV($data);
113  break;
114  case 'json':
115  $this->outputJSON($data);
116  break;
117  }
118  }
119 
124  private function ConvertStatusToString($status)
125  {
126  $statusString = 'Open';
127  if ($status == 2) {
128  $statusString = 'In progress';
129  } else if ($status == 3) {
130  $statusString = 'Closed';
131  } else if ($status == 4) {
132  $statusString = 'Rejected';
133  }
134 
135  return $statusString;
136  }
137 
142  private function outputCSV($data)
143  {
144  header('Content-Type: text/csv');
145  header('Content-Disposition: attachment; filename="folder_export.csv"');
146 
147  $output = fopen('php://output', 'w');
148  fputcsv($output, ['Upload id', 'Upload name', 'Size (bytes)', 'Clearing duration (seconds)', 'Upload date', 'Upload status']);
149 
150  foreach ($data as $row) {
151  fputcsv($output, [
152  $row['uploadid'],
153  $row['name'],
154  $row['size'],
155  $row['duration'],
156  $row['date'],
157  $row['status']
158  ]);
159  }
160  fclose($output);
161  exit;
162  }
163 
168  private function outputJSON($data)
169  {
170  header('Content-Type: application/json');
171  header('Content-Disposition: attachment; filename="folder_export.json"');
172  echo json_encode($data, JSON_PRETTY_PRINT);
173  exit;
174  }
175 
179  public function Output()
180  {
181  $exportFormat = GetParm('export', PARM_STRING);
182  if (!empty($exportFormat) && in_array($exportFormat, ['csv', 'json'])) {
183  $folderId = GetParm('folder', PARM_INTEGER);
184  $this->generateExportData($folderId, $exportFormat);
185  }
186  /* If this is a POST, then process the request. */
187  $folderId = GetParm('selectfolderid', PARM_INTEGER);
188  if (empty($folderId)) {
189  $folderId = FolderGetTop();
190  }
191  list($tableVars, $wholeFolderSize) = $this->getFolderAndUploadSize($folderId);
192 
193  /* Display the form */
194  $formVars["onchangeURI"] = Traceback_uri() . "?mod=" . $this->Name . "&selectfolderid=";
195  $formVars["folderListOption"] = FolderListOption(-1, 0, 1, $folderId);
196  $formVars["tableVars"] = $tableVars;
197  $formVars["wholeFolderSize"] = $wholeFolderSize;
198  $formVars["currentFolderId"] = $folderId;
199  $formVars["pluginName"] = $this->Name;
200  return $this->renderString("admin-folder-size-form.html.twig", $formVars);
201  }
202 }
203 $NewPlugin = new size_dashboard;
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
renderString($templateName, $vars=null)
Definition: FO_Plugin.php:414
getFolderAndUploadSize($folderId)
Given a folder's ID function will get size of the folder and uploads under it.
__construct()
base constructor. Most plugins will just use this
Output()
Generate the text for this plugin.
outputJSON($data)
Outputs data in JSON format.
ConvertStatusToString($status)
convert numaric status into string.
generateExportData($folderId, $format)
Generate export data in CSV or JSON format for a given folder ID.
outputCSV($data)
Outputs data in CSV format.
FolderListOption($ParentFolder, $Depth, $IncludeTop=1, $SelectId=-1, $linkParent=false, $OldParent=0)
Create the folder tree, using OPTION tags.
FolderGetTop()
DEPRECATED! Find the top-of-tree folder_pk for the current user.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_INTEGER
Definition: common-parm.php:14
const PARM_STRING
Definition: common-parm.php:18
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
HumanSize( $bytes)
Translate a byte number to a proper type, xxx bytes to xxx B/KB/MB/GB/TB/PB.
Definition: common-ui.php:100
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16