_max_age = $max_age; if (!is_array($storage_array)) { $storage_array = array(); } $this->_values =& $storage_array; } /** Adds a value to the array * @param float The value to add * @param int The timestamp to use for this value, defaults to now */ function add($value, $timestamp=null) { if (!$timestamp) $timestamp = time(); $this->_values[$timestamp] = $value; } /** Calculate the average per second value * @return The average value, as a rate per second */ function average() { $this->_clean(); $avgs = array(); $last_time = false; $last_val = false; foreach ($this->_values as $time=>$val) { if ($last_time) { $avgs[] = ($val - $last_val) / ($time - $last_time); } $last_time = $time; $last_val = $val; } // return the average of all our averages if ($count = count($avgs)) { return array_sum($avgs) / $count; } else { return 'unknown'; } } /** Clean old values out of the array */ function _clean() { $too_old = time() - $this->_max_age; foreach (array_keys($this->_values) as $key) { if ($key < $too_old) { unset($this->_values[$key]); } } } } ?>