FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Stats.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 /**
6  * Statistics component.
7  */
8 class Stats extends Component{
9 
10  public $path = null; //!< Path to flush (dump) the stats to.
11  public $ext = '.stats';
12  public $mode = 0666;
13  public $ignoreAddr = []; //!< Addresses (subnets; CIDR notation) to ignore.
14 
15  protected $_stats = [];
16 
17  protected function init(){
18  parent::init();
19  $this->component('event')->listen(\Rsi\Fred::EVENT_HALT,[$this,'flush']);
20  }
21  /**
22  * Add a statistic.
23  * @param string $id Name of the statistic (usually with a class name prefix).
24  * @param float $delta Value to add to the counter of this statistic.
25  */
26  public function inc($id,$delta = 0){
27  if(($remote_addr = $this->component('security')->remoteAddr)
28  ? \Rsi\Http::inSubnet($this->ignoreAddr,$remote_addr)
29  : in_array(null,$this->ignoreAddr)
30  ) return false;
31  $this->component('log')->debug(__CLASS__ . "::inc('$id',$delta)",__FILE__,__LINE__);
32  $this->_stats[] = ['time' => time(),'id' => $id,'delta' => $delta];
33  }
34  /**
35  * Flush statistics to disk.
36  */
37  public function flush(){
38  if($this->path && $this->_stats){
39  \Rsi\File::serialize(
40  $this->path . \Rsi\Str::random() . $this->ext,
41  [
42  'addr' => $this->component('security')->remoteAddr,
43  'ua' => $this->component('client')->userAgent,
44  'stats' => $this->_stats
45  ],
46  $this->mode
47  );
48  $this->_stats = [];
49  }
50  }
51 
52 }
Rsi\Fred\Stats\init
init()
Definition: Stats.php:17
Rsi\Fred\Stats\$mode
$mode
Definition: Stats.php:12
Rsi
Rsi\Fred\Stats
Statistics component.
Definition: Stats.php:8
Rsi\Fred\Stats\flush
flush()
Flush statistics to disk.
Definition: Stats.php:37
Rsi\Fred\Stats\$ext
$ext
Definition: Stats.php:11
Rsi\Fred\Component
Basic component class.
Definition: Component.php:8
Rsi\Fred\Component\component
component($name)
Get a component (local or default).
Definition: Component.php:81
Rsi\Fred\Stats\$path
$path
Path to flush (dump) the stats to.
Definition: Stats.php:10
Rsi\Fred\Stats\inc
inc($id, $delta=0)
Add a statistic.
Definition: Stats.php:26
Rsi\Fred\Stats\$_stats
$_stats
Definition: Stats.php:15
Rsi\Fred\Stats\$ignoreAddr
$ignoreAddr
Addresses (subnets; CIDR notation) to ignore.
Definition: Stats.php:13
Rsi\Fred
Definition: Alive.php:3