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.
26 package org.openecomp.sparky.inventory;
28 import javax.json.Json;
29 import javax.json.JsonArray;
30 import javax.json.JsonArrayBuilder;
31 import javax.json.JsonObject;
32 import javax.json.JsonObjectBuilder;
35 * The Class EntityHistoryQueryBuilder.
37 public class EntityHistoryQueryBuilder {
39 private static final String TABLE = "table";
40 private static final String GRAPH = "graph";
45 * @param type the type
48 public static JsonObject getQuery(String type) {
49 if (type.equalsIgnoreCase(TABLE)) {
50 return createTableQuery();
51 } else if (type.equalsIgnoreCase(GRAPH)) {
52 return createGraphQuery();
59 * Creates the graph query.
61 * @return the json object
63 public static JsonObject createGraphQuery() {
64 JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
66 jsonBuilder.add("aggs",
67 Json.createObjectBuilder().add("group_by_entityType",
68 Json.createObjectBuilder()
69 .add("terms", Json.createObjectBuilder().add("field", "entityType").add("size", 0))
70 .add("aggs", Json.createObjectBuilder().add("group_by_date",
71 Json.createObjectBuilder().add("date_histogram", createDateHistogram())
72 .add("aggs", Json.createObjectBuilder().add("sort_by_date",
73 Json.createObjectBuilder().add("top_hits", createTopHitsBlob())))))));
74 jsonBuilder.add("size", 0);
76 return jsonBuilder.build();
80 * Creates the table query.
82 * @return the json object
84 public static JsonObject createTableQuery() {
85 JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
87 jsonBuilder.add("aggs",
88 Json.createObjectBuilder().add("group_by_entityType",
89 Json.createObjectBuilder()
90 .add("terms", Json.createObjectBuilder().add("field", "entityType").add("size", 0))
91 .add("aggs", Json.createObjectBuilder().add("sort_by_date",
92 Json.createObjectBuilder().add("top_hits", createTopHitsBlob())))));
93 jsonBuilder.add("size", 0);
95 return jsonBuilder.build();
99 * Creates the date histogram.
101 * @return the json object
103 private static JsonObject createDateHistogram() {
104 JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
106 jsonBuilder.add("field", "timestamp");
107 jsonBuilder.add("min_doc_count", 1);
108 jsonBuilder.add("interval", "day");
109 jsonBuilder.add("format", "epoch_millis");
111 return jsonBuilder.build();
115 * Creates the top hits blob.
117 * @return the json object
119 private static JsonObject createTopHitsBlob() {
120 JsonObjectBuilder builder = Json.createObjectBuilder();
121 builder.add("size", 1);
122 builder.add("sort", getSortCriteria());
123 return builder.build();
126 public static JsonArray getSortCriteria() {
127 JsonArrayBuilder jsonBuilder = Json.createArrayBuilder();
128 jsonBuilder.add(Json.createObjectBuilder().add("timestamp",
129 Json.createObjectBuilder().add("order", "desc")));
131 return jsonBuilder.build();
137 * @param args the arguments
139 public static void main(String[] args) {
140 System.out.println("TABLE-QUERY: " + createTableQuery().toString());
141 System.out.println("GRAPH_QUERY: " + createGraphQuery().toString());