PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/pgfouine-1.2/include/lib/common.lib.php

#
PHP | 297 lines | 225 code | 52 blank | 20 comment | 17 complexity | 52bde8a6ddb0dccb46cd1bc19059fd87 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0
  1. <?php
  2. /*
  3. * This file is part of pgFouine.
  4. *
  5. * pgFouine - a PostgreSQL log analyzer
  6. * Copyright (c) 2005-2008 Guillaume Smet
  7. *
  8. * pgFouine is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * pgFouine is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with pgFouine; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  21. */
  22. define('MIN_TIMESTAMP', 0);
  23. define('MAX_TIMESTAMP', 9999999999);
  24. define('EVENT_QUERY', 'query_event_type');
  25. define('EVENT_ERROR', 'error_event_type');
  26. define('EVENT_DURATION_ONLY', 'duration_only_event_type');
  27. define('EVENT_VACUUM_TABLE', 'vacuum_table');
  28. define('EVENT_ANALYZE_TABLE', 'analyze_table');
  29. define('EVENT_FSM_INFORMATION', 'fsm_information');
  30. define('UNKNOWN_DATABASE', 'unknown');
  31. function debug($string, $displayLineNumber = false) {
  32. stderr($string, $displayLineNumber);
  33. }
  34. function stderr($string, $displayLineNumber = false) {
  35. global $stderr, $lineParsedCounter;
  36. if($displayLineNumber && $lineParsedCounter) {
  37. $string .= ' - log line '.$lineParsedCounter;
  38. }
  39. if($stderr) {
  40. fwrite($stderr, $string."\n");
  41. }
  42. }
  43. function stderrArray(& $array) {
  44. $content = getFormattedArray($array);
  45. stderr($content);
  46. }
  47. function getFormattedArray(& $array) {
  48. ob_start();
  49. print_r($array);
  50. $content = ob_get_contents();
  51. ob_end_clean();
  52. return $content;
  53. }
  54. function getMemoryUsage() {
  55. $memoryUsage = memory_get_usage();
  56. $output = 'Memory usage: ';
  57. if($memoryUsage < 1024) {
  58. $output .= intval($memoryUsage).' o';
  59. } elseif($memoryUsage < 1024*1024) {
  60. $output .= intval($memoryUsage/1024).' ko';
  61. } else {
  62. $output .= number_format(($memoryUsage/(1024*1024)), 2, '.', ' ').' mo';
  63. }
  64. return $output;
  65. }
  66. function formatTimestamp($timestamp) {
  67. return date('Y-m-d H:i:s', $timestamp);
  68. }
  69. function getExactPercentage($number, $total) {
  70. if($total > 0) {
  71. $percentage = $number*100/$total;
  72. } else {
  73. $percentage = 0;
  74. }
  75. return $percentage;
  76. }
  77. function normalizeWhitespaces($text, $keepOnlyIndent = false) {
  78. if($keepOnlyIndent) {
  79. $toReplace = '/(?<=[^\s])[ \t]+/m';
  80. } else {
  81. $text = trim($text);
  82. $toReplace = '/\s+/';
  83. }
  84. $text = preg_replace($toReplace, ' ', $text);
  85. return $text;
  86. }
  87. function &last(& $array) {
  88. if(empty($array)) {
  89. $last = false;
  90. } else {
  91. end($array);
  92. $last =& $array[key($array)];
  93. }
  94. return $last;
  95. }
  96. function &pop(& $array) {
  97. if(empty($array)) {
  98. $last = false;
  99. } else {
  100. $last =& last($array);
  101. array_pop($array);
  102. }
  103. return $last;
  104. }
  105. function arrayAdd($array1, $array2) {
  106. $size = count($array1);
  107. $sum = array();
  108. for($i = 0; $i < $size; $i++) {
  109. $sum[] = $array1[$i] + $array2[$i];
  110. }
  111. return $sum;
  112. }
  113. function str_putcsv($input, $delimiter = ',', $enclosure = '"') {
  114. $fp = fopen('php://temp', 'r+');
  115. fputcsv($fp, $input, $delimiter, $enclosure);
  116. rewind($fp);
  117. $data = fread($fp, 1048576);
  118. fclose($fp);
  119. return $data;
  120. }
  121. class RegExp {
  122. var $pattern;
  123. function RegExp($pattern) {
  124. $this->pattern = $pattern;
  125. }
  126. function & match($text) {
  127. $found = preg_match($this->pattern, $text, $matches, PREG_OFFSET_CAPTURE);
  128. $match = false;
  129. if($found) {
  130. $match = new RegExpMatch($text, $matches);
  131. }
  132. return $match;
  133. }
  134. function & matchAll($text) {
  135. $matches = array();
  136. $found = preg_match_all($this->pattern, $text, $matches, PREG_SET_ORDER);
  137. return $matches;
  138. }
  139. function replace($text, $replacement) {
  140. return preg_replace($this->pattern, $replacement, $text);
  141. }
  142. function getPattern() {
  143. return $this->pattern;
  144. }
  145. }
  146. class RegExpMatch {
  147. var $text;
  148. var $matches = array();
  149. function RegExpMatch($text, & $matches) {
  150. $this->text = $text;
  151. $this->matches =& $matches;
  152. }
  153. function & getMatches() {
  154. return $this->matches;
  155. }
  156. function getMatch($position) {
  157. if(isset($this->matches[$position])) {
  158. return $this->matches[$position][0];
  159. } else {
  160. return false;
  161. }
  162. }
  163. function getPostMatch() {
  164. $postMatch = substr($this->text, $this->matches[0][1] + strlen($this->matches[0][0]));
  165. return $postMatch;
  166. }
  167. function getMatchCount() {
  168. return count($this->matches);
  169. }
  170. }
  171. class QueryCounter {
  172. var $queryCount = 0;
  173. var $queryDuration = 0;
  174. var $identifiedQueryCount = 0;
  175. var $identifiedQueryDuration = 0;
  176. var $selectCount = 0;
  177. var $selectDuration = 0;
  178. var $updateCount = 0;
  179. var $updateDuration = 0;
  180. var $insertCount = 0;
  181. var $insertDuration = 0;
  182. var $deleteCount = 0;
  183. var $deleteDuration = 0;
  184. function incrementQuery($duration) {
  185. $this->queryCount ++;
  186. $this->queryDuration += $duration;
  187. }
  188. function incrementIdentifiedQuery($duration) {
  189. $this->identifiedQueryCount ++;
  190. $this->identifiedQueryDuration += $duration;
  191. }
  192. function incrementSelect($duration) {
  193. $this->selectCount ++;
  194. $this->selectDuration += $duration;
  195. }
  196. function incrementUpdate($duration) {
  197. $this->updateCount ++;
  198. $this->updateDuration += $duration;
  199. }
  200. function incrementInsert($duration) {
  201. $this->insertCount ++;
  202. $this->insertDuration += $duration;
  203. }
  204. function incrementDelete($duration) {
  205. $this->deleteCount ++;
  206. $this->deleteDuration += $duration;
  207. }
  208. function getQueryCount() {
  209. return $this->queryCount;
  210. }
  211. function getQueryDuration() {
  212. return $this->queryDuration;
  213. }
  214. function getIdentifiedQueryCount() {
  215. return $this->identifiedQueryCount;
  216. }
  217. function getIdentifiedQueryDuration() {
  218. return $this->identifiedQueryDuration;
  219. }
  220. function getSelectCount() {
  221. return $this->selectCount;
  222. }
  223. function getSelectDuration() {
  224. return $this->selectDuration;
  225. }
  226. function getUpdateCount() {
  227. return $this->updateCount;
  228. }
  229. function getUpdateDuration() {
  230. return $this->updateDuration;
  231. }
  232. function getInsertCount() {
  233. return $this->insertCount;
  234. }
  235. function getInsertDuration() {
  236. return $this->insertDuration;
  237. }
  238. function getDeleteCount() {
  239. return $this->deleteCount;
  240. }
  241. function getDeleteDuration() {
  242. return $this->deleteDuration;
  243. }
  244. }
  245. ?>