Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
report_rollback.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/assets/system/simple_report/simple_report.inc';
18 
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  parent::__construct($assetid);
44 
45  }//end constructor
46 
47 
58  public static function paintReport(Backend_Outputter &$o, $type_code)
59  {
60  $report = Report_Rollback::loadReport($type_code);
61 
62  if (empty($report)) {
64  return TRUE;
65  }
66 
67  $generated = $report['generated'];
68  $report = $report['report_data'];
69 
70  $row_format = '<tr>
71  <td class="sq-backend-table-cell">
72  %s
73  </td>
74  <td class="sq-backend-table-cell" align="right">
75  %s
76  </td>
77  </tr>';
78 
79  $current_asset_rollback_table = '<br /><table class="sq-backend-table">
80  <tr>
81  <th class="sq-backend-table-header" width="45%%">Rollback asset entries currently in the system</th>
82  <td align="right">'.$report['num_asset_rb_entries_today'].'</td>
83  </tr>
84  </table>';
85 
86  $current_link_rollback_table = '<br /><table class="sq-backend-table">
87  <tr>
88  <th class="sq-backend-table-header" width="45%%">Rollback link entries currently in the system</th>
89  <td align="right">'.$report['num_link_rb_entries_today'].'</td>
90  </tr>
91  </table>';
92 
93  $row_string = '';
94  $asset_report = $report['asset_date_report'];
95  foreach ($asset_report as $date_description => $date_data) {
96  $row_string .= sprintf($row_format, $date_description, $date_data['num_assets']);
97  }
98 
99  $table_format = '<table class="sq-backend-table">
100  <tr>
101  <th class="sq-backend-table-header">Period</th>
102  <th class="sq-backend-table-header" width="35%%" align="right">Rollback %s entries in the system</th>
103  </tr>
104  %s
105  </table>';
106 
107  $asset_report_table = sprintf($table_format, 'asset', $row_string);
108 
109  $row_string = '';
110  $link_report = $report['link_date_report'];
111  foreach ($link_report as $date_description => $date_data) {
112  $row_string .= sprintf($row_format, $date_description, $date_data['num_assets']);
113  }
114 
115  $link_report_table = sprintf($table_format, 'link', $row_string);
116 
117  $am = $GLOBALS['SQ_SYSTEM']->am;
118 
119  $table_format = '<table class="sq-backend-table">
120  <tr>
121  <th class="sq-backend-table-header">Icon</th>
122  <th class="sq-backend-table-header">Asset ID</th>
123  <th class="sq-backend-table-header">Name</th>
124  <th class="sq-backend-table-header">First Rollback Entry</th>
125  <th class="sq-backend-table-header">Last Rollback Entry</th>
126  <th class="sq-backend-table-header">Total Entries</th>
127  </tr>
128  %s
129  </table>';
130 
131  $row_format = '<tr>
132  <td class="sq-backend-table-cell">
133  <image src="%s">
134  </td>
135  <td class="sq-backend-table-cell">
136  %s
137  </td>
138  <td class="sq-backend-table-cell">
139  %s
140  </td>
141  <td class="sq-backend-table-cell">
142  %s
143  </td>
144  <td class="sq-backend-table-cell">
145  %s
146  </td>
147  <td class="sq-backend-table-cell" align="right">
148  %s
149  </td>
150  </tr>';
151 
152 
153  $row_string = '';
154  $most_asset_rollback_entries_report = $report['most_asset_rb_entries_report'];
155  foreach ($most_asset_rollback_entries_report as $asset_data) {
156  $row_string .= sprintf($row_format, $am->getAssetIconURL($asset_data['type_code']), $asset_data['assetid'],
157  $asset_data['name'], $asset_data['first_rb_entry'], $asset_data['last_rb_entry'],
158  $asset_data['total']);
159  }
160 
161  $most_asset_rollback_entries_table = sprintf($table_format, $row_string);
162 
163  Report_Rollback::paintLastGeneratedDate($type_code, date('d M Y, H:i:s', $generated));
164  Report_Rollback::paintRecommendedTasks($o, $type_code, Array('tool_rollback_management'));
165 
166  echo '<br />'.$current_asset_rollback_table;
167  echo $asset_report_table;
168 
169  if (!empty($rows_string)) {
170  echo $most_asset_rollback_entries_table;
171  }
172 
173  echo '<br />'.$current_link_rollback_table;
174  echo $link_report_table;
175 
176  }//end paintReport()
177 
178 
189  public static function processReport(Backend_Outputter &$o, $type_code)
190  {
191  $today = time();
192 
193  $year = date('Y', $today);
194  $month = date('m', $today);
195  $date = date('d', $today);
196 
197  $last_month = str_pad(date('m', $today) - 1, 2, '0', STR_PAD_LEFT);
198 
199  $date_yesterday = date('Y-m-d', $today - (24*3600));
200  $last_week_date = date('Y-m-d', $today - (24*3600*7));
201  $last_month_date = date('Y-m-d', $today - (30*24*3600));
202  $last_quarter_date = date('Y-m-d', $today - (90*24*3600));
203  $last_semester_date = date('Y-m-d', $today - (180*24*3600));
204  $last_year_date = date('Y-m-d', $today - (365*24*3600));
205 
206  $asset_date_report = Array(
207  'Yesterday' => Array(
208  'date' => $date_yesterday,
209  'num_assets' => 0,
210  ),
211  'Last week' => Array(
212  'date' => $last_week_date,
213  'num_assets' => 0,
214  ),
215  'Last month' => Array(
216  'date' => $last_month_date,
217  'num_assets' => 0,
218  ),
219  'Last three months' => Array(
220  'date' => $last_quarter_date,
221  'num_assets' => 0,
222  ),
223  'Last six months' => Array(
224  'date' => $last_semester_date,
225  'num_assets' => 0,
226  ),
227  'Last year' => Array(
228  'date' => $last_year_date,
229  'num_assets' => 0,
230  ),
231  );
232 
233  $link_date_report = $asset_date_report;
234 
235  $assets_since_last_period = Array();
236 
237  $num_asset_rb_entries_today = Report_Rollback::_getTotalAssetRollbackEntries();
238  $num_link_rb_entries_today = Report_Rollback::_getTotalLinkRollbackEntries();
239 
240  // Asset Rollback report
241  $total = 0;
242  $last_period = 0;
243  $remove_date_entries = Array();
244  foreach ($asset_date_report as $date_description => $date_data) {
245  if ($last_period == 1) {
246  $last_period++;
247  } else if ($last_period > 1) {
248  $remove_date_entries[] = $date_description;
249  continue;
250  }
251 
252  $num_assets = Report_Rollback::_getTotalAssetRollbackEntries($date_data['date']);
253  $asset_date_report[$date_description]['num_assets'] = $num_asset_rb_entries_today - $num_assets;
254  $total += $asset_date_report[$date_description]['num_assets'];
255  if ($total == $num_asset_rb_entries_today) {
256  $last_period++;
257  }
258  }
259 
260  foreach ($remove_date_entries as $date_description) {
261  unset($asset_date_report[$date_description]);
262  }
263 
264 
265  // Links Rollback report
266  $total = 0;
267  $last_period = 0;
268  $remove_date_entries = Array();
269  foreach ($link_date_report as $date_description => $date_data) {
270  if ($last_period == 1) {
271  $last_period++;
272  } else if ($last_period > 1) {
273  $remove_date_entries[] = $date_description;
274  continue;
275  }
276 
277  $num_assets = Report_Rollback::_getTotalLinkRollbackEntries($date_data['date']);
278  $link_date_report[$date_description]['num_assets'] = $num_link_rb_entries_today - $num_assets;
279  $total += $link_date_report[$date_description]['num_assets'];
280  if ($total == $num_link_rb_entries_today) {
281  $last_period++;
282  }
283  }
284 
285  foreach ($remove_date_entries as $date_description) {
286  unset($link_date_report[$date_description]);
287  }
288 
289  // Most Asset Rollback entries
290 
291  $most_asset_rb_entries_report = Report_Rollback::_getMostAssetRollbackEntries();
292 
293  $report = Array(
294  'generated' => $today,
295  'report_data' => Array(
296  'num_asset_rb_entries_today' => $num_asset_rb_entries_today,
297  'num_link_rb_entries_today' => $num_link_rb_entries_today,
298  'asset_date_report' => $asset_date_report,
299  'link_date_report' => $link_date_report,
300  'most_asset_rb_entries_report' => $most_asset_rb_entries_report,
301  ),
302  );
303 
304  Report_Rollback::saveReport($type_code, $report);
305 
306  return TRUE;
307 
308  }//end processReport()
309 
310 
318  private static function _getMostAssetRollbackEntries()
319  {
320  $db = MatrixDAL::getDb();
321  $sql = 'SELECT r.assetid, a.name, a.type_code, MIN(r.sq_eff_from) AS first_rb_entry, MAX(r.sq_eff_to) AS last_rb_entry, '.
322  'COUNT(*) AS total '.
323  'FROM sq_rb_ast r, sq_ast a '.
324  'WHERE a.assetid = r.assetid '.
325  'GROUP BY r.assetid, a.name, a.type_code '.
326  'ORDER BY total DESC, first_rb_entry DESC, last_rb_entry DESC';
327  $sql = db_extras_modify_limit_clause($sql, MatrixDAL::getDbType(), 10);
328 
329  $rows = MatrixDAL::executeSqlAssoc($sql);
330 
331  return $rows;
332 
333  }//end _getMostAssetRollbackEntries()
334 
335 
343  private static function _getTotalAssetRollbackEntries($after_date='')
344  {
345  $after_date_query = '';
346  if ($after_date != '') {
347  $after_date_query = ' WHERE sq_eff_from >= '.MatrixDAL::quote($after_date);
348  }
349 
350  $db = MatrixDAL::getDb();
351  $sql = 'SELECT COUNT(*) AS total '.
352  'FROM sq_rb_ast'.$after_date_query;
353 
354  $num_entries = MatrixDAL::executeSqlOne($sql);
355 
356  return $num_entries;
357 
358  }//end _getNumAssetRollbackEntries()
359 
360 
368  private static function _getTotalLinkRollbackEntries($after_date='')
369  {
370  $after_date_query = '';
371  if ($after_date != '') {
372  $after_date_query = ' WHERE sq_eff_from >= '.MatrixDAL::quote($after_date);
373  }
374 
375  $db = MatrixDAL::getDb();
376  $sql = 'SELECT COUNT(*) AS total '.
377  'FROM sq_rb_ast_lnk'.$after_date_query;
378 
379  $num_entries = MatrixDAL::executeSqlOne($sql);
380 
381  return $num_entries;
382 
383  }//end _getTotalLinkRollbackEntries()
384 
385 
386 }//end class
387 ?>