Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / inventory / EntityHistoryQueryBuilder.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.openecomp.sparky.inventory;
27
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;
33
34 /**
35  * The Class EntityHistoryQueryBuilder.
36  */
37 public class EntityHistoryQueryBuilder {
38
39   private static final String TABLE = "table";
40   private static final String GRAPH = "graph";
41
42   /**
43    * Gets the query.
44    *
45    * @param type the type
46    * @return the query
47    */
48   public static JsonObject getQuery(String type) {
49     if (type.equalsIgnoreCase(TABLE)) {
50       return createTableQuery();
51     } else if (type.equalsIgnoreCase(GRAPH)) {
52       return createGraphQuery();
53     } else {
54       return null;
55     }
56   }
57
58   /**
59    * Creates the graph query.
60    *
61    * @return the json object
62    */
63   public static JsonObject createGraphQuery() {
64     JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
65
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);
75
76     return jsonBuilder.build();
77   }
78
79   /**
80    * Creates the table query.
81    *
82    * @return the json object
83    */
84   public static JsonObject createTableQuery() {
85     JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
86
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);
94
95     return jsonBuilder.build();
96   }
97
98   /**
99    * Creates the date histogram.
100    *
101    * @return the json object
102    */
103   private static JsonObject createDateHistogram() {
104     JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
105
106     jsonBuilder.add("field", "timestamp");
107     jsonBuilder.add("min_doc_count", 1);
108     jsonBuilder.add("interval", "day");
109     jsonBuilder.add("format", "epoch_millis");
110
111     return jsonBuilder.build();
112   }
113
114   /**
115    * Creates the top hits blob.
116    *
117    * @return the json object
118    */
119   private static JsonObject createTopHitsBlob() {
120     JsonObjectBuilder builder = Json.createObjectBuilder();
121     builder.add("size", 1);
122     builder.add("sort", getSortCriteria());
123     return builder.build();
124   }
125
126   public static JsonArray getSortCriteria() {
127     JsonArrayBuilder jsonBuilder = Json.createArrayBuilder();
128     jsonBuilder.add(Json.createObjectBuilder().add("timestamp",
129         Json.createObjectBuilder().add("order", "desc")));
130
131     return jsonBuilder.build();
132   }
133
134   /**
135    * The main method.
136    *
137    * @param args the arguments
138    */
139   public static void main(String[] args) {
140     System.out.println("TABLE-QUERY: " + createTableQuery().toString());
141     System.out.println("GRAPH_QUERY: " + createGraphQuery().toString());
142   }
143
144 }