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 7.7KB

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