Skip to content

Commit

Permalink
Issue #59: Display the connector variables
Browse files Browse the repository at this point in the history
* Changed the output format to make it more readable and concise.
* Tested all the changes on metricshub-doc.
* Updated the screenshots on the PR description.
  • Loading branch information
CherfaElyes committed Sep 30, 2024
1 parent 4fdb9ce commit c2dad16
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 40 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
<!-- checkstyle -->
<plugin>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
<linkXref>true</linkXref>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public class ConnectorJsonNodeReader {
"^\\s*([^\\{]*)\\{.*(\\s*state\\s*=\\s*.*)\\}\\s*$"
);

/**
* Defines a regular expression pattern for matching connector variables name.
*/
private static final Pattern CONNECTOR_VARIABLE_PATTERN = Pattern.compile("\\$\\{var::(.*?)\\}");

private final JsonNode connector;

/**
Expand Down Expand Up @@ -122,7 +127,7 @@ public String getPlatformsOrDefault(final String defaultValue) {
public List<String> getSupersedes() {
final JsonNode detection = getDetection();
if (nonNull(detection)) {
final JsonNode supersedes = detection.get("supersedes");
final JsonNode supersedes = detection.get("supersedes"); // NOSONAR nonNull() is already called
return nodeToStringList(supersedes);
}
return Collections.emptyList();
Expand All @@ -136,7 +141,7 @@ public List<String> getSupersedes() {
public List<String> getAppliesTo() {
final JsonNode detection = getDetection();
if (nonNull(detection)) {
final JsonNode appliesTo = detection.get("appliesTo");
final JsonNode appliesTo = detection.get("appliesTo"); // NOSONAR nonNull() is already called
return nodeToStringList(appliesTo);
}
return Collections.emptyList();
Expand All @@ -156,7 +161,7 @@ public String getRequiredMetricsHubVersion() {
final JsonNode criteria = getDetectionCriteria();

// If criteria information is not available or is not an array, return null
if (!nonNull(criteria) || !criteria.isArray()) {
if (!nonNull(criteria) || !criteria.isArray()) { // NOSONAR nonNull() is already called
return null;
}

Expand Down Expand Up @@ -196,7 +201,7 @@ private JsonNode getDetectionCriteria() {
return null;
}

return detection.get("criteria");
return detection.get("criteria"); // NOSONAR nonNull() is already called
}

/**
Expand Down Expand Up @@ -308,7 +313,7 @@ public List<String> getSudoCommands() {
public Set<String> getConnectionTypes() {
final JsonNode detection = getDetection();
if (nonNull(detection)) {
final JsonNode connectionTypes = detection.get("connectionTypes");
final JsonNode connectionTypes = detection.get("connectionTypes"); // NOSONAR nonNull() is already called
return nodeToCaseInsensitiveSet(connectionTypes);
}
return Collections.emptySet();
Expand Down Expand Up @@ -339,7 +344,7 @@ private Set<String> nodeToCaseInsensitiveSet(final JsonNode node) {
public boolean isAutoDetectionDisabled() {
final JsonNode detection = getDetection();
if (nonNull(detection)) {
final JsonNode disableAutoDetection = detection.get("disableAutoDetection");
final JsonNode disableAutoDetection = detection.get("disableAutoDetection"); // NOSONAR nonNull() is already called
if (nonNull(disableAutoDetection) && disableAutoDetection.isBoolean()) {
return disableAutoDetection.asBoolean();
}
Expand All @@ -355,7 +360,7 @@ public boolean isAutoDetectionDisabled() {
public String getOnLastResort() {
final JsonNode detection = getDetection();
if (nonNull(detection)) {
final JsonNode onLastResort = detection.get("onLastResort");
final JsonNode onLastResort = detection.get("onLastResort"); // NOSONAR nonNull() is already called
if (nonNull(onLastResort)) {
return onLastResort.asText();
}
Expand All @@ -371,7 +376,7 @@ public String getOnLastResort() {
public List<JsonNode> getCriteria() {
final JsonNode detectionCriteria = getDetectionCriteria();

if (nonNull(detectionCriteria) && detectionCriteria.isArray()) {
if (nonNull(detectionCriteria) && detectionCriteria.isArray()) { // NOSONAR nonNull() is already called
return stream((ArrayNode) detectionCriteria).collect(Collectors.toList());
}

Expand Down Expand Up @@ -572,7 +577,7 @@ public static final String extractMetricName(final String name) {
public boolean hasBladeMonitorJob() {
final JsonNode monitors = getMonitors().orElse(null);
if (nonNull(monitors)) {
final JsonNode bladeMonitorJob = monitors.get("blade");
final JsonNode bladeMonitorJob = monitors.get("blade"); // NOSONAR nonNull() is already called
if (nonNull(bladeMonitorJob)) {
final JsonNode[] bladeJobs = getMonitorJobs(bladeMonitorJob);
for (JsonNode bladeJob : bladeJobs) {
Expand Down Expand Up @@ -603,7 +608,7 @@ private JsonNode getMapping(final JsonNode job) {
public List<String> getTags() {
JsonNode detection = getDetection();
if (nonNull(detection)) {
final JsonNode tagsNode = detection.get("tags");
final JsonNode tagsNode = detection.get("tags"); // NOSONAR nonNull() is already called
return nodeToStringList(tagsNode);
}
return Collections.emptyList();
Expand All @@ -617,11 +622,9 @@ public List<String> getTags() {
*/
public Set<String> getVariablesNames() {
final String stringConnector = connector.toString();
final String variableRegex = "\\$\\{var::(.*?)\\}";
final Set<String> variables = new HashSet<>();

final Pattern pattern = Pattern.compile(variableRegex);
final Matcher matcher = pattern.matcher(stringConnector);
final Matcher matcher = CONNECTOR_VARIABLE_PATTERN.matcher(stringConnector);

while (matcher.find()) {
variables.add(matcher.group(1));
Expand All @@ -641,19 +644,19 @@ public Map<String, ConnectorDefaultVariable> getDefaultVariables() {

final Map<String, ConnectorDefaultVariable> defaultVariables = new HashMap<>();
if (nonNull(variablesNode)) {
variablesNode
variablesNode // NOSONAR nonNull() is already called
.fields()
.forEachRemaining(entry -> {
final String variableName = entry.getKey();
final JsonNode variableValue = entry.getValue();

final String description = variableValue.get("description").asText();
final String defaultValue = variableValue.get("defaultValue").asText();
final JsonNode description = variableValue.get("description");
final JsonNode defaultValue = variableValue.get("defaultValue");

// Create a ConnectorDefaultVariable object and put it into the map
final ConnectorDefaultVariable connectorDefaultVariable = new ConnectorDefaultVariable(
description,
defaultValue
nonNullTextOrDefault(description, null),
nonNullTextOrDefault(defaultValue, null)
);
defaultVariables.put(variableName, connectorDefaultVariable);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,10 @@ public void produce(
for (final String variable : connectorVariables) {
sink.listItem();
sink.rawText(String.format("<code>%s</code>", variable));
sink.list();
sink.listItem();
final ConnectorDefaultVariable defaultVariable = connectorDefaultVariables.getOrDefault(
variable,
new ConnectorDefaultVariable("", "No default value.")
);
sink.text("description: ");
sink.text(defaultVariable.getDescription());
sink.listItem_();

sink.listItem();
sink.text("default value: ");
sink.text(defaultVariable.getDefaultValue());
sink.listItem_();
sink.list_();
final ConnectorDefaultVariable connectorDefaultVariable = connectorDefaultVariables.get(variable);
if (connectorDefaultVariable != null) {
produceVariableSection(sink, connectorDefaultVariables.get(variable));
}
sink.listItem_();
}
sink.list_();
Expand Down Expand Up @@ -519,8 +508,12 @@ private void produceMetricsHubExamplesContent(
// Connector variable
if (connectorVariables != null && !connectorVariables.isEmpty()) {
yamlBuilder.append(" variables:\n");
yamlBuilder.append(String.format(" %s: %s", connectorVariables.iterator().next(), "<VALUE>"));
yamlBuilder.append(" # Replace with desired value.\n");
connectorVariables
.iterator()
.forEachRemaining(variable -> {
yamlBuilder.append(String.format(" %s: %s", variable, "<VALUE>"));
yamlBuilder.append(" # Replace with desired value.\n");
});
}
// CLI
sink.section3();
Expand Down Expand Up @@ -698,4 +691,24 @@ private void produceSudoCommandsContent(final Sink sink, final List<String> sudo
sink.paragraph_();
}
}

/**
* Produces a section of text for the connector variable, including its description and default value,
* and writes it to the provided sink.
*
* @param sink The sink used for generating content.
* @param defaultVariable The variable containing the description and default value to be formatted and output.
*/
private void produceVariableSection(final Sink sink, ConnectorDefaultVariable defaultVariable) {
final String variableDescription = defaultVariable.getDescription();
final String variableDefaultValue = defaultVariable.getDefaultValue();

final String defaultDescriptionString = variableDescription != null
? String.format(": %s", variableDescription)
: "";
final String defaultValueString = variableDefaultValue != null
? String.format("(default: <code>%s</code>)", variableDefaultValue)
: "";
sink.rawText(String.format("%s %s", defaultDescriptionString, defaultValueString));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package org.sentrysoftware.maven.metricshub.connector.producer.model.common;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/*-
* ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
* MetricsHub Connector Maven Plugin
Expand All @@ -25,6 +20,11 @@
* ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
*/

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Data
Expand Down

0 comments on commit c2dad16

Please sign in to comment.