control.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. /**
  3. * The control file of metric module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author zhouxin <zhouxin@easycorp.ltd>
  8. * @package metric
  9. * @version $Id: control.php 5145 2013-07-15 06:47:26Z zhouxin@easycorp.ltd $
  10. * @link http://www.zentao.net
  11. */
  12. class metric extends control
  13. {
  14. /**
  15. * __construct.
  16. *
  17. * @access public
  18. * @return void
  19. */
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. }
  24. /**
  25. * 度量项预览列表。
  26. * Preview metric list.
  27. *
  28. * @param string $scope
  29. * @param string $viewType
  30. * @param int $metricID
  31. * @param string $filtersBase64
  32. * @param int $recTotal
  33. * @param int $recPerPage
  34. * @param int $pageID
  35. * @access public
  36. * @return void
  37. */
  38. public function preview($scope = 'project', $viewType = 'single', $metricID = 0, $filtersBase64 = '', $recTotal = 0, $recPerPage = 100, $pageID = 1)
  39. {
  40. $this->metric->processScopeList('released');
  41. $this->app->loadClass('pager', true);
  42. $pager = new pager($recTotal, $recPerPage, $pageID);
  43. $filters = array();
  44. if($scope == 'filter')
  45. {
  46. $filters = json_decode(base64_decode($filtersBase64), true);
  47. if(!is_array($filters)) $filters = array();
  48. $metrics = $this->metric->getListByFilter($filters, 'released');
  49. }
  50. elseif($scope == 'collect')
  51. {
  52. $metrics = $this->metric->getListByCollect('released');
  53. }
  54. else
  55. {
  56. $metrics = $this->metric->getList($scope, 'released');
  57. }
  58. $groupMetrics = $this->metric->groupMetricByObject($metrics);
  59. $current = $this->metric->getByID($metricID);
  60. if(empty($current) and $groupMetrics) $current = current(current($groupMetrics));
  61. $resultHeader = array();
  62. $resultData = array();
  63. $allResultData = array();
  64. if(!empty($current))
  65. {
  66. $current = $this->metric->getByID($current->id);
  67. $result = $this->metric->getResultByCode($current->code, array(), 'cron', $pager);
  68. $allResult = $this->metric->getResultByCode($current->code, array(), 'cron');
  69. $result = $this->completeMissingRecords($result, $current);
  70. $allResult = $this->completeMissingRecords($allResult, $current);
  71. $resultHeader = $this->metric->getViewTableHeader($current);
  72. $resultData = $this->metric->getViewTableData($current, $result);
  73. $allResultData = $this->metric->getViewTableData($current, $allResult);
  74. }
  75. $currentDateType = $current ? $current->dateType : 'nodate';
  76. $currentCode = $current ? $current->code : '';
  77. $currentScope = $current ? $current->scope : '';
  78. list($groupHeader, $groupData) = $this->metric->getGroupTable($resultHeader, $resultData, $currentDateType);
  79. $this->view->groupHeader = $groupHeader;
  80. $this->view->groupData = $groupData;
  81. $this->view->dateType = $currentDateType;
  82. $this->view->dateLabels = $this->metric->getDateLabels($currentDateType);
  83. $this->view->defaultDate = $this->metric->getDefaultDate($this->view->dateLabels);
  84. $this->view->tableWidth = $this->metricZen->getViewTableWidth($groupHeader);
  85. $this->view->pagerExtra = $this->metricZen->getPagerExtra($this->view->tableWidth);
  86. $this->view->headerGroup = $this->metric->isHeaderGroup($groupHeader);
  87. $this->view->metrics = $metrics;
  88. $this->view->groupMetrics = $groupMetrics;
  89. $this->view->current = $current;
  90. $this->view->metricList = $this->lang->metric->metricList;
  91. $this->view->scope = $scope;
  92. $this->view->title = $this->lang->metric->common;
  93. $this->view->viewType = $viewType;
  94. $this->view->recTotal = count($metrics);
  95. $this->view->filters = $filters;
  96. $this->view->filtersBase64 = $filtersBase64;
  97. $this->view->dtablePager = $pager;
  98. $this->view->chartTypeList = $this->metric->getChartTypeList($resultHeader);
  99. $this->view->echartOptions = $this->metric->getEchartsOptions($resultHeader, $allResultData);
  100. $this->view->metricRecordType = $this->metric->getMetricRecordType($currentCode, $currentScope);
  101. $this->view->noDataTip = $this->metric->getNoDataTip($currentCode);
  102. $this->display();
  103. }
  104. /**
  105. * 获取度量项列表。
  106. * Get metric list by ajax.
  107. *
  108. * @param string $scope
  109. * @param string $filters
  110. * @access public
  111. * @return void
  112. */
  113. public function ajaxGetMetrics($scope, $filters)
  114. {
  115. if($scope == 'filter')
  116. {
  117. $filters = json_decode(base64_decode($filters), true);
  118. if(!is_array($filters)) $filters = array();
  119. $metrics = $this->metric->getListByFilter($filters, 'released');
  120. }
  121. elseif($scope == 'collect')
  122. {
  123. $metrics = $this->metric->getListByCollect('released');
  124. }
  125. else
  126. {
  127. $metrics = $this->metric->getList($scope, 'released');
  128. }
  129. echo(json_encode($metrics));
  130. }
  131. /**
  132. * 查看度量项的详情。
  133. * View metric details.
  134. *
  135. * @param int $metricID
  136. * @access public
  137. * @return void
  138. */
  139. public function details($metricID)
  140. {
  141. $metric = $this->metric->getByID($metricID);
  142. $this->view->metric = $metric;
  143. $this->view->legendBasic = $this->metricZen->getBasicInfo($this->view,'scope,object,purpose,dateType,name,alias,code,unit,desc,definition');
  144. $this->view->createEditInfo = $this->metricZen->getCreateEditInfo($this->view, 'createdBy,implementedBy,lastEdited');
  145. $this->display();
  146. }
  147. /**
  148. * 计算度量项。
  149. * Execute metric.
  150. *
  151. * @access public
  152. * @return void
  153. */
  154. public function updateMetricLib()
  155. {
  156. /* 保存当前的错误报告级别和显示错误的设置。*/
  157. /* Save the current error reporting level and display error settings. */
  158. $originalDebug = $this->config->debug;
  159. /* 开启调试模式。*/
  160. /* Enable debug mode. */
  161. $this->config->debug = 2;
  162. $this->metric->saveLogs('----------------------------------');
  163. $this->metric->saveLogs('| The new task: updateMetricLib. |');
  164. $this->metric->saveLogs('----------------------------------');
  165. $calcList = $this->metric->getCalcInstanceList();
  166. $classifiedCalcGroup = $this->metric->classifyCalc($calcList);
  167. $this->metricZen->calculateMetric($classifiedCalcGroup);
  168. /* 恢复之前的调试状态。*/
  169. /* Restore the previous debug state. */
  170. $this->config->debug = $originalDebug;
  171. if(dao::isError())
  172. {
  173. echo dao::getError();
  174. return false;
  175. }
  176. echo 'success';
  177. }
  178. public function updateDashboardMetricLib()
  179. {
  180. /* 保存当前的错误报告级别和显示错误的设置。*/
  181. /* Save the current error reporting level and display error settings. */
  182. $originalDebug = $this->config->debug;
  183. /* 开启调试模式。*/
  184. /* Enable debug mode. */
  185. $this->config->debug = 2;
  186. $this->metric->saveLogs('-------------------------------------------');
  187. $this->metric->saveLogs('| The new task: updateDashboardMetricLib. |');
  188. $this->metric->saveLogs('-------------------------------------------');
  189. $dashboards = array_keys($this->config->metric->dashboard);
  190. $calcList = $this->metric->getCalcInstanceList($dashboards);
  191. $classifiedCalcGroup = $this->metric->classifyCalc($calcList);
  192. $this->metricZen->calculateMetric($classifiedCalcGroup);
  193. /* 恢复之前的调试状态。*/
  194. /* Restore the previous debug state. */
  195. $this->config->debug = $originalDebug;
  196. if(dao::isError())
  197. {
  198. echo dao::getError();
  199. return false;
  200. }
  201. echo 'success';
  202. }
  203. /**
  204. * 获取数据表格的数据。
  205. * Get data of datatable.
  206. *
  207. * @param int $metricID
  208. * @access public
  209. * @return string
  210. */
  211. public function ajaxCollectMetric($metricID)
  212. {
  213. $metric = $this->metric->getByID($metricID);
  214. $isCollect = strpos($metric->collector, ',' . $this->app->user->account . ',') !== false;
  215. $collector = explode(',', $metric->collector);
  216. if($isCollect)
  217. {
  218. $key = array_search($this->app->user->account, $collector);
  219. if($key) unset($collector[$key]);
  220. }
  221. else
  222. {
  223. $collector[] = $this->app->user->account;
  224. }
  225. $collector = array_filter($collector);
  226. $metric = new stdclass();
  227. $metric->collector = ',' . implode(',', $collector) . ',';
  228. $this->metric->updateMetricFields($metricID, $metric);
  229. $response = new stdclass();
  230. $response->result = 'success';
  231. $response->collect = !$isCollect;
  232. echo json_encode($response);
  233. }
  234. /**
  235. * 获取度量项默认值和测试值的的下拉选项。
  236. * Get options of default value and query value.
  237. *
  238. * @param string $optionType
  239. * @access public
  240. * @return string
  241. */
  242. public function ajaxGetControlOptions($optionType)
  243. {
  244. $options = $this->metric->getControlOptions($optionType);
  245. $optionList = array();
  246. foreach($options as $value => $option) $optionList[] = array('value' => $value, 'text' => $option, 'keys' => $option);
  247. return $this->send($optionList);
  248. }
  249. /**
  250. * 获取度量项的侧边栏。
  251. * Get side tree widget by ajax.
  252. *
  253. * @param string $scope
  254. * @param string $checkedList
  255. * @access public
  256. * @return string
  257. */
  258. public function ajaxGetMetricSideTree($scope, $checkedList, $filtersBase64 = '')
  259. {
  260. $checkedList = explode(',', $checkedList);
  261. $filters = json_decode(base64_decode($filtersBase64), true);
  262. if(!is_array($filters)) $filters = array();
  263. if($scope == 'collect')
  264. {
  265. $metrics = $this->metric->getListByCollect('released');
  266. }
  267. elseif(!empty($filters))
  268. {
  269. $metrics = $this->metric->getListByFilter($filters, 'released');
  270. }
  271. else
  272. {
  273. $metrics = $this->metric->getList($scope, 'released');
  274. }
  275. $this->view->groupMetrics = $this->metric->groupMetricByObject($metrics);
  276. $this->view->checkedList = $checkedList;
  277. $this->view->scope = $scope;
  278. $this->display();
  279. }
  280. public function ajaxGetMultipleMetricBox($metricID, $recTotal = 0, $recPerPage = 100, $pageID = 1)
  281. {
  282. $this->app->loadClass('pager', true);
  283. $pager = new pager($recTotal, $recPerPage, $pageID);
  284. $metric = $this->metric->getByID($metricID);
  285. $result = $this->metric->getResultByCode($metric->code, array(), 'cron', $pager);
  286. $allResult = $this->metric->getResultByCode($metric->code, array(), 'cron');
  287. $resultHeader = $this->metric->getViewTableHeader($metric);
  288. $resultData = $this->metric->getViewTableData($metric, $result);
  289. $allResultData = $this->metric->getViewTableData($metric, $allResult);
  290. $dateType = $metric->dateType;
  291. list($groupHeader, $groupData) = $this->metric->getGroupTable($resultHeader, $resultData, $dateType);
  292. $this->view->groupHeader = $groupHeader;
  293. $this->view->groupData = $groupData;
  294. $this->view->dateType = $dateType;
  295. $this->view->dateLabels = $this->metric->getDateLabels($dateType);
  296. $this->view->defaultDate = $this->metric->getDefaultDate($this->view->dateLabels);
  297. $this->view->tableWidth = $this->metricZen->getViewTableWidth($groupHeader);
  298. $this->view->pagerExtra = $this->metricZen->getPagerExtra($this->view->tableWidth);
  299. $this->view->headerGroup = $this->metric->isHeaderGroup($groupHeader);
  300. $this->view->dtablePager = $pager;
  301. $this->view->metricRecordType = $this->metric->getMetricRecordType($metric->code, $metric->scope);
  302. $this->view->metric = $metric;
  303. $this->view->chartTypeList = $this->metric->getChartTypeList($resultHeader);
  304. $this->view->echartOptions = $this->metric->getEchartsOptions($resultHeader, $allResultData);
  305. $this->view->noDataTip = $this->metric->getNoDataTip($metric->code);
  306. $this->display();
  307. }
  308. public function ajaxGetTableAndCharts($metricID, $scope = '', $calcDate = '', $dateBegin = '', $dateEnd = '', $viewType = 'single', $recTotal = 0, $recPerPage = 100, $pageID = 1)
  309. {
  310. $usePager = empty($scope);
  311. $dateBegin = str_replace('_', '-', $dateBegin);
  312. $dateEnd = str_replace('_', '-', $dateEnd);
  313. $dateLabel = isset($_POST['dateLabel']) ? $_POST['dateLabel'] : '';
  314. $options = array_filter(array('scope' => $scope, 'calcDate' => $calcDate, 'dateLabel' => $dateLabel, 'dateBegin' => $dateBegin, 'dateEnd' => $dateEnd));
  315. $this->app->loadClass('pager', true);
  316. $pager = new pager($recTotal, $recPerPage, $pageID);
  317. $metric = $this->metric->getByID($metricID);
  318. $result = $this->metric->getResultByCode($metric->code, $options, 'cron', $usePager ? $pager : null);
  319. $allResult = $this->metric->getResultByCode($metric->code, $options, 'cron');
  320. $resultHeader = $this->metric->getViewTableHeader($metric);
  321. $resultData = $this->metric->getViewTableData($metric, $result);
  322. $allResultData = $this->metric->getViewTableData($metric, $allResult);
  323. if($usePager) $this->view->dtablePager = $pager;
  324. list($groupHeader, $groupData) = $this->metric->getGroupTable($resultHeader, $resultData, $metric->dateType);
  325. $this->view->groupHeader = $groupHeader;
  326. $this->view->groupData = $groupData;
  327. $this->view->tableWidth = $this->metricZen->getViewTableWidth($groupHeader);
  328. $this->view->pagerExtra = $this->metricZen->getPagerExtra($this->view->tableWidth);
  329. $this->view->headerGroup = $this->metric->isHeaderGroup($groupHeader);
  330. $this->view->metricRecordType = $this->metric->getMetricRecordType($metric->code, $metric->scope);
  331. $this->view->viewType = $viewType;
  332. $this->view->metric = $metric;
  333. $this->view->chartTypeList = $this->metric->getChartTypeList($resultHeader);
  334. $this->view->echartOptions = $this->metric->getEchartsOptions($resultHeader, $allResultData);
  335. $this->view->noDataTip = $this->metric->getNoDataTip($metric->code);
  336. $this->display();
  337. }
  338. /**
  339. * 获取数据表格的数据。
  340. * Get data of datatable.
  341. *
  342. * @param int $metricID
  343. * @param string $chartType
  344. * @access public
  345. * @return string
  346. */
  347. public function ajaxGetEchartsOptions($metricID, $chartType = 'line')
  348. {
  349. $metric = $this->metric->getByID($metricID);
  350. $result = $this->metric->getResultByCode($metric->code, $_POST, 'cron');
  351. $resultHeader = $this->metric->getViewTableHeader($metric);
  352. $resultData = $this->metric->getViewTableData($metric, $result);
  353. $echartOptions = $this->metric->getEchartsOptions($resultHeader, $resultData, $chartType);
  354. echo json_encode($echartOptions);
  355. }
  356. }