Skip to content

Commit bc61ed6

Browse files
authored
Merge pull request #567 from bredy/RAIL-511
Add 'aggregation' to AttributeSortItem
2 parents ab4e35d + bc11ec1 commit bc61ed6

4 files changed

Lines changed: 91 additions & 4 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2007-2017, GoodData(R) Corporation. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE.txt file in the root directory of this source tree.
5+
*/
6+
7+
package com.gooddata.executeafm.resultspec;
8+
9+
import com.fasterxml.jackson.annotation.JsonValue;
10+
11+
/**
12+
* Represents aggregation used when putting together all related
13+
* values belonging to attribute element when we are sorting by this
14+
* attribute. You can find more details in {@link AttributeSortItem}.
15+
*/
16+
public enum AttributeSortAggregation {
17+
SUM;
18+
19+
@JsonValue
20+
@Override
21+
public String toString() {
22+
return name().toLowerCase();
23+
}
24+
}

src/main/java/com/gooddata/executeafm/resultspec/AttributeSortItem.java

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package com.gooddata.executeafm.resultspec;
77

88
import com.fasterxml.jackson.annotation.JsonCreator;
9+
import com.fasterxml.jackson.annotation.JsonInclude;
910
import com.fasterxml.jackson.annotation.JsonProperty;
1011
import com.fasterxml.jackson.annotation.JsonTypeInfo;
1112
import com.fasterxml.jackson.annotation.JsonTypeName;
@@ -17,24 +18,66 @@
1718

1819
/**
1920
* Define sort by specific attribute
21+
*
22+
* <p>With "aggregation" active you can sort all elements of attribute
23+
* by "aggregation fn" applied to all valid values belonging to each
24+
* element. This is extremely useful when sorting stacked
25+
* visualizations like stack bar/area charts. Currently supported is
26+
* only "sum", see {@link AttributeSortAggregation}</p>
27+
*
28+
* <p>Simple example (dimension = Year, measureGroup; 2 metrics; sort
29+
* on Year with aggregation="sum", descending):</p>
30+
* <pre>
31+
* Year 2006 2007
32+
* Names M1 M2 M1 M2
33+
* Values 1 2 3 4
34+
* </pre>
35+
*
36+
* <p>We take all values belonging to each attribute element of chosen attribute
37+
* and apply selected function (sum) on them. Notice that we are summarising
38+
* values from different metrics:</p>
39+
* <pre>
40+
* 2006 (1 + 2 = 3)
41+
* 2007 (3 + 4 = 7)
42+
* </pre>
43+
*
44+
* <p>After that we shuffle year attribute elements related to results from "sum"
45+
* function:</p>
46+
* <pre>
47+
* Year 2007 2006
48+
* Names M1 M2 M1 M2
49+
* Values 3 4 1 2
50+
* </pre>
2051
*/
2152
@JsonTypeInfo(include = As.WRAPPER_OBJECT, use = Id.NAME)
2253
@JsonTypeName("attributeSortItem")
54+
@JsonInclude(JsonInclude.Include.NON_NULL)
2355
public class AttributeSortItem implements SortItem {
2456

2557
private final String direction;
26-
2758
private final String attributeIdentifier;
59+
private final String aggregation;
2860

2961
@JsonCreator
3062
public AttributeSortItem(@JsonProperty("direction") final String direction,
31-
@JsonProperty("attributeIdentifier") final String attributeIdentifier) {
63+
@JsonProperty("attributeIdentifier") final String attributeIdentifier,
64+
@JsonProperty("aggregation") final String aggregation) {
3265
this.attributeIdentifier = attributeIdentifier;
3366
this.direction = direction;
67+
this.aggregation = aggregation;
68+
}
69+
70+
public AttributeSortItem(final String direction,
71+
final String attributeIdentifier) {
72+
this(direction, attributeIdentifier, null);
3473
}
3574

3675
public AttributeSortItem(final Direction direction, final String attributeIdentifier) {
37-
this(notNull(direction, "direction").toString(), attributeIdentifier);
76+
this(notNull(direction, "direction").toString(), attributeIdentifier, null);
77+
}
78+
79+
public AttributeSortItem(final Direction direction, final String attributeIdentifier, final AttributeSortAggregation aggregation) {
80+
this(notNull(direction, "direction").toString(), attributeIdentifier, notNull(aggregation, "aggregation").toString());
3881
}
3982

4083
public String getDirection() {
@@ -45,6 +88,10 @@ public String getAttributeIdentifier() {
4588
return attributeIdentifier;
4689
}
4790

91+
public String getAggregation() {
92+
return aggregation;
93+
}
94+
4895
@Override
4996
public String toString() {
5097
return GoodDataToStringBuilder.defaultToString(this);

src/test/groovy/com/gooddata/executeafm/resultspec/AttributeSortItemTest.groovy

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,29 @@ import static spock.util.matcher.HamcrestSupport.that
1515
class AttributeSortItemTest extends Specification {
1616

1717
private static final String ATTRIBUTE_SORT_ITEM_JSON = 'executeafm/resultspec/attributeSortItem.json'
18+
private static final String ATTRIBUTE_SORT_ITEM_WITH_AGGREGATION_JSON = 'executeafm/resultspec/attributeSortItemWithAggregation.json'
19+
1820

1921
def "should serialize values"() {
2022
expect:
2123
that new AttributeSortItem(Direction.ASC, 'aId'),
2224
jsonEquals(resource(ATTRIBUTE_SORT_ITEM_JSON))
2325
}
2426

27+
def "should serialize values with aggregation"() {
28+
expect:
29+
that new AttributeSortItem(Direction.ASC, 'aId', AttributeSortAggregation.SUM),
30+
jsonEquals(resource(ATTRIBUTE_SORT_ITEM_WITH_AGGREGATION_JSON))
31+
}
32+
2533
def "should deserialize values"() {
2634
when:
27-
AttributeSortItem item = readObjectFromResource("/$ATTRIBUTE_SORT_ITEM_JSON", AttributeSortItem)
35+
AttributeSortItem item = readObjectFromResource("/$ATTRIBUTE_SORT_ITEM_WITH_AGGREGATION_JSON", AttributeSortItem)
2836

2937
then:
3038
item.attributeIdentifier == 'aId'
3139
item.direction == 'asc'
40+
item.aggregation == 'sum'
3241
item.toString()
3342
}
3443
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"attributeSortItem": {
3+
"attributeIdentifier": "aId",
4+
"direction": "asc",
5+
"aggregation": "sum"
6+
}
7+
}

0 commit comments

Comments
 (0)