Skip to content

Commit

Permalink
Merge pull request #49 from sentrysoftware/feature/issue-47-add-a-new…
Browse files Browse the repository at this point in the history
…-column-to-indicate-if-a-connector-is-enterprise-or-not

Issue #47: Add a new column to indicate if a connector is enterprise
  • Loading branch information
NassimBtk authored Jul 8, 2024
2 parents 1c212a5 + abb8d03 commit f5f95c7
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/it/metricshub-connectors/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<dependency>
<groupId>org.sentrysoftware.maven</groupId>
<artifactId>maven-skin-tools</artifactId>
<version>1.2.00</version>
<version>1.3.00</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@
import com.fasterxml.jackson.databind.JsonNode;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.Getter;
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Parameter;
Expand All @@ -47,6 +56,8 @@ public abstract class AbstractConnectorReport extends AbstractMavenReport {

protected Map<String, JsonNode> connectors;

protected List<String> enterpriseConnectorIds = new ArrayList<>();

@Override
protected void executeReport(Locale locale) throws MavenReportException {
// Get and set the logger
Expand Down Expand Up @@ -75,10 +86,61 @@ protected void executeReport(Locale locale) throws MavenReportException {
// Parse the connector library
connectors = parseConnectors();

// Retrieve the enterprise connector identifiers from the manifest file.
try {
enterpriseConnectorIds = detectEnterpriseConnectors();
} catch (IOException e) {
final String message = "Could not read the eneterprise connectors manifest: enterprise-connectors-manifest.txt";
logger.error(message);
throw new MavenReportException(message);
}

// Produce the report
doReport();
}

/**
* Detect the enterprise connector identifiers.
*
* @return List of string values containing the connector identifiers.
* @throws IOException If any I/O error occurs.
*/
private List<String> detectEnterpriseConnectors() throws IOException {
final EnterpriseManifestVisitor fileVisitor = new EnterpriseManifestVisitor();

Files.walkFileTree(sourceDirectory.toPath(), fileVisitor);

return fileVisitor.getConnectorEnterpriseList();
}

/**
* This inner class allows to visit the files in order to extract the enterprise manifest file content
*/
private static class EnterpriseManifestVisitor extends SimpleFileVisitor<Path> {

@Getter
private List<String> connectorEnterpriseList = new ArrayList<>();

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// Skip this path if it is a directory or not a YAML file
if (!Files.isDirectory(file) && "enterprise-connectors-manifest.txt".equals(file.toFile().getName())) {
connectorEnterpriseList =
Files
.readAllLines(file)
.stream()
.map(String::trim)
.filter(line -> !line.isEmpty())
.map(filename -> filename.substring(0, filename.lastIndexOf('.')))
.collect(Collectors.toCollection(ArrayList::new));

return FileVisitResult.TERMINATE;
}

return FileVisitResult.CONTINUE;
}
}

/**
* Performs the main logic of generating the report. Subclasses should implement this method to define
* the specific report generation logic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,19 @@ protected void doReport() throws MavenReportException {
}

// Main page
produceMainPage(connectors);
produceMainPage();

// Connector pages
produceConnectorPages(connectors, connectorSubdirectory, buildSupersededMap(connectors));
produceConnectorPages(connectorSubdirectory, buildSupersededMap());
}

/**
* Builds a map representing the superseded relationships between connectors.
*
* @param connectors A map of connector IDs to JsonNode objects representing connectors.
* @return A map where each key is a connector ID that is superseded by one or more connectors,
* and the associated value is a list of connectors that supersede it.
*/
private Map<String, List<String>> buildSupersededMap(final Map<String, JsonNode> connectors) {
private Map<String, List<String>> buildSupersededMap() {
final Map<String, List<String>> supersededMap = new HashMap<>();

connectors.forEach((connectorId, connector) ->
Expand All @@ -115,16 +114,12 @@ private Map<String, List<String>> buildSupersededMap(final Map<String, JsonNode>
/**
* Produces individual connector pages for the Maven report
*
* @param connectors A map of connector IDs to their corresponding JSON nodes.
* @param connectorSubdirectory The subdirectory where individual connector pages are located.
* @param supersededMap A map representing the superseded relationships among connectors.
* @throws MavenReportException If an error occurs while producing the connector pages.
*/
private void produceConnectorPages(
final Map<String, JsonNode> connectors,
final File connectorSubdirectory,
final Map<String, List<String>> supersededMap
) throws MavenReportException {
private void produceConnectorPages(final File connectorSubdirectory, final Map<String, List<String>> supersededMap)
throws MavenReportException {
for (Entry<String, JsonNode> connectorEntry : connectors.entrySet()) {
final String connectorId = connectorEntry.getKey();
// Create a new sink!
Expand All @@ -143,19 +138,18 @@ private void produceConnectorPages(
.withConnector(connectorEntry.getValue())
.withLogger(logger)
.build()
.produce(sink, supersededMap);
.produce(sink, supersededMap, enterpriseConnectorIds);
}
}

/**
* Produces the main page for the Maven report
*
* @param connectors A map of connector IDs to their corresponding JSON nodes.
*/
private void produceMainPage(final Map<String, JsonNode> connectors) throws MavenReportException {
private void produceMainPage() throws MavenReportException {
final Sink mainSink = getMainSink();

new MainPageReferenceProducer(CONNECTOR_SUBDIRECTORY_NAME, logger).produce(mainSink, connectors);
new MainPageReferenceProducer(CONNECTOR_SUBDIRECTORY_NAME, logger)
.produce(mainSink, connectors, enterpriseConnectorIds);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ public class ConnectorPageReferenceProducer {
/**
* Produces a page reference for the current connector and generates the corresponding sink for documentation output.
*
* @param sink The sink used for generating content.
* @param supersededMap Map of superseded connectors.
* This method generates a table with connector information, adding a new column to indicate if a connector is an enterprise connector.
*
* @param sink The sink used for generating content.
* @param supersededMap Map of superseded connectors.
* @param enterpriseConnectorIds List of IDs for enterprise connectors.
*/
public void produce(final Sink sink, final Map<String, List<String>> supersededMap) {
public void produce(final Sink sink, final Map<String, List<String>> supersededMap, final List<String> enterpriseConnectorIds) {
Objects.requireNonNull(connectorId, () -> "connectorId cannot be null.");
Objects.requireNonNull(connector, () -> "connector cannot be null.");
Objects.requireNonNull(supersededMap, () -> "supersededMap cannot be null.");
Expand Down Expand Up @@ -95,6 +98,10 @@ public void produce(final Sink sink, final Map<String, List<String>> supersededM
sink.section1();
sink.sectionTitle1();
sink.text(displayName);
// If the connector is Enterprise
if (enterpriseConnectorIds.contains(connectorId)) {
sink.rawText(SinkHelper.bootstrapLabel("Enterprise", "metricshub-enterprise-label"));
}
sink.sectionTitle1_();

// Description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@

import com.fasterxml.jackson.databind.JsonNode;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import lombok.AllArgsConstructor;
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.doxia.sink.SinkEventAttributes;
import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
import org.apache.maven.plugin.logging.Log;
import org.sentrysoftware.maven.metricshub.connector.ReferenceReport;
import org.sentrysoftware.maven.metricshub.connector.producer.model.common.OsType;
Expand All @@ -45,10 +48,15 @@ public class MainPageReferenceProducer {
/**
* Produces the main page reference that lists all the connectors.
*
* @param mainSink The main sink used for generating content.
* @param connectors The map of connector identifiers to their corresponding JsonNodes.
* @param mainSink The main sink used for generating content.
* @param connectors The map of connector identifiers to their corresponding JsonNodes.
* @param enterpriseConnectorIds The enterprise connector identifiers.
*/
public void produce(final Sink mainSink, final Map<String, JsonNode> connectors) {
public void produce(
final Sink mainSink,
final Map<String, JsonNode> connectors,
final List<String> enterpriseConnectorIds
) {
Objects.requireNonNull(connectorSubdirectoryName, () -> "connectorSubdirectoryName cannot be null.");
Objects.requireNonNull(mainSink, () -> "mainSink cannot be null.");
Objects.requireNonNull(logger, () -> "logger cannot be null.");
Expand Down Expand Up @@ -94,6 +102,9 @@ public void produce(final Sink mainSink, final Map<String, JsonNode> connectors)
mainSink.tableHeaderCell(SinkHelper.setClass(BOOTSTRAP_MEDIUM_3_CLASS));
mainSink.text("Operating Systems");
mainSink.tableHeaderCell_();
mainSink.tableHeaderCell(SinkHelper.setClass(BOOTSTRAP_MEDIUM_3_CLASS));
mainSink.text("Enterprise");
mainSink.tableHeaderCell_();
mainSink.tableRow_();

// A comparison function which compare connectors by display name
Expand Down Expand Up @@ -143,6 +154,11 @@ public void produce(final Sink mainSink, final Map<String, JsonNode> connectors)
mainSink.text(String.join(", ", OsType.mapToDisplayNames(connectorJsonNodeReader.getAppliesTo())));
mainSink.tableCell_();

SinkEventAttributes attributes = new SinkEventAttributeSet(SinkEventAttributes.ALIGN, "center");
mainSink.tableCell(attributes);
mainSink.text(enterpriseConnectorIds.contains(connectorId) ? "\u2713" : "");
mainSink.tableCell_();

mainSink.tableRow_();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.regex.Pattern;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.doxia.sink.SinkEventAttributes;
import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
Expand Down Expand Up @@ -120,6 +121,21 @@ public static String glyphIcon(final String iconName) {
return String.format("<i class=\"glyphicon glyphicon-%s\" aria-hidden=\"true\"></i>", glyphIconName);
}

/**
* Create a bootstrap badge with the following content.
*
* @param content text of the badge.
* @return the HTML code for this badge.
*/
public static String bootstrapLabel(@NonNull final String content, String customClassname) {
if (customClassname == null) {
customClassname = "";
} else {
customClassname = customClassname.trim() + " ";
}
return String.format(" <span class=\"%slabel label-default\">%s</span>", customClassname, content);
}

/**
* Builds the HTML page file name corresponding to the specified connector identifier.
*
Expand Down

0 comments on commit f5f95c7

Please sign in to comment.