Skip to content

Commit

Permalink
add logs and statistics (#144)
Browse files Browse the repository at this point in the history
Signed-off-by: Nicklas Körtge <[email protected]>
  • Loading branch information
n1ckl0sk0rtge authored Sep 16, 2024
1 parent 8cfb4bd commit 8cab570
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 7 deletions.
1 change: 1 addition & 0 deletions java/src/main/java/com/ibm/plugin/JavaAggregator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ private JavaAggregator() {

public static void addNodes(@Nonnull List<INode> newNodes) {
detectedNodes.addAll(newNodes);
IAggregator.log(newNodes);
}

@Nonnull
Expand Down
19 changes: 18 additions & 1 deletion output/src/main/java/com/ibm/output/IAggregator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,21 @@
*/
package com.ibm.output;

public interface IAggregator {}
import com.ibm.mapper.model.INode;
import java.util.List;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public interface IAggregator {
Logger LOGGER = LoggerFactory.getLogger(IAggregator.class);

static void log(@Nonnull List<INode> nodes) {
nodes.forEach(
node ->
LOGGER.info(
"Detected ({}) {}",
node.getKind().getSimpleName(),
node.asString()));
}
}
28 changes: 28 additions & 0 deletions output/src/main/java/com/ibm/output/statistics/IStatistics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* SonarQube Cryptography Plugin
* Copyright (C) 2024 IBM
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibm.output.statistics;

import java.util.function.Consumer;
import javax.annotation.Nonnull;

public interface IStatistics {

void print(@Nonnull Consumer<String> out);
}
51 changes: 51 additions & 0 deletions output/src/main/java/com/ibm/output/statistics/ScanStatistics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* SonarQube Cryptography Plugin
* Copyright (C) 2024 IBM
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibm.output.statistics;

import com.ibm.mapper.model.INode;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
import javax.annotation.Nonnull;

public final class ScanStatistics implements IStatistics {
private final int numberOfDetectedAssets;
@Nonnull private final Map<Class<? extends INode>, Long> numberOfAssetsPerType;

public ScanStatistics(
@Nonnull IntSupplier numberOfDetectedAssetsSupplier,
@Nonnull Supplier<Map<Class<? extends INode>, Long>> numberOfAssetsPerTypeSupplier) {
this.numberOfDetectedAssets = numberOfDetectedAssetsSupplier.getAsInt();
this.numberOfAssetsPerType = numberOfAssetsPerTypeSupplier.get();
}

@Override
public void print(@Nonnull Consumer<String> out) {
out.accept("========== CBOM Statistics ==========");
out.accept(String.format("%-33s: %s", "Detected Assets", numberOfDetectedAssets));
for (Map.Entry<Class<? extends INode>, Long> entry : numberOfAssetsPerType.entrySet()) {
out.accept(
String.format(
" - %-30s: %s", entry.getKey().getSimpleName(), entry.getValue()));
}
out.accept("=====================================");
}
}
1 change: 1 addition & 0 deletions python/src/main/java/com/ibm/plugin/PythonAggregator.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static List<INode> getDetectedNodes() {

public static void addNodes(@Nonnull List<INode> newNodes) {
detectedNodes.addAll(newNodes);
IAggregator.log(newNodes);
}

public static void reset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.sonar.api.batch.postjob.PostJobDescriptor;

public class OutputFileJob implements PostJob {

private static final Logger LOGGER = LoggerFactory.getLogger(OutputFileJob.class);

@Override
Expand All @@ -45,7 +44,10 @@ public void execute(@Nonnull PostJobContext postJobContext) {
.get(Constants.CBOM_OUTPUT_NAME)
.orElse(Constants.CBOM_OUTPUT_NAME_DEFAULT);
ScannerManager scannerManager = new ScannerManager(new CBOMOutputFileFactory());
scannerManager.getOutputFile().saveTo(new File(cbomFilename + ".json"));
LOGGER.info("CBOM was successfully generated '" + cbomFilename + ".json'.");
final File cbom = new File(cbomFilename + ".json");
scannerManager.getOutputFile().saveTo(cbom);
LOGGER.info("CBOM was successfully generated '{}'.", cbom.getAbsolutePath());

scannerManager.getStatistics().print(LOGGER::info);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import com.ibm.mapper.model.INode;
import com.ibm.output.IOutputFile;
import com.ibm.output.IOutputFileFactory;
import com.ibm.output.statistics.IStatistics;
import com.ibm.output.statistics.ScanStatistics;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand All @@ -37,12 +40,28 @@ public ScannerManager(@Nullable IOutputFileFactory outputFileFactory) {

@Nonnull
public IOutputFile getOutputFile() {
return Optional.ofNullable(this.outputFileFactory)
.orElse(IOutputFileFactory.DEFAULT)
.createOutputFormat(getAggregatedNodes());
}

@Nonnull
public IStatistics getStatistics() {
return new ScanStatistics(
() -> getAggregatedNodes().size(), // numberOfDetectedAssetsSupplier
() ->
getAggregatedNodes().stream() // numberOfAssetsPerTypeSupplier
.collect(
Collectors.groupingBy(
INode::getKind, Collectors.counting())));
}

@Nonnull
private List<INode> getAggregatedNodes() {
List<INode> nodes = new ArrayList<>();
nodes.addAll(JavaAggregator.getDetectedNodes());
nodes.addAll(PythonAggregator.getDetectedNodes());
return Optional.ofNullable(this.outputFileFactory)
.orElse(IOutputFileFactory.DEFAULT)
.createOutputFormat(nodes);
return nodes;
}

public void reset() {
Expand Down

0 comments on commit 8cab570

Please sign in to comment.