Skip to content

Commit

Permalink
Finish 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zitzewitz committed May 24, 2022
2 parents 5f4c0b3 + b3cddec commit a34f78f
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

<groupId>com.hello2morrow</groupId>
<artifactId>MetricExtractor</artifactId>
<version>1.2</version>
<version>1.3</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<slf4j.version>1.7.36</slf4j.version>
Expand Down Expand Up @@ -68,7 +69,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
<version>${jackson.version}.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand All @@ -80,5 +81,11 @@
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>23.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
120 changes: 108 additions & 12 deletions src/main/java/com/hello2morrow/me/MetricExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.jetbrains.annotations.NotNull;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hello2morrow.sonargraph.integration.access.controller.ControllerFactory;
Expand All @@ -28,14 +35,52 @@ public class MetricExtractor
public static void main(String[] args)
{
MetricExtractor me = new MetricExtractor();
String host = null;
String org = null;
String branch = null;
String fileName = null;
boolean useId = false;

for (String xmlFileName : args)
for (String arg : args)
{
me.process(xmlFileName);
if (arg.startsWith("-host="))
{
host = arg.substring(6);
}
else if (arg.startsWith("-org="))
{
org = arg.substring(5);
}
else if (arg.startsWith("-branch="))
{
branch = arg.substring(8);
}
else if (arg.equals("-useId"))
{
useId = true;
}
else if (fileName == null && !arg.startsWith("-"))
{
fileName = arg;
}
else
{
System.err.println("Invalid parameter: " + arg);
System.exit(1);
}
}
if (fileName != null)
{
me.process(fileName, host, org, branch, useId);
}
else
{
System.err.println("Missing file parameter for Sonargraph XML report");
System.exit(1);
}
}

Map<String, Object> extractMetrics(String xmlFileName)
@NotNull Map<String, Object> extractMetrics(@NotNull String xmlFileName)
{
final ISonargraphSystemController controller = ControllerFactory.createController();
final Result result = controller.loadSystemReport(new File(xmlFileName));
Expand All @@ -44,7 +89,6 @@ Map<String, Object> extractMetrics(String xmlFileName)
{
System.err.println(result.toString());
System.exit(1);
return null;
}

ISystemInfoProcessor proc = controller.createSystemInfoProcessor();
Expand Down Expand Up @@ -99,30 +143,82 @@ Map<String, Object> extractMetrics(String xmlFileName)
return systemMetrics;
}

String process(String xmlFileName)
String process(@NotNull String xmlFileName, String host, String org, String branch, boolean useId)
{
Map<String, Object> systemMetrics = extractMetrics(xmlFileName);
ObjectMapper objectMapper = new ObjectMapper();

try
{
String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(systemMetrics);
String jsonFileName = xmlFileName.substring(0, xmlFileName.length()-3) + "json";

try (BufferedWriter writer = new BufferedWriter(new FileWriter(jsonFileName)))
if (host == null)
{
writer.write(json);
String jsonFileName = xmlFileName.substring(0, xmlFileName.length()-3) + "json";

try (BufferedWriter writer = new BufferedWriter(new FileWriter(jsonFileName)))
{
writer.write(json);
}
catch (IOException e)
{
System.err.println(e.getMessage());
System.exit(1);
}
return jsonFileName;
}
catch (IOException e)
else
{
System.err.println(e.getMessage());
uploadJsonData(json, host, org, branch, useId);
}
return jsonFileName;
}
catch (JsonProcessingException e)
{
e.printStackTrace();
System.err.println(e.getMessage());
System.exit(1);
}
return null;
}

private void uploadJsonData(@NotNull String jsonData, @NotNull String host, String org, String branch, boolean useId)
{
HttpClient client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofMinutes(2))
.build();

HttpRequest.Builder builder = HttpRequest.newBuilder()
.uri(URI.create(host + "/upload/sonargraphJsonReport"))
.header("Content-Type", "application/json")
.timeout(Duration.ofMinutes(2))
.POST(HttpRequest.BodyPublishers.ofString(jsonData));

if (org != null && org.length() > 0)
{
builder.header("X-Organization", org);
}
if (branch != null && branch.length() > 0)
{
builder.header("X-SonargraphBranch", branch);
}
if (useId)
{
builder.header("X-UseSonargraphId", "true");
}
try
{
HttpResponse<String> response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString());

if (response.statusCode() != 200)
{
System.err.println(String.format("Request failed with status %d: %s", response.statusCode(), response.body()));
System.exit(1);
}
}
catch (Exception e)
{
System.err.println("Cannot send request: " + e.getMessage());
System.exit(1);
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/hello2morrow/me/ExtractorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void testMetricExtractor()
public void testMetricExtractorJson()
{
MetricExtractor me = new MetricExtractor();
String jsonFileName = me.process("src/test/data/Sonargraph_2022-04-22_11-54-43.xml");
String jsonFileName = me.process("src/test/data/Sonargraph_2022-04-22_11-54-43.xml", null, null, null, false);

assertNotNull(jsonFileName);
try
Expand Down

0 comments on commit a34f78f

Please sign in to comment.