private void sortAccordingFailedTests(Map<String, CompoundSuiteHistoryResult> results) {
   final long old = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2);
   for (CompoundSuiteHistoryResult compoundSuiteHistoryResult : results.values()) {
     Collections.sort(
         compoundSuiteHistoryResult.getResults(),
         new Comparator<SuiteResult>() {
           public int compare(SuiteResult s1, SuiteResult s2) {
             try {
               long s1Last = format.parse(s1.getTimestamp()).getTime();
               long s2Last = format.parse(s2.getTimestamp()).getTime();
               if (s1Last < old || s2Last < old) {
                 return (int) (s1Last - s2Last);
               }
             } catch (ParseException e) {
               e.printStackTrace();
             }
             if (s1.getTotalTestsRun() == 0) {
               return s1.getTotalTestsRun() - s2.getTotalTestsRun();
             }
             double diff =
                 (s1.getPassedTests() / (double) (s1.getTotalTestsRun() - s1.getSuspectedTests()))
                     - (s2.getPassedTests()
                         / (double) (s2.getTotalTestsRun() - s2.getSuspectedTests()));
             return diff > 0 ? 1 : diff < 0 ? -1 : 0;
           }
         });
   }
 }
  @Override
  public Map<String, CompoundSuiteHistoryResult> submitSuiteResultQuery() {
    EntityManager entityManager = null;
    try {
      entityManager = entityManagerFactory.createEntityManager();

      List<String> xapVersions = getXAPVersions(entityManager);
      Map<String, List<SuiteResult>> results = new HashMap<String, List<SuiteResult>>();

      String sqlFromQueryPart =
          "select * from ( select * from SgtestResult order by timestamp desc ) as t group by buildVersion, suiteName, jvmType";

      List<SuiteResult> suiteResults =
          new ArrayList<SuiteResult>(
              entityManager.createNativeQuery(sqlFromQueryPart, SuiteResult.class).getResultList());

      List<SuiteResult> tmpResult = null;
      for (String xapVersion : xapVersions) {
        String cloudifyVersion = props.getProperty("version" + xapVersion);
        tmpResult = new ArrayList<SuiteResult>();
        for (SuiteResult suiteResult : suiteResults) {
          if (suiteResult.getCompoundKey().getBuildVersion().equals(xapVersion)
              || suiteResult.getCompoundKey().getBuildVersion().equals(cloudifyVersion)) {
            tmpResult.add(suiteResult);
          }
        }
        results.put(xapVersion, tmpResult);
      }

      Map<String, CompoundSuiteHistoryResult> compoundSuiteHistoryResults =
          new HashMap<String, CompoundSuiteHistoryResult>();

      Set<String> keysMap = results.keySet();
      for (String xapVersion : keysMap) {
        CompoundSuiteHistoryResult suiteHistoryResult = new CompoundSuiteHistoryResult();
        suiteHistoryResult.setResults(results.get(xapVersion));
        Map<String, List<SuiteHistory>> suitePassedTests =
            new HashMap<String, List<SuiteHistory>>();
        for (SuiteResult result : suiteHistoryResult.getResults()) {
          List<SuiteHistory> passedTestsHistory = getSuiteHistory(entityManager, result);
          suitePassedTests.put(result.getCompoundKey().getSuiteName(), passedTestsHistory);
        }
        suiteHistoryResult.setSuiteHistory(suitePassedTests);
        compoundSuiteHistoryResults.put(xapVersion, suiteHistoryResult);
      }
      sortAccordingFailedTests(compoundSuiteHistoryResults);
      return compoundSuiteHistoryResults;
    } finally {
      if (entityManager != null) entityManager.close();
    }
  }