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

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