private static String generateSubject(String fullProjectName, String alertLevel, boolean isNewAlert) { StringBuilder subjectBuilder = new StringBuilder(); if (Metric.Level.OK.toString().equals(alertLevel)) { subjectBuilder.append("\"").append(fullProjectName).append("\" is back to green"); } else if (isNewAlert) { subjectBuilder.append("New quality gate threshold reached on \"").append(fullProjectName).append("\""); } else { subjectBuilder.append("Quality gate status changed on \"").append(fullProjectName).append("\""); } return subjectBuilder.toString(); }
private static void writeQualityGate(JsonWriter writer, EvaluatedQualityGate gate) { writer .name("qualityGate") .beginObject() .prop("name", gate.getQualityGate().getName()) .prop(PROPERTY_STATUS, gate.getStatus().toString()) .name("conditions") .beginArray(); for (EvaluatedCondition evaluatedCondition : gate.getEvaluatedConditions()) { Condition condition = evaluatedCondition.getCondition(); writer .beginObject() .prop("metric", condition.getMetricKey()) .prop("operator", condition.getOperator().name()); evaluatedCondition.getValue().ifPresent(t -> writer.prop("value", t)); writer .prop(PROPERTY_STATUS, evaluatedCondition.getStatus().name()) .prop("errorThreshold", condition.getErrorThreshold()) .endObject(); } writer .endArray() .endObject(); }
private static String generateSubject(String fullProjectName, String alertLevel, boolean isNewAlert) { StringBuilder subjectBuilder = new StringBuilder(); if (Metric.Level.OK.toString().equals(alertLevel)) { subjectBuilder.append("\"").append(fullProjectName).append("\" is back to green"); } else if (isNewAlert) { subjectBuilder.append("New quality gate threshold reached on \"").append(fullProjectName).append("\""); } else { subjectBuilder.append("Quality gate status changed on \"").append(fullProjectName).append("\""); } return subjectBuilder.toString(); }
private static void writeQualityGate(JsonWriter writer, EvaluatedQualityGate gate) { writer .name("qualityGate") .beginObject() .prop("name", gate.getQualityGate().getName()) .prop(PROPERTY_STATUS, gate.getStatus().toString()) .name("conditions") .beginArray(); for (EvaluatedCondition evaluatedCondition : gate.getEvaluatedConditions()) { Condition condition = evaluatedCondition.getCondition(); writer .beginObject() .prop("metric", condition.getMetricKey()) .prop("operator", condition.getOperator().name()); evaluatedCondition.getValue().ifPresent(t -> writer.prop("value", t)); writer .prop(PROPERTY_STATUS, evaluatedCondition.getStatus().name()) .prop("onLeakPeriod", condition.isOnLeakPeriod()) .prop("errorThreshold", condition.getErrorThreshold().orElse(null)) .prop("warningThreshold", condition.getWarningThreshold().orElse(null)) .endObject(); } writer .endArray() .endObject(); }
JsonObject toJson() { JsonObject result = new JsonObject(); result.addProperty("metric", condition.metricKey()); result.addProperty("op", condition.operator()); if (condition.period() != null) { result.addProperty("period", condition.period()); } if (condition.warningThreshold() != null) { result.addProperty("warning", condition.warningThreshold()); } if (condition.errorThreshold() != null) { result.addProperty("error", condition.errorThreshold()); } result.addProperty("actual", actualValue); result.addProperty(FIELD_LEVEL, level.toString()); return result; } }
/** * Sets an alert level as the data field * * @param level the alert level * @return the measure object instance */ public Measure setData(Metric.Level level) { if (level == null) { this.data = null; } else { this.data = level.toString(); } return this; }
/** * Creates a measure with a metric and an alert level * * @param metric the metric * @param level the alert level */ public Measure(Metric metric, Metric.Level level) { this.metric = metric; this.metricKey = metric.getKey(); if (level != null) { this.data = level.toString(); } }
/** * Sets the measure alert status * * @return the current object */ public MeasureModel setAlertStatus(Metric.Level level) { if (level != null) { this.alertStatus = level.toString(); } else { this.alertStatus = null; } return this; }
String toJson() { JsonObject details = new JsonObject(); details.addProperty(FIELD_LEVEL, level.toString()); JsonArray conditionResults = new JsonArray(); for (EvaluatedCondition condition: this.conditions) { conditionResults.add(condition.toJson()); } details.add("conditions", conditionResults); return details.toString(); }
/** * Creates a measure based on a metric and an alert level */ public MeasureModel(int metricId, Metric.Level level) { this.metricId = metricId; if (level != null) { this.textValue = level.toString(); } }
protected void notifyUsers(Resource resource, String alertName, String alertText, Level alertLevel, boolean isNewAlert) { Notification notification = new Notification("alerts") .setDefaultMessage("Alert on " + resource.getLongName() + ": " + alertName) .setFieldValue("projectName", resource.getLongName()) .setFieldValue("projectKey", resource.getKey()) .setFieldValue("projectId", String.valueOf(resource.getId())) .setFieldValue("alertName", alertName) .setFieldValue("alertText", alertText) .setFieldValue("alertLevel", alertLevel.toString()) .setFieldValue("isNewAlert", Boolean.toString(isNewAlert)); notificationManager.scheduleForSending(notification); }
private static String getValueToEval(ResolvedCondition condition, Metric.Level alertLevel) { if (alertLevel.equals(Metric.Level.ERROR)) { return condition.errorThreshold(); } else if (alertLevel.equals(Metric.Level.WARN)) { return condition.warningThreshold(); } else { throw new IllegalStateException(alertLevel.toString()); } }