Commits

Chris Doble committed dc53f9f

Add pagination.

Comments (0)

Files changed (2)

src/main/java/com/atlassian/bamboo/plugins/failureleaderboard/ViewFailureLeaderboard.java

 
 import com.atlassian.bamboo.build.FilterController;
 import com.atlassian.bamboo.chains.ChainResultsSummary;
+import com.atlassian.bamboo.filter.Pager;
 import com.atlassian.bamboo.resultsummary.BuildResultsSummary;
 import com.atlassian.bamboo.resultsummary.ResultsSummary;
 import com.atlassian.bamboo.resultsummary.tests.*;
 import com.atlassian.bamboo.ww2.actions.ChainActionSupport;
+import com.atlassian.bamboo.ww2.aware.LazyPaginationAware;
 import com.atlassian.bamboo.ww2.aware.ResultsListAware;
 import com.google.common.collect.Lists;
 
 /**
  * An action that calculates and displays the failure leaderboard.
  */
-public class ViewFailureLeaderboard extends ChainActionSupport implements ResultsListAware {
+public class ViewFailureLeaderboard extends ChainActionSupport implements LazyPaginationAware<TestCaseFailures>, ResultsListAware {
     private FilterController filterController;
     private List<TestCaseFailures> leadingTestCaseFailures;
+    private Pager<TestCaseFailures> pager;
     private List<ResultsSummary> resultsList;
 
     /**
         return !resultsSummary.hasChanges() && changedTests > 0;
     }
 
-    public List<TestCaseFailures> getLeadingTestCaseFailures() {
+    /**
+     * Returns a human-readable description of a test case's failure rate.
+     *
+     * @param testCaseFailures The test case failures to be described.
+     * @return A human-readable description of {@code testCaseFailures}.
+     */
+    public String getFailureRateText(TestCaseFailures testCaseFailures) {
+        int buildCount = resultsList.size();
+        int failureCount = testCaseFailures.getFailedTestCaseResults().size();
+        return String.format("This test has failed %d time%s over %d build%s.",
+                failureCount,
+                failureCount == 1 ? "" : "s",
+                buildCount,
+                buildCount == 1 ? "" : "s"
+        );
+    }
+
+    private List<TestCaseFailures> getLeadingTestCaseFailures() {
         if (leadingTestCaseFailures == null) {
             // A map of TestCase IDs to associated TestCaseFailure objects.
             Map<Long, TestCaseFailures> testCaseMap = new HashMap<Long, TestCaseFailures>();
     }
 
     @Override
+    public Pager<TestCaseFailures> getPager() {
+        return pager;
+    }
+
+    @Override
+    public void setPager(Pager<TestCaseFailures> pager) {
+        this.pager = pager;
+    }
+
+    @Override
+    public List<TestCaseFailures> getFilteredResultsList(int firstResult, int maxResults) {
+        int toIndex = Math.min(getLeadingTestCaseFailures().size(), firstResult + maxResults);
+        return getLeadingTestCaseFailures().subList(firstResult, toIndex);
+    }
+
+    @Override
+    public int getResultsCount() {
+        return getLeadingTestCaseFailures().size();
+    }
+
+    @Override
     public List<? extends ResultsSummary> getResultsList() {
         return resultsList;
     }

src/main/resources/templates/viewFailureLeaderboard.ftl

         [@cp.filterDropDown filterController=filterController/]
         <h1>Failure Leaderboard</h1>
 
-        [#if leadingTestCaseFailures?has_content]
+        [#if (pager.page.list)?has_content]
             <div class="twixie-controls">
                 <a href="#" class="expand-all"><span class="icon icon-expand"></span>Expand all</a>
                 <a href="#" class="collapse-all"><span class="icon icon-collapse"></span>Collapse all</a>
                 </thead>
                 <tbody>
                     [#assign buildCount = resultsList?size/]
-                    [#list leadingTestCaseFailures as testCaseFailures]
+                    [#list pager.page.list as testCaseFailures]
                         <tr>
                             [#assign testCase = testCaseFailures.testCase/]
                             [#assign failureRate = testCaseFailures.failedTestCaseResults.size() * 100.0 / buildCount/]
                             <td class="twixie"><span class="icon icon-expand" title="Expand">Expand</span></td>
-                            <td class="failure-rate">${failureRate?string("0")}%</td>
+                            <td class="failure-rate" title="${action.getFailureRateText(testCaseFailures)}"">${failureRate?string("0")}%</td>
                             <td>
                                 <p class="test-name">${testCase.testClass.shortName} ${testCase.name}</p>
                                 [#if !action.isTestCaseFailing(testCase)]
                         </tr>
                     [/#list]
                 </tbody>
+                [#if (pager.page)?has_content]
+                    <tfoot>
+                        <tr>
+                            <td colspan="3">
+                                [@cp.pagination/]
+                            </td>
+                        </tr>
+                    </tfoot>
+                [/#if]
             </table>
         [#else]
             <p>No builds have been run.</p>