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.

MetricsPage.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.ParseException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.ArrayList;
  21. import java.util.Calendar;
  22. import java.util.Collections;
  23. import java.util.Comparator;
  24. import java.util.Date;
  25. import java.util.List;
  26. import org.apache.wicket.PageParameters;
  27. import org.apache.wicket.behavior.HeaderContributor;
  28. import org.apache.wicket.markup.html.basic.Label;
  29. import org.eclipse.jgit.lib.Repository;
  30. import com.gitblit.models.Metric;
  31. import com.gitblit.utils.MetricUtils;
  32. import com.gitblit.utils.StringUtils;
  33. import com.gitblit.wicket.CacheControl;
  34. import com.gitblit.wicket.CacheControl.LastModified;
  35. import com.gitblit.wicket.WicketUtils;
  36. import com.gitblit.wicket.charting.Chart;
  37. import com.gitblit.wicket.charting.Charts;
  38. import com.gitblit.wicket.charting.Flotr2Charts;
  39. @CacheControl(LastModified.REPOSITORY)
  40. public class MetricsPage extends RepositoryPage {
  41. public MetricsPage(PageParameters params) {
  42. super(params);
  43. Repository r = getRepository();
  44. if (StringUtils.isEmpty(objectId)) {
  45. add(new Label("branchTitle", getRepositoryModel().HEAD));
  46. } else {
  47. add(new Label("branchTitle", objectId));
  48. }
  49. Metric metricsTotal = null;
  50. List<Metric> metrics = MetricUtils.getDateMetrics(r, objectId, true, null, getTimeZone());
  51. metricsTotal = metrics.remove(0);
  52. if (metricsTotal == null) {
  53. add(new Label("branchStats", ""));
  54. } else {
  55. add(new Label("branchStats",
  56. MessageFormat.format(getString("gb.branchStats"), metricsTotal.count,
  57. metricsTotal.tag, getTimeUtils().duration(metricsTotal.duration))));
  58. }
  59. Charts charts = new Flotr2Charts();
  60. add(WicketUtils.newBlankImage("commitsChart"));
  61. add(WicketUtils.newBlankImage("dayOfWeekChart"));
  62. add(WicketUtils.newBlankImage("authorsChart"));
  63. createLineChart(charts, "commitsChart", metrics);
  64. createBarChart(charts, "dayOfWeekChart", getDayOfWeekMetrics(r, objectId));
  65. createPieChart(charts, "authorsChart", getAuthorMetrics(r, objectId));
  66. add(new HeaderContributor(charts));
  67. }
  68. private void createLineChart(Charts charts, String id, List<Metric> metrics) {
  69. if ((metrics != null) && (metrics.size() > 0)) {
  70. Chart chart = charts.createLineChart(id, "", "day",
  71. getString("gb.commits"));
  72. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  73. String displayFormat = "MMM dd";
  74. if(metrics.size() > 0 && metrics.get(0).name.length() == 7){
  75. df = new SimpleDateFormat("yyyy-MM");
  76. displayFormat = "yyyy MMM";
  77. }
  78. df.setTimeZone(getTimeZone());
  79. chart.setDateFormat(displayFormat);
  80. for (Metric metric : metrics) {
  81. Date date;
  82. try {
  83. date = df.parse(metric.name);
  84. } catch (ParseException e) {
  85. logger.error("Unable to parse date: " + metric.name);
  86. return;
  87. }
  88. chart.addValue(date, (int)metric.count);
  89. if(metric.tag > 0 ){
  90. chart.addHighlight(date, (int)metric.count);
  91. }
  92. }
  93. charts.addChart(chart);
  94. }
  95. }
  96. private void createPieChart(Charts charts, String id, List<Metric> metrics) {
  97. if ((metrics != null) && (metrics.size() > 0)) {
  98. Chart chart = charts.createPieChart(id, "", "day",
  99. getString("gb.commits"));
  100. for (Metric metric : metrics) {
  101. chart.addValue(metric.name, (int)metric.count);
  102. }
  103. charts.addChart(chart);
  104. }
  105. }
  106. private void createBarChart(Charts charts, String id, List<Metric> metrics) {
  107. if ((metrics != null) && (metrics.size() > 0)) {
  108. Chart chart = charts.createBarChart(id, "", "day",
  109. getString("gb.commits"));
  110. for (Metric metric : metrics) {
  111. chart.addValue(metric.name, (int)metric.count);
  112. }
  113. charts.addChart(chart);
  114. }
  115. }
  116. private List<Metric> getDayOfWeekMetrics(Repository repository, String objectId) {
  117. List<Metric> list = MetricUtils.getDateMetrics(repository, objectId, false, "E", getTimeZone());
  118. SimpleDateFormat sdf = new SimpleDateFormat("E");
  119. Calendar cal = Calendar.getInstance();
  120. List<Metric> sorted = new ArrayList<Metric>();
  121. int firstDayOfWeek = cal.getFirstDayOfWeek();
  122. int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
  123. // rewind date to first day of week
  124. cal.add(Calendar.DATE, firstDayOfWeek - dayOfWeek);
  125. for (int i = 0; i < 7; i++) {
  126. String day = sdf.format(cal.getTime());
  127. for (Metric metric : list) {
  128. if (metric.name.equals(day)) {
  129. sorted.add(metric);
  130. list.remove(metric);
  131. break;
  132. }
  133. }
  134. cal.add(Calendar.DATE, 1);
  135. }
  136. return sorted;
  137. }
  138. private List<Metric> getAuthorMetrics(Repository repository, String objectId) {
  139. List<Metric> authors = MetricUtils.getAuthorMetrics(repository, objectId, true);
  140. Collections.sort(authors, new Comparator<Metric>() {
  141. @Override
  142. public int compare(Metric o1, Metric o2) {
  143. if (o1.count > o2.count) {
  144. return -1;
  145. } else if (o1.count < o2.count) {
  146. return 1;
  147. }
  148. return 0;
  149. }
  150. });
  151. if (authors.size() > 10) {
  152. return authors.subList(0, 9);
  153. }
  154. return authors;
  155. }
  156. @Override
  157. protected String getPageName() {
  158. return getString("gb.metrics");
  159. }
  160. @Override
  161. protected Class<? extends BasePage> getRepoNavPageClass() {
  162. return SummaryPage.class;
  163. }
  164. }