public QualityGateConditionDto createCondition(
      DbSession dbSession,
      long qGateId,
      String metricKey,
      String operator,
      @Nullable String warningThreshold,
      @Nullable String errorThreshold,
      @Nullable Integer period) {
    getNonNullQgate(dbSession, qGateId);
    MetricDto metric = getNonNullMetric(dbSession, metricKey);
    validateCondition(metric, operator, warningThreshold, errorThreshold, period);
    checkConditionDoesNotAlreadyExistOnSameMetricAndPeriod(
        getConditions(dbSession, qGateId, null), metric, period);

    QualityGateConditionDto newCondition =
        new QualityGateConditionDto()
            .setQualityGateId(qGateId)
            .setMetricId(metric.getId())
            .setMetricKey(metric.getKey())
            .setOperator(operator)
            .setWarningThreshold(warningThreshold)
            .setErrorThreshold(errorThreshold)
            .setPeriod(period);
    dbClient.gateConditionDao().insert(newCondition, dbSession);
    return newCondition;
  }
Пример #2
0
 private void updateMetricInDb(
     DbSession dbSession, MetricDto metricInDb, MetricDto metricTemplate) {
   String key = metricTemplate.getKey();
   String name = metricTemplate.getShortName();
   String type = metricTemplate.getValueType();
   String domain = metricTemplate.getDomain();
   String description = metricTemplate.getDescription();
   if (key != null) {
     metricInDb.setKey(key);
   }
   if (name != null) {
     metricInDb.setShortName(name);
   }
   if (type != null) {
     metricInDb.setValueType(type);
   }
   if (domain != null) {
     metricInDb.setDomain(domain);
   }
   if (description != null) {
     metricInDb.setDescription(description);
   }
   dbClient.metricDao().update(dbSession, metricInDb);
   dbSession.commit();
 }
 private static void checkRatingMetric(
     MetricDto metric,
     @Nullable String warningThreshold,
     @Nullable String errorThreshold,
     @Nullable Integer period,
     Errors errors) {
   if (!metric.getValueType().equals(RATING.name())) {
     return;
   }
   if (period != null && !metric.getKey().startsWith("new_")) {
     errors.add(
         Message.of(
             format("The metric '%s' cannot be used on the leak period", metric.getShortName())));
   }
   if (!isValidRating(warningThreshold)) {
     addInvalidRatingError(warningThreshold, errors);
     return;
   }
   if (!isValidRating(errorThreshold)) {
     addInvalidRatingError(errorThreshold, errors);
     return;
   }
   checkRatingGreaterThanOperator(warningThreshold, errors);
   checkRatingGreaterThanOperator(errorThreshold, errors);
 }
 private static void checkPeriod(MetricDto metric, @Nullable Integer period, Errors errors) {
   if (period == null) {
     errors.check(
         !metric.getKey().startsWith("new_"),
         "A period must be selected for differential metrics.");
   } else {
     errors.check(period == 1, "The only valid quality gate period is 1, the leak period.");
   }
 }
Пример #5
0
 private static void writeMetric(JsonWriter json, MetricDto metric) {
   json.beginObject();
   json.prop(FIELD_ID, String.valueOf(metric.getId()));
   json.prop(FIELD_KEY, metric.getKey());
   json.prop(FIELD_TYPE, metric.getValueType());
   json.prop(FIELD_NAME, metric.getShortName());
   json.prop(FIELD_DOMAIN, metric.getDomain());
   json.prop(FIELD_DESCRIPTION, metric.getDescription());
   json.endObject();
 }
Пример #6
0
 private void checkNoOtherMetricWithTargetKey(
     DbSession dbSession, MetricDto metricInDb, MetricDto template) {
   String targetKey = template.getKey();
   MetricDto metricWithTargetKey = dbClient.metricDao().selectByKey(dbSession, targetKey);
   if (isMetricFoundInDb(metricWithTargetKey)
       && !metricInDb.getId().equals(metricWithTargetKey.getId())) {
     throw new BadRequestException(
         String.format("The key '%s' is already used by an existing metric.", targetKey));
   }
 }
Пример #7
0
 @Override
 public Metric apply(@Nonnull MetricDto dto) {
   Metric<Serializable> metric = new Metric<>();
   metric.setId(dto.getId());
   metric.setKey(dto.getKey());
   metric.setDescription(dto.getDescription());
   metric.setName(dto.getShortName());
   metric.setBestValue(dto.getBestValue());
   metric.setDomain(dto.getDomain());
   metric.setEnabled(dto.isEnabled());
   metric.setDirection(dto.getDirection());
   metric.setHidden(dto.isHidden());
   metric.setQualitative(dto.isQualitative());
   metric.setType(Metric.ValueType.valueOf(dto.getValueType()));
   metric.setOptimizedBestValue(dto.isOptimizedBestValue());
   metric.setUserManaged(dto.isUserManaged());
   metric.setWorstValue(dto.getWorstValue());
   return metric;
 }
  public QualityGateConditionDto updateCondition(
      DbSession dbSession,
      long condId,
      String metricKey,
      String operator,
      @Nullable String warningThreshold,
      @Nullable String errorThreshold,
      @Nullable Integer period) {
    QualityGateConditionDto condition = getNonNullCondition(dbSession, condId);
    MetricDto metric = getNonNullMetric(dbSession, metricKey);
    validateCondition(metric, operator, warningThreshold, errorThreshold, period);
    checkConditionDoesNotAlreadyExistOnSameMetricAndPeriod(
        getConditions(dbSession, condition.getQualityGateId(), condition.getId()), metric, period);

    condition
        .setMetricId(metric.getId())
        .setMetricKey(metric.getKey())
        .setOperator(operator)
        .setWarningThreshold(warningThreshold)
        .setErrorThreshold(errorThreshold)
        .setPeriod(period);
    dbClient.gateConditionDao().update(condition, dbSession);
    return condition;
  }
 @Override
 public boolean apply(@Nonnull MetricDto input) {
   return input.getKey().equals(metricKeyToSort);
 }
 private static boolean isAvailableForInit(MetricDto metric) {
   return !metric.isDataType() && !CoreMetrics.ALERT_STATUS_KEY.equals(metric.getKey());
 }
 private static void validateMetric(MetricDto metric, Errors errors) {
   errors.check(
       isAlertable(metric),
       format("Metric '%s' cannot be used to define a condition.", metric.getKey()));
 }