PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/Trackers/monitor/Monitor.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 244 lines | 123 code | 32 blank | 89 comment | 17 complexity | cf0bb11c1d16a061d4a0237c682a7d35 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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. require_once('modules/Trackers/Metric.php');
  38. require_once('modules/Trackers/Trackable.php');
  39. define('MAX_SESSION_LENGTH', 36);
  40. class Monitor implements Trackable {
  41. var $metricsFile;
  42. var $name;
  43. protected $metrics;
  44. protected $cachedStores;
  45. var $stores;
  46. var $monitor_id;
  47. var $table_name;
  48. protected $enabled = true;
  49. protected $dirty = false;
  50. var $date_start;
  51. var $date_end;
  52. var $active;
  53. var $round_trips;
  54. var $seconds;
  55. var $session_id;
  56. /**
  57. * Monitor constructor
  58. */
  59. function Monitor($name='', $monitorId='', $metadata='', $store='') {
  60. if(empty($metadata) || !file_exists($metadata)) {
  61. $GLOBALS['log']->error($GLOBALS['app_strings']['ERR_MONITOR_FILE_MISSING'] . "($metadata)");
  62. throw new Exception($GLOBALS['app_strings']['ERR_MONITOR_FILE_MISSING'] . "($metadata)");
  63. }
  64. $this->name = $name;
  65. $this->metricsFile = $metadata;
  66. require($this->metricsFile);
  67. $fields = $dictionary[$this->name]['fields'];
  68. $this->table_name = !empty($dictionary[$this->name]['table']) ? $dictionary[$this->name]['table'] : $this->name;
  69. $this->metrics = array();
  70. foreach($fields as $field) {
  71. //We need to skip auto_increment fields; they are not real metrics
  72. //since they are generated by the database.
  73. if(isset($field['auto_increment'])) {
  74. continue;
  75. }
  76. $type = isset($field['dbType']) ? $field['dbType'] : $field['type'];
  77. $name = $field['name'];
  78. $this->metrics[$name] = new Metric($type, $name);
  79. }
  80. $this->monitor_id = $monitorId;
  81. $this->stores = $store;
  82. if(isset($this->metrics['session_id'])) {
  83. //set the value of the session id for 2 reasons:
  84. //1) it is the same value no matter where it is set
  85. //2) ensure it follows some filter rules.
  86. $this->setValue('session_id', $this->getSessionId());
  87. }
  88. }
  89. /**
  90. * setValue
  91. * Sets the value defined in the monitor's metrics for the given name
  92. * @param $name String value of metric name
  93. * @param $value Mixed value
  94. * @throws Exception Thrown if metric name is not configured for monitor instance
  95. */
  96. public function setValue($name, $value) {
  97. if(!isset($this->metrics[$name])) {
  98. $GLOBALS['log']->error($GLOBALS['app_strings']['ERR_UNDEFINED_METRIC'] . "($name)");
  99. throw new Exception($GLOBALS['app_strings']['ERR_UNDEFINED_METRIC'] . "($name)");
  100. } else if($this->metrics[$name]->isMutable()) {
  101. $this->$name = is_object($value) ? get_class($value) : $value;
  102. $this->dirty = true;
  103. }
  104. }
  105. public function getValue($name){
  106. return $this->$name;
  107. }
  108. /**
  109. * getStores
  110. * Returns Array of store names defined for monitor instance
  111. * @return Array of store names defined for monitor instance
  112. */
  113. function getStores() {
  114. return $this->stores;
  115. }
  116. /**
  117. * getMetrics
  118. * Returns Array of metric instances defined for monitor instance
  119. * @return Array of metric instances defined for monitor instance
  120. */
  121. function getMetrics() {
  122. return $this->metrics;
  123. }
  124. /**
  125. * isDirty
  126. * Returns if the monitor has data that needs to be saved
  127. * @return $dirty boolean
  128. */
  129. function isDirty(){
  130. return $this->dirty;
  131. }
  132. /**
  133. * save
  134. * This method retrieves the Store instances associated with monitor and calls
  135. * the flush method passing with the montior ($this) instance.
  136. * @param $flush boolean parameter indicating whether or not to flush the instance data to store or possibly cache
  137. */
  138. public function save($flush=true) {
  139. //If the monitor is not enabled, do not save
  140. if(!$this->isEnabled()&&$this->name!='tracker_sessions')return false;
  141. //if the monitor does not have values set no need to do the work saving.
  142. if(!$this->dirty)return false;
  143. if(empty($GLOBALS['tracker_' . $this->table_name])) {
  144. foreach($this->stores as $s) {
  145. $store = $this->getStore($s);
  146. $store->flush($this);
  147. }
  148. }
  149. $this->clear();
  150. }
  151. /**
  152. * clear
  153. * This function clears the metrics data in the monitor instance
  154. */
  155. public function clear() {
  156. $metrics = $this->getMetrics();
  157. foreach($metrics as $name=>$metric) {
  158. if($metric->isMutable()) {
  159. $this->$name = '';
  160. }
  161. }
  162. $this->dirty = false;
  163. }
  164. /**
  165. * getStore
  166. * This method checks if the Store implementation has already been instantiated and
  167. * will return the one stored; otherwise it will create the Store implementation and
  168. * save it to the Array of Stores.
  169. * @param $store The name of the store as defined in the 'modules/Trackers/config.php' settings
  170. * @return An instance of a Store implementation
  171. * @throws Exception Thrown if $store class cannot be loaded
  172. */
  173. protected function getStore($store) {
  174. if(isset($this->cachedStores[$store])) {
  175. return $this->cachedStores[$store];
  176. }
  177. if(!file_exists("modules/Trackers/store/$store.php")) {
  178. $GLOBALS['log']->error($GLOBALS['app_strings']['ERR_STORE_FILE_MISSING'] . "($store)");
  179. throw new Exception($GLOBALS['app_strings']['ERR_STORE_FILE_MISSING'] . "($store)");
  180. }
  181. require_once("modules/Trackers/store/$store.php");
  182. $s = new $store();
  183. $this->cachedStores[$store] = $s;
  184. return $s;
  185. }
  186. public function getSessionId(){
  187. $sessionid = session_id();
  188. if(!empty($sessionid) && strlen($sessionid) > MAX_SESSION_LENGTH) {
  189. $sessionid = md5($sessionid);
  190. }
  191. return $sessionid;
  192. }
  193. /**
  194. * Returns the monitor's metrics/values as an Array
  195. * @return An Array of data for the monitor's corresponding metrics
  196. */
  197. public function toArray() {
  198. $to_arr = array();
  199. $metrics = $this->getMetrics();
  200. foreach($metrics as $name=>$metric) {
  201. $to_arr[$name] = isset($this->$name) ? $this->$name : null;
  202. }
  203. return $to_arr;
  204. }
  205. public function setEnabled($enable=true) {
  206. $this->enabled = $enable;
  207. }
  208. public function isEnabled() {
  209. return $this->enabled;
  210. }
  211. }
  212. ?>