James Moger
2012-09-10 fabe060d3a435f116128851f828e35c2af5fde67
commit | author | age
f1720c 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.awt.Color;
19 import java.awt.Dimension;
1fa5e8 20 import java.text.MessageFormat;
f1720c 21 import java.text.SimpleDateFormat;
JM 22 import java.util.ArrayList;
23 import java.util.Calendar;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.List;
27
28 import org.apache.wicket.PageParameters;
1fa5e8 29 import org.apache.wicket.markup.html.basic.Label;
f1720c 30 import org.eclipse.jgit.lib.Repository;
JM 31 import org.wicketstuff.googlecharts.Chart;
32 import org.wicketstuff.googlecharts.ChartAxis;
33 import org.wicketstuff.googlecharts.ChartAxisType;
34 import org.wicketstuff.googlecharts.ChartProvider;
35 import org.wicketstuff.googlecharts.ChartType;
36 import org.wicketstuff.googlecharts.IChartData;
37 import org.wicketstuff.googlecharts.LineStyle;
38 import org.wicketstuff.googlecharts.MarkerType;
39 import org.wicketstuff.googlecharts.ShapeMarker;
40
41 import com.gitblit.models.Metric;
424fe1 42 import com.gitblit.utils.MetricUtils;
7c08c7 43 import com.gitblit.utils.StringUtils;
f1720c 44 import com.gitblit.wicket.WicketUtils;
JM 45
424fe1 46 public class MetricsPage extends RepositoryPage {
f1720c 47
424fe1 48     public MetricsPage(PageParameters params) {
008322 49         super(params);
f1720c 50         Repository r = getRepository();
7c08c7 51         if (StringUtils.isEmpty(objectId)) {
JM 52             add(new Label("branchTitle", getRepositoryModel().HEAD));
53         } else {
54             add(new Label("branchTitle", objectId));
55         }
1fa5e8 56         Metric metricsTotal = null;
40538c 57         List<Metric> metrics = MetricUtils.getDateMetrics(r, objectId, true, null, getTimeZone());
1fa5e8 58         metricsTotal = metrics.remove(0);
JM 59         if (metricsTotal == null) {
008322 60             add(new Label("branchStats", ""));
1fa5e8 61         } else {
008322 62             add(new Label("branchStats",
6caa93 63                     MessageFormat.format(getString("gb.branchStats"), metricsTotal.count,
9adf62 64                             metricsTotal.tag, getTimeUtils().duration(metricsTotal.duration))));
1fa5e8 65         }
JM 66         insertLinePlot("commitsChart", metrics);
67         insertBarPlot("dayOfWeekChart", getDayOfWeekMetrics(r, objectId));
68         insertPieChart("authorsChart", getAuthorMetrics(r, objectId));
f1720c 69     }
JM 70
71     private void insertLinePlot(String wicketId, List<Metric> metrics) {
72         if ((metrics != null) && (metrics.size() > 0)) {
73             IChartData data = WicketUtils.getChartData(metrics);
74
fd6ac6 75             ChartProvider provider = new ChartProvider(new Dimension(400, 100), ChartType.LINE,
f1720c 76                     data);
JM 77             ChartAxis dateAxis = new ChartAxis(ChartAxisType.BOTTOM);
78             dateAxis.setLabels(new String[] { metrics.get(0).name,
79                     metrics.get(metrics.size() / 2).name, metrics.get(metrics.size() - 1).name });
80             provider.addAxis(dateAxis);
81
82             ChartAxis commitAxis = new ChartAxis(ChartAxisType.LEFT);
83             commitAxis.setLabels(new String[] { "",
84                     String.valueOf((int) WicketUtils.maxValue(metrics)) });
85             provider.addAxis(commitAxis);
86
87             provider.setLineStyles(new LineStyle[] { new LineStyle(2, 4, 0), new LineStyle(0, 4, 1) });
88             provider.addShapeMarker(new ShapeMarker(MarkerType.CIRCLE, Color.BLUE, 1, -1, 5));
89
90             add(new Chart(wicketId, provider));
91         } else {
92             add(WicketUtils.newBlankImage(wicketId));
93         }
94     }
95
96     private void insertBarPlot(String wicketId, List<Metric> metrics) {
97         if ((metrics != null) && (metrics.size() > 0)) {
98             IChartData data = WicketUtils.getChartData(metrics);
99
fd6ac6 100             ChartProvider provider = new ChartProvider(new Dimension(400, 100),
f1720c 101                     ChartType.BAR_VERTICAL_SET, data);
JM 102             ChartAxis dateAxis = new ChartAxis(ChartAxisType.BOTTOM);
103             List<String> labels = new ArrayList<String>();
104             for (Metric metric : metrics) {
105                 labels.add(metric.name);
106             }
107             dateAxis.setLabels(labels.toArray(new String[labels.size()]));
108             provider.addAxis(dateAxis);
109
110             ChartAxis commitAxis = new ChartAxis(ChartAxisType.LEFT);
111             commitAxis.setLabels(new String[] { "",
112                     String.valueOf((int) WicketUtils.maxValue(metrics)) });
113             provider.addAxis(commitAxis);
114
115             add(new Chart(wicketId, provider));
116         } else {
117             add(WicketUtils.newBlankImage(wicketId));
118         }
119     }
120
121     private void insertPieChart(String wicketId, List<Metric> metrics) {
122         if ((metrics != null) && (metrics.size() > 0)) {
123             IChartData data = WicketUtils.getChartData(metrics);
124             List<String> labels = new ArrayList<String>();
125             for (Metric metric : metrics) {
126                 labels.add(metric.name);
127             }
36b3ff 128             ChartProvider provider = new ChartProvider(new Dimension(800, 200), ChartType.PIE, data);
f1720c 129             provider.setPieLabels(labels.toArray(new String[labels.size()]));
JM 130             add(new Chart(wicketId, provider));
131         } else {
132             add(WicketUtils.newBlankImage(wicketId));
133         }
134     }
135
1fa5e8 136     private List<Metric> getDayOfWeekMetrics(Repository repository, String objectId) {
40538c 137         List<Metric> list = MetricUtils.getDateMetrics(repository, objectId, false, "E", getTimeZone());
f1720c 138         SimpleDateFormat sdf = new SimpleDateFormat("E");
JM 139         Calendar cal = Calendar.getInstance();
140
c22722 141         List<Metric> sorted = new ArrayList<Metric>();
f1720c 142         int firstDayOfWeek = cal.getFirstDayOfWeek();
JM 143         int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
144
145         // rewind date to first day of week
146         cal.add(Calendar.DATE, firstDayOfWeek - dayOfWeek);
147         for (int i = 0; i < 7; i++) {
148             String day = sdf.format(cal.getTime());
149             for (Metric metric : list) {
150                 if (metric.name.equals(day)) {
c22722 151                     sorted.add(metric);
f1720c 152                     list.remove(metric);
JM 153                     break;
154                 }
155             }
156             cal.add(Calendar.DATE, 1);
157         }
158         return sorted;
159     }
160
1fa5e8 161     private List<Metric> getAuthorMetrics(Repository repository, String objectId) {
JM 162         List<Metric> authors = MetricUtils.getAuthorMetrics(repository, objectId, true);
f1720c 163         Collections.sort(authors, new Comparator<Metric>() {
JM 164             @Override
165             public int compare(Metric o1, Metric o2) {
166                 if (o1.count > o2.count) {
167                     return -1;
168                 } else if (o1.count < o2.count) {
1fa5e8 169                     return 1;
f1720c 170                 }
JM 171                 return 0;
172             }
173         });
174         if (authors.size() > 10) {
175             return authors.subList(0, 9);
176         }
177         return authors;
178     }
179
180     @Override
181     protected String getPageName() {
424fe1 182         return getString("gb.metrics");
f1720c 183     }
JM 184 }