2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
39 require_once('modules/Trackers/Metric.php');
40 require_once('modules/Trackers/Trackable.php');
42 define('MAX_SESSION_LENGTH', 36);
44 class Monitor implements Trackable {
49 protected $cachedStores;
53 protected $enabled = true;
54 protected $dirty = false;
66 function Monitor($name='', $monitorId='', $metadata='', $store='') {
68 if(empty($metadata) || !file_exists($metadata)) {
69 $GLOBALS['log']->error($GLOBALS['app_strings']['ERR_MONITOR_FILE_MISSING'] . "($metadata)");
70 throw new Exception($GLOBALS['app_strings']['ERR_MONITOR_FILE_MISSING'] . "($metadata)");
74 $this->metricsFile = $metadata;
76 require($this->metricsFile);
77 $fields = $dictionary[$this->name]['fields'];
78 $this->table_name = !empty($dictionary[$this->name]['table']) ? $dictionary[$this->name]['table'] : $this->name;
79 $this->metrics = array();
80 foreach($fields as $field) {
82 //We need to skip auto_increment fields; they are not real metrics
83 //since they are generated by the database.
84 if(isset($field['auto_increment'])) {
88 $type = isset($field['dbType']) ? $field['dbType'] : $field['type'];
89 $name = $field['name'];
90 $this->metrics[$name] = new Metric($type, $name);
93 $this->monitor_id = $monitorId;
94 $this->stores = $store;
96 if(isset($this->metrics['session_id'])) {
97 //set the value of the session id for 2 reasons:
98 //1) it is the same value no matter where it is set
99 //2) ensure it follows some filter rules.
100 $this->setValue('session_id', $this->getSessionId());
106 * Sets the value defined in the monitor's metrics for the given name
107 * @param $name String value of metric name
108 * @param $value Mixed value
109 * @throws Exception Thrown if metric name is not configured for monitor instance
111 public function setValue($name, $value) {
112 if(!isset($this->metrics[$name])) {
113 $GLOBALS['log']->error($GLOBALS['app_strings']['ERR_UNDEFINED_METRIC'] . "($name)");
114 throw new Exception($GLOBALS['app_strings']['ERR_UNDEFINED_METRIC'] . "($name)");
115 } else if($this->metrics[$name]->isMutable()) {
116 $this->$name = is_object($value) ? get_class($value) : $value;
121 public function getValue($name){
127 * Returns Array of store names defined for monitor instance
128 * @return Array of store names defined for monitor instance
130 function getStores() {
131 return $this->stores;
136 * Returns Array of metric instances defined for monitor instance
137 * @return Array of metric instances defined for monitor instance
139 function getMetrics() {
140 return $this->metrics;
145 * Returns if the monitor has data that needs to be saved
146 * @return $dirty boolean
154 * This method retrieves the Store instances associated with monitor and calls
155 * the flush method passing with the montior ($this) instance.
156 * @param $flush boolean parameter indicating whether or not to flush the instance data to store or possibly cache
158 public function save($flush=true) {
159 //If the monitor is not enabled, do not save
160 if(!$this->isEnabled()&&$this->name!='tracker_sessions')return false;
162 //if the monitor does not have values set no need to do the work saving.
163 if(!$this->dirty)return false;
165 if(empty($GLOBALS['tracker_' . $this->table_name])) {
166 foreach($this->stores as $s) {
167 $store = $this->getStore($s);
168 $store->flush($this);
176 * This function clears the metrics data in the monitor instance
178 public function clear() {
179 $metrics = $this->getMetrics();
180 foreach($metrics as $name=>$metric) {
181 if($metric->isMutable()) {
185 $this->dirty = false;
190 * This method checks if the Store implementation has already been instantiated and
191 * will return the one stored; otherwise it will create the Store implementation and
192 * save it to the Array of Stores.
193 * @param $store The name of the store as defined in the 'modules/Trackers/config.php' settings
194 * @return An instance of a Store implementation
195 * @throws Exception Thrown if $store class cannot be loaded
197 protected function getStore($store) {
199 if(isset($this->cachedStores[$store])) {
200 return $this->cachedStores[$store];
203 if(!file_exists("modules/Trackers/store/$store.php")) {
204 $GLOBALS['log']->error($GLOBALS['app_strings']['ERR_STORE_FILE_MISSING'] . "($store)");
205 throw new Exception($GLOBALS['app_strings']['ERR_STORE_FILE_MISSING'] . "($store)");
208 require_once("modules/Trackers/store/$store.php");
210 $this->cachedStores[$store] = $s;
214 public function getSessionId(){
215 $sessionid = session_id();
216 if(!empty($sessionid) && strlen($sessionid) > MAX_SESSION_LENGTH) {
217 $sessionid = md5($sessionid);
223 * Returns the monitor's metrics/values as an Array
224 * @return An Array of data for the monitor's corresponding metrics
226 public function toArray() {
228 $metrics = $this->getMetrics();
229 foreach($metrics as $name=>$metric) {
230 $to_arr[$name] = isset($this->$name) ? $this->$name : null;
235 public function setEnabled($enable=true) {
236 $this->enabled = $enable;
239 public function isEnabled() {
240 return $this->enabled;