Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logs and statistics #144

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
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("=====================================");
}
}
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
Loading