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 csv file support #63

Merged
merged 2 commits into from
Sep 11, 2022
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
3 changes: 3 additions & 0 deletions etc/result.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,incorrect,manually,accurate
aktie,Aktie,3441,348,5199
derivat,Derivat,593,9028,1008
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<bootstrap5-api.version>5.2.0-3</bootstrap5-api.version>
<prism-api.version>1.28.0-2</prism-api.version>
<everit-json-schema.version>1.14.1</everit-json-schema.version>
<jackson-dataformat.version>2.13.3</jackson-dataformat.version>
<jackson-dataformat.version>2.13.4</jackson-dataformat.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -109,6 +109,11 @@
<artifactId>everit-json-schema</artifactId>
<version>${everit-json-schema.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>${jackson-dataformat.version}</version>
</dependency>

<!-- Workflow dependencies -->
<dependency>
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/io/jenkins/plugins/reporter/model/Result.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.jenkins.plugins.reporter.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

Expand All @@ -22,10 +23,6 @@ public class Result implements Serializable {
private static final long serialVersionUID = 7878818807240640969L;
private static final String DEFAULT_COLOR = "#9E9E9E";

public void setName(String name) {
this.name = name;
}

@JsonProperty(value = "id", required = true)
private String id = String.valueOf(hashCode());

Expand All @@ -36,7 +33,7 @@ public void setName(String name) {
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<Item> items;

@JsonProperty(value = "colors", required = true)
@JsonProperty(value = "colors")
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, String> colors;

Expand All @@ -51,7 +48,11 @@ public void setId(String id) {
public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}

public List<Item> getItems() {
return items;
}
Expand Down
42 changes: 37 additions & 5 deletions src/main/java/io/jenkins/plugins/reporter/model/ResultParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package io.jenkins.plugins.reporter.model;

import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.csv.CsvFactory;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import edu.hm.hafner.echarts.JacksonFacade;
Expand All @@ -13,11 +17,8 @@
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Optional;
import java.io.*;
import java.util.*;

public class ResultParser {

Expand All @@ -42,6 +43,37 @@ public Optional<Result> parseResult(File file) throws IOException {
case "json":
json = FileUtils.readFileToString(file, "UTF-8");
break;
case "csv":
CsvMapper mapper = new CsvMapper();
CsvSchema csvSchema = mapper.typedSchemaFor(Map.class).withHeader();

MappingIterator<Map<String, String>> it = mapper.readerFor(Map.class)
.with(csvSchema.withColumnSeparator(','))
.readValues(file);

Result result = new Result();
result.setId(String.valueOf(file.getName().hashCode()));
result.setName(FilenameUtils.removeExtension(file.getName()));
List<Item> items = new ArrayList<>();

while (it.hasNextValue()) {
Map<String, String> row = it.nextValue();
Item item = new Item();
item.setId(row.get("id"));
item.setName(row.get("name"));
LinkedHashMap<String, Integer> res = row.keySet()
.stream().filter(key -> !key.equals("id") && !key.equals("name"))
.collect(
LinkedHashMap::new,
(map, key) -> map.put(key, Integer.parseInt(row.get(key))),
Map::putAll);
item.setResult(res);
items.add(item);
}

result.setItems(items);
json = jsonWriter.writeValueAsString(result);
break;
default:
return Optional.empty();
}
Expand Down