Skip to content

Commit 23dacd3

Browse files
Merge pull request #8 from jenkinsci/relative-data-tables
add relative columns for result of `item` to data-table
2 parents aaa0f20 + 1ed5672 commit 23dacd3

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/main/java/io/jenkins/plugins/reporter/model/Item.java

+4
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,8 @@ public List<Item> getItems() {
7878
public void setItems(List<Item> items) {
7979
this.items = items;
8080
}
81+
82+
public int getTotal() {
83+
return getResult().values().stream().reduce(0, Integer::sum);
84+
}
8185
}

src/main/java/io/jenkins/plugins/reporter/model/ItemTableModel.java

+26-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.apache.commons.text.CaseUtils;
1212

1313
import java.util.ArrayList;
14+
import java.util.HashMap;
1415
import java.util.List;
1516
import java.util.Map;
1617
import java.util.stream.Collectors;
@@ -56,7 +57,10 @@ public List<TableColumn> getColumns() {
5657
columns.add(createIdColumn());
5758
columns.add(createNameColumn());
5859

59-
item.getResult().keySet().forEach(property -> columns.add(createResultColumn(property)));
60+
item.getResult().keySet().forEach(property -> {
61+
columns.add(createResultAbsoluteColumn(property));
62+
columns.add(createResultRelativeColumn(property));
63+
});
6064

6165
columns.add(createDistributionColumn());
6266

@@ -87,14 +91,22 @@ protected TableColumn createNameColumn() {
8791
.build();
8892
}
8993

90-
protected TableColumn createResultColumn(String property) {
94+
protected TableColumn createResultAbsoluteColumn(String property) {
9195
return new TableColumn.ColumnBuilder()
92-
.withDataPropertyKey(property)
93-
.withHeaderLabel(CaseUtils.toCamelCase(property, true))
96+
.withDataPropertyKey(String.format("%s-absolute", property))
97+
.withHeaderLabel(String.format("# %s", CaseUtils.toCamelCase(property, true)))
9498
.withHeaderClass(TableColumn.ColumnCss.NUMBER)
9599
.build();
96100
}
97101

102+
protected TableColumn createResultRelativeColumn(String property) {
103+
return new TableColumn.ColumnBuilder()
104+
.withDataPropertyKey(String.format("%s-relative", property))
105+
.withHeaderLabel(String.format("%s (in %%)", CaseUtils.toCamelCase(property, true)))
106+
.withHeaderClass(TableColumn.ColumnCss.PERCENTAGE)
107+
.build();
108+
}
109+
98110
protected TableColumn createDistributionColumn() {
99111
return new TableColumn.ColumnBuilder()
100112
.withDataPropertyKey("distribution")
@@ -145,21 +157,26 @@ public DetailedCell<String> getDistribution() {
145157
* @return the result.
146158
*/
147159
@JsonAnyGetter
148-
public Map<String, Integer> getResult() {
149-
return item.getResult();
160+
public Map<String, Number> getResult() {
161+
Map<String, Number> result = new HashMap<>();
162+
163+
item.getResult().forEach((String key, Integer value) -> {
164+
result.put(String.format("%s-absolute", key), value);
165+
result.put(String.format("%s-relative", key), (value / (double) item.getTotal()));
166+
});
167+
168+
return result;
150169
}
151170

152171
protected DetailedCell<String> createColoredResultColumn(final Item item) {
153172
List<ContainerTag> spans = new ArrayList<>();
154173

155-
int total = item.getResult().values().stream().reduce(0, Integer::sum);
156-
157174
for (Map.Entry<String, String> color : colorProvider.getColorMapping().entrySet()) {
158175
String id = color.getKey();
159176
String hex = color.getValue();
160177

161178
int val = item.getResult().get(id);
162-
double percentage = (val / (double) total) * 100;
179+
double percentage = (val / (double) item.getTotal()) * 100;
163180

164181
spans.add(span()
165182
.withTitle(String.format("%s: %.2f%%", id, percentage))

0 commit comments

Comments
 (0)