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.

ActivityUtils.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.MessageFormat;
  19. import java.text.SimpleDateFormat;
  20. import java.util.ArrayList;
  21. import java.util.Calendar;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Set;
  27. import java.util.TimeZone;
  28. import java.util.TreeSet;
  29. import org.eclipse.jgit.lib.Constants;
  30. import org.eclipse.jgit.lib.Repository;
  31. import com.gitblit.IStoredSettings;
  32. import com.gitblit.Keys;
  33. import com.gitblit.manager.IRepositoryManager;
  34. import com.gitblit.models.Activity;
  35. import com.gitblit.models.RefModel;
  36. import com.gitblit.models.RepositoryCommit;
  37. import com.gitblit.models.RepositoryModel;
  38. /**
  39. * Utility class for building activity information from repositories.
  40. *
  41. * @author James Moger
  42. *
  43. */
  44. public class ActivityUtils {
  45. /**
  46. * Gets the recent activity from the repositories for the last daysBack days
  47. * on the specified branch.
  48. *
  49. * @param settings
  50. * the runtime settings
  51. * @param repositoryManager
  52. * the repository manager
  53. * @param models
  54. * the list of repositories to query
  55. * @param daysBack
  56. * the number of days back from Now to collect
  57. * @param objectId
  58. * the branch to retrieve. If this value is null or empty all
  59. * branches are queried.
  60. * @param timezone
  61. * the timezone for aggregating commits
  62. * @return
  63. */
  64. public static List<Activity> getRecentActivity(
  65. IStoredSettings settings,
  66. IRepositoryManager repositoryManager,
  67. List<RepositoryModel> models,
  68. int daysBack,
  69. String objectId,
  70. TimeZone timezone) {
  71. // Activity panel shows last daysBack of activity across all
  72. // repositories.
  73. Date thresholdDate = new Date(System.currentTimeMillis() - daysBack * TimeUtils.ONEDAY);
  74. // Build a map of DailyActivity from the available repositories for the
  75. // specified threshold date.
  76. DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  77. df.setTimeZone(timezone);
  78. Calendar cal = Calendar.getInstance();
  79. cal.setTimeZone(timezone);
  80. // aggregate author exclusions
  81. Set<String> authorExclusions = new TreeSet<String>();
  82. authorExclusions.addAll(settings.getStrings(Keys.web.metricAuthorExclusions));
  83. for (RepositoryModel model : models) {
  84. if (!ArrayUtils.isEmpty(model.metricAuthorExclusions)) {
  85. authorExclusions.addAll(model.metricAuthorExclusions);
  86. }
  87. }
  88. Map<String, Activity> activity = new HashMap<String, Activity>();
  89. for (RepositoryModel model : models) {
  90. if (!model.isShowActivity()) {
  91. // skip this repository
  92. continue;
  93. }
  94. if (model.hasCommits && model.lastChange.after(thresholdDate)) {
  95. if (model.isCollectingGarbage) {
  96. continue;
  97. }
  98. Repository repository = repositoryManager.getRepository(model.name);
  99. List<String> branches = new ArrayList<String>();
  100. if (StringUtils.isEmpty(objectId)) {
  101. for (RefModel local : JGitUtils.getLocalBranches(
  102. repository, true, -1)) {
  103. if (!local.getDate().after(thresholdDate)) {
  104. // branch not recently updated
  105. continue;
  106. }
  107. branches.add(local.getName());
  108. }
  109. } else {
  110. branches.add(objectId);
  111. }
  112. for (String branch : branches) {
  113. String shortName = branch;
  114. if (shortName.startsWith(Constants.R_HEADS)) {
  115. shortName = shortName.substring(Constants.R_HEADS.length());
  116. }
  117. List<RepositoryCommit> commits = CommitCache.instance().getCommits(model.name, repository, branch, thresholdDate);
  118. if (model.maxActivityCommits > 0 && commits.size() > model.maxActivityCommits) {
  119. // trim commits to maximum count
  120. commits = commits.subList(0, model.maxActivityCommits);
  121. }
  122. for (RepositoryCommit commit : commits) {
  123. Date date = commit.getCommitDate();
  124. String dateStr = df.format(date);
  125. if (!activity.containsKey(dateStr)) {
  126. // Normalize the date to midnight
  127. cal.setTime(date);
  128. cal.set(Calendar.HOUR_OF_DAY, 0);
  129. cal.set(Calendar.MINUTE, 0);
  130. cal.set(Calendar.SECOND, 0);
  131. cal.set(Calendar.MILLISECOND, 0);
  132. Activity a = new Activity(cal.getTime());
  133. a.excludeAuthors(authorExclusions);
  134. activity.put(dateStr, a);
  135. }
  136. activity.get(dateStr).addCommit(commit);
  137. }
  138. }
  139. // close the repository
  140. repository.close();
  141. }
  142. }
  143. List<Activity> recentActivity = new ArrayList<Activity>(activity.values());
  144. return recentActivity;
  145. }
  146. /**
  147. * Creates a Gravatar thumbnail url from the specified email address.
  148. *
  149. * @param email
  150. * address to query Gravatar
  151. * @param width
  152. * size of thumbnail. if width <= 0, the default of 50 is used.
  153. * @return
  154. */
  155. public static String getGravatarIdenticonUrl(String email, int width) {
  156. if (width <= 0) {
  157. width = 50;
  158. }
  159. String emailHash = StringUtils.getMD5(email);
  160. String url = MessageFormat.format(
  161. "https://www.gravatar.com/avatar/{0}?s={1,number,0}&d=identicon", emailHash, width);
  162. return url;
  163. }
  164. /**
  165. * Creates a Gravatar thumbnail url from the specified email address.
  166. *
  167. * @param email
  168. * address to query Gravatar
  169. * @param width
  170. * size of thumbnail. if width <= 0, the default of 50 is used.
  171. * @return
  172. */
  173. public static String getGravatarThumbnailUrl(String email, int width) {
  174. if (width <= 0) {
  175. width = 50;
  176. }
  177. String emailHash = StringUtils.getMD5(email);
  178. String url = MessageFormat.format(
  179. "https://www.gravatar.com/avatar/{0}?s={1,number,0}&d=mm", emailHash, width);
  180. return url;
  181. }
  182. }