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.

ActivityPage.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.wicket.pages;
  17. import java.text.MessageFormat;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.HashSet;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import org.apache.wicket.PageParameters;
  26. import org.apache.wicket.behavior.HeaderContributor;
  27. import org.apache.wicket.markup.html.basic.Label;
  28. import com.gitblit.GitBlit;
  29. import com.gitblit.Keys;
  30. import com.gitblit.models.Activity;
  31. import com.gitblit.models.Metric;
  32. import com.gitblit.models.RepositoryModel;
  33. import com.gitblit.utils.ActivityUtils;
  34. import com.gitblit.utils.StringUtils;
  35. import com.gitblit.wicket.CacheControl;
  36. import com.gitblit.wicket.PageRegistration;
  37. import com.gitblit.wicket.CacheControl.LastModified;
  38. import com.gitblit.wicket.PageRegistration.DropDownMenuItem;
  39. import com.gitblit.wicket.PageRegistration.DropDownMenuRegistration;
  40. import com.gitblit.wicket.WicketUtils;
  41. import com.gitblit.wicket.charting.GoogleChart;
  42. import com.gitblit.wicket.charting.GoogleCharts;
  43. import com.gitblit.wicket.charting.GoogleLineChart;
  44. import com.gitblit.wicket.charting.GooglePieChart;
  45. import com.gitblit.wicket.panels.ActivityPanel;
  46. /**
  47. * Activity Page shows a list of recent commits across all visible Gitblit
  48. * repositories.
  49. *
  50. * @author James Moger
  51. *
  52. */
  53. @CacheControl(LastModified.ACTIVITY)
  54. public class ActivityPage extends RootPage {
  55. public ActivityPage(PageParameters params) {
  56. super(params);
  57. setupPage("", "");
  58. // parameters
  59. int daysBack = WicketUtils.getDaysBack(params);
  60. if (daysBack < 1) {
  61. daysBack = GitBlit.getInteger(Keys.web.activityDuration, 7);
  62. }
  63. String objectId = WicketUtils.getObject(params);
  64. // determine repositories to view and retrieve the activity
  65. List<RepositoryModel> models = getRepositories(params);
  66. List<Activity> recentActivity = ActivityUtils.getRecentActivity(models,
  67. daysBack, objectId, getTimeZone());
  68. String headerPattern;
  69. if (daysBack == 1) {
  70. // today
  71. if (recentActivity.size() == 0) {
  72. headerPattern = getString("gb.todaysActivityNone");
  73. } else {
  74. headerPattern = getString("gb.todaysActivityStats");
  75. }
  76. } else {
  77. // multiple days
  78. if (recentActivity.size() == 0) {
  79. headerPattern = getString("gb.recentActivityNone");
  80. } else {
  81. headerPattern = getString("gb.recentActivityStats");
  82. }
  83. }
  84. if (recentActivity.size() == 0) {
  85. // no activity, skip graphs and activity panel
  86. add(new Label("subheader", MessageFormat.format(headerPattern,
  87. daysBack)));
  88. add(new Label("activityPanel"));
  89. } else {
  90. // calculate total commits and total authors
  91. int totalCommits = 0;
  92. Set<String> uniqueAuthors = new HashSet<String>();
  93. for (Activity activity : recentActivity) {
  94. totalCommits += activity.getCommitCount();
  95. uniqueAuthors.addAll(activity.getAuthorMetrics().keySet());
  96. }
  97. int totalAuthors = uniqueAuthors.size();
  98. // add the subheader with stat numbers
  99. add(new Label("subheader", MessageFormat.format(headerPattern,
  100. daysBack, totalCommits, totalAuthors)));
  101. // create the activity charts
  102. GoogleCharts charts = createCharts(recentActivity);
  103. add(new HeaderContributor(charts));
  104. // add activity panel
  105. add(new ActivityPanel("activityPanel", recentActivity));
  106. }
  107. }
  108. @Override
  109. protected boolean reusePageParameters() {
  110. return true;
  111. }
  112. @Override
  113. protected void addDropDownMenus(List<PageRegistration> pages) {
  114. DropDownMenuRegistration filters = new DropDownMenuRegistration("gb.filters",
  115. ActivityPage.class);
  116. PageParameters currentParameters = getPageParameters();
  117. int daysBack = GitBlit.getInteger(Keys.web.activityDuration, 7);
  118. if (currentParameters != null && !currentParameters.containsKey("db")) {
  119. currentParameters.put("db", daysBack);
  120. }
  121. // preserve time filter options on repository choices
  122. filters.menuItems.addAll(getRepositoryFilterItems(currentParameters));
  123. // preserve repository filter options on time choices
  124. filters.menuItems.addAll(getTimeFilterItems(currentParameters));
  125. if (filters.menuItems.size() > 0) {
  126. // Reset Filter
  127. filters.menuItems.add(new DropDownMenuItem(getString("gb.reset"), null, null));
  128. }
  129. pages.add(filters);
  130. }
  131. /**
  132. * Creates the daily activity line chart, the active repositories pie chart,
  133. * and the active authors pie chart
  134. *
  135. * @param recentActivity
  136. * @return
  137. */
  138. private GoogleCharts createCharts(List<Activity> recentActivity) {
  139. // activity metrics
  140. Map<String, Metric> repositoryMetrics = new HashMap<String, Metric>();
  141. Map<String, Metric> authorMetrics = new HashMap<String, Metric>();
  142. // aggregate repository and author metrics
  143. for (Activity activity : recentActivity) {
  144. // aggregate author metrics
  145. for (Map.Entry<String, Metric> entry : activity.getAuthorMetrics().entrySet()) {
  146. String author = entry.getKey();
  147. if (!authorMetrics.containsKey(author)) {
  148. authorMetrics.put(author, new Metric(author));
  149. }
  150. authorMetrics.get(author).count += entry.getValue().count;
  151. }
  152. // aggregate repository metrics
  153. for (Map.Entry<String, Metric> entry : activity.getRepositoryMetrics().entrySet()) {
  154. String repository = StringUtils.stripDotGit(entry.getKey());
  155. if (!repositoryMetrics.containsKey(repository)) {
  156. repositoryMetrics.put(repository, new Metric(repository));
  157. }
  158. repositoryMetrics.get(repository).count += entry.getValue().count;
  159. }
  160. }
  161. // build google charts
  162. GoogleCharts charts = new GoogleCharts();
  163. // sort in reverse-chronological order and then reverse that
  164. Collections.sort(recentActivity);
  165. Collections.reverse(recentActivity);
  166. // daily line chart
  167. GoogleChart chart = new GoogleLineChart("chartDaily", getString("gb.dailyActivity"), "day",
  168. getString("gb.commits"));
  169. SimpleDateFormat df = new SimpleDateFormat("MMM dd");
  170. df.setTimeZone(getTimeZone());
  171. for (Activity metric : recentActivity) {
  172. chart.addValue(df.format(metric.startDate), metric.getCommitCount());
  173. }
  174. charts.addChart(chart);
  175. // active repositories pie chart
  176. chart = new GooglePieChart("chartRepositories", getString("gb.activeRepositories"),
  177. getString("gb.repository"), getString("gb.commits"));
  178. for (Metric metric : repositoryMetrics.values()) {
  179. chart.addValue(metric.name, metric.count);
  180. }
  181. chart.setShowLegend(false);
  182. charts.addChart(chart);
  183. // active authors pie chart
  184. chart = new GooglePieChart("chartAuthors", getString("gb.activeAuthors"),
  185. getString("gb.author"), getString("gb.commits"));
  186. for (Metric metric : authorMetrics.values()) {
  187. chart.addValue(metric.name, metric.count);
  188. }
  189. chart.setShowLegend(false);
  190. charts.addChart(chart);
  191. return charts;
  192. }
  193. }