PageRenderTime 20ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/testability-explorer/src/main/java/com/google/test/metric/report/GradeCategories.java

http://testability-explorer.googlecode.com/
Java | 159 lines | 127 code | 17 blank | 15 comment | 13 complexity | a65cc7a355d80694d52991a1c0c96ee6 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2007 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.test.metric.report;
  17. import static com.google.test.metric.report.chart.GoogleChartAPI.GREEN;
  18. import static com.google.test.metric.report.chart.GoogleChartAPI.RED;
  19. import static com.google.test.metric.report.chart.GoogleChartAPI.YELLOW;
  20. import static java.lang.Integer.MAX_VALUE;
  21. import static java.lang.Math.ceil;
  22. import static java.lang.Math.log;
  23. import static java.lang.Math.max;
  24. import static java.lang.Math.min;
  25. import java.util.List;
  26. import com.google.common.base.Function;
  27. import com.google.test.metric.report.chart.GoodnessChart;
  28. import com.google.test.metric.report.chart.Histogram;
  29. import com.google.test.metric.report.chart.HistogramChartUrl;
  30. import com.google.test.metric.report.chart.PieChartUrl;
  31. import com.google.test.metric.report.chart.Histogram.Linear;
  32. public class GradeCategories {
  33. private static final int MAX_HISTOGRAM_BINS = 30;
  34. private static final int HISTOGRAM_LEGEND_WIDTH = 130;
  35. private final int maxExcellentCost;
  36. private final int maxAcceptableCost;
  37. public GradeCategories(int maxExcellentCost, int maxAcceptableCost) {
  38. this.maxExcellentCost = maxExcellentCost;
  39. this.maxAcceptableCost = maxAcceptableCost;
  40. }
  41. public int getExcellentCount(List<Integer> costs) {
  42. int count = 0;
  43. for (int cost : costs) {
  44. if (cost <= maxExcellentCost) {
  45. count++;
  46. }
  47. }
  48. return count;
  49. }
  50. public int getGoodCount(List<Integer> costs) {
  51. int count = 0;
  52. for (int cost : costs) {
  53. if (cost > maxExcellentCost && cost <= maxAcceptableCost) {
  54. count++;
  55. }
  56. }
  57. return count;
  58. }
  59. public int getNeedsWorkCount(List<Integer> costs) {
  60. int count = 0;
  61. for (int cost : costs) {
  62. if (cost > maxAcceptableCost) {
  63. count++;
  64. }
  65. }
  66. return count;
  67. }
  68. public GoodnessChart createOverallChart(int value) {
  69. GoodnessChart chart = new GoodnessChart(maxExcellentCost, maxAcceptableCost,
  70. 10 * maxAcceptableCost, 100 * maxAcceptableCost);
  71. chart.setUnscaledValues(value);
  72. return chart;
  73. }
  74. public PieChartUrl createDistributionChart(List<Integer> costs) {
  75. PieChartUrl chart = new PieChartUrl();
  76. chart.setItemLabel("Excellent", "Good", "Needs Work");
  77. chart.setColors(GREEN, YELLOW, RED);
  78. int excellentCount = getExcellentCount(costs);
  79. int goodCount = getGoodCount(costs);
  80. int needsWorkCount = getNeedsWorkCount(costs);
  81. chart.setValues(excellentCount, goodCount, needsWorkCount);
  82. return chart;
  83. }
  84. public HistogramChartUrl createHistogram(int width, int height, List<Integer> costs) {
  85. return createHistogram(width, height, costs, new Linear());
  86. }
  87. public HistogramChartUrl createHistogram(int width, int height, List<Integer> costs,
  88. Function<Integer, Double> scalingFunction) {
  89. int maxScale = 61;
  90. MultiHistogramDataModel model = buildHistogramDataModel(costs, scalingFunction);
  91. int[] excellent = model.getExcellent().getScaledBinRange(0, MAX_VALUE, maxScale);
  92. int[] good = model.getGood().getScaledBinRange(0, MAX_VALUE, maxScale);
  93. int[] needsWork = model.getNeedsWork().getScaledBinRange(0, MAX_VALUE, maxScale);
  94. HistogramChartUrl chart = new HistogramChartUrl();
  95. chart.setItemLabel(model.getOverallHistogram().getBinLabels(20));
  96. chart.setValues(excellent, good, needsWork);
  97. chart.setYMark(0, model.getOverallHistogram().getMaxBin(), scalingFunction);
  98. chart.setSize(width, height);
  99. chart.setBarWidth((width - HISTOGRAM_LEGEND_WIDTH) / model.getBinCount(), 0, 0);
  100. chart.setChartLabel("Excellent", "Good", "Needs Work");
  101. chart.setColors(GREEN, YELLOW, RED);
  102. return chart;
  103. }
  104. public MultiHistogramDataModel buildHistogramDataModel(List<Integer> costs,
  105. Function<Integer, Double> scalingFunction) {
  106. int binCount = min(MAX_HISTOGRAM_BINS, 10 * (int) log(costs.size()) + 1);
  107. int binWidth = (int) ceil((double) findMax(costs) / binCount);
  108. Histogram overallHistogram = new Histogram(0, binWidth, binCount, scalingFunction);
  109. Histogram excellentHistogram = new Histogram(0, binWidth, binCount, scalingFunction);
  110. Histogram goodHistogram = new Histogram(0, binWidth, binCount, scalingFunction);
  111. Histogram needsWorkHistogram = new Histogram(0, binWidth, binCount, scalingFunction);
  112. for (int overallCost : costs) {
  113. if (overallCost <= maxExcellentCost) {
  114. excellentHistogram.value(overallCost);
  115. } else if (overallCost <= maxAcceptableCost) {
  116. goodHistogram.value(overallCost);
  117. } else {
  118. needsWorkHistogram.value(overallCost);
  119. }
  120. overallHistogram.value(overallCost);
  121. }
  122. int maxBin = overallHistogram.getMaxBin();
  123. excellentHistogram.setMaxBin(maxBin);
  124. goodHistogram.setMaxBin(maxBin);
  125. needsWorkHistogram.setMaxBin(maxBin);
  126. return new MultiHistogramDataModel(excellentHistogram, goodHistogram, needsWorkHistogram,
  127. overallHistogram, binCount, binWidth);
  128. }
  129. private int findMax(List<Integer> values) {
  130. int maxValue = Integer.MIN_VALUE;
  131. for (int value : values) {
  132. maxValue = max(maxValue, value);
  133. }
  134. return maxValue;
  135. }
  136. public int getMaxExcellentCost() {
  137. return maxExcellentCost;
  138. }
  139. public int getMaxAcceptableCost() {
  140. return maxAcceptableCost;
  141. }
  142. }