2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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 package org.onap.aai.sparky.inventory;
23 import javax.json.Json;
24 import javax.json.JsonArray;
25 import javax.json.JsonArrayBuilder;
26 import javax.json.JsonObject;
27 import javax.json.JsonObjectBuilder;
30 * The Class EntityHistoryQueryBuilder.
32 public class EntityHistoryQueryBuilder {
34 private static final String TABLE = "table";
35 private static final String GRAPH = "graph";
40 * @param type the type
43 public static JsonObject getQuery(String type) {
44 if (type.equalsIgnoreCase(TABLE)) {
45 return createTableQuery();
46 } else if (type.equalsIgnoreCase(GRAPH)) {
47 return createGraphQuery();
54 * Creates the graph query.
56 * @return the json object
58 public static JsonObject createGraphQuery() {
59 JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
61 jsonBuilder.add("aggs",
62 Json.createObjectBuilder().add("group_by_entityType",
63 Json.createObjectBuilder()
64 .add("terms", Json.createObjectBuilder().add("field", "entityType").add("size", 0))
65 .add("aggs", Json.createObjectBuilder().add("group_by_date",
66 Json.createObjectBuilder().add("date_histogram", createDateHistogram())
67 .add("aggs", Json.createObjectBuilder().add("sort_by_date",
68 Json.createObjectBuilder().add("top_hits", createTopHitsBlob())))))));
69 jsonBuilder.add("size", 0);
71 return jsonBuilder.build();
75 * Creates the table query.
77 * @return the json object
79 public static JsonObject createTableQuery() {
80 JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
82 jsonBuilder.add("aggs",
83 Json.createObjectBuilder().add("group_by_entityType",
84 Json.createObjectBuilder()
85 .add("terms", Json.createObjectBuilder().add("field", "entityType").add("size", 0))
86 .add("aggs", Json.createObjectBuilder().add("sort_by_date",
87 Json.createObjectBuilder().add("top_hits", createTopHitsBlob())))));
88 jsonBuilder.add("size", 0);
90 return jsonBuilder.build();
94 * Creates the date histogram.
96 * @return the json object
98 private static JsonObject createDateHistogram() {
99 JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
101 jsonBuilder.add("field", "timestamp");
102 jsonBuilder.add("min_doc_count", 1);
103 jsonBuilder.add("interval", "day");
104 jsonBuilder.add("format", "epoch_millis");
106 return jsonBuilder.build();
110 * Creates the top hits blob.
112 * @return the json object
114 private static JsonObject createTopHitsBlob() {
115 JsonObjectBuilder builder = Json.createObjectBuilder();
116 builder.add("size", 1);
117 builder.add("sort", getSortCriteria());
118 return builder.build();
121 public static JsonArray getSortCriteria() {
122 JsonArrayBuilder jsonBuilder = Json.createArrayBuilder();
123 jsonBuilder.add(Json.createObjectBuilder().add("timestamp",
124 Json.createObjectBuilder().add("order", "desc")));
126 return jsonBuilder.build();
132 * @param args the arguments
134 public static void main(String[] args) {
135 System.out.println("TABLE-QUERY: " + createTableQuery().toString());
136 System.out.println("GRAPH_QUERY: " + createGraphQuery().toString());