Alpha Framework alpha--util
[ class tree: alpha--util ] [ index: alpha--util ] [ all elements ]

Source for file AlphaKPI.inc

Documentation is available at AlphaKPI.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/util/helpers/AlphaValidator.inc';
  4. require_once $config->get('sysRoot').'alpha/model/types/Timestamp.inc';
  5. require_once $config->get('sysRoot').'alpha/model/types/String.inc';
  6.  
  7. /**
  8.  *
  9.  * A Key Performance Indicator (KPI) logging class
  10.  * 
  11.  * @package alpha::util
  12.  * @since 1.1
  13.  * @author John Collins <dev@alphaframework.org>
  14.  * @version $Id: AlphaKPI.inc 1454 2011-12-04 15:14:05Z johnc $
  15.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  16.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  17.  *  All rights reserved.
  18.  * 
  19.  *  <pre>
  20.  *  Redistribution and use in source and binary forms, with or
  21.  *  without modification, are permitted provided that the
  22.  *  following conditions are met:
  23.  * 
  24.  *  * Redistributions of source code must retain the above
  25.  *    copyright notice, this list of conditions and the
  26.  *    following disclaimer.
  27.  *  * Redistributions in binary form must reproduce the above
  28.  *    copyright notice, this list of conditions and the
  29.  *    following disclaimer in the documentation and/or other
  30.  *    materials provided with the distribution.
  31.  *  * Neither the name of the Alpha Framework nor the names
  32.  *    of its contributors may be used to endorse or promote
  33.  *    products derived from this software without specific
  34.  *    prior written permission.
  35.  *   
  36.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  37.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  38.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  39.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  40.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  41.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  43.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  46.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  47.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  48.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49.  *  </pre>
  50.  *  
  51.  */
  52. class AlphaKPI {
  53.     /**
  54.      * The date/time of the KPI event
  55.      * 
  56.      * @var Timestamp 
  57.      * @since 1.1
  58.      */
  59.     private $timeStamp;
  60.     
  61.     /**
  62.      * The name of the KPI
  63.      * 
  64.      * @var String 
  65.      * @since 1.1
  66.      */
  67.     private $name;
  68.     
  69.     /**
  70.      * The session ID of the current HTTP session
  71.      * 
  72.      * @var string 
  73.      * @since 1.1
  74.      */
  75.     private $sessionID;
  76.     
  77.     /**
  78.      * The start time of the KPI event (UNIX timestamp in seconds)
  79.      * 
  80.      * @var float 
  81.      * @since 1.1
  82.      */
  83.     private $startTime;
  84.     
  85.     /**
  86.      * The end time of the KPI event (UNIX timestamp in seconds)
  87.      * 
  88.      * @var float 
  89.      * @since 1.1
  90.      */
  91.     private $endTime;
  92.     
  93.     /**
  94.      * The duration in seconds
  95.      * 
  96.      * @var float 
  97.      * @since 1.1
  98.      */
  99.     private $duration;
  100.     
  101.     /**
  102.      * Constructor
  103.      * 
  104.      * @param string $name The name of the KPI which is used in the log files, must only be letters and/or numbers.
  105.      * @throws IllegalArguementException
  106.      * @since 1.1
  107.      */
  108.     public function __construct($name{
  109.         $this->name new String();
  110.         $this->name->setRule(AlphaValidator::REQUIRED_ALPHA_NUMBEIC);
  111.         $this->name->setHelper('The KPI name can only contain letters and numbers');
  112.         
  113.         $this->name->setValue($name);
  114.         
  115.         $this->timeStamp new Timestamp(date('Y-m-d H:i:s'));
  116.         
  117.         $this->startTime microtime(true);
  118.         
  119.         if(!isset($_SESSION))
  120.              session_start();
  121.          
  122.          // a startTime value may have been passed from a previous request
  123.          if(isset($_SESSION[$name.'-startTime'])) {
  124.              $this->startTime $_SESSION[$name.'-startTime'];
  125.              $_SESSION[$name.'-startTime'null;
  126.          }
  127.              
  128.          $this->sessionID session_id();
  129.     }
  130.     
  131.     /**
  132.      * Stores the current startTime for the KPI in the session, useful for multi-request KPI tracking.
  133.      * 
  134.      * @since 1.0
  135.      */
  136.     public function storeStartTimeInSession({
  137.         $_SESSION[$this->name->getValue().'-startTime'$this->startTime;
  138.     }
  139.     
  140.     /**
  141.      * Writes the KPI event to a log file named logs/kpi-'.$this->name->getValue().'.csv, which will be created if it does
  142.      * not exist.
  143.      * 
  144.      * @since 1.1
  145.      */
  146.     public function log({
  147.         global $config;
  148.         
  149.         $this->endTime microtime(true);
  150.         
  151.         $this->duration $this->endTime $this->startTime;
  152.         
  153.         $logfile new LogFile($config->get('sysRoot').'logs/kpi-'.$this->name->getValue().'.csv');
  154.         
  155.         $logfile->setMaxSize($config->get('sysLogFileMaxSize'));
  156.         $logfile->setSeperator(',');
  157.         
  158.         $logfile->writeLine(array($this->timeStamp$this->name->getValue()$this->sessionID$this->startTime$this->endTime
  159.             $this->duration));
  160.     }
  161.     
  162.     /**
  163.      * Writes a step in the KPI event to a log file named logs/kpi-'.$this->name->getValue().'.csv, which will be created if it does
  164.      * not exist.
  165.      * 
  166.      * @since 1.1
  167.      */
  168.     public function logStep($stepName{
  169.         global $config;
  170.         
  171.         $this->endTime microtime(true);
  172.         
  173.         $this->duration $this->endTime $this->startTime;
  174.         
  175.         $logfile new LogFile($config->get('sysRoot').'logs/kpi-'.$this->name->getValue().'.csv');
  176.         
  177.         $logfile->setMaxSize($config->get('sysLogFileMaxSize'));
  178.         $logfile->setSeperator(',');
  179.         
  180.         $logfile->writeLine(array($this->timeStamp$this->name->getValue().' ['.$stepName.']'$this->sessionID$this->startTime
  181.             $this->endTime$this->duration));
  182.     }
  183. }
  184.  
  185. ?>

Documentation generated on Tue, 13 Dec 2011 20:26:09 +0000 by phpDocumentor 1.4.3