1<?php
2
3/*
4 * This file is part of pgFouine.
5 *
6 * pgFouine - a PostgreSQL log analyzer
7 * Copyright (c) 2005-2008 Guillaume Smet
8 *
9 * pgFouine is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * pgFouine is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with pgFouine; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
22 */
23
24class NormalizedQuery {
25	var $normalizedText;
26	var $duration = 0;
27	var $count = 0;
28	var $examples = false;
29	var $hourlyStatistics = array();
30
31	function NormalizedQuery(& $query) {
32		$this->normalizedText = $query->getNormalizedText();
33		$maxExamples = CONFIG_MAX_NUMBER_OF_EXAMPLES;
34		if($maxExamples) {
35			$this->examples = new SlowestQueryList($maxExamples);
36		}
37
38		$this->addQuery($query);
39	}
40
41	function addQuery(& $query) {
42		$this->count ++;
43		$this->duration += $query->getDuration();
44
45		$formattedTimestamp = date('Y-m-d H:00:00', $query->getTimestamp());
46		if(!isset($this->hourlyStatistics[$formattedTimestamp])) {
47			$this->hourlyStatistics[$formattedTimestamp]['count'] = 0;
48			$this->hourlyStatistics[$formattedTimestamp]['duration'] = 0;
49		}
50		$this->hourlyStatistics[$formattedTimestamp]['count']++;
51		$this->hourlyStatistics[$formattedTimestamp]['duration']+= $query->getDuration();
52
53		if($this->examples) {
54			$this->examples->addQuery($query);
55		}
56	}
57
58	function & getQuery() {
59		return $this->examples->getLastQuery();
60	}
61
62	function getNormalizedText() {
63		return $this->normalizedText;
64	}
65
66	function getTotalDuration() {
67		return $this->duration;
68	}
69
70	function getTimesExecuted() {
71		return $this->count;
72	}
73
74	function getAverageDuration() {
75		$average = 0;
76		if($this->count > 0) {
77			$average = ($this->duration/$this->count);
78		}
79		return $average;
80	}
81
82	function & getFilteredExamplesArray() {
83		$returnExamples = false;
84
85		$examples =& $this->examples->getSortedQueries();
86		$exampleCount = count($examples);
87		for($i = 0; $i < $exampleCount; $i++) {
88			$example =& $examples[$i];
89			if($example->getText() != $this->getNormalizedText()) {
90				return $examples;
91			}
92			unset($example);
93		}
94		$examples = array();
95		return $examples;
96	}
97
98	function & getHourlyStatistics() {
99		ksort($this->hourlyStatistics);
100		return $this->hourlyStatistics;
101	}
102}
103
104?>