2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.inventory;
25 import javax.json.Json;
26 import javax.json.JsonArray;
27 import javax.json.JsonArrayBuilder;
28 import javax.json.JsonObject;
29 import javax.json.JsonObjectBuilder;
32 * The Class EntityHistoryQueryBuilder.
34 public class EntityHistoryQueryBuilder {
36 private static final String TABLE = "table";
37 private static final String GRAPH = "graph";
42 * @param type the type
45 public static JsonObject getQuery(String type) {
46 if (type.equalsIgnoreCase(TABLE)) {
47 return createTableQuery();
48 } else if (type.equalsIgnoreCase(GRAPH)) {
49 return createGraphQuery();
56 * Creates the graph query.
58 * @return the json object
60 public static JsonObject createGraphQuery() {
61 JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
63 jsonBuilder.add("aggs",
64 Json.createObjectBuilder().add("group_by_entityType",
65 Json.createObjectBuilder()
66 .add("terms", Json.createObjectBuilder().add("field", "entityType").add("size", 0))
67 .add("aggs", Json.createObjectBuilder().add("group_by_date",
68 Json.createObjectBuilder().add("date_histogram", createDateHistogram())
69 .add("aggs", Json.createObjectBuilder().add("sort_by_date",
70 Json.createObjectBuilder().add("top_hits", createTopHitsBlob())))))));
71 jsonBuilder.add("size", 0);
73 return jsonBuilder.build();
77 * Creates the table query.
79 * @return the json object
81 public static JsonObject createTableQuery() {
82 JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
84 jsonBuilder.add("aggs",
85 Json.createObjectBuilder().add("group_by_entityType",
86 Json.createObjectBuilder()
87 .add("terms", Json.createObjectBuilder().add("field", "entityType").add("size", 0))
88 .add("aggs", Json.createObjectBuilder().add("sort_by_date",
89 Json.createObjectBuilder().add("top_hits", createTopHitsBlob())))));
90 jsonBuilder.add("size", 0);
92 return jsonBuilder.build();
96 * Creates the date histogram.
98 * @return the json object
100 private static JsonObject createDateHistogram() {
101 JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
103 jsonBuilder.add("field", "timestamp");
104 jsonBuilder.add("min_doc_count", 1);
105 jsonBuilder.add("interval", "day");
106 jsonBuilder.add("format", "epoch_millis");
108 return jsonBuilder.build();
112 * Creates the top hits blob.
114 * @return the json object
116 private static JsonObject createTopHitsBlob() {
117 JsonObjectBuilder builder = Json.createObjectBuilder();
118 builder.add("size", 1);
119 builder.add("sort", getSortCriteria());
120 return builder.build();
123 public static JsonArray getSortCriteria() {
124 JsonArrayBuilder jsonBuilder = Json.createArrayBuilder();
125 jsonBuilder.add(Json.createObjectBuilder().add("timestamp",
126 Json.createObjectBuilder().add("order", "desc")));
128 return jsonBuilder.build();
134 * @param args the arguments
136 public static void main(String[] args) {
137 System.out.println("TABLE-QUERY: " + createTableQuery().toString());
138 System.out.println("GRAPH_QUERY: " + createGraphQuery().toString());