count_of_daily_code_commits.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * 按系统统计的每日代码提交次数
  4. * Count of daily commits.
  5. *
  6. * 范围:repo
  7. * 对象:commit
  8. * 目的:rate
  9. * 度量名称:按系统统计的每日代码提交次数
  10. * 单位:次
  11. * 描述:按系统统计的每日代码提交次数是指本系统每日的全部代码提交的数量,这个度量项可以反映组织的每日开发活动频率和代码更新情况。
  12. * 定义:所有代码库的代码提交次数求和,提交时间为某日。
  13. *
  14. * @copyright Copyright 2009-2024 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  15. * @author Yang Li <liyang@chandao.com>
  16. * @package
  17. * @uses func
  18. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  19. * @Link https://www.zentao.net
  20. */
  21. class count_of_daily_code_commits extends baseCalc
  22. {
  23. public $useSCM = true;
  24. public $result = array();
  25. /**
  26. * 通过API获取提交次数。
  27. * Get commits by API.
  28. *
  29. * @param object $repo
  30. * @param string $begin
  31. * @param string $end
  32. * @access public
  33. * @return object
  34. */
  35. public function getCommitCount($repo, $begin, $end)
  36. {
  37. $repo->client = '';
  38. $repo->account = '';
  39. $repo->encoding = 'utf-8';
  40. $repo->password = $repo->token;
  41. $repo->apiPath = $repo->serverUrl . '/api/v1';
  42. $repo->SCM = 'GitFox';
  43. $this->scm->setEngine($repo);
  44. $result = $this->scm->engine->getCodeFrequencyByRepo((string)$repo->gitfoxID, 'day', $begin, $end);
  45. if(empty($result)) return false;
  46. foreach($result->stats as $stats)
  47. {
  48. $repo->time = $stats->key;
  49. $repo->commitCount = $stats->commits;
  50. $this->setResult($repo);
  51. }
  52. }
  53. /**
  54. * 设置结果集。
  55. * Set result set.
  56. *
  57. * @param object $row
  58. * @access public
  59. * @return void
  60. */
  61. public function setResult($row)
  62. {
  63. $date = substr($row->time, 0, 10);
  64. list($year, $month, $day) = explode('-', $date);
  65. if(!isset($this->result[$year])) $this->result[$year] = array();
  66. if(!isset($this->result[$year][$month])) $this->result[$year][$month] = array();
  67. if(!isset($this->result[$year][$month][$day])) $this->result[$year][$month][$day] = 0;
  68. $this->result[$year][$month][$day] += $row->commitCount;
  69. }
  70. /**
  71. * 获取结果。
  72. * Get result.
  73. *
  74. * @param array $options
  75. * @access public
  76. * @return void
  77. */
  78. public function getResult($options = array())
  79. {
  80. if(empty($options))
  81. {
  82. $begin = date('Y-m-d', strtotime('-1 year'));
  83. $end = date('Y-m-d');
  84. }
  85. else
  86. {
  87. $year = (int)$options['year'];
  88. $month = (int)$options['month'];
  89. $day = $options['day'];
  90. $begin = $end = $day;
  91. if(strpos($day, ',') !== false) list($end, $begin) = explode(',', $day);
  92. $begin = "{$year}-{$month}-{$begin}";
  93. $end = "{$year}-{$month}-{$end}";
  94. }
  95. if(!empty($this->repos) && !empty($this->scm))
  96. {
  97. foreach($this->repos as $repo)
  98. {
  99. $this->getCommitCount($repo, $begin, $end);
  100. }
  101. }
  102. return $this->getRecords(array('year', 'month', 'day'));
  103. }
  104. }