]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Trackers/monitor/Monitor.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Trackers / monitor / Monitor.php
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-2012 SugarCRM Inc.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38
39 require_once('modules/Trackers/Metric.php');
40 require_once('modules/Trackers/Trackable.php');
41
42 define('MAX_SESSION_LENGTH', 36);
43
44 class Monitor implements Trackable {
45
46     var $metricsFile;
47     var $name;
48     protected $metrics;
49     protected $cachedStores;
50     var $stores;
51     var $monitor_id;
52     var $table_name;
53     protected $enabled = true;
54         protected $dirty = false;
55
56         var $date_start;
57         var $date_end;
58         var $active;
59         var $round_trips;
60         var $seconds;
61         var $session_id;
62
63     /**
64      * Monitor constructor
65      */
66     function Monitor($name='', $monitorId='', $metadata='', $store='') {
67
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)");
71         }
72
73         $this->name = $name;
74         $this->metricsFile = $metadata;
75
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) {
81
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'])) {
85                   continue;
86            }
87
88            $type = isset($field['dbType']) ? $field['dbType'] : $field['type'];
89            $name = $field['name'];
90            $this->metrics[$name] = new Metric($type, $name);
91         }
92
93         $this->monitor_id = $monitorId;
94         $this->stores = $store;
95
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());
101         }
102     }
103
104     /**
105      * setValue
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
110      */
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;
117           $this->dirty = true;
118         }
119     }
120
121     public function getValue($name){
122         return $this->$name;
123     }
124
125     /**
126      * getStores
127      * Returns Array of store names defined for monitor instance
128      * @return Array of store names defined for monitor instance
129      */
130     function getStores() {
131         return $this->stores;
132     }
133
134     /**
135      * getMetrics
136      * Returns Array of metric instances defined for monitor instance
137      * @return Array of metric instances defined for monitor instance
138      */
139     function getMetrics() {
140         return $this->metrics;
141     }
142
143         /**
144          * isDirty
145          * Returns if the monitor has data that needs to be saved
146          * @return $dirty boolean
147          */
148         function isDirty(){
149                 return $this->dirty;
150         }
151
152     /**
153      * save
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
157      */
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;
161
162         //if the monitor does not have values set no need to do the work saving.
163         if(!$this->dirty)return false;
164
165         if(empty($GLOBALS['tracker_' . $this->table_name])) {
166             foreach($this->stores as $s) {
167                         $store = $this->getStore($s);
168                         $store->flush($this);
169                 }
170         }
171         $this->clear();
172     }
173
174         /**
175          * clear
176          * This function clears the metrics data in the monitor instance
177          */
178         public function clear() {
179             $metrics = $this->getMetrics();
180             foreach($metrics as $name=>$metric) {
181                     if($metric->isMutable()) {
182                    $this->$name = '';
183                     }
184             }
185                 $this->dirty = false;
186         }
187
188         /**
189          * getStore
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
196          */
197         protected function getStore($store) {
198
199                 if(isset($this->cachedStores[$store])) {
200                    return $this->cachedStores[$store];
201                 }
202
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)");
206         }
207
208                 require_once("modules/Trackers/store/$store.php");
209                 $s = new $store();
210                 $this->cachedStores[$store] = $s;
211                 return $s;
212         }
213
214         public function getSessionId(){
215                 $sessionid = session_id();
216             if(!empty($sessionid) && strlen($sessionid) > MAX_SESSION_LENGTH) {
217                $sessionid = md5($sessionid);
218             }
219             return $sessionid;
220         }
221
222         /**
223          * Returns the monitor's metrics/values as an Array
224          * @return An Array of data for the monitor's corresponding metrics
225          */
226         public function toArray() {
227                 $to_arr = array();
228                 $metrics = $this->getMetrics();
229             foreach($metrics as $name=>$metric) {
230                     $to_arr[$name] = isset($this->$name) ? $this->$name : null;
231             }
232             return $to_arr;
233         }
234
235         public function setEnabled($enable=true) {
236                 $this->enabled = $enable;
237         }
238
239         public function isEnabled() {
240                 return $this->enabled;
241         }
242 }
243
244 ?>