Monitor   B
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 52.33%

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 213
ccs 45
cts 86
cp 0.5233
rs 8.3999
wmc 38
lcom 1
cbo 1

14 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 37 8
A Monitor() 0 10 2
A setValue() 0 9 4
A getValue() 0 3 1
A getStores() 0 3 1
A getMetrics() 0 3 1
A isDirty() 0 3 1
B save() 0 15 6
A clear() 0 9 3
A getStore() 0 16 3
A getSessionId() 0 7 3
A toArray() 0 8 3
A setEnabled() 0 3 1
A isEnabled() 0 3 1
1
<?php
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-2013 SugarCRM Inc.
6
7
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
8
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
9
 *
10
 * This program is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU Affero General Public License version 3 as published by the
12
 * Free Software Foundation with the addition of the following permission added
13
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
14
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
15
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
16
 *
17
 * This program is distributed in the hope that it will be useful, but WITHOUT
18
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
20
 * details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License along with
23
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
24
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
 * 02110-1301 USA.
26
 *
27
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
28
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
29
 *
30
 * The interactive user interfaces in modified source and object code versions
31
 * of this program must display Appropriate Legal Notices, as required under
32
 * Section 5 of the GNU Affero General Public License version 3.
33
 *
34
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
35
 * these Appropriate Legal Notices must retain the display of the "Powered by
36
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
37
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
38
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
39
 ********************************************************************************/
40
41
42
require_once('modules/Trackers/Metric.php');
43
require_once('modules/Trackers/Trackable.php');
44
45
define('MAX_SESSION_LENGTH', 36);
46
47
class Monitor implements Trackable {
48
49
    var $metricsFile;
50
    var $name;
51
    protected $metrics;
52
    protected $cachedStores;
53
    var $stores;
54
    var $monitor_id;
55
    var $table_name;
56
    protected $enabled = true;
57
	protected $dirty = false;
58
59
	var $date_start;
60
	var $date_end;
61
	var $active;
62
	var $round_trips;
63
	var $seconds;
64
	var $session_id;
65
66
    /**
67
     * Monitor constructor
68
     */
69 2
    public function __construct($name='', $monitorId='', $metadata='', $store='') {
70
71 2
    	if(empty($metadata) || !file_exists($metadata)) {
72
    	   $GLOBALS['log']->error($GLOBALS['app_strings']['ERR_MONITOR_FILE_MISSING'] . "($metadata)");
73
    	   throw new Exception($GLOBALS['app_strings']['ERR_MONITOR_FILE_MISSING'] . "($metadata)");
74
    	}
75
76 2
    	$this->name = $name;
77 2
    	$this->metricsFile = $metadata;
78
79 2
    	require($this->metricsFile);
80 2
    	$fields = $dictionary[$this->name]['fields'];
81 2
    	$this->table_name = !empty($dictionary[$this->name]['table']) ? $dictionary[$this->name]['table'] : $this->name;
82 2
    	$this->metrics = array();
83 2
    	foreach($fields as $field) {
84
85
    	   //We need to skip auto_increment fields; they are not real metrics
86
    	   //since they are generated by the database.
87 2
    	   if(isset($field['auto_increment'])) {
88 2
    	   	  continue;
89
    	   }
90
91 2
    	   $type = isset($field['dbType']) ? $field['dbType'] : $field['type'];
92 2
    	   $name = $field['name'];
93 2
    	   $this->metrics[$name] = new Metric($type, $name);
94
    	}
95
96 2
    	$this->monitor_id = $monitorId;
97 2
    	$this->stores = $store;
98
99 2
    	if(isset($this->metrics['session_id'])) {
100
	 		//set the value of the session id for 2 reasons:
101
	 		//1) it is the same value no matter where it is set
102
	 		//2) ensure it follows some filter rules.
103 2
	 		$this->setValue('session_id', $this->getSessionId());
104
    	}
105 2
    }
106
107
	/**
108
	 * @deprecated deprecated since version 7.6, PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code, use __construct instead
109
	 */
110
	public function Monitor($name='', $monitorId='', $metadata='', $store=''){
111
		$deprecatedMessage = 'PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code';
112
		if(isset($GLOBALS['log'])) {
113
			$GLOBALS['log']->deprecated($deprecatedMessage);
114
		}
115
		else {
116
			trigger_error($deprecatedMessage, E_USER_DEPRECATED);
117
		}
118
		self::__construct($name, $monitorId, $metadata, $store);
119
	}
120
121
    /**
122
     * setValue
123
     * Sets the value defined in the monitor's metrics for the given name
124
     * @param $name String value of metric name
125
     * @param $value Mixed value
126
     * @throws Exception Thrown if metric name is not configured for monitor instance
127
     */
128 69
    public function setValue($name, $value) {
129 69
        if(!isset($this->metrics[$name])) {
130
          $GLOBALS['log']->error($GLOBALS['app_strings']['ERR_UNDEFINED_METRIC'] . "($name)");
131
          throw new Exception($GLOBALS['app_strings']['ERR_UNDEFINED_METRIC'] . "($name)");
132 69
        } else if($this->metrics[$name]->isMutable()) {
133 69
          $this->$name = is_object($value) ? get_class($value) : $value;
134 69
          $this->dirty = true;
135
        }
136 69
    }
137
138 1
    public function getValue($name){
139 1
    	return $this->$name;
140
    }
141
142
    /**
143
     * getStores
144
     * Returns Array of store names defined for monitor instance
145
     * @return Array of store names defined for monitor instance
146
     */
147
    function getStores() {
148
        return $this->stores;
149
    }
150
151
    /**
152
     * getMetrics
153
     * Returns Array of metric instances defined for monitor instance
154
     * @return Array of metric instances defined for monitor instance
155
     */
156 1
    function getMetrics() {
157 1
    	return $this->metrics;
158
    }
159
160
	/**
161
	 * isDirty
162
	 * Returns if the monitor has data that needs to be saved
163
	 * @return $dirty boolean
0 ignored issues
show
Documentation introduced by
The doc-type $dirty could not be parsed: Unknown type name "$dirty" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
164
	 */
165
	function isDirty(){
166
		return $this->dirty;
167
	}
168
169
    /**
170
     * save
171
     * This method retrieves the Store instances associated with monitor and calls
172
     * the flush method passing with the montior ($this) instance.
173
     * @param $flush boolean parameter indicating whether or not to flush the instance data to store or possibly cache
174
     */
175
    public function save($flush=true) {
176
    	//If the monitor is not enabled, do not save
177
    	if(!$this->isEnabled()&&$this->name!='tracker_sessions')return false;
178
179
    	//if the monitor does not have values set no need to do the work saving.
180
    	if(!$this->dirty)return false;
181
182
    	if(empty($GLOBALS['tracker_' . $this->table_name])) {
183
    	    foreach($this->stores as $s) {
0 ignored issues
show
Bug introduced by
The expression $this->stores of type string is not traversable.
Loading history...
184
	    		$store = $this->getStore($s);
185
	    		$store->flush($this);
186
    		}
187
    	}
188
    	$this->clear();
189
    }
190
191
	/**
192
	 * clear
193
	 * This function clears the metrics data in the monitor instance
194
	 */
195 1
	public function clear() {
196 1
	    $metrics = $this->getMetrics();
197 1
	    foreach($metrics as $name=>$metric) {
198 1
	    	    if($metric->isMutable()) {
199 1
                   $this->$name = '';
200
	    	    }
201
	    }
202 1
		$this->dirty = false;
203 1
	}
204
205
	/**
206
	 * getStore
207
	 * This method checks if the Store implementation has already been instantiated and
208
	 * will return the one stored; otherwise it will create the Store implementation and
209
	 * save it to the Array of Stores.
210
	 * @param $store The name of the store as defined in the 'modules/Trackers/config.php' settings
211
	 * @return An instance of a Store implementation
212
	 * @throws Exception Thrown if $store class cannot be loaded
213
	 */
214
	protected function getStore($store) {
215
216
		if(isset($this->cachedStores[$store])) {
217
		   return $this->cachedStores[$store];
218
		}
219
220
        if(!file_exists("modules/Trackers/store/$store.php")) {
221
           $GLOBALS['log']->error($GLOBALS['app_strings']['ERR_STORE_FILE_MISSING'] . "($store)");
222
           throw new Exception($GLOBALS['app_strings']['ERR_STORE_FILE_MISSING'] . "($store)");
223
        }
224
225
		require_once("modules/Trackers/store/$store.php");
226
		$s = new $store();
227
		$this->cachedStores[$store] = $s;
228
		return $s;
229
	}
230
231 2
 	public function getSessionId(){
232 2
 		$sessionid = session_id();
233 2
	    if(!empty($sessionid) && strlen($sessionid) > MAX_SESSION_LENGTH) {
234
	       $sessionid = md5($sessionid);
235
	    }
236 2
	    return $sessionid;
237
 	}
238
239
 	/**
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
240
 	 * Returns the monitor's metrics/values as an Array
241
 	 * @return An Array of data for the monitor's corresponding metrics
242
 	 */
243
 	public function toArray() {
244
 		$to_arr = array();
245
 		$metrics = $this->getMetrics();
246
	    foreach($metrics as $name=>$metric) {
247
	    	    $to_arr[$name] = isset($this->$name) ? $this->$name : null;
248
	    }
249
	    return $to_arr;
250
 	}
251
252 2
 	public function setEnabled($enable=true) {
253 2
 		$this->enabled = $enable;
254 2
 	}
255
256 1
 	public function isEnabled() {
257 1
 		return $this->enabled;
258
 	}
259
}
260
261
?>
262