Skip to content

Commit

Permalink
Add the sort option for lines between first and last lines. Remove th…
Browse files Browse the repository at this point in the history
…e System.exit() because the maven plugin exec-maven-plugin will stop if present. Change version 1.1
  • Loading branch information
DABURON Vincent committed Oct 17, 2022
1 parent cae3e5a commit 8afd3b5
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 36 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ Odd lines are gray, even lines are white

The input csv file comes from a "Save Table Data"" of a **JMeter Report** (Synthesis Report, Aggregate Report, Summary Report) or from jmeter-graph-tool-maven-plugin

It is preferable to sort the lines (except the header) before generating the table.
If you want to sort the array then add a third argument : sort

The array will be sorted except the first line (headers) and the last line (footer TOTAL). The sorting is done on the Label (First column).

The generated HTML table can be directly included in an HTML page with the GenereHtmlForDirectory tool.

Expand All @@ -33,7 +35,7 @@ The maven groupId, artifactId and version, this plugin is in the **Maven Central
```xml
<groupId>io.github.vdaburon</groupId>
<artifactId>csv-report-to-html</artifactId>
<version>1.0</version>
<version>1.1</version>
```
Just include the plugin in your `pom.xml` and execute `mvn verify`.

Expand All @@ -44,7 +46,7 @@ Just include the plugin in your `pom.xml` and execute `mvn verify`.
<dependency>
<groupId>io.github.vdaburon</groupId>
<artifactId>csv-report-to-html</artifactId>
<version>1.0</version>
<version>1.1</version>
</dependency>
</dependencies>

Expand All @@ -66,6 +68,7 @@ Just include the plugin in your `pom.xml` and execute `mvn verify`.
<arguments>
<argument>${project.build.directory}/jmeter/results/AggregateReport.csv</argument>
<argument>${project.build.directory}/jmeter/results/AggregateReport.html</argument>
<argument>sort</argument>
</arguments>
</configuration>
</execution>
Expand All @@ -80,6 +83,7 @@ Just include the plugin in your `pom.xml` and execute `mvn verify`.
<arguments>
<argument>${project.build.directory}/jmeter/results/SynthesisReport.csv</argument>
<argument>${project.build.directory}/jmeter/results/SynthesisReport.html</argument>
<argument>sort</argument>
</arguments>
</configuration>
</execution>
Expand All @@ -96,8 +100,10 @@ This tool is a java jar, so it's could be use as simple jar (look at [Release](h
Need also commons-csv.jar librarie

https://commons.apache.org/proper/commons-csv/download_csv.cgi

The third parameter "sort" is optional. The sorting is done on the Label (First column)
<pre>
java -cp csv-report-to-html-&lt;version&gt;.jar;commons-csv-&lt;version&gt;.jar io.github.vdaburon.jmeter.utils.ReportCsv2Html AggregateReport.csv AggregateReport.html
java -cp csv-report-to-html-&lt;version&gt;.jar;commons-csv-&lt;version&gt;.jar io.github.vdaburon.jmeter.utils.ReportCsv2Html AggregateReport.csv AggregateReport.html sort
</pre>

## Link to others projects
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.vdaburon</groupId>
<artifactId>csv-report-to-html</artifactId>
<version>1.0</version>
<version>1.1</version>
<packaging>jar</packaging>
<name>Reads a csv file and generates an html div block as an html table</name>
<description>Reads a csv file and generates an html div block as an html table with an embedded stylesheet</description>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/github/vdaburon/jmeter/utils/CompareRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.vdaburon.jmeter.utils;

import org.apache.commons.csv.CSVRecord;
import java.util.Comparator;

public class CompareRecord implements Comparator<CSVRecord> {
public int compare(CSVRecord a, CSVRecord b) {
// sort by the first column : Label
return a.get(0).compareTo(b.get(0));
}
}
100 changes: 69 additions & 31 deletions src/main/java/io/github/vdaburon/jmeter/utils/ReportCsv2Html.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
Expand All @@ -18,34 +22,67 @@

public class ReportCsv2Html {
public static void main(String[] args) {
int statusReturn = 1; // KO

if (args.length != 2) {
boolean bSort = false;
if (args.length != 2 && args.length != 3) {
usage();
System.exit(statusReturn);
System.exit(1);
}
String fileIn = args[0];
String fileOut = args[1];

if (args.length == 3) {
if ("sort".equalsIgnoreCase(args[2])) {
bSort = true;
}
}
try {
creatHtmlFromCsv(fileIn, fileOut);
statusReturn = 0; // OK
creatHtmlFromCsv(fileIn, fileOut, bSort);
}
catch (Exception ex) {
ex.printStackTrace();
}
System.exit(statusReturn);
}

private static void usage() {
System.err.println("java -jar ReportCsv2Html.jar fileCsvIn fileHtmlOut");
System.err.println("java -jar csv-report-to-html-<version>.jar fileCsvIn fileHtmlOut [sort]");
}

private static void creatHtmlFromCsv(String fileIn, String fileout) throws IOException {
private static void creatHtmlFromCsv(String fileIn, String fileout, boolean bSort) throws IOException {
Reader in = new FileReader(fileIn);
Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(in);

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileout)));

// read all
Iterator<CSVRecord> iterator = records.iterator();

List<CSVRecord> listRecordsBetweenFirstAndLast = new ArrayList();
CSVRecord firstRecord = null;
CSVRecord lastRecord = null;

int i = 0;
while(iterator.hasNext()) {
i++;
CSVRecord record = iterator.next();
if (i == 1) {
// save the Header line
firstRecord = record;
}
else {
listRecordsBetweenFirstAndLast.add(record);
lastRecord = record;
}
}
if (listRecordsBetweenFirstAndLast.size() >= 1) {
// remove the footer line (Total)
listRecordsBetweenFirstAndLast.remove(listRecordsBetweenFirstAndLast.size() -1);
}
if (bSort) {
// and sort records (expect first line and last line)
Collections.sort(listRecordsBetweenFirstAndLast, new CompareRecord());
}

in.close();

out.println("<div>");
out.println("<style>");
out.println("table.table_jp {border-collapse: collapse;}");
Expand All @@ -60,33 +97,34 @@ private static void creatHtmlFromCsv(String fileIn, String fileout) throws IOExc
out.println("table.table_jp th:nth-child(-n+1) { text-align: left; }");
out.println("</style>");
out.println("<table class=\"table_jp\">");

int i = 0;
for (CSVRecord record : records) {
i++;

// the header
int nbFirstColums = firstRecord.size();
out.println("<tr>");
for (int j = 0; j < nbFirstColums; j++) {
out.println("<th>" + textToHtml(firstRecord.get(j)) + "</th>");
}
out.println("</tr>");

for (CSVRecord record : listRecordsBetweenFirstAndLast) {
int nbColums = record.size();
if (i == 1) {
// headers
out.println("<tr>");
for (int j = 0; j < nbColums; j++) {
out.println("<th>" + textToHtml(record.get(j)) + "</th>");
}
out.println("</tr>");
}
else {
out.println("<tr>");
for (int j = 0; j < nbColums; j++) {
out.println("<td>" + textToHtml(record.get(j)) + "</td>");
}
out.println("</tr>");
out.println("<tr>");
for (int j = 0; j < nbColums; j++) {
out.println("<td>" + textToHtml(record.get(j)) + "</td>");
}
out.println("</tr>");
}


// the footer
int nbLastColums = lastRecord.size();
out.println("<tr>");
for (int j = 0; j < nbLastColums; j++) {
out.println("<td>" + textToHtml(lastRecord.get(j)) + "</td>");
}
out.println("</tr>");
out.println("</table>");
out.println("</div>");
out.close();

in.close();
}

/** Converts a character string into strings readable by a web browser
Expand Down

0 comments on commit 8afd3b5

Please sign in to comment.