1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/*
* Copyright 2011 gitblit.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitblit.wicket.pages;
import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.markup.html.basic.Label;
import org.eclipse.jgit.lib.Repository;
import com.gitblit.models.Metric;
import com.gitblit.utils.MetricUtils;
import com.gitblit.utils.StringUtils;
import com.gitblit.wicket.CacheControl;
import com.gitblit.wicket.CacheControl.LastModified;
import com.gitblit.wicket.WicketUtils;
import com.gitblit.wicket.charting.Chart;
import com.gitblit.wicket.charting.Charts;
import com.gitblit.wicket.charting.Flotr2Charts;
@CacheControl(LastModified.REPOSITORY)
public class MetricsPage extends RepositoryPage {
public MetricsPage(PageParameters params) {
super(params);
Repository r = getRepository();
if (StringUtils.isEmpty(objectId)) {
add(new Label("branchTitle", getRepositoryModel().HEAD));
} else {
add(new Label("branchTitle", objectId));
}
Metric metricsTotal = null;
List<Metric> metrics = MetricUtils.getDateMetrics(r, objectId, true, null, getTimeZone());
metricsTotal = metrics.remove(0);
if (metricsTotal == null) {
add(new Label("branchStats", ""));
} else {
add(new Label("branchStats",
MessageFormat.format(getString("gb.branchStats"), metricsTotal.count,
metricsTotal.tag, getTimeUtils().duration(metricsTotal.duration))));
}
Charts charts = new Flotr2Charts();
add(WicketUtils.newBlankImage("commitsChart"));
add(WicketUtils.newBlankImage("dayOfWeekChart"));
add(WicketUtils.newBlankImage("authorsChart"));
createLineChart(charts, "commitsChart", metrics);
createBarChart(charts, "dayOfWeekChart", getDayOfWeekMetrics(r, objectId));
createPieChart(charts, "authorsChart", getAuthorMetrics(r, objectId));
add(new HeaderContributor(charts));
}
private void createLineChart(Charts charts, String id, List<Metric> metrics) {
if ((metrics != null) && (metrics.size() > 0)) {
Chart chart = charts.createLineChart(id, "", "day",
getString("gb.commits"));
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String displayFormat = "MMM dd";
if(metrics.size() > 0 && metrics.get(0).name.length() == 7){
df = new SimpleDateFormat("yyyy-MM");
displayFormat = "yyyy MMM";
}
df.setTimeZone(getTimeZone());
chart.setDateFormat(displayFormat);
for (Metric metric : metrics) {
Date date;
try {
date = df.parse(metric.name);
} catch (ParseException e) {
logger().error("Unable to parse date: " + metric.name);
return;
}
chart.addValue(date, (int)metric.count);
if(metric.tag > 0 ){
chart.addHighlight(date, (int)metric.count);
}
}
charts.addChart(chart);
}
}
private void createPieChart(Charts charts, String id, List<Metric> metrics) {
if ((metrics != null) && (metrics.size() > 0)) {
Chart chart = charts.createPieChart(id, "", "day",
getString("gb.commits"));
for (Metric metric : metrics) {
chart.addValue(metric.name, (int)metric.count);
}
charts.addChart(chart);
}
}
private void createBarChart(Charts charts, String id, List<Metric> metrics) {
if ((metrics != null) && (metrics.size() > 0)) {
Chart chart = charts.createBarChart(id, "", "day",
getString("gb.commits"));
for (Metric metric : metrics) {
chart.addValue(metric.name, (int)metric.count);
}
charts.addChart(chart);
}
}
private List<Metric> getDayOfWeekMetrics(Repository repository, String objectId) {
List<Metric> list = MetricUtils.getDateMetrics(repository, objectId, false, "E", getTimeZone());
SimpleDateFormat sdf = new SimpleDateFormat("E");
Calendar cal = Calendar.getInstance();
List<Metric> sorted = new ArrayList<Metric>();
int firstDayOfWeek = cal.getFirstDayOfWeek();
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
// rewind date to first day of week
cal.add(Calendar.DATE, firstDayOfWeek - dayOfWeek);
for (int i = 0; i < 7; i++) {
String day = sdf.format(cal.getTime());
for (Metric metric : list) {
if (metric.name.equals(day)) {
sorted.add(metric);
list.remove(metric);
break;
}
}
cal.add(Calendar.DATE, 1);
}
return sorted;
}
private List<Metric> getAuthorMetrics(Repository repository, String objectId) {
List<Metric> authors = MetricUtils.getAuthorMetrics(repository, objectId, true);
Collections.sort(authors, new Comparator<Metric>() {
@Override
public int compare(Metric o1, Metric o2) {
if (o1.count > o2.count) {
return -1;
} else if (o1.count < o2.count) {
return 1;
}
return 0;
}
});
if (authors.size() > 10) {
return authors.subList(0, 9);
}
return authors;
}
@Override
protected String getPageName() {
return getString("gb.metrics");
}
@Override
protected boolean isCommitPage() {
return true;
}
@Override
protected Class<? extends BasePage> getRepoNavPageClass() {
return SummaryPage.class;
}
}
|