public Integer getCheapCount()

in rest-api/src/jetbrains/buildServer/server/rest/data/finder/impl/BuildPromotionFinder.java [2125:2188]


      public Integer getCheapCount(@Nullable final String locatorText) {
        /*
        This emulates build's isUsedByOtherBuilds() via a request like:
            .../app/rest/builds?fields=build(id,related(builds(count,$locator(count:1,defaultFilter:false,item:(count:1,defaultFilter:false,snapshotDependency:(from:(id:$context.build.id),recursive:false)),item:(count:1,defaultFilter:false,artifactDependency:(from:(id:$context.build.id),recursive:false))))))
         */
        Locator locator;
        try {
          locator = createLocator(locatorText, null);
        } catch (Exception e) {
          return null;
        }
        setLocatorDefaults(locator);
        Long count = locator.getSingleDimensionValueAsLong(PagerData.COUNT);

        Integer result = null;

        final List<String> itemDimension = locator.getDimensionValue(DIMENSION_ITEM);
        if (!itemDimension.isEmpty()) {
          if (count != null) {
            int[] max = new int[1];
            max[0] = -1;
            boolean exceedsCount = itemDimension.stream().map(l -> getCheapCount(l)).anyMatch(cheapCount -> {
              if (cheapCount == null) {
                max[0] = -2; //there is not cheap count
                return false;
              }
              if (max[0] != -2) {
                max[0] = Math.max(max[0], cheapCount);
              }
              return cheapCount >= count;
            });
            if (exceedsCount) {
              result = count.intValue();
            } else if (max[0] == 0) { //all counts were cheap and all were 0
              result = 0;
            }
          }
        } else {
          //optimization method counts all builds and these can be set by defaultFilter
          if (locator.getSingleDimensionValueAsBoolean(PERSONAL) != null) return null;
          if (locator.getSingleDimensionValueAsBoolean(CANCELED) != null) return null;
          if (locator.getSingleDimensionValueAsBoolean(FAILED_TO_START) != null) return null;
          if (locator.getSingleDimensionValueAsBoolean(BRANCH) != null) return null;

          final String snapshotDepDimension = locator.getSingleDimensionValue(SNAPSHOT_DEP);
          if (snapshotDepDimension != null) {
            result = getSnapshotRelatedBuildsCheapCount(snapshotDepDimension, count == null ? null : count.intValue());
          }
          if (result == null) {
            if (count == null) {
              return null;
            }
            final String artifactDepDimension = locator.getSingleDimensionValue(ARTIFACT_DEP);
            if (artifactDepDimension != null) {
              result = getArtifactRelatedBuildsCheapCount(artifactDepDimension, count.intValue());
            }
          }
        }

        if (!locator.getUnusedDimensions().isEmpty()) return null;

        if (result == null) return null;
        return count != null ? Math.min(count.intValue(), result) : result;
      }