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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 org.apache.wicket.markup.html.panel.Fragment;
  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.CacheControl.LastModified;
  37. import com.gitblit.wicket.PageRegistration;
  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 = app().settings().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(
  67. app().settings(),
  68. app().repositories(),
  69. models,
  70. daysBack,
  71. objectId,
  72. getTimeZone());
  73. String headerPattern;
  74. if (daysBack == 1) {
  75. // today
  76. if (recentActivity.size() == 0) {
  77. headerPattern = getString("gb.todaysActivityNone");
  78. } else {
  79. headerPattern = getString("gb.todaysActivityStats");
  80. }
  81. } else {
  82. // multiple days
  83. if (recentActivity.size() == 0) {
  84. headerPattern = getString("gb.recentActivityNone");
  85. } else {
  86. headerPattern = getString("gb.recentActivityStats");
  87. }
  88. }
  89. if (recentActivity.size() == 0) {
  90. // no activity, skip graphs and activity panel
  91. add(new Label("subheader", MessageFormat.format(headerPattern,
  92. daysBack)));
  93. add(new Label("chartsPanel").setVisible(false));
  94. add(new Label("activityPanel"));
  95. } else {
  96. // calculate total commits and total authors
  97. int totalCommits = 0;
  98. Set<String> uniqueAuthors = new HashSet<String>();
  99. for (Activity activity : recentActivity) {
  100. totalCommits += activity.getCommitCount();
  101. uniqueAuthors.addAll(activity.getAuthorMetrics().keySet());
  102. }
  103. int totalAuthors = uniqueAuthors.size();
  104. // add the subheader with stat numbers
  105. add(new Label("subheader", MessageFormat.format(headerPattern,
  106. daysBack, totalCommits, totalAuthors)));
  107. // create the activity charts
  108. if (app().settings().getBoolean(Keys.web.generateActivityGraph, true)) {
  109. GoogleCharts charts = createCharts(recentActivity);
  110. add(new HeaderContributor(charts));
  111. add(new Fragment("chartsPanel", "chartsFragment", this));
  112. } else {
  113. add(new Label("chartsPanel").setVisible(false));
  114. }
  115. // add activity panel
  116. add(new ActivityPanel("activityPanel", recentActivity));
  117. }
  118. }
  119. @Override
  120. protected boolean reusePageParameters() {
  121. return true;
  122. }
  123. @Override
  124. protected void addDropDownMenus(List<PageRegistration> pages) {
  125. DropDownMenuRegistration filters = new DropDownMenuRegistration("gb.filters",
  126. ActivityPage.class);
  127. PageParameters currentParameters = getPageParameters();
  128. int daysBack = app().settings().getInteger(Keys.web.activityDuration, 7);
  129. if (currentParameters != null && !currentParameters.containsKey("db")) {
  130. currentParameters.put("db", daysBack);
  131. }
  132. // preserve time filter options on repository choices
  133. filters.menuItems.addAll(getRepositoryFilterItems(currentParameters));
  134. // preserve repository filter options on time choices
  135. filters.menuItems.addAll(getTimeFilterItems(currentParameters));
  136. if (filters.menuItems.size() > 0) {
  137. // Reset Filter
  138. filters.menuItems.add(new DropDownMenuItem(getString("gb.reset"), null, null));
  139. }
  140. pages.add(filters);
  141. }
  142. /**
  143. * Creates the daily activity line chart, the active repositories pie chart,
  144. * and the active authors pie chart
  145. *
  146. * @param recentActivity
  147. * @return
  148. */
  149. private GoogleCharts createCharts(List<Activity> recentActivity) {
  150. // activity metrics
  151. Map<String, Metric> repositoryMetrics = new HashMap<String, Metric>();
  152. Map<String, Metric> authorMetrics = new HashMap<String, Metric>();
  153. // aggregate repository and author metrics
  154. for (Activity activity : recentActivity) {
  155. // aggregate author metrics
  156. for (Map.Entry<String, Metric> entry : activity.getAuthorMetrics().entrySet()) {
  157. String author = entry.getKey();
  158. if (!authorMetrics.containsKey(author)) {
  159. authorMetrics.put(author, new Metric(author));
  160. }
  161. authorMetrics.get(author).count += entry.getValue().count;
  162. }
  163. // aggregate repository metrics
  164. for (Map.Entry<String, Metric> entry : activity.getRepositoryMetrics().entrySet()) {
  165. String repository = StringUtils.stripDotGit(entry.getKey());
  166. if (!repositoryMetrics.containsKey(repository)) {
  167. repositoryMetrics.put(repository, new Metric(repository));
  168. }
  169. repositoryMetrics.get(repository).count += entry.getValue().count;
  170. }
  171. }
  172. // build google charts
  173. GoogleCharts charts = new GoogleCharts();
  174. // sort in reverse-chronological order and then reverse that
  175. Collections.sort(recentActivity);
  176. Collections.reverse(recentActivity);
  177. // daily line chart
  178. GoogleChart chart = new GoogleLineChart("chartDaily", getString("gb.dailyActivity"), "day",
  179. getString("gb.commits"));
  180. SimpleDateFormat df = new SimpleDateFormat("MMM dd");
  181. df.setTimeZone(getTimeZone());
  182. for (Activity metric : recentActivity) {
  183. chart.addValue(df.format(metric.startDate), metric.getCommitCount());
  184. }
  185. charts.addChart(chart);
  186. // active repositories pie chart
  187. chart = new GooglePieChart("chartRepositories", getString("gb.activeRepositories"),
  188. getString("gb.repository"), getString("gb.commits"));
  189. for (Metric metric : repositoryMetrics.values()) {
  190. chart.addValue(metric.name, metric.count);
  191. }
  192. chart.setShowLegend(false);
  193. charts.addChart(chart);
  194. // active authors pie chart
  195. chart = new GooglePieChart("chartAuthors", getString("gb.activeAuthors"),
  196. getString("gb.author"), getString("gb.commits"));
  197. for (Metric metric : authorMetrics.values()) {
  198. chart.addValue(metric.name, metric.count);
  199. }
  200. chart.setShowLegend(false);
  201. charts.addChart(chart);
  202. return charts;
  203. }
  204. }