calc.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. <?php
  2. /**
  3. * 度量项定义基类。
  4. * Base class of measurement func.
  5. *
  6. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  7. * @author qixinzhi <qixinzhi@easycorp.ltd>
  8. * @package
  9. * @license LGPL
  10. * @Link https://www.zentao.net
  11. */
  12. class baseCalc
  13. {
  14. /**
  15. * 来源数据集。
  16. * dataset
  17. *
  18. * @var int
  19. * @access public
  20. */
  21. public $dataset = null;
  22. /**
  23. * 数据库连接。
  24. * Database connection.
  25. *
  26. * @var object
  27. * @access public
  28. */
  29. public $dao = null;
  30. /**
  31. * Git 仓库对象。
  32. * Git repository object.
  33. *
  34. * @var object
  35. * @access public
  36. */
  37. public $scm = null;
  38. /**
  39. * 参数列表。
  40. * fieldList
  41. *
  42. * @var array
  43. * @access public
  44. */
  45. public $fieldList = array();
  46. /**
  47. * 数据主表。
  48. * Main table.
  49. *
  50. * @var string
  51. * @access public
  52. */
  53. public $mainTable;
  54. /**
  55. * 连接表。
  56. * Left join tables.
  57. *
  58. * @var array
  59. * @access public
  60. */
  61. public $subTables;
  62. /**
  63. * 过滤条件。
  64. * Filters of data.
  65. *
  66. * @var array
  67. * @access public
  68. */
  69. public $filters;
  70. /**
  71. * 指标结果。
  72. * Result of indicators.
  73. *
  74. * @var array|float
  75. * @access public
  76. */
  77. public $result = 0;
  78. /**
  79. * 节假日信息。
  80. * holidays
  81. *
  82. * @var array
  83. * @access public
  84. */
  85. public $holidays = array();
  86. /**
  87. * 休息日
  88. * weekend
  89. *
  90. * @var float
  91. * @access public
  92. */
  93. public $weekend = 2;
  94. /**
  95. * 是否复用
  96. *
  97. * @var bool
  98. * @access public
  99. */
  100. public $reuse = false;
  101. /**
  102. * 是否启用initMetricRecords,尽可能补零逻辑
  103. *
  104. * @var bool
  105. * @access public
  106. */
  107. public $initRecord = true;
  108. /**
  109. * 是否支持独立查询
  110. *
  111. * @var bool
  112. * @access public
  113. */
  114. public $supportSingleQuery = false;
  115. /**
  116. * 是否使用独立查询
  117. *
  118. * @var bool
  119. * @access public
  120. */
  121. public $useSingleQuery = false;
  122. /**
  123. * 独立查询sql
  124. *
  125. * @var string
  126. * @access public
  127. */
  128. public $singleSql = '';
  129. /**
  130. * 是否使用 SCM。
  131. *
  132. * @var bool
  133. * @access public
  134. */
  135. public $useSCM = false;
  136. /**
  137. * GitFox 仓库列表
  138. *
  139. * @var array
  140. * @access public
  141. */
  142. public $repos = array();
  143. /**
  144. * 设置DAO 。
  145. * Set DAO.
  146. *
  147. * @param object $dao
  148. * @access public
  149. * @return void
  150. */
  151. public function setDAO($dao)
  152. {
  153. $this->dao = $dao;
  154. }
  155. public function setSCM($scm)
  156. {
  157. $this->scm = $scm;
  158. }
  159. public function setHolidays($holidays)
  160. {
  161. $this->holidays = $holidays;
  162. }
  163. public function setWeekend($weekend)
  164. {
  165. $this->weekend = $weekend;
  166. }
  167. public function setGitFoxRepos($repos)
  168. {
  169. $this->repos = $repos;
  170. }
  171. /**
  172. * 获取数据查询句柄。
  173. * Get pdo statement of sql query.
  174. *
  175. * @param object $dao
  176. * @access public
  177. * @return PDOStatement
  178. */
  179. public function getStatement()
  180. {
  181. }
  182. /**
  183. * 计算度量项。
  184. * Calculate metric.
  185. *
  186. * @param object $data
  187. * @access public
  188. * @return void
  189. */
  190. public function calculate($data)
  191. {
  192. }
  193. /**
  194. * 获取度量项结果。
  195. *
  196. * @param array $options
  197. * @access public
  198. * @return mixed
  199. */
  200. public function getResult($options = array())
  201. {
  202. if(empty($this->result)) return null;
  203. return array((object)array('value' => 0));
  204. }
  205. /**
  206. * 根据选项过滤数据。
  207. * Filter rows by options.
  208. *
  209. * @param array|int $rows
  210. * @param array $options array('product' => '1,2,3,4')
  211. * @access protected
  212. * @return array|false
  213. */
  214. protected function filterByOptions($rows, $options)
  215. {
  216. if(empty($options)) return $rows;
  217. $rows = (array)$rows;
  218. $options = $this->expandOptions($options);
  219. $filteredRows = array();
  220. foreach($rows as $row)
  221. {
  222. $satisify = true;
  223. foreach($options as $scope => $option)
  224. {
  225. $row = (array)$row;
  226. if(!isset($row[$scope])) continue;
  227. $satisify = ($satisify && in_array($row[$scope], $option));
  228. }
  229. if($satisify) $filteredRows[] = $row;
  230. }
  231. return !empty($filteredRows) ? $filteredRows : false;
  232. }
  233. /**
  234. * 判断是否为日期。
  235. * Check if a string is a date.
  236. *
  237. * @param string $dateString
  238. * @access public
  239. * @return bool
  240. */
  241. public function isDate($dateString)
  242. {
  243. if(empty($dateString)) return false;
  244. $format = 'Y-m-d';
  245. $dateTime = DateTime::createFromFormat($format, $dateString);
  246. return $dateTime && $dateTime->format($format) === $dateString;
  247. }
  248. /**
  249. * 扩展选项。
  250. * Expand options.
  251. *
  252. * @param array $options
  253. * @access protected
  254. * @return array
  255. */
  256. protected function expandOptions($options)
  257. {
  258. foreach($options as $scope => $option) $options[$scope] = explode(',', $option);
  259. return $options;
  260. }
  261. /**
  262. * 获取数据源的SQL语句
  263. * Get SQL of data source.
  264. *
  265. * @param object $dao
  266. * @param object $config
  267. * @access public
  268. * @return string
  269. */
  270. public function getSQL($dao, $config)
  271. {
  272. if(!empty($this->dataset))
  273. {
  274. $dataSource = $this->dataset;
  275. include_once dirname(__FILE__) . '/dataset.php';
  276. $dataset = new dataset($dao, $config);
  277. $statement = $dataset->$dataSource(implode(',', $this->fieldList));
  278. return $statement->queryString;
  279. }
  280. return $this->dao->get();
  281. }
  282. /**
  283. * 打印数据源的SQL语句
  284. * Print SQL of data source.
  285. *
  286. * @param object $dao
  287. * @param object $config
  288. * @access public
  289. * @return mixed
  290. */
  291. public function printSQL($dao, $config)
  292. {
  293. echo $this->getSQL($dao, $config);
  294. }
  295. /**
  296. * 获取数据源的结果集。
  297. * Get rows of data source.
  298. *
  299. * @param object $dao
  300. * @param object $config
  301. * @access public
  302. * @return array
  303. */
  304. public function getRows($dao, $config)
  305. {
  306. if(!empty($this->dataset))
  307. {
  308. $dataSource = $this->dataset;
  309. include_once dirname(__FILE__) . '/dataset.php';
  310. $dataset = new dataset($dao, $config);
  311. $statement = $dataset->$dataSource(implode(',', $this->fieldList));
  312. return $statement->fetchAll();
  313. }
  314. return $this->getStatement()->fetchAll();
  315. }
  316. /**
  317. * 转换计算结果数组为度量记录数据数组。
  318. * Get records from result.
  319. *
  320. * @param array $keyNames
  321. * @access public
  322. * @return array
  323. */
  324. public function getRecords($keyNames, $result = null)
  325. {
  326. if($this->useSingleQuery) return $this->singleQuery();
  327. if(empty($result)) $result = $this->result;
  328. if(empty($keyNames)) return $result;
  329. if(current($keyNames) == 'value') return array(array('value' => $result));
  330. $records = array();
  331. $keyName = array_shift($keyNames);
  332. foreach($result as $key => $value) $records[] = array($keyName => $key, 'value' => $value);
  333. while($keyName = array_shift($keyNames))
  334. {
  335. if($keyName == 'value') break;
  336. $newRecords = array();
  337. foreach($records as $record)
  338. {
  339. foreach($record['value'] as $key => $value)
  340. {
  341. $record[$keyName] = $key;
  342. $record['value'] = $value;
  343. $newRecords[] = $record;
  344. }
  345. }
  346. $records = $newRecords;
  347. }
  348. return $records;
  349. }
  350. /**
  351. * 独立查询。
  352. *
  353. * @access public
  354. * @return void
  355. */
  356. public function singleQuery()
  357. {
  358. $sql = $this->singleSql;
  359. return $this->dao->select('*')->from("($sql) tt")->fetchAll();
  360. }
  361. /**
  362. * 设置独立查询sql语句。
  363. *
  364. * @param string $sql
  365. * @access public
  366. * @return void
  367. */
  368. public function setSingleSql($sql)
  369. {
  370. $this->singleSql = $sql;
  371. }
  372. /**
  373. * 获取独立查询sql语句。
  374. *
  375. * @access public
  376. * @return void
  377. */
  378. public function getSingleSql()
  379. {
  380. return "({$this->singleSql}) tt";
  381. }
  382. /**
  383. * 启用独立查询。
  384. *
  385. * @access public
  386. * @return void
  387. */
  388. public function enableSingleQuery()
  389. {
  390. $this->useSingleQuery = true;
  391. }
  392. /**
  393. * 获取日期的周数。
  394. * Get week of date.
  395. *
  396. * @param string $dateStr
  397. * @access public
  398. * @return string
  399. */
  400. public function getWeek($dateStr)
  401. {
  402. if(empty($dateStr)) return false;
  403. if(strlen($dateStr) > 10) $dateStr = substr($dateStr, 0, 10);
  404. $date = DateTime::createFromFormat('Y-m-d', $dateStr);
  405. return substr($date->format('oW'), -2);
  406. }
  407. /**
  408. * 获取日期的年份。
  409. * Get year of date.
  410. *
  411. * @param string $dateStr
  412. * @access public
  413. * @return string|false
  414. */
  415. public function getYear($dateStr)
  416. {
  417. if(empty($dateStr)) return false;
  418. if(strlen($dateStr) > 8) $dateStr = substr($dateStr, 0, 8);
  419. $year = substr($dateStr, 0, 4);
  420. return $year == '0000' ? false : $year;
  421. }
  422. /**
  423. * GetLastDay
  424. *
  425. * @param int $date
  426. * @access public
  427. * @return string
  428. */
  429. public function getLastDay($date)
  430. {
  431. $monday = $this->getThisMonday($date);
  432. $sunday = $this->getThisSunday($date);
  433. $workdays = $this->getActualWorkingDays($monday, $sunday);
  434. return end($workdays);
  435. }
  436. /**
  437. * Get monday for a date.
  438. *
  439. * @param int $date
  440. * @access public
  441. * @return date
  442. */
  443. public function getThisMonday($date)
  444. {
  445. $timestamp = strtotime($date);
  446. $day = date('w', $timestamp);
  447. if($day == 0) $day = 7;
  448. return date('Y-m-d', $timestamp - (($day - 1) * 24 * 3600));
  449. }
  450. /**
  451. * Get fridays.
  452. *
  453. * @param string $start
  454. * @param string $end
  455. * @access public
  456. * @return array
  457. */
  458. public function getFridays($start, $end)
  459. {
  460. $start = new DateTime($start);
  461. $end = new DateTime($end);
  462. $interval = new DateInterval('P1D'); // 间隔为1天
  463. $period = new DatePeriod($start, $interval, $end);
  464. $fridays = array();
  465. foreach ($period as $date) {
  466. if ($date->format('N') == 5) { // 5代表星期五
  467. $fridays[] = $date->format('Y-m-d');
  468. }
  469. }
  470. return $fridays;
  471. }
  472. public function getFridayByWeek($year, $week) {
  473. $first_day = date("Y-m-d", strtotime($year . "-01-01"));
  474. $first_friday = date("Y-m-d", strtotime("{$first_day} next Friday"));
  475. $friday = date("Y-m-d", strtotime("{$first_friday} + " . ($week - 1) . " weeks"));
  476. return $friday;
  477. }
  478. /**
  479. * GetThisSunday
  480. *
  481. * @param int $date
  482. * @access public
  483. * @return date
  484. */
  485. public function getThisSunday($date)
  486. {
  487. $monday = $this->getThisMonday($date);
  488. return date('Y-m-d', strtotime($monday) + (6 * 24 * 3600));
  489. }
  490. /**
  491. * 获取开始和结束日期间的日期。
  492. * Get the dates between the begin and end.
  493. *
  494. * @param string $begin
  495. * @param string $end
  496. * @access public
  497. * @return array
  498. */
  499. public function getDaysBetween($begin, $end)
  500. {
  501. $beginTime = strtotime($begin);
  502. $endTime = strtotime($end);
  503. $days = ($endTime - $beginTime) / 86400;
  504. $dateList = array();
  505. for($i = 0; $i <= $days; $i ++) $dateList[] = date('Y-m-d', strtotime("+{$i} days", $beginTime));
  506. return $dateList;
  507. }
  508. /**
  509. * 通过开始和结束日期获取节假日。
  510. * Get holidays by begin and end.
  511. *
  512. * @param string $begin
  513. * @param string $end
  514. * @access public
  515. * @return array
  516. */
  517. public function getHolidays($begin, $end)
  518. {
  519. $records = array();
  520. foreach($this->holidays as $holiday)
  521. {
  522. if($holiday->type != 'holiday') continue;
  523. if($holiday->begin > $end || $holiday->end < $begin) continue;
  524. $records[] = $holiday;
  525. }
  526. $naturalDays = $this->getDaysBetween($begin, $end);
  527. $holidays = array();
  528. foreach($records as $record)
  529. {
  530. $dates = $this->getDaysBetween($record->begin, $record->end);
  531. $holidays = array_merge($holidays, $dates);
  532. }
  533. return array_intersect($naturalDays, $holidays);
  534. }
  535. /**
  536. * 获取工作日。
  537. * Get working days.
  538. *
  539. * @param string $begin
  540. * @param string $end
  541. * @access public
  542. * @return array
  543. */
  544. public function getWorkingDays($begin = '', $end = '')
  545. {
  546. $records = array();
  547. foreach($this->holidays as $holiday)
  548. {
  549. if($holiday->type != 'working') continue;
  550. if($holiday->begin > $end || $holiday->end < $begin) continue;
  551. $records[] = $holiday;
  552. }
  553. $workingDays = array();
  554. foreach($records as $record)
  555. {
  556. $dates = $this->getDaysBetween($record->begin, $record->end);
  557. $workingDays = array_merge($workingDays, $dates);
  558. }
  559. return $workingDays;
  560. }
  561. /**
  562. * 获取系统休息日配置。
  563. * Get system weekend.
  564. *
  565. * @access public
  566. * @return void
  567. */
  568. public function getSystemWeekend()
  569. {
  570. $weekend = $this->dao->select('value')->from(TABLE_CONFIG)
  571. ->where('module')->eq('execution')
  572. ->andWhere('key')->eq('weekend')
  573. ->fetch('value');
  574. return $weekend ? $weekend : 2;
  575. }
  576. /**
  577. * 获取实际工作日。
  578. * Get actual working days.
  579. *
  580. * @param string $begin
  581. * @param string $end
  582. * @access public
  583. * @return array
  584. */
  585. public function getActualWorkingDays($begin, $end)
  586. {
  587. if(empty($begin) || empty($end) || $begin == '0000-00-00' || $end == '0000-00-00') return array();
  588. /* Get holidays, working days and weekend days .*/
  589. $holidays = $this->getHolidays($begin, $end);
  590. $workingDays = $this->getWorkingDays($begin, $end);
  591. $weekend = $this->weekend;
  592. /* When the start date and end date are the same. */
  593. $actualDays = array();
  594. if($begin == $end)
  595. {
  596. if(in_array($begin, $workingDays)) return array($begin);
  597. if(in_array($begin, $holidays)) return array();
  598. $w = date('w', strtotime($begin));
  599. if($w == 0 || ($weekend == 2 && $w == 6)) return array();
  600. return array($begin);
  601. }
  602. /* Process actual working days. */
  603. for($i = 0, $currentDay = $begin; $currentDay < $end; $i ++)
  604. {
  605. $currentDay = date('Y-m-d', strtotime("{$begin} + {$i} days"));
  606. $w = date('w', strtotime($currentDay));
  607. if(in_array($currentDay, $workingDays))
  608. {
  609. $actualDays[] = $currentDay;
  610. continue;
  611. }
  612. if(in_array($currentDay, $holidays)) continue;
  613. if($w == 0 || ($weekend == 2 && $w == 6)) continue;
  614. $actualDays[] = $currentDay;
  615. }
  616. return $actualDays;
  617. }
  618. public function getDevAccountList()
  619. {
  620. global $dao;
  621. return $dao->select('account')->from(TABLE_USER)->where('role')->eq('dev')->andWhere('deleted')->eq('0')->fetchPairs();
  622. }
  623. public function getExecutions()
  624. {
  625. global $dao;
  626. $executions = $dao->select("t1.id, t1.name, if(t1.multiple='1', t1.status, t2.status) as status, if(t1.multiple='1', t1.realBegan, t2.realBegan) as realBegan, if(t1.multiple='1', t1.closedDate, t2.closedDate) as closedDate")->from(TABLE_PROJECT)->alias('t1')
  627. ->leftJoin(TABLE_PROJECT)->alias('t2')->on('t1.project = t2.id')
  628. ->where('t1.deleted')->eq(0)
  629. ->andWhere('t2.deleted')->eq(0)
  630. ->andWhere('t1.type')->in('sprint,stage,kanban')
  631. ->andWhere("t1.vision LIKE '%rnd%'", true)
  632. ->orWhere("t1.vision IS NULL")->markRight(1)
  633. ->fetchAll('id');
  634. return $executions;
  635. }
  636. /**
  637. * 检查日期。
  638. * Validate date.
  639. *
  640. * @param string $date
  641. * @access public
  642. * @return bool
  643. */
  644. public function validateDate($date)
  645. {
  646. return $this->getYear($date) !== false;
  647. }
  648. /**
  649. * 按日期递增计数。
  650. * Count incrementally by date.
  651. *
  652. * @param int $dataID
  653. * @param string $date
  654. * @access public
  655. * @return void
  656. */
  657. public function incrementDateCount($dataID, $date)
  658. {
  659. $date = substr($date, 0, 10);
  660. list($year, $month, $day) = explode('-', $date);
  661. if(!isset($this->result[$dataID])) $this->result[$dataID] = array();
  662. if(!isset($this->result[$dataID][$year])) $this->result[$dataID][$year] = array();
  663. if(!isset($this->result[$dataID][$year][$month])) $this->result[$dataID][$year][$month] = array();
  664. if(!isset($this->result[$dataID][$year][$month][$day])) $this->result[$dataID][$year][$month][$day] = 0;
  665. $this->result[$dataID][$year][$month][$day]++;
  666. }
  667. }