You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MetricUtils.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.utils;
  17. import java.text.DateFormat;
  18. import java.text.SimpleDateFormat;
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.Date;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.eclipse.jgit.lib.Constants;
  26. import org.eclipse.jgit.lib.ObjectId;
  27. import org.eclipse.jgit.lib.Repository;
  28. import org.eclipse.jgit.revwalk.RevCommit;
  29. import org.eclipse.jgit.revwalk.RevWalk;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. import com.gitblit.models.Metric;
  33. import com.gitblit.models.RefModel;
  34. public class MetricUtils {
  35. private static final Logger LOGGER = LoggerFactory.getLogger(MetricUtils.class);
  36. public static List<Metric> getDateMetrics(Repository r, boolean includeTotal, String format) {
  37. Metric total = new Metric("TOTAL");
  38. final Map<String, Metric> metricMap = new HashMap<String, Metric>();
  39. if (JGitUtils.hasCommits(r)) {
  40. try {
  41. RevWalk walk = new RevWalk(r);
  42. ObjectId object = r.resolve(Constants.HEAD);
  43. RevCommit lastCommit = walk.parseCommit(object);
  44. walk.markStart(lastCommit);
  45. SimpleDateFormat df = new SimpleDateFormat(format);
  46. Iterable<RevCommit> revlog = walk;
  47. for (RevCommit rev : revlog) {
  48. Date d = JGitUtils.getCommitDate(rev);
  49. String p = df.format(d);
  50. if (!metricMap.containsKey(p)) {
  51. metricMap.put(p, new Metric(p));
  52. }
  53. Metric m = metricMap.get(p);
  54. m.count++;
  55. total.count++;
  56. }
  57. } catch (Throwable t) {
  58. JGitUtils.LOGGER.error("Failed to mine log history for metrics", t);
  59. }
  60. }
  61. List<String> keys = new ArrayList<String>(metricMap.keySet());
  62. Collections.sort(keys);
  63. List<Metric> metrics = new ArrayList<Metric>();
  64. for (String key : keys) {
  65. metrics.add(metricMap.get(key));
  66. }
  67. if (includeTotal) {
  68. metrics.add(0, total);
  69. }
  70. return metrics;
  71. }
  72. public static List<Metric> getDateMetrics(Repository r, boolean includeTotal) {
  73. Metric total = new Metric("TOTAL");
  74. final Map<String, Metric> metricMap = new HashMap<String, Metric>();
  75. if (JGitUtils.hasCommits(r)) {
  76. final List<RefModel> tags = JGitUtils.getTags(r, -1);
  77. final Map<ObjectId, RefModel> tagMap = new HashMap<ObjectId, RefModel>();
  78. for (RefModel tag : tags) {
  79. tagMap.put(tag.getCommitId(), tag);
  80. }
  81. try {
  82. RevWalk walk = new RevWalk(r);
  83. ObjectId object = r.resolve(Constants.HEAD);
  84. RevCommit firstCommit = JGitUtils.getFirstCommit(r, Constants.HEAD);
  85. RevCommit lastCommit = walk.parseCommit(object);
  86. int diffDays = (lastCommit.getCommitTime() - firstCommit.getCommitTime())
  87. / (60 * 60 * 24);
  88. total.duration = diffDays;
  89. DateFormat df;
  90. if (diffDays <= 90) {
  91. // Days
  92. df = new SimpleDateFormat("yyyy-MM-dd");
  93. } else if (diffDays > 90 && diffDays < 365) {
  94. // Weeks
  95. df = new SimpleDateFormat("yyyy-MM (w)");
  96. } else {
  97. // Months
  98. df = new SimpleDateFormat("yyyy-MM");
  99. }
  100. walk.markStart(lastCommit);
  101. Iterable<RevCommit> revlog = walk;
  102. for (RevCommit rev : revlog) {
  103. Date d = JGitUtils.getCommitDate(rev);
  104. String p = df.format(d);
  105. if (!metricMap.containsKey(p)) {
  106. metricMap.put(p, new Metric(p));
  107. }
  108. Metric m = metricMap.get(p);
  109. m.count++;
  110. total.count++;
  111. if (tagMap.containsKey(rev.getId())) {
  112. m.tag++;
  113. total.tag++;
  114. }
  115. }
  116. } catch (Throwable t) {
  117. JGitUtils.LOGGER.error("Failed to mine log history for metrics", t);
  118. }
  119. }
  120. List<String> keys = new ArrayList<String>(metricMap.keySet());
  121. Collections.sort(keys);
  122. List<Metric> metrics = new ArrayList<Metric>();
  123. for (String key : keys) {
  124. metrics.add(metricMap.get(key));
  125. }
  126. if (includeTotal) {
  127. metrics.add(0, total);
  128. }
  129. return metrics;
  130. }
  131. public static List<Metric> getAuthorMetrics(Repository r) {
  132. Metric total = new Metric("TOTAL");
  133. final Map<String, Metric> metricMap = new HashMap<String, Metric>();
  134. if (JGitUtils.hasCommits(r)) {
  135. try {
  136. RevWalk walk = new RevWalk(r);
  137. ObjectId object = r.resolve(Constants.HEAD);
  138. RevCommit lastCommit = walk.parseCommit(object);
  139. walk.markStart(lastCommit);
  140. Iterable<RevCommit> revlog = walk;
  141. for (RevCommit rev : revlog) {
  142. String p = rev.getAuthorIdent().getName();
  143. if (StringUtils.isEmpty(p)) {
  144. p = rev.getAuthorIdent().getEmailAddress();
  145. }
  146. if (!metricMap.containsKey(p)) {
  147. metricMap.put(p, new Metric(p));
  148. }
  149. Metric m = metricMap.get(p);
  150. m.count++;
  151. total.count++;
  152. }
  153. } catch (Throwable t) {
  154. JGitUtils.LOGGER.error("Failed to mine log history for metrics", t);
  155. }
  156. }
  157. List<String> keys = new ArrayList<String>(metricMap.keySet());
  158. Collections.sort(keys);
  159. List<Metric> metrics = new ArrayList<Metric>();
  160. for (String key : keys) {
  161. metrics.add(metricMap.get(key));
  162. }
  163. return metrics;
  164. }
  165. }