Skip to content

Commit

Permalink
Merge pull request #113 from sonatype-nexus-community/develop
Browse files Browse the repository at this point in the history
Merge develop to main for release 100
  • Loading branch information
rpanman-sonatype authored Feb 11, 2023
2 parents 498c549 + 4f787ab commit b0e07de
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 72 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@ There are two modes to the application.

The view-metrics script processes metrics stored in the `./nexusiq` directory and presents them as detailed aggregated charts in a web view or in data files in the `./datafiles` directory.

⚠ There must be a `successmetrics.csv` in the `./iqmetrics` directory.
⚠ In order to aggregate and process success metrics a minimum of three data points (weeks or months) are needed.

⚠ In order to aggregate and process metrics a minimum of three data points (weeks or months) are needed.

⚠ Only fully completed months (or weeks) are included in the data extract.
⚠ Only fully completed months (or weeks) are included in the success metrics data extract.

### View-metrics configuration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.stream.Stream;

Expand All @@ -35,8 +36,18 @@ public void writeCsvFile(String filename, List<String[]> data) {

String metricsFile = metricsDir + "/" + filename;

try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(metricsFile))) {
File f = new File(metricsFile);
Boolean first_time_writing_to_file = !(f.exists() && !f.isDirectory());

try (BufferedWriter writer =
Files.newBufferedWriter(
Paths.get(metricsFile),
StandardOpenOption.CREATE,
StandardOpenOption.APPEND)) {
CSVWriter csvWriter = new CSVWriter(writer);
if (!first_time_writing_to_file) {
data.remove(0);
}
csvWriter.writeAll(data);
csvWriter.flush();
csvWriter.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonatype.cs.getmetrics.model.PayloadItem;
import org.sonatype.cs.getmetrics.util.DateCheck;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -61,6 +62,22 @@ public NexusIQSuccessMetrics(
}

public void createSuccessMetricsCsvFile() throws IOException, JSONException, HttpException {
try {
DateCheck.checkDateFormat(iqSmPeriod, iqApiFirstTimePeriod);
} catch (Exception e) {
throw new IllegalArgumentException(
"Please check iq.api.sm.period and iq.api.sm.payload.timeperiod.first", e);
}

try {
if (!iqApiLastTimePeriod.equals("")) {
DateCheck.checkDateFormat(iqSmPeriod, iqApiLastTimePeriod);
}
} catch (Exception e) {
throw new IllegalArgumentException(
"Please check iq.api.sm.period and iq.api.sm.payload.timeperiod.last", e);
}

String apiPayload = getPayload();

String content =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.sonatype.cs.getmetrics.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateCheck {
public static boolean checkDateFormat(String period, String date) {
if (!period.toUpperCase().equals("MONTH") && !period.toUpperCase().equals("WEEK")) {
throw new IllegalArgumentException("The period must be either MONTH or WEEK");
}
if (period.toUpperCase().equals("MONTH")) {
Pattern pattern = Pattern.compile("^\\d{4}-\\d{2}$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(date);
if (matcher.find()) {
return true;
} else {
throw new IllegalArgumentException(
"iq.api.sm.period is set to MONTH but the dates prodived are not in 2022-01"
+ " format. Perhaps there is a W in there for week format?");
}
}
Pattern pattern = Pattern.compile("^\\d{4}-W\\d{2}$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(date);
if (matcher.find()) {
return true;
} else {
throw new IllegalArgumentException(
"iq.api.sm.period is set to WEEK but the dates prodived are not in 2022-W01"
+ " format. Perhaps you've missed out the W?");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ private static String getCVE(JsonArray reasons) {
List<String> cves = new ArrayList<>();

for (JsonObject reason : reasons.getValuesAs(JsonObject.class)) {
JsonObject reference = reason.getJsonObject("reference");
String cve = reference.getString("value");
if (!reason.isNull("reference")) {
JsonObject reference = reason.getJsonObject("reference");
String cve = reference.getString("value");

if (!cves.contains(cve)) {
cves.add(cve);
if (!cves.contains(cve)) {
cves.add(cve);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.sonatype.cs.getmetrics.util;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class DateCheckTest {
@Test
void testValidDates() {
Assertions.assertTrue(DateCheck.checkDateFormat("MONTH", "2022-01"));
Assertions.assertTrue(DateCheck.checkDateFormat("WEEK", "2022-W01"));
Assertions.assertTrue(DateCheck.checkDateFormat("month", "2022-01"));
Assertions.assertTrue(DateCheck.checkDateFormat("week", "2022-w01"));
Assertions.assertTrue(DateCheck.checkDateFormat("week", "2022-W01"));
}

@Test
void testInvalidDates() {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> DateCheck.checkDateFormat("MONTH", "2022-W01"),
"Expected checkDateFormat() to throw, but it didn't");
Assertions.assertThrows(
IllegalArgumentException.class,
() -> DateCheck.checkDateFormat("WEEK", "2022-01"),
"Expected checkDateFormat() to throw, but it didn't");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

@SpringBootApplication
@EnableJpaRepositories
Expand Down Expand Up @@ -78,24 +79,19 @@ public void run(String... args) throws Exception {

successMetricsFileLoaded = loaderService.loadAllMetrics(activeProfile);

if (isSuccessMetricsFileLoaded()) {
if (runMode.contains("SERVLET")) {
// web app
this.startUp();
if (runMode.contains("SERVLET")) {
// web app
this.startUp();
} else {
// non-interactive mode
this.timestamp =
DateTimeFormatter.ofPattern("ddMMyy_HHmm")
.format(LocalDateTime.now(ZoneId.systemDefault()));
if (Objects.equals(activeProfile, "data")) {
createDataFiles();
} else {
// non-interactive mode
this.timestamp =
DateTimeFormatter.ofPattern("ddMMyy_HHmm")
.format(LocalDateTime.now(ZoneId.systemDefault()));
if ("data".equals(activeProfile)) {
createDataFiles();
} else {
log.error("unknown profile");
}
log.error("unknown profile");
}
} else {
log.error("No data files found");
System.exit(-1);
}
}

Expand Down
43 changes: 19 additions & 24 deletions view-metrics/src/main/resources/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,40 @@
<div class="text-center">

<div th:if="${successmetricsreport}">
<a th:href="@{summary.html}">Summary Report <em>(anonymised)</em></a>
<br><br>
<a th:href="@{applications.html}">Applications Report</a>
<br><br>
<a th:href="@{securityviolations.html}">Security Violations Report</a>
<br><br>
<a th:href="@{licenseviolations.html}">License Violations Report</a>
<br><br>
<!--
<a th:href="@{analysis}">Insights Analysis Data</a>
<br><br>
</div>
-->
<a th:href="@{summary.html}">Summary Report <em>(anonymised)</em></a>
<br><br>
<a th:href="@{applications.html}">Applications Report</a>
<br><br>
<a th:href="@{securityviolations.html}">Security Violations Report</a>
<br><br>
<a th:href="@{licenseviolations.html}">License Violations Report</a>
<br><br>
</div>

<div th:if="${firewallreport}">
<a th:href="@{firewall.html}">Firewall Report</a>
<br><br>
</div>

<div th:if="${policyViolationsreport}">
<a th:href="@{violationsage.html}">Policy Violations Report</a>
<br><br>
</div>
<div th:if="${policyViolationsreport}">
<a th:href="@{violationsage.html}">Policy Violations Report</a>
<br><br>
</div>

<div th:if="${applicationEvaluationsreport}">
<a th:href="@{evaluations.html}">Application Evaluations Report</a>
<br><br>
<div th:if="${applicationEvaluationsreport}">
<a th:href="@{evaluations.html}">Application Evaluations Report</a>
<br><br>
</div>

<div th:if="${componentWaiversReport}">
<a th:href="@{waivers.html}">Component Waivers Report</a>
<br><br>
</div>

<div th:if="${smdatabase}">
<a th:href="@{/h2}">Database</a>
</div>
<div th:if="${smdatabase}">
<a th:href="@{/h2}">Database</a>
</div>

</div>
</div>

</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,44 +154,39 @@
<div class="text-center">

<div>
<a href="summary.html">Summary Report <em>(anonymised)</em></a>
<br><br>
<a href="applications.html">Applications Report</a>
<br><br>
<a href="securityviolations.html">Security Violations Report</a>
<br><br>
<a href="licenseviolations.html">License Violations Report</a>
<br><br>
<!--
<a th:href="@{analysis}">Insights Analysis Data</a>
<br><br>
</div>
-->
<a href="summary.html">Summary Report <em>(anonymised)</em></a>
<br><br>
<a href="applications.html">Applications Report</a>
<br><br>
<a href="securityviolations.html">Security Violations Report</a>
<br><br>
<a href="licenseviolations.html">License Violations Report</a>
<br><br>
</div>

<div>
<a href="firewall.html">Firewall Report</a>
<br><br>
</div>

<div>
<a href="violationsage.html">Policy Violations Report</a>
<br><br>
</div>
<div>
<a href="violationsage.html">Policy Violations Report</a>
<br><br>
</div>

<div>
<a href="evaluations.html">Application Evaluations Report</a>
<br><br>
<div>
<a href="evaluations.html">Application Evaluations Report</a>
<br><br>
</div>

<div>
<a href="waivers.html">Component Waivers Report</a>
<br><br>
</div>


</div>
</div>

</body>
</html>
</html>

0 comments on commit b0e07de

Please sign in to comment.