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