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

implement multilevel csv provider #75

Merged
merged 2 commits into from
Sep 16, 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
1 change: 1 addition & 0 deletions etc/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pipeline {
publishReport name: "JSON Report", displayType: "dual", provider: json(pattern: "etc/report-1-part-*.json")
publishReport name: "XML Report", displayType: "dual", provider: xml(pattern: "etc/*.xml")
publishReport name: "YAML Report", displayType: "dual", provider: yaml(pattern: "etc/*.yaml")
publishReport name: "CSV Report", displayType: "dual", provider: csv(id: "csv-one", pattern: "etc/*.csv")
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions etc/report.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
id,name,incorrect,manually,accurate
aktie,Aktie,3441,348,5199
derivat,Derivat,593,9028,1008
Level 1,Level 2,Level 3,incorrect,manually,accurate
Aktie,Aktie 1,Aktie 1 1,13,3,59
Aktie,Aktie 1,Aktie 1 2,31,48,9
Aktie,Aktie 1,Aktie 1 3,34,8,51
Derivat,Derivat 1,,1,2,3
Derivat,Derivat 2,,5,9,1
Not Found,,,1,3,9
44 changes: 35 additions & 9 deletions src/main/java/io/jenkins/plugins/reporter/model/Provider.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import hudson.model.Descriptor;
import hudson.model.Run;
import hudson.util.FormValidation;
import io.jenkins.plugins.reporter.Messages;
import io.jenkins.plugins.reporter.provider.Csv;
import io.jenkins.plugins.reporter.util.FilesScanner;
import io.jenkins.plugins.reporter.util.LogHandler;
import io.jenkins.plugins.util.JenkinsFacade;
Expand All @@ -21,10 +23,10 @@
public abstract class Provider extends AbstractDescribableImpl<Provider> implements Serializable {

private static final long serialVersionUID = -1356603376948787474L;

private String name = StringUtils.EMPTY;

private String pattern = StringUtils.EMPTY;

private String id = StringUtils.EMPTY;

private JenkinsFacade jenkins = new JenkinsFacade();

Expand All @@ -39,16 +41,31 @@ protected Object readResolve() {
return this;
}

/**
* Sets the id of this provider.
*
* @param id
* the id
*/
@DataBoundSetter
public void setName(final String name) {
this.name = name;
public void setId(String id) {
this.id = id;
}

public String getId() {
return id;
}

public String getName() {
return name;
/**
* Returns the actual ID of the tool. If no user defined ID is given, then the default ID is returned.
*
* @return the ID
* @see #setId(String)
*/
public String getActualId() {
return StringUtils.defaultIfBlank(getId(), getDescriptor().getId());
}


/**
* Sets the Ant file-set pattern of files to work with. If the pattern is undefined then the console log is
* scanned.
Expand Down Expand Up @@ -109,11 +126,11 @@ public abstract static class ProviderDescriptor extends Descriptor<Provider> {
* Creates a new instance of {@link ProviderDescriptor} with the given ID.
*
* @param defaultId
* the unique ID of the tool
* the unique ID of the provider
*/
protected ProviderDescriptor(final String defaultId) {
super();

this.defaultId = defaultId;
}

Expand Down Expand Up @@ -156,5 +173,14 @@ public FormValidation doCheckPattern(@QueryParameter("pattern") String pattern)

return FormValidation.ok();
}

@POST
public FormValidation doCheckId(@QueryParameter("id") String id) {
if (getSymbolName().equals("csv") && StringUtils.isEmpty(id)) {
return FormValidation.error(Messages.Provider_Error());
}

return FormValidation.ok();
}
}
}
50 changes: 6 additions & 44 deletions src/main/java/io/jenkins/plugins/reporter/model/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Report implements Serializable {
public class Report extends ReportBase implements Serializable {

private static final long serialVersionUID = 302445084497230108L;

Expand Down Expand Up @@ -166,56 +166,18 @@ public LinkedHashMap<String, Integer> aggregate(List<Item> items) {
.collect(Collectors.groupingBy(Map.Entry::getKey, LinkedHashMap::new, Collectors.summingInt(Map.Entry::getValue)));
}

public List<String> getColorIds() {
if (aggregate().size() == 1) {
return flattItems(getItems()).stream().map(Item::getId).collect(Collectors.toList());
}

return new ArrayList<>(aggregate().keySet());
}

private List<Item> flattItems(List<Item> items)
{
List<Item> flatten = new ArrayList<>();

for (Item i: items) {
if (i.hasItems()) {
flatten.addAll(flattItems(i.getItems()));
}

flatten.add(i);
}

return flatten;
}

public Optional<Item> findItem(String id) {
return findItem(id, items);
}

public Optional<Item> findItem(String id, List<Item> items) {
if (items != null) {
for (Item i: items) {
if (i.getId().equals(id)) {
return Optional.of(i);
} else {
Optional<Item> sub = findItem(id, i.getItems());
if (sub.isPresent()) {
return sub;
}
}
}

public List<String> getColorIds() {
if (aggregate().size() == 1) {
return flattItems(getItems()).stream().map(Item::getId).collect(Collectors.toList());
}

return Optional.empty();
return new ArrayList<>(aggregate().keySet());
}


/**
* Aggregates the results of all items. The values are added together, grouped by key.
*
* @return the aggregated result.
*/

public LinkedHashMap<String, Integer> aggregate() {
return aggregate(getItems());
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/io/jenkins/plugins/reporter/model/ReportBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.jenkins.plugins.reporter.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public abstract class ReportBase {

/**
* Find the item with id in given list of items.
*
* @param id
* the id to find.
* @param items
* the items to search in.
* @return
* the {@link Item} as {@link Optional}.
*/
public Optional<Item> findItem(String id, List<Item> items) {
if (items != null) {
for (Item i: items) {
if (i.getId().equals(id)) {
return Optional.of(i);
} else {
Optional<Item> sub = findItem(id, i.getItems());
if (sub.isPresent()) {
return sub;
}
}
}
}

return Optional.empty();
}

/**
* Flatten all items of report.
*
* @param items
* the items to flatten.
*
* @return
* list with all items.
*/
List<Item> flattItems(List<Item> items)
{
List<Item> flatten = new ArrayList<>();

for (Item i: items) {
if (i.hasItems()) {
flatten.addAll(flattItems(i.getItems()));
}

flatten.add(i);
}

return flatten;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class ReportDto {
import java.util.*;

public class ReportDto extends ReportBase {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.Serializable;

public abstract class ReportParser implements Serializable {

private static final long serialVersionUID = -7720644051441434411L;
public abstract ReportDto parse(File file) throws IOException;

Expand Down
Loading