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(\Rsi\Http::inSubnet($this->ignoreAddr,$this->component('security')->remoteAddr)) return false;
28  $this->component('log')->debug(__CLASS__ . "::inc('$id',$delta)",__FILE__,__LINE__);
29  $this->_stats[] = ['time' => time(),'id' => $id,'delta' => $delta];
30  }
31  /**
32  * Flush statistics to disk.
33  */
34  public function flush(){
35  if($this->path && $this->_stats){
36  \Rsi\File::serialize(
37  $this->path . \Rsi\Str::random() . $this->ext,
38  [
39  'addr' => $this->component('security')->remoteAddr,
40  'ua' => $this->component('client')->userAgent,
41  'stats' => $this->_stats
42  ],
43  $this->mode
44  );
45  $this->_stats = [];
46  }
47  }
48 
49 }
flush()
Flush statistics to disk.
Definition: Stats.php:34
$path
Path to flush (dump) the stats to.
Definition: Stats.php:10
inc($id, $delta=0)
Add a statistic.
Definition: Stats.php:26
$ignoreAddr
Addresses (subnets; CIDR notation) to ignore.
Definition: Stats.php:13
Statistics component.
Definition: Stats.php:8
Basic component class.
Definition: Component.php:8
const EVENT_HALT
Definition: Fred.php:20
component($name)
Get a component (local or default).
Definition: Component.php:80