Skip to content
This repository was archived by the owner on Jul 20, 2024. It is now read-only.

Commit 25aff29

Browse files
author
Fred Ross
committed
Implemented pivot column splits.
1 parent 5eb6188 commit 25aff29

10 files changed

Lines changed: 516 additions & 13 deletions
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2014 Splunk, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package com.splunk;
17+
18+
import com.google.gson.JsonElement;
19+
import com.google.gson.JsonObject;
20+
21+
public class BooleanPivotColumnSplit extends PivotColumnSplit {
22+
private final String trueLabel, falseLabel;
23+
24+
public BooleanPivotColumnSplit(DataModelObject owner, String fieldName, String trueLabel, String falseLabel) {
25+
super(owner, fieldName);
26+
this.trueLabel = trueLabel;
27+
this.falseLabel = falseLabel;
28+
}
29+
30+
@Override
31+
public JsonElement toJson() {
32+
JsonObject root = new JsonObject();
33+
addCommonFields(root);
34+
35+
root.addProperty("trueLabel", trueLabel);
36+
root.addProperty("falseLabel", falseLabel);
37+
38+
return root;
39+
}
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2014 Splunk, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package com.splunk;
17+
18+
import com.google.gson.JsonElement;
19+
import com.google.gson.JsonObject;
20+
import com.google.gson.JsonPrimitive;
21+
22+
public class NumericPivotColumnSplit extends PivotColumnSplit {
23+
public NumericPivotColumnSplit(DataModelObject dataModelObject, String field) {
24+
super(dataModelObject, field);
25+
}
26+
27+
@Override
28+
public JsonElement toJson() {
29+
JsonObject root = new JsonObject();
30+
31+
addCommonFields(root);
32+
root.add("display", new JsonPrimitive("all"));
33+
34+
return root;
35+
}
36+
}

splunk/com/splunk/PivotColumn.java

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2014 Splunk, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package com.splunk;
17+
18+
import com.google.gson.JsonElement;
19+
import com.google.gson.JsonObject;
20+
21+
public abstract class PivotColumnSplit {
22+
private final String fieldName;
23+
private final DataModelObject owner;
24+
25+
public PivotColumnSplit(DataModelObject owner, String fieldName) {
26+
this.fieldName = fieldName;
27+
this.owner = owner;
28+
}
29+
30+
public String getFieldName() { return this.fieldName; }
31+
32+
public DataModelObject getOwner() { return this.owner; }
33+
34+
protected void addCommonFields(JsonObject obj) {
35+
Field field = this.owner.getField(this.fieldName);
36+
37+
obj.addProperty("fieldName", this.fieldName);
38+
obj.addProperty("owner", Util.join(".", field.getOwnerLineage()));
39+
obj.addProperty("type", field.getType().toString());
40+
}
41+
42+
public abstract JsonElement toJson();
43+
}

splunk/com/splunk/PivotSpecification.java

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class PivotSpecification {
3030
private DataModelObject dataModelObject;
3131
String namespace = null;
3232

33-
List<PivotColumn> columns = new ArrayList<PivotColumn>();
33+
List<PivotColumnSplit> columns = new ArrayList<PivotColumnSplit>();
3434
List<PivotFilter> filters = new ArrayList<PivotFilter>();
3535
List<PivotCell> cells = new ArrayList<PivotCell>();
3636
List<PivotRowSplit> rows = new ArrayList<PivotRowSplit>();
@@ -153,13 +153,16 @@ public PivotSpecification addFilter(String field, String sortAttribute,
153153
*/
154154
public PivotSpecification addRowSplit(String field, String label) {
155155
FieldType t = this.dataModelObject.getField(field).getType();
156-
if (t != FieldType.NUMBER) {
157-
throw new IllegalArgumentException("Expected a field of type number; found type " + t.toString());
156+
PivotRowSplit split;
157+
if (t == FieldType.NUMBER) {
158+
split = new NumberPivotRowSplit(this.dataModelObject, field, label);
159+
} else if (t == FieldType.STRING) {
160+
split = new StringPivotRowSplit(this.dataModelObject, field, label);
161+
} else {
162+
throw new IllegalArgumentException("Expected a field of type number or string; found type " + t.toString());
158163
}
159164

160-
PivotRowSplit split = new NumberPivotRowSplit(this.dataModelObject, field, label);
161165
rows.add(split);
162-
163166
return this;
164167
}
165168

@@ -212,6 +215,61 @@ public PivotSpecification addRowSplit(String field, String label, TimestampBinni
212215
return this;
213216
}
214217

218+
public PivotSpecification addColumnSplit(String field) {
219+
FieldType t = this.dataModelObject.getField(field).getType();
220+
PivotColumnSplit split;
221+
222+
if (t == FieldType.NUMBER) {
223+
split = new NumericPivotColumnSplit(this.dataModelObject, field);
224+
} else if (t == FieldType.STRING) {
225+
split = new StringPivotColumnSplit(this.dataModelObject, field);
226+
} else {
227+
throw new IllegalArgumentException("Expected a field of type number or string; found type " + t.toString());
228+
}
229+
230+
columns.add(split);
231+
return this;
232+
}
233+
234+
public PivotSpecification addColumnSplit(String field, int start, int end, int step, int limit) {
235+
FieldType t = this.dataModelObject.getField(field).getType();
236+
237+
if (t != FieldType.NUMBER) {
238+
throw new IllegalArgumentException("Expected a field of type number; found type " + t.toString());
239+
}
240+
241+
PivotColumnSplit split = new RangePivotColumnSplit(this.dataModelObject, field, start, end, step, limit);
242+
243+
columns.add(split);
244+
return this;
245+
}
246+
247+
public PivotSpecification addColumnSplit(String field, String trueDisplayValue, String falseDisplayValue) {
248+
FieldType t = this.dataModelObject.getField(field).getType();
249+
250+
if (t != FieldType.BOOLEAN) {
251+
throw new IllegalArgumentException("Expected a field of type boolean; found type " + t.toString());
252+
}
253+
254+
PivotColumnSplit split = new BooleanPivotColumnSplit(this.dataModelObject, field,
255+
trueDisplayValue, falseDisplayValue);
256+
257+
columns.add(split);
258+
return this;
259+
}
260+
261+
public PivotSpecification addColumnSplit(String field, TimestampBinning binning) {
262+
FieldType t = this.dataModelObject.getField(field).getType();
263+
264+
if (t != FieldType.TIMESTAMP) {
265+
throw new IllegalArgumentException("Expected a field of type timestamp; found type " + t.toString());
266+
}
267+
268+
PivotColumnSplit split = new TimestampPivotColumnSplit(this.dataModelObject, field, binning);
269+
270+
columns.add(split);
271+
return this;
272+
}
215273

216274
public JsonElement getDescription() {
217275
JsonObject root = new JsonObject();
@@ -230,5 +288,6 @@ public JsonElement getDescription() {
230288

231289
public Collection<PivotFilter> getFilters() { return this.filters; }
232290
public Collection<PivotRowSplit> getRowSplits() { return this.rows; }
291+
public Collection<PivotColumnSplit> getColumnSplits() { return this.columns; }
233292

234293
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2014 Splunk, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package com.splunk;
17+
18+
import com.google.gson.JsonElement;
19+
import com.google.gson.JsonObject;
20+
import com.google.gson.JsonPrimitive;
21+
22+
public class RangePivotColumnSplit extends PivotColumnSplit {
23+
private final int start, end, step, limit;
24+
25+
public RangePivotColumnSplit(DataModelObject dataModelObject, String field,
26+
int start, int end, int step, int limit) {
27+
super(dataModelObject, field);
28+
this.start = start;
29+
this.end = end;
30+
this.step = step;
31+
this.limit = limit;
32+
}
33+
34+
public int getStart() { return this.start; }
35+
public int getEnd() { return this.end; }
36+
public int getStep() { return this.step; }
37+
public int getLimit() { return this.limit; }
38+
39+
@Override
40+
public JsonElement toJson() {
41+
JsonObject root = new JsonObject();
42+
43+
addCommonFields(root);
44+
45+
JsonObject ranges = new JsonObject();
46+
ranges.add("start", new JsonPrimitive(start));
47+
ranges.add("end", new JsonPrimitive(end));
48+
ranges.add("size", new JsonPrimitive(step));
49+
ranges.add("maxNumberOf", new JsonPrimitive(limit));
50+
root.add("ranges", ranges);
51+
root.add("display", new JsonPrimitive("ranges"));
52+
53+
return root;
54+
}
55+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2014 Splunk, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package com.splunk;
17+
18+
import com.google.gson.JsonElement;
19+
import com.google.gson.JsonObject;
20+
import com.google.gson.JsonPrimitive;
21+
22+
public class StringPivotColumnSplit extends PivotColumnSplit {
23+
public StringPivotColumnSplit(DataModelObject dataModelObject, String field) {
24+
super(dataModelObject, field);
25+
}
26+
27+
@Override
28+
public JsonElement toJson() {
29+
JsonObject root = new JsonObject();
30+
31+
addCommonFields(root);
32+
33+
return root;
34+
}
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2014 Splunk, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package com.splunk;
17+
18+
import com.google.gson.JsonElement;
19+
import com.google.gson.JsonObject;
20+
21+
public class StringPivotRowSplit extends PivotRowSplit {
22+
public StringPivotRowSplit(DataModelObject dataModelObject, String field, String label) {
23+
super(dataModelObject, field, label);
24+
}
25+
26+
@Override
27+
public JsonElement toJson() {
28+
JsonObject root = new JsonObject();
29+
addCommonFields(root);
30+
return root;
31+
}
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2014 Splunk, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package com.splunk;
17+
18+
import com.google.gson.JsonElement;
19+
import com.google.gson.JsonObject;
20+
21+
public class TimestampPivotColumnSplit extends PivotColumnSplit {
22+
private final TimestampBinning binning;
23+
24+
public TimestampPivotColumnSplit(DataModelObject owner, String fieldName, TimestampBinning binning) {
25+
super(owner, fieldName);
26+
this.binning = binning;
27+
}
28+
29+
@Override
30+
public JsonElement toJson() {
31+
JsonObject root = new JsonObject();
32+
addCommonFields(root);
33+
root.addProperty("period", binning.toString());
34+
return root;
35+
}
36+
}

0 commit comments

Comments
 (0)