2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.inventory;
27 import javax.json.Json;
28 import javax.json.JsonArray;
29 import javax.json.JsonArrayBuilder;
30 import javax.json.JsonObject;
31 import javax.json.JsonObjectBuilder;
34 * The Class EntityHistoryQueryBuilder.
36 public class EntityHistoryQueryBuilder {
38 private static final String TABLE = "table";
39 private static final String GRAPH = "graph";
44 * @param type the type
47 public static JsonObject getQuery(String type) {
48 if (type.equalsIgnoreCase(TABLE)) {
49 return createTableQuery();
50 } else if (type.equalsIgnoreCase(GRAPH)) {
51 return createGraphQuery();
58 * Creates the graph query.
60 * @return the json object
62 public static JsonObject createGraphQuery() {
63 JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
65 jsonBuilder.add("aggs",
66 Json.createObjectBuilder().add("group_by_entityType",
67 Json.createObjectBuilder()
68 .add("terms", Json.createObjectBuilder().add("field", "entityType").add("size", 0))
69 .add("aggs", Json.createObjectBuilder().add("group_by_date",
70 Json.createObjectBuilder().add("date_histogram", createDateHistogram())
71 .add("aggs", Json.createObjectBuilder().add("sort_by_date",
72 Json.createObjectBuilder().add("top_hits", createTopHitsBlob())))))));
73 jsonBuilder.add("size", 0);
75 return jsonBuilder.build();
79 * Creates the table query.
81 * @return the json object
83 public static JsonObject createTableQuery() {
84 JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
86 jsonBuilder.add("aggs",
87 Json.createObjectBuilder().add("group_by_entityType",
88 Json.createObjectBuilder()
89 .add("terms", Json.createObjectBuilder().add("field", "entityType").add("size", 0))
90 .add("aggs", Json.createObjectBuilder().add("sort_by_date",
91 Json.createObjectBuilder().add("top_hits", createTopHitsBlob())))));
92 jsonBuilder.add("size", 0);
94 return jsonBuilder.build();
98 * Creates the date histogram.
100 * @return the json object
102 private static JsonObject createDateHistogram() {
103 JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
105 jsonBuilder.add("field", "timestamp");
106 jsonBuilder.add("min_doc_count", 1);
107 jsonBuilder.add("interval", "day");
108 jsonBuilder.add("format", "epoch_millis");
110 return jsonBuilder.build();
114 * Creates the top hits blob.
116 * @return the json object
118 private static JsonObject createTopHitsBlob() {
119 JsonObjectBuilder builder = Json.createObjectBuilder();
120 builder.add("size", 1);
121 builder.add("sort", getSortCriteria());
122 return builder.build();
125 public static JsonArray getSortCriteria() {
126 JsonArrayBuilder jsonBuilder = Json.createArrayBuilder();
127 jsonBuilder.add(Json.createObjectBuilder().add("timestamp",
128 Json.createObjectBuilder().add("order", "desc")));
130 return jsonBuilder.build();
136 * @param args the arguments
138 public static void main(String[] args) {
139 System.out.println("TABLE-QUERY: " + createTableQuery().toString());
140 System.out.println("GRAPH_QUERY: " + createGraphQuery().toString());