This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
/
ezperfloggercsvstorage.php
93 lines (85 loc) · 3.03 KB
/
ezperfloggercsvstorage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
* Class used to store performance data in a csv file.
*
* The idea is to store data in a format more friendly to spreadsheets than the web server access log
*
* @author G. Giunta
* @copyright (C) eZ Systems AS 2008-2013
* @license Licensed under GNU General Public License v2.0. See file license.txt
*/
class eZPerfLoggerCSVStorage implements eZPerfLoggerStorage, eZPerfLoggerLogParser
{
/**
* @see eZPerfLoggerStorage::insertStats
* @see http://tools.ietf.org/html/rfc4180
* @param array $data
* @return bool
*/
public static function insertStats( array $data )
{
list( $csvfile, $separator, $quotes ) = eZPerfLoggerINI::variableMulti( 'csvSettings', array( 'FileName', 'Separator', 'Quotes' ) );
$addheader = false;
if ( !file_exists( $csvfile ) )
{
$addheader = true;
}
$fp = fopen( $csvfile, 'a' );
if ( !$fp )
{
return false;
}
if ( $addheader )
{
fwrite( $fp, "Timestamp{$separator}" );
fwrite( $fp, implode( $separator, eZPerfLoggerINI::variable( 'GeneralSettings', 'TrackVariables' ) ) );
fwrite( $fp, "{$separator}Date{$separator}IP Address{$separator}Response Status{$separator}Response size{$separator}URL\n" );
}
foreach( $data as $line )
{
$data = array_merge( array( $line['time'] ), $line['counters'] );
$data[] = date( 'd/M/Y:H:i:s O', $line['time'] );
$data[] = $line['ip'];
$data[] = $line['response_status'];
$data[] = $line['response_size'];
$data[] = $quotes . str_replace( $quotes, $quotes . $quotes, $line['url'] ). $quotes;
fwrite( $fp, implode( $separator, $data ) . "\n" );
}
fclose( $fp );
return true;
}
static public function parseLogLine( $line, $counters = array(), $excludeRegexps = array() )
{
$countersCount = count( $counters );
$separator = eZPerfLoggerINI::variable( 'csvSettings', 'Separator' );
$logPartArray = explode( $separator, $line, $countersCount + 6 );
if ( count( $logPartArray ) < $countersCount + 6 )
{
return false;
}
$url = rtrim( $logPartArray[$countersCount + 5], "\n" );
foreach( $excludeRegexps as $regexp )
{
if ( preg_match( $regexp, $url ) )
{
return true;
}
}
return array(
'url' => $url,
'time' => $logPartArray[0],
'ip' => $logPartArray[$countersCount + 2],
'response_status' => $logPartArray[$countersCount + 3],
'response_size' => $logPartArray[$countersCount + 4],
'counters' => array_slice( $logPartArray, 1, $countersCount ) );
}
/**
* This one is empty, as we take all options from ini files.
*
* @todo !important refactor so that we pass thorugh here instead
*/
static public function setOptions( array $opts )
{
}
}
?>