/hudson-core/src/main/java/hudson/tasks/junit/History.java

http://github.com/hudson/hudson · Java · 296 lines · 214 code · 42 blank · 40 comment · 18 complexity · c053bdc62d463176c8e2aef87b3df25a MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2010, Sun Microsystems, Inc., Tom Huybrechts, Yahoo!, Inc., Seiji Sogabe
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.tasks.junit;
  25. import hudson.model.AbstractBuild;
  26. import hudson.model.Hudson;
  27. import hudson.tasks.test.TestObject;
  28. import hudson.tasks.test.TestResult;
  29. import hudson.util.ChartUtil;
  30. import hudson.util.ColorPalette;
  31. import hudson.util.DataSetBuilder;
  32. import hudson.util.Graph;
  33. import hudson.util.ShiftedCategoryAxis;
  34. import hudson.util.StackedAreaRenderer2;
  35. import java.awt.Color;
  36. import java.awt.Paint;
  37. import java.util.ArrayList;
  38. import java.util.List;
  39. import org.jfree.chart.ChartFactory;
  40. import org.jfree.chart.JFreeChart;
  41. import org.jfree.chart.axis.CategoryAxis;
  42. import org.jfree.chart.axis.CategoryLabelPositions;
  43. import org.jfree.chart.axis.NumberAxis;
  44. import org.jfree.chart.plot.CategoryPlot;
  45. import org.jfree.chart.plot.PlotOrientation;
  46. import org.jfree.chart.renderer.category.StackedAreaRenderer;
  47. import org.jfree.data.category.CategoryDataset;
  48. import org.jfree.ui.RectangleInsets;
  49. import org.kohsuke.stapler.Stapler;
  50. /**
  51. * History of {@link hudson.tasks.test.TestObject} over time.
  52. *
  53. * @since 1.320
  54. */
  55. public class History {
  56. private final TestObject testObject;
  57. public History(TestObject testObject) {
  58. this.testObject = testObject;
  59. }
  60. public TestObject getTestObject() {
  61. return testObject;
  62. }
  63. public boolean historyAvailable() {
  64. if (testObject.getOwner().getParent().getBuilds().size() > 1)
  65. return true;
  66. else
  67. return false;
  68. }
  69. public List<TestResult> getList(int start, int end) {
  70. List<TestResult> list = new ArrayList<TestResult>();
  71. end = Math.min(end, testObject.getOwner().getParent().getBuilds().size());
  72. for (AbstractBuild<?,?> b: testObject.getOwner().getParent().getBuilds().subList(start, end)) {
  73. if (b.isBuilding()) continue;
  74. TestResult o = testObject.getResultInBuild(b);
  75. if (o != null) {
  76. list.add(o);
  77. }
  78. }
  79. return list;
  80. }
  81. public List<TestResult> getList() {
  82. return getList(0, testObject.getOwner().getParent().getBuilds().size());
  83. }
  84. /**
  85. * Graph of duration of tests over time.
  86. */
  87. public Graph getDurationGraph() {
  88. return new GraphImpl("seconds") {
  89. protected DataSetBuilder<String, ChartLabel> createDataSet() {
  90. DataSetBuilder<String, ChartLabel> data = new DataSetBuilder<String, ChartLabel>();
  91. List<TestResult> list;
  92. try {
  93. list = getList(
  94. Integer.parseInt(Stapler.getCurrentRequest().getParameter("start")),
  95. Integer.parseInt(Stapler.getCurrentRequest().getParameter("end")));
  96. } catch (NumberFormatException e) {
  97. list = getList();
  98. }
  99. for (hudson.tasks.test.TestResult o: list) {
  100. data.add(((double) o.getDuration()) / (1000), "", new ChartLabel(o) {
  101. @Override
  102. public Color getColor() {
  103. if (o.getFailCount() > 0)
  104. return ColorPalette.RED;
  105. else if (o.getSkipCount() > 0)
  106. return ColorPalette.YELLOW;
  107. else
  108. return ColorPalette.BLUE;
  109. }
  110. });
  111. }
  112. return data;
  113. }
  114. };
  115. }
  116. /**
  117. * Graph of # of tests over time.
  118. */
  119. public Graph getCountGraph() {
  120. return new GraphImpl("") {
  121. protected DataSetBuilder<String, ChartLabel> createDataSet() {
  122. DataSetBuilder<String, ChartLabel> data = new DataSetBuilder<String, ChartLabel>();
  123. List<TestResult> list;
  124. try {
  125. list = getList(
  126. Integer.parseInt(Stapler.getCurrentRequest().getParameter("start")),
  127. Integer.parseInt(Stapler.getCurrentRequest().getParameter("end")));
  128. } catch (NumberFormatException e) {
  129. list = getList();
  130. }
  131. for (TestResult o: list) {
  132. data.add(o.getPassCount(), "2Passed", new ChartLabel(o));
  133. data.add(o.getFailCount(), "1Failed", new ChartLabel(o));
  134. data.add(o.getSkipCount(), "0Skipped", new ChartLabel(o));
  135. }
  136. return data;
  137. }
  138. };
  139. }
  140. private abstract class GraphImpl extends Graph {
  141. private final String yLabel;
  142. protected GraphImpl(String yLabel) {
  143. super(-1,600,300); // cannot use timestamp, since ranges may change
  144. this.yLabel = yLabel;
  145. }
  146. protected abstract DataSetBuilder<String, ChartLabel> createDataSet();
  147. protected JFreeChart createGraph() {
  148. final CategoryDataset dataset = createDataSet().build();
  149. final JFreeChart chart = ChartFactory.createStackedAreaChart(null, // chart
  150. // title
  151. null, // unused
  152. yLabel, // range axis label
  153. dataset, // data
  154. PlotOrientation.VERTICAL, // orientation
  155. false, // include legend
  156. true, // tooltips
  157. false // urls
  158. );
  159. chart.setBackgroundPaint(Color.white);
  160. final CategoryPlot plot = chart.getCategoryPlot();
  161. // plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
  162. plot.setBackgroundPaint(Color.WHITE);
  163. plot.setOutlinePaint(null);
  164. plot.setForegroundAlpha(0.8f);
  165. // plot.setDomainGridlinesVisible(true);
  166. // plot.setDomainGridlinePaint(Color.white);
  167. plot.setRangeGridlinesVisible(true);
  168. plot.setRangeGridlinePaint(Color.black);
  169. CategoryAxis domainAxis = new ShiftedCategoryAxis(null);
  170. plot.setDomainAxis(domainAxis);
  171. domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
  172. domainAxis.setLowerMargin(0.0);
  173. domainAxis.setUpperMargin(0.0);
  174. domainAxis.setCategoryMargin(0.0);
  175. final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
  176. ChartUtil.adjustChebyshev(dataset, rangeAxis);
  177. rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
  178. rangeAxis.setAutoRange(true);
  179. StackedAreaRenderer ar = new StackedAreaRenderer2() {
  180. @Override
  181. public Paint getItemPaint(int row, int column) {
  182. ChartLabel key = (ChartLabel) dataset.getColumnKey(column);
  183. if (key.getColor() != null) return key.getColor();
  184. return super.getItemPaint(row, column);
  185. }
  186. @Override
  187. public String generateURL(CategoryDataset dataset, int row,
  188. int column) {
  189. ChartLabel label = (ChartLabel) dataset.getColumnKey(column);
  190. return label.getUrl();
  191. }
  192. @Override
  193. public String generateToolTip(CategoryDataset dataset, int row,
  194. int column) {
  195. ChartLabel label = (ChartLabel) dataset.getColumnKey(column);
  196. return label.o.getOwner().getDisplayName() + " : "
  197. + label.o.getDurationString();
  198. }
  199. };
  200. plot.setRenderer(ar);
  201. ar.setSeriesPaint(0,ColorPalette.RED); // Failures.
  202. ar.setSeriesPaint(1,ColorPalette.YELLOW); // Skips.
  203. ar.setSeriesPaint(2,ColorPalette.BLUE); // Total.
  204. // crop extra space around the graph
  205. plot.setInsets(new RectangleInsets(0, 0, 0, 5.0));
  206. return chart;
  207. }
  208. }
  209. class ChartLabel implements Comparable<ChartLabel> {
  210. TestResult o;
  211. String url;
  212. public ChartLabel(TestResult o) {
  213. this.o = o;
  214. this.url = null;
  215. }
  216. public String getUrl() {
  217. if (this.url == null) generateUrl();
  218. return url;
  219. }
  220. private void generateUrl() {
  221. AbstractBuild<?,?> build = o.getOwner();
  222. String buildLink = build.getUrl();
  223. String actionUrl = o.getTestResultAction().getUrlName();
  224. this.url = Hudson.getInstance().getRootUrl() + buildLink + actionUrl + o.getUrl();
  225. }
  226. public int compareTo(ChartLabel that) {
  227. return this.o.getOwner().number - that.o.getOwner().number;
  228. }
  229. @Override
  230. public boolean equals(Object o) {
  231. if (!(o instanceof ChartLabel)) {
  232. return false;
  233. }
  234. ChartLabel that = (ChartLabel) o;
  235. return this.o == that.o;
  236. }
  237. public Color getColor() {
  238. return null;
  239. }
  240. @Override
  241. public int hashCode() {
  242. return o.hashCode();
  243. }
  244. @Override
  245. public String toString() {
  246. String l = o.getOwner().getDisplayName();
  247. String s = o.getOwner().getBuiltOnStr();
  248. if (s != null)
  249. l += ' ' + s;
  250. return l;
  251. // return o.getDisplayName() + " " + o.getOwner().getDisplayName();
  252. }
  253. }
  254. }