@Override
  public DataResponse<Iterable<TestResult>> historySearch(String componentId, String jobName) {
    ObjectId cId = null;
    if (componentId != null && !componentId.isEmpty()) {
      cId = new ObjectId(componentId);
    }
    Component component = componentRepository.findOne(cId);
    if (!component.getCollectorItems().containsKey(CollectorType.Test)) {

      return new DataResponse<>(null, 0L);
    }
    List<TestResult> result = new ArrayList<>();

    for (CollectorItem item : component.getCollectorItems().get(CollectorType.Test)) {

      QTestResult testResult = new QTestResult("testResult");
      BooleanBuilder builder = new BooleanBuilder();

      builder.and(testResult.collectorItemId.eq(item.getId()));

      if (jobName != null && !jobName.isEmpty()) {
        builder.and(testResult.jobName.eq(jobName));
      }

      result.addAll(
          Lists.newArrayList(
              testResultRepository.findAll(builder.getValue(), testResult.timestamp.desc())));
    }

    return new DataResponse<>(result, 0L);
  }
  @Override
  public DataResponse<Iterable<TestResult>> search(TestResultRequest request) {
    Component component = componentRepository.findOne(request.getComponentId());
    if (!component.getCollectorItems().containsKey(CollectorType.Test)) {
      return new DataResponse<>(null, 0L);
    }
    List<TestResult> result = new ArrayList<>();

    for (CollectorItem item : component.getCollectorItems().get(CollectorType.Test)) {

      QTestResult testResult = new QTestResult("testResult");
      BooleanBuilder builder = new BooleanBuilder();

      builder.and(testResult.collectorItemId.eq(item.getId()));

      if (request.validStartDateRange()) {
        builder.and(
            testResult.startTime.between(request.getStartDateBegins(), request.getStartDateEnds()));
      }
      if (request.validEndDateRange()) {
        builder.and(
            testResult.endTime.between(request.getEndDateBegins(), request.getEndDateEnds()));
      }

      if (request.validDurationRange()) {
        builder.and(
            testResult.duration.between(
                request.getDurationGreaterThan(), request.getDurationLessThan()));
      }

      if (!request.getTypes().isEmpty()) {
        builder.and(testResult.testCapabilities.any().type.in(request.getTypes()));
      }

      if (request.getMax() == null) {
        result.addAll(
            Lists.newArrayList(
                testResultRepository.findAll(builder.getValue(), testResult.timestamp.desc())));
      } else {
        PageRequest pageRequest =
            new PageRequest(0, request.getMax(), Sort.Direction.DESC, "timestamp");
        result.addAll(
            Lists.newArrayList(
                testResultRepository.findAll(builder.getValue(), pageRequest).getContent()));
      }
    }
    // One collector per Type. get(0) is hardcoded.
    if (!CollectionUtils.isEmpty(component.getCollectorItems().get(CollectorType.Test))
        && (component.getCollectorItems().get(CollectorType.Test).get(0) != null)) {
      Collector collector =
          collectorRepository.findOne(
              component.getCollectorItems().get(CollectorType.Test).get(0).getCollectorId());
      if (collector != null) {
        return new DataResponse<>(
            pruneToDepth(result, request.getDepth()), collector.getLastExecuted());
      }
    }

    return new DataResponse<>(null, 0L);
  }
 /**
  * Clean up unused deployment collector items
  *
  * @param collector the {@link UDeployCollector}
  */
 @SuppressWarnings("PMD.AvoidDeeplyNestedIfStmts")
 private void clean(OctopusCollector collector) {
   deleteUnwantedJobs(collector);
   Set<ObjectId> uniqueIDs = new HashSet<>();
   for (com.capitalone.dashboard.model.Component comp : dbComponentRepository.findAll()) {
     if (comp.getCollectorItems() == null || comp.getCollectorItems().isEmpty()) continue;
     List<CollectorItem> itemList = comp.getCollectorItems().get(CollectorType.Deployment);
     if (itemList == null) continue;
     for (CollectorItem ci : itemList) {
       if (ci == null) continue;
       uniqueIDs.add(ci.getId());
     }
   }
   List<OctopusApplication> appList = new ArrayList<>();
   Set<ObjectId> udId = new HashSet<>();
   udId.add(collector.getId());
   for (OctopusApplication app : octopusApplicationRepository.findByCollectorIdIn(udId)) {
     if (app != null) {
       app.setEnabled(uniqueIDs.contains(app.getId()));
       appList.add(app);
     }
   }
   octopusApplicationRepository.save(appList);
 }