@SuppressWarnings("unchecked") @Test public void getUtilizationByAssetGroupTest2() throws Exception { final ElasticSearchRepository classUnderTest = PowerMockito.spy(new ElasticSearchRepository()); ReflectionTestUtils.setField(classUnderTest, "esUrl", "esUrl123"); mockStatic(PacHttpUtils.class); PowerMockito.when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenThrow(Exception.class); assertThatThrownBy(() -> classUnderTest.getUtilizationByAssetGroup(eq("asseGroup"))).isInstanceOf(Exception.class); }
public List<Map<String, Object>> getUtilizationByAssetGroup(String asseGroup) throws Exception { String urlToQuery = buildAggsURL(esUrl, asseGroup, EC2_UTILIZATION); Map<String, Object> requestBody = new HashMap<String, Object>(); Map<String, Object> cpuUtilizationDetails = new HashMap<String, Object>(); Map<String, Object> cpuUtilization = new HashMap<String, Object>(); Map<String, Object> range = new HashMap<String, Object>(); Map<String, Object> must = new HashMap<String, Object>(); Map<String, Object> bool = new HashMap<String, Object>(); Map<String, Object> aggs = new HashMap<String, Object>(); Map<String, Object> histogram = new HashMap<String, Object>(); Map<String, Object> histogramDetails = new HashMap<String, Object>(); Map<String, Object> avgValuesperDay = new HashMap<String, Object>(); List<Map<String, Object>> utilizationList = new ArrayList<Map<String, Object>>(); Map<String, Object> utilization = null; Map<String, Object> field = null; Map<String, Object> sum = null; Map<String, Object> avg = null; cpuUtilizationDetails.put("gte", "now-30d"); cpuUtilizationDetails.put("lte", "now-1d"); cpuUtilizationDetails.put("format", "yyyy-MM-dd HH:mm:ss"); cpuUtilization.put("#Datetime-CPU-Utilization", cpuUtilizationDetails); range.put(RANGE, cpuUtilization); must.put(MUST, range); bool.put(BOOL, must); field = new HashMap(); avg = new HashMap(); field.put("field", AVG_CPU_UTILIZATION); avg.put(AVG, field); aggs.put(AVG_CPU_UTILIZATION, avg); field = new HashMap(); sum = new HashMap(); field.put("field", SUM_NETWORK_IN); sum.put(SUM, field); aggs.put("Avg-NetworkIn", sum); field = new HashMap(); sum = new HashMap(); field.put("field", SUM_NETWORK_OUT); sum.put(SUM, field); aggs.put("Avg-NetworkOut", sum); field = new HashMap(); sum = new HashMap(); field.put("field", "Sum-DiskReadBytes"); sum.put(SUM, field); aggs.put("Avg-DiskReadinBytes", sum); field = new HashMap(); sum = new HashMap(); field.put("field", "Sum-DiskWriteBytes"); sum.put(SUM, field); aggs.put("Avg-DiskWriteinBytes", sum); field = new HashMap(); field.put("_key", "desc"); histogramDetails.put("field", "#Datetime-CPU-Utilization"); histogramDetails.put("interval", "day"); histogramDetails.put("format", "yyyy-MM-dd HH:mm:ss"); histogramDetails.put(ORDER, field); histogram.put("date_histogram", histogramDetails); histogram.put(AGGS, aggs); avgValuesperDay.put("avg-values-per-day", histogram); requestBody.put(QUERY, bool); requestBody.put(AGGS, avgValuesperDay); requestBody.put(SIZE, 0); Gson gson = new GsonBuilder().create(); try { String request = gson.toJson(requestBody, Object.class); String responseDetails = null; responseDetails = PacHttpUtils.doHttpPost(urlToQuery, request); JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); responseDetailsjson.get("aggregations"); Object aggregations = responseDetailsjson.get("aggregations"); String aggregationsstr = aggregations.toString(); parser = new JsonParser(); JsonObject aggregationsjson = (JsonObject) parser.parse(aggregationsstr); Object avgvalues = aggregationsjson.get("avg-values-per-day"); String avgvaluestr = avgvalues.toString(); parser = new JsonParser(); JsonObject avgvaluesjson = (JsonObject) parser.parse(avgvaluestr); Object bucketObj = avgvaluesjson.get(BUCKETS); String bucketstr = bucketObj.toString(); parser = new JsonParser(); JsonArray buckets = parser.parse(bucketstr).getAsJsonArray(); for (JsonElement jsonElement : buckets) { utilization = new HashMap<String, Object>(); JsonObject bucketdetails = jsonElement.getAsJsonObject(); utilization.put(DATE, bucketdetails.get("key_as_string").getAsString()); Object cpuUtilizationObj = bucketdetails.get(AVG_CPU_UTILIZATION); String cpuUtilizationStr = cpuUtilizationObj.toString(); parser = new JsonParser(); JsonObject cpuUtilizationjson = (JsonObject) parser.parse(cpuUtilizationStr); if (!cpuUtilizationjson.get("value").isJsonNull()) { utilization.put(CPU_UTILIZATION, cpuUtilizationjson.get("value").getAsDouble()); } else { utilization.put(CPU_UTILIZATION, 0); } Object networkInUtilizationObj = bucketdetails.get("Avg-NetworkIn"); String networkInUtilizationStr = networkInUtilizationObj.toString(); parser = new JsonParser(); JsonObject networkInUtilizationjson = (JsonObject) parser.parse(networkInUtilizationStr); if (!networkInUtilizationjson.get("value").isJsonNull()) { utilization.put(NETWORK_IN, networkInUtilizationjson.get("value").getAsLong()); } else { utilization.put(NETWORK_IN, 0); } Object networkOutUtilizationObj = bucketdetails.get("Avg-NetworkOut"); String networkOutUtilizationStr = networkOutUtilizationObj.toString(); parser = new JsonParser(); JsonObject networkOutUtilizationjson = (JsonObject) parser.parse(networkOutUtilizationStr); if (!networkOutUtilizationjson.get("value").isJsonNull()) { utilization.put(NETWORK_OUT, networkOutUtilizationjson.get("value").getAsLong()); } else { utilization.put(NETWORK_OUT, 0); } Object diskReadUtilizationObj = bucketdetails.get("Avg-DiskReadinBytes"); String diskReadUtilizationStr = diskReadUtilizationObj.toString(); parser = new JsonParser(); JsonObject diskReadUtilizationjson = (JsonObject) parser.parse(diskReadUtilizationStr); if (!diskReadUtilizationjson.get("value").isJsonNull()) { utilization.put(DISK_READ_IN_BYTES, diskReadUtilizationjson.get("value").getAsLong()); } else { utilization.put(DISK_READ_IN_BYTES, 0); } Object diskRWriteUtilizationObj = bucketdetails.get("Avg-DiskWriteinBytes"); String diskRWriteUtilizationStr = diskRWriteUtilizationObj.toString(); parser = new JsonParser(); JsonObject diskRWriteUtilizationjson = (JsonObject) parser.parse(diskRWriteUtilizationStr); if (!diskRWriteUtilizationjson.get("value").isJsonNull()) { utilization.put(DISK_WRITE_IN_BYTES, diskRWriteUtilizationjson.get("value").getAsLong()); } else { utilization.put(DISK_WRITE_IN_BYTES, 0); } utilizationList.add(utilization); } return utilizationList; } catch (Exception e) { LOGGER.error(ERROR_RETRIEVING_INVENTORY_FROM_ES, e); throw e; } }
ElasticSearchRepository implements Constants { public List<Map<String, Object>> getUtilizationByAssetGroup(String asseGroup) throws Exception { String urlToQuery = buildAggsURL(esUrl, asseGroup, EC2_UTILIZATION); Map<String, Object> requestBody = new HashMap<String, Object>(); Map<String, Object> cpuUtilizationDetails = new HashMap<String, Object>(); Map<String, Object> cpuUtilization = new HashMap<String, Object>(); Map<String, Object> range = new HashMap<String, Object>(); Map<String, Object> must = new HashMap<String, Object>(); Map<String, Object> bool = new HashMap<String, Object>(); Map<String, Object> aggs = new HashMap<String, Object>(); Map<String, Object> histogram = new HashMap<String, Object>(); Map<String, Object> histogramDetails = new HashMap<String, Object>(); Map<String, Object> avgValuesperDay = new HashMap<String, Object>(); List<Map<String, Object>> utilizationList = new ArrayList<Map<String, Object>>(); Map<String, Object> utilization = null; Map<String, Object> field = null; Map<String, Object> sum = null; Map<String, Object> avg = null; cpuUtilizationDetails.put("gte", "now-30d"); cpuUtilizationDetails.put("lte", "now-1d"); cpuUtilizationDetails.put("format", "yyyy-MM-dd HH:mm:ss"); cpuUtilization.put("#Datetime-CPU-Utilization", cpuUtilizationDetails); range.put(RANGE, cpuUtilization); must.put(MUST, range); bool.put(BOOL, must); field = new HashMap(); avg = new HashMap(); field.put("field", AVG_CPU_UTILIZATION); avg.put(AVG, field); aggs.put(AVG_CPU_UTILIZATION, avg); field = new HashMap(); sum = new HashMap(); field.put("field", SUM_NETWORK_IN); sum.put(SUM, field); aggs.put("Avg-NetworkIn", sum); field = new HashMap(); sum = new HashMap(); field.put("field", SUM_NETWORK_OUT); sum.put(SUM, field); aggs.put("Avg-NetworkOut", sum); field = new HashMap(); sum = new HashMap(); field.put("field", "Sum-DiskReadBytes"); sum.put(SUM, field); aggs.put("Avg-DiskReadinBytes", sum); field = new HashMap(); sum = new HashMap(); field.put("field", "Sum-DiskWriteBytes"); sum.put(SUM, field); aggs.put("Avg-DiskWriteinBytes", sum); field = new HashMap(); field.put("_key", "desc"); histogramDetails.put("field", "#Datetime-CPU-Utilization"); histogramDetails.put("interval", "day"); histogramDetails.put("format", "yyyy-MM-dd HH:mm:ss"); histogramDetails.put(ORDER, field); histogram.put("date_histogram", histogramDetails); histogram.put(AGGS, aggs); avgValuesperDay.put("avg-values-per-day", histogram); requestBody.put(QUERY, bool); requestBody.put(AGGS, avgValuesperDay); requestBody.put(SIZE, 0); Gson gson = new GsonBuilder().create(); try { String request = gson.toJson(requestBody, Object.class); String responseDetails = null; responseDetails = PacHttpUtils.doHttpPost(urlToQuery, request); JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); responseDetailsjson.get("aggregations"); Object aggregations = responseDetailsjson.get("aggregations"); String aggregationsstr = aggregations.toString(); parser = new JsonParser(); JsonObject aggregationsjson = (JsonObject) parser.parse(aggregationsstr); Object avgvalues = aggregationsjson.get("avg-values-per-day"); String avgvaluestr = avgvalues.toString(); parser = new JsonParser(); JsonObject avgvaluesjson = (JsonObject) parser.parse(avgvaluestr); Object bucketObj = avgvaluesjson.get(BUCKETS); String bucketstr = bucketObj.toString(); parser = new JsonParser(); JsonArray buckets = parser.parse(bucketstr).getAsJsonArray(); for (JsonElement jsonElement : buckets) { utilization = new HashMap<String, Object>(); JsonObject bucketdetails = jsonElement.getAsJsonObject(); utilization.put(DATE, bucketdetails.get("key_as_string").getAsString()); Object cpuUtilizationObj = bucketdetails.get(AVG_CPU_UTILIZATION); String cpuUtilizationStr = cpuUtilizationObj.toString(); parser = new JsonParser(); JsonObject cpuUtilizationjson = (JsonObject) parser.parse(cpuUtilizationStr); if (!cpuUtilizationjson.get("value").isJsonNull()) { utilization.put(CPU_UTILIZATION, cpuUtilizationjson.get("value").getAsDouble()); } else { utilization.put(CPU_UTILIZATION, 0); } Object networkInUtilizationObj = bucketdetails.get("Avg-NetworkIn"); String networkInUtilizationStr = networkInUtilizationObj.toString(); parser = new JsonParser(); JsonObject networkInUtilizationjson = (JsonObject) parser.parse(networkInUtilizationStr); if (!networkInUtilizationjson.get("value").isJsonNull()) { utilization.put(NETWORK_IN, networkInUtilizationjson.get("value").getAsLong()); } else { utilization.put(NETWORK_IN, 0); } Object networkOutUtilizationObj = bucketdetails.get("Avg-NetworkOut"); String networkOutUtilizationStr = networkOutUtilizationObj.toString(); parser = new JsonParser(); JsonObject networkOutUtilizationjson = (JsonObject) parser.parse(networkOutUtilizationStr); if (!networkOutUtilizationjson.get("value").isJsonNull()) { utilization.put(NETWORK_OUT, networkOutUtilizationjson.get("value").getAsLong()); } else { utilization.put(NETWORK_OUT, 0); } Object diskReadUtilizationObj = bucketdetails.get("Avg-DiskReadinBytes"); String diskReadUtilizationStr = diskReadUtilizationObj.toString(); parser = new JsonParser(); JsonObject diskReadUtilizationjson = (JsonObject) parser.parse(diskReadUtilizationStr); if (!diskReadUtilizationjson.get("value").isJsonNull()) { utilization.put(DISK_READ_IN_BYTES, diskReadUtilizationjson.get("value").getAsLong()); } else { utilization.put(DISK_READ_IN_BYTES, 0); } Object diskRWriteUtilizationObj = bucketdetails.get("Avg-DiskWriteinBytes"); String diskRWriteUtilizationStr = diskRWriteUtilizationObj.toString(); parser = new JsonParser(); JsonObject diskRWriteUtilizationjson = (JsonObject) parser.parse(diskRWriteUtilizationStr); if (!diskRWriteUtilizationjson.get("value").isJsonNull()) { utilization.put(DISK_WRITE_IN_BYTES, diskRWriteUtilizationjson.get("value").getAsLong()); } else { utilization.put(DISK_WRITE_IN_BYTES, 0); } utilizationList.add(utilization); } return utilizationList; } catch (Exception e) { LOGGER.error(ERROR_RETRIEVING_INVENTORY_FROM_ES, e); throw e; } } }
ElasticSearchRepository implements Constants { public List<Map<String, Object>> getUtilizationByAssetGroup(String asseGroup) throws Exception { String urlToQuery = buildAggsURL(esUrl, asseGroup, EC2_UTILIZATION); Map<String, Object> requestBody = new HashMap<String, Object>(); Map<String, Object> cpuUtilizationDetails = new HashMap<String, Object>(); Map<String, Object> cpuUtilization = new HashMap<String, Object>(); Map<String, Object> range = new HashMap<String, Object>(); Map<String, Object> must = new HashMap<String, Object>(); Map<String, Object> bool = new HashMap<String, Object>(); Map<String, Object> aggs = new HashMap<String, Object>(); Map<String, Object> histogram = new HashMap<String, Object>(); Map<String, Object> histogramDetails = new HashMap<String, Object>(); Map<String, Object> avgValuesperDay = new HashMap<String, Object>(); List<Map<String, Object>> utilizationList = new ArrayList<Map<String, Object>>(); Map<String, Object> utilization = null; Map<String, Object> field = null; Map<String, Object> sum = null; Map<String, Object> avg = null; cpuUtilizationDetails.put("gte", "now-30d"); cpuUtilizationDetails.put("lte", "now-1d"); cpuUtilizationDetails.put("format", "yyyy-MM-dd HH:mm:ss"); cpuUtilization.put("#Datetime-CPU-Utilization", cpuUtilizationDetails); range.put(RANGE, cpuUtilization); must.put(MUST, range); bool.put(BOOL, must); field = new HashMap(); avg = new HashMap(); field.put("field", AVG_CPU_UTILIZATION); avg.put(AVG, field); aggs.put(AVG_CPU_UTILIZATION, avg); field = new HashMap(); sum = new HashMap(); field.put("field", SUM_NETWORK_IN); sum.put(SUM, field); aggs.put("Avg-NetworkIn", sum); field = new HashMap(); sum = new HashMap(); field.put("field", SUM_NETWORK_OUT); sum.put(SUM, field); aggs.put("Avg-NetworkOut", sum); field = new HashMap(); sum = new HashMap(); field.put("field", "Sum-DiskReadBytes"); sum.put(SUM, field); aggs.put("Avg-DiskReadinBytes", sum); field = new HashMap(); sum = new HashMap(); field.put("field", "Sum-DiskWriteBytes"); sum.put(SUM, field); aggs.put("Avg-DiskWriteinBytes", sum); field = new HashMap(); field.put("_key", "desc"); histogramDetails.put("field", "#Datetime-CPU-Utilization"); histogramDetails.put("interval", "day"); histogramDetails.put("format", "yyyy-MM-dd HH:mm:ss"); histogramDetails.put(ORDER, field); histogram.put("date_histogram", histogramDetails); histogram.put(AGGS, aggs); avgValuesperDay.put("avg-values-per-day", histogram); requestBody.put(QUERY, bool); requestBody.put(AGGS, avgValuesperDay); requestBody.put(SIZE, 0); Gson gson = new GsonBuilder().create(); try { String request = gson.toJson(requestBody, Object.class); String responseDetails = null; responseDetails = PacHttpUtils.doHttpPost(urlToQuery, request); JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); responseDetailsjson.get("aggregations"); Object aggregations = responseDetailsjson.get("aggregations"); String aggregationsstr = aggregations.toString(); parser = new JsonParser(); JsonObject aggregationsjson = (JsonObject) parser.parse(aggregationsstr); Object avgvalues = aggregationsjson.get("avg-values-per-day"); String avgvaluestr = avgvalues.toString(); parser = new JsonParser(); JsonObject avgvaluesjson = (JsonObject) parser.parse(avgvaluestr); Object bucketObj = avgvaluesjson.get(BUCKETS); String bucketstr = bucketObj.toString(); parser = new JsonParser(); JsonArray buckets = parser.parse(bucketstr).getAsJsonArray(); for (JsonElement jsonElement : buckets) { utilization = new HashMap<String, Object>(); JsonObject bucketdetails = jsonElement.getAsJsonObject(); utilization.put(DATE, bucketdetails.get("key_as_string").getAsString()); Object cpuUtilizationObj = bucketdetails.get(AVG_CPU_UTILIZATION); String cpuUtilizationStr = cpuUtilizationObj.toString(); parser = new JsonParser(); JsonObject cpuUtilizationjson = (JsonObject) parser.parse(cpuUtilizationStr); if (!cpuUtilizationjson.get("value").isJsonNull()) { utilization.put(CPU_UTILIZATION, cpuUtilizationjson.get("value").getAsDouble()); } else { utilization.put(CPU_UTILIZATION, 0); } Object networkInUtilizationObj = bucketdetails.get("Avg-NetworkIn"); String networkInUtilizationStr = networkInUtilizationObj.toString(); parser = new JsonParser(); JsonObject networkInUtilizationjson = (JsonObject) parser.parse(networkInUtilizationStr); if (!networkInUtilizationjson.get("value").isJsonNull()) { utilization.put(NETWORK_IN, networkInUtilizationjson.get("value").getAsLong()); } else { utilization.put(NETWORK_IN, 0); } Object networkOutUtilizationObj = bucketdetails.get("Avg-NetworkOut"); String networkOutUtilizationStr = networkOutUtilizationObj.toString(); parser = new JsonParser(); JsonObject networkOutUtilizationjson = (JsonObject) parser.parse(networkOutUtilizationStr); if (!networkOutUtilizationjson.get("value").isJsonNull()) { utilization.put(NETWORK_OUT, networkOutUtilizationjson.get("value").getAsLong()); } else { utilization.put(NETWORK_OUT, 0); } Object diskReadUtilizationObj = bucketdetails.get("Avg-DiskReadinBytes"); String diskReadUtilizationStr = diskReadUtilizationObj.toString(); parser = new JsonParser(); JsonObject diskReadUtilizationjson = (JsonObject) parser.parse(diskReadUtilizationStr); if (!diskReadUtilizationjson.get("value").isJsonNull()) { utilization.put(DISK_READ_IN_BYTES, diskReadUtilizationjson.get("value").getAsLong()); } else { utilization.put(DISK_READ_IN_BYTES, 0); } Object diskRWriteUtilizationObj = bucketdetails.get("Avg-DiskWriteinBytes"); String diskRWriteUtilizationStr = diskRWriteUtilizationObj.toString(); parser = new JsonParser(); JsonObject diskRWriteUtilizationjson = (JsonObject) parser.parse(diskRWriteUtilizationStr); if (!diskRWriteUtilizationjson.get("value").isJsonNull()) { utilization.put(DISK_WRITE_IN_BYTES, diskRWriteUtilizationjson.get("value").getAsLong()); } else { utilization.put(DISK_WRITE_IN_BYTES, 0); } utilizationList.add(utilization); } return utilizationList; } catch (Exception e) { LOGGER.error(ERROR_RETRIEVING_INVENTORY_FROM_ES, e); throw e; } }  }
ElasticSearchRepository implements Constants { public List<Map<String, Object>> getUtilizationByAssetGroup(String asseGroup) throws Exception { String urlToQuery = buildAggsURL(esUrl, asseGroup, EC2_UTILIZATION); Map<String, Object> requestBody = new HashMap<String, Object>(); Map<String, Object> cpuUtilizationDetails = new HashMap<String, Object>(); Map<String, Object> cpuUtilization = new HashMap<String, Object>(); Map<String, Object> range = new HashMap<String, Object>(); Map<String, Object> must = new HashMap<String, Object>(); Map<String, Object> bool = new HashMap<String, Object>(); Map<String, Object> aggs = new HashMap<String, Object>(); Map<String, Object> histogram = new HashMap<String, Object>(); Map<String, Object> histogramDetails = new HashMap<String, Object>(); Map<String, Object> avgValuesperDay = new HashMap<String, Object>(); List<Map<String, Object>> utilizationList = new ArrayList<Map<String, Object>>(); Map<String, Object> utilization = null; Map<String, Object> field = null; Map<String, Object> sum = null; Map<String, Object> avg = null; cpuUtilizationDetails.put("gte", "now-30d"); cpuUtilizationDetails.put("lte", "now-1d"); cpuUtilizationDetails.put("format", "yyyy-MM-dd HH:mm:ss"); cpuUtilization.put("#Datetime-CPU-Utilization", cpuUtilizationDetails); range.put(RANGE, cpuUtilization); must.put(MUST, range); bool.put(BOOL, must); field = new HashMap(); avg = new HashMap(); field.put("field", AVG_CPU_UTILIZATION); avg.put(AVG, field); aggs.put(AVG_CPU_UTILIZATION, avg); field = new HashMap(); sum = new HashMap(); field.put("field", SUM_NETWORK_IN); sum.put(SUM, field); aggs.put("Avg-NetworkIn", sum); field = new HashMap(); sum = new HashMap(); field.put("field", SUM_NETWORK_OUT); sum.put(SUM, field); aggs.put("Avg-NetworkOut", sum); field = new HashMap(); sum = new HashMap(); field.put("field", "Sum-DiskReadBytes"); sum.put(SUM, field); aggs.put("Avg-DiskReadinBytes", sum); field = new HashMap(); sum = new HashMap(); field.put("field", "Sum-DiskWriteBytes"); sum.put(SUM, field); aggs.put("Avg-DiskWriteinBytes", sum); field = new HashMap(); field.put("_key", "desc"); histogramDetails.put("field", "#Datetime-CPU-Utilization"); histogramDetails.put("interval", "day"); histogramDetails.put("format", "yyyy-MM-dd HH:mm:ss"); histogramDetails.put(ORDER, field); histogram.put("date_histogram", histogramDetails); histogram.put(AGGS, aggs); avgValuesperDay.put("avg-values-per-day", histogram); requestBody.put(QUERY, bool); requestBody.put(AGGS, avgValuesperDay); requestBody.put(SIZE, 0); Gson gson = new GsonBuilder().create(); try { String request = gson.toJson(requestBody, Object.class); String responseDetails = null; responseDetails = PacHttpUtils.doHttpPost(urlToQuery, request); JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); responseDetailsjson.get("aggregations"); Object aggregations = responseDetailsjson.get("aggregations"); String aggregationsstr = aggregations.toString(); parser = new JsonParser(); JsonObject aggregationsjson = (JsonObject) parser.parse(aggregationsstr); Object avgvalues = aggregationsjson.get("avg-values-per-day"); String avgvaluestr = avgvalues.toString(); parser = new JsonParser(); JsonObject avgvaluesjson = (JsonObject) parser.parse(avgvaluestr); Object bucketObj = avgvaluesjson.get(BUCKETS); String bucketstr = bucketObj.toString(); parser = new JsonParser(); JsonArray buckets = parser.parse(bucketstr).getAsJsonArray(); for (JsonElement jsonElement : buckets) { utilization = new HashMap<String, Object>(); JsonObject bucketdetails = jsonElement.getAsJsonObject(); utilization.put(DATE, bucketdetails.get("key_as_string").getAsString()); Object cpuUtilizationObj = bucketdetails.get(AVG_CPU_UTILIZATION); String cpuUtilizationStr = cpuUtilizationObj.toString(); parser = new JsonParser(); JsonObject cpuUtilizationjson = (JsonObject) parser.parse(cpuUtilizationStr); if (!cpuUtilizationjson.get("value").isJsonNull()) { utilization.put(CPU_UTILIZATION, cpuUtilizationjson.get("value").getAsDouble()); } else { utilization.put(CPU_UTILIZATION, 0); } Object networkInUtilizationObj = bucketdetails.get("Avg-NetworkIn"); String networkInUtilizationStr = networkInUtilizationObj.toString(); parser = new JsonParser(); JsonObject networkInUtilizationjson = (JsonObject) parser.parse(networkInUtilizationStr); if (!networkInUtilizationjson.get("value").isJsonNull()) { utilization.put(NETWORK_IN, networkInUtilizationjson.get("value").getAsLong()); } else { utilization.put(NETWORK_IN, 0); } Object networkOutUtilizationObj = bucketdetails.get("Avg-NetworkOut"); String networkOutUtilizationStr = networkOutUtilizationObj.toString(); parser = new JsonParser(); JsonObject networkOutUtilizationjson = (JsonObject) parser.parse(networkOutUtilizationStr); if (!networkOutUtilizationjson.get("value").isJsonNull()) { utilization.put(NETWORK_OUT, networkOutUtilizationjson.get("value").getAsLong()); } else { utilization.put(NETWORK_OUT, 0); } Object diskReadUtilizationObj = bucketdetails.get("Avg-DiskReadinBytes"); String diskReadUtilizationStr = diskReadUtilizationObj.toString(); parser = new JsonParser(); JsonObject diskReadUtilizationjson = (JsonObject) parser.parse(diskReadUtilizationStr); if (!diskReadUtilizationjson.get("value").isJsonNull()) { utilization.put(DISK_READ_IN_BYTES, diskReadUtilizationjson.get("value").getAsLong()); } else { utilization.put(DISK_READ_IN_BYTES, 0); } Object diskRWriteUtilizationObj = bucketdetails.get("Avg-DiskWriteinBytes"); String diskRWriteUtilizationStr = diskRWriteUtilizationObj.toString(); parser = new JsonParser(); JsonObject diskRWriteUtilizationjson = (JsonObject) parser.parse(diskRWriteUtilizationStr); if (!diskRWriteUtilizationjson.get("value").isJsonNull()) { utilization.put(DISK_WRITE_IN_BYTES, diskRWriteUtilizationjson.get("value").getAsLong()); } else { utilization.put(DISK_WRITE_IN_BYTES, 0); } utilizationList.add(utilization); } return utilizationList; } catch (Exception e) { LOGGER.error(ERROR_RETRIEVING_INVENTORY_FROM_ES, e); throw e; } }  @SuppressWarnings("unchecked") @Deprecated List<Map<String, Object>> getDataFromES(String dataSource, String targetType, Map<String, Object> mustFilter,
			final Map<String, Object> mustNotFilter, final HashMultimap<String, Object> shouldFilter,
			List<String> fields, Map<String, Object> mustTermsFilter); @SuppressWarnings("unchecked") List<Map<String, Object>> getSortedDataFromES(String dataSource, String targetType,
			Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, Map<String, Object> mustTermsFilter,
			List<Map<String, Object>> sortFieldMapList); Map<String, Object> buildAggs(String distributionName, int size, String aggsName, Map<String, Object> nestedAggs); Map<String, Object> buildQuery(final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, final String searchText,
			final Map<String, Object> mustTermsFilter,Map<String, List<String>> matchPhrasePrefix); String buildScrollRequest(String scrollId, String esPageScrollTtl); String processResponseAndSendTheScrollBack(String responseDetails, List<Map<String, Object>> results); @SuppressWarnings("unchecked") long getTotalDocumentCountForIndexAndType(String index, String type, Map<String, Object> mustFilter,
			Map<String, Object> mustNotFilter, HashMultimap<String, Object> shouldFilter, String searchText,
			Map<String, Object> mustTermsFilter); Map<String, Long> getTotalDistributionForIndexAndType(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String aggsFilter, int size, Map<String, Object> mustTermsFilter); String buildAggsURL(String url, String index, String type); List<Map<String, Object>> getUtilizationByAssetGroup(String asseGroup); @Deprecated List<Map<String, Object>> getDataFromESBySize(String dataSource, String targetType,
			final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, int from, int size, String searchText,
			final Map<String, Object> mustTermsFilter); List<Map<String, Object>> getSortedDataFromESBySize(String dataSource, String targetType,
			final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, int from, int size, String searchText,
			final Map<String, Object> mustTermsFilter, List<Map<String, Object>> sortFieldMapList); @SuppressWarnings("unchecked") Boolean updatePartialDataToES(String dataSource, String targetType, String id, String routing, String parent,
			Map<String, Object> partialDocument); @SuppressWarnings("unchecked") Boolean saveExceptionDataToES(String dataSource, String routing, Map<String, Object> data); Map<String, Long> getTotalDistributionForIndexAndTypeBySize(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String aggsFilter, int size, int from, String searchText); Map<String, Long> getDateHistogramForIndexAndTypeByInterval(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String dateFieldName, String interval); Map<String, String> getAccountsByMultiAggs(String index, String type, Map<String, Object> filter,
			Map<String, Object> mustNotFilter, HashMultimap<String, Object> shouldFilter,
			Map<String, Object> aggsFilter, int size); List<LinkedHashMap<String, Object>> getDetailsFromESBySize(String dataSource, String targetType,
			final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, int from, int size, String searchText,
			final Map<String, Object> mustTermsFilter); @SuppressWarnings("unchecked") @Deprecated List<Map<String, Object>> getDataFromESWithMustNotTermsFilter(String dataSource, String targetType,
			Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, Map<String, Object> mustTermsFilter,
			Map<String, Object> mustNotTermsFilter, Map<String, Object> mustNotWildCardFilter); @SuppressWarnings("unchecked") List<Map<String, Object>> getSortedDataFromESWithMustNotTermsFilter(String dataSource, String targetType,
			Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, Map<String, Object> mustTermsFilter,
			Map<String, Object> mustNotTermsFilter, Map<String, Object> mustNotWildCardFilter,
			List<Map<String, Object>> sortFieldMapList); long getTotalDocumentCountForIndexAndTypeWithMustNotTermsFilter(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String searchText, Map<String, Object> mustTermsFilter,
			Map<String, Object> mustNotTermsFilter, Map<String, Object> mustNotWildCardFilter); @SuppressWarnings("unchecked") long getTotalDistributionForIndexAndTypeWithMatchPhrase(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String searchText,
			Map<String, Object> mustTermsFilter,
			Map<String, List<String>> matchPhrasePrefix); String buildESURL(String url, String index, String type, int size, int from); Map<String, Object> getEnvAndTotalDistributionForIndexAndType(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String aggsFilter, Map<String, Object> nestedaggs, int size, Map<String, Object> mustTermsFilter); }
ElasticSearchRepository implements Constants { public List<Map<String, Object>> getUtilizationByAssetGroup(String asseGroup) throws Exception { String urlToQuery = buildAggsURL(esUrl, asseGroup, EC2_UTILIZATION); Map<String, Object> requestBody = new HashMap<String, Object>(); Map<String, Object> cpuUtilizationDetails = new HashMap<String, Object>(); Map<String, Object> cpuUtilization = new HashMap<String, Object>(); Map<String, Object> range = new HashMap<String, Object>(); Map<String, Object> must = new HashMap<String, Object>(); Map<String, Object> bool = new HashMap<String, Object>(); Map<String, Object> aggs = new HashMap<String, Object>(); Map<String, Object> histogram = new HashMap<String, Object>(); Map<String, Object> histogramDetails = new HashMap<String, Object>(); Map<String, Object> avgValuesperDay = new HashMap<String, Object>(); List<Map<String, Object>> utilizationList = new ArrayList<Map<String, Object>>(); Map<String, Object> utilization = null; Map<String, Object> field = null; Map<String, Object> sum = null; Map<String, Object> avg = null; cpuUtilizationDetails.put("gte", "now-30d"); cpuUtilizationDetails.put("lte", "now-1d"); cpuUtilizationDetails.put("format", "yyyy-MM-dd HH:mm:ss"); cpuUtilization.put("#Datetime-CPU-Utilization", cpuUtilizationDetails); range.put(RANGE, cpuUtilization); must.put(MUST, range); bool.put(BOOL, must); field = new HashMap(); avg = new HashMap(); field.put("field", AVG_CPU_UTILIZATION); avg.put(AVG, field); aggs.put(AVG_CPU_UTILIZATION, avg); field = new HashMap(); sum = new HashMap(); field.put("field", SUM_NETWORK_IN); sum.put(SUM, field); aggs.put("Avg-NetworkIn", sum); field = new HashMap(); sum = new HashMap(); field.put("field", SUM_NETWORK_OUT); sum.put(SUM, field); aggs.put("Avg-NetworkOut", sum); field = new HashMap(); sum = new HashMap(); field.put("field", "Sum-DiskReadBytes"); sum.put(SUM, field); aggs.put("Avg-DiskReadinBytes", sum); field = new HashMap(); sum = new HashMap(); field.put("field", "Sum-DiskWriteBytes"); sum.put(SUM, field); aggs.put("Avg-DiskWriteinBytes", sum); field = new HashMap(); field.put("_key", "desc"); histogramDetails.put("field", "#Datetime-CPU-Utilization"); histogramDetails.put("interval", "day"); histogramDetails.put("format", "yyyy-MM-dd HH:mm:ss"); histogramDetails.put(ORDER, field); histogram.put("date_histogram", histogramDetails); histogram.put(AGGS, aggs); avgValuesperDay.put("avg-values-per-day", histogram); requestBody.put(QUERY, bool); requestBody.put(AGGS, avgValuesperDay); requestBody.put(SIZE, 0); Gson gson = new GsonBuilder().create(); try { String request = gson.toJson(requestBody, Object.class); String responseDetails = null; responseDetails = PacHttpUtils.doHttpPost(urlToQuery, request); JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); responseDetailsjson.get("aggregations"); Object aggregations = responseDetailsjson.get("aggregations"); String aggregationsstr = aggregations.toString(); parser = new JsonParser(); JsonObject aggregationsjson = (JsonObject) parser.parse(aggregationsstr); Object avgvalues = aggregationsjson.get("avg-values-per-day"); String avgvaluestr = avgvalues.toString(); parser = new JsonParser(); JsonObject avgvaluesjson = (JsonObject) parser.parse(avgvaluestr); Object bucketObj = avgvaluesjson.get(BUCKETS); String bucketstr = bucketObj.toString(); parser = new JsonParser(); JsonArray buckets = parser.parse(bucketstr).getAsJsonArray(); for (JsonElement jsonElement : buckets) { utilization = new HashMap<String, Object>(); JsonObject bucketdetails = jsonElement.getAsJsonObject(); utilization.put(DATE, bucketdetails.get("key_as_string").getAsString()); Object cpuUtilizationObj = bucketdetails.get(AVG_CPU_UTILIZATION); String cpuUtilizationStr = cpuUtilizationObj.toString(); parser = new JsonParser(); JsonObject cpuUtilizationjson = (JsonObject) parser.parse(cpuUtilizationStr); if (!cpuUtilizationjson.get("value").isJsonNull()) { utilization.put(CPU_UTILIZATION, cpuUtilizationjson.get("value").getAsDouble()); } else { utilization.put(CPU_UTILIZATION, 0); } Object networkInUtilizationObj = bucketdetails.get("Avg-NetworkIn"); String networkInUtilizationStr = networkInUtilizationObj.toString(); parser = new JsonParser(); JsonObject networkInUtilizationjson = (JsonObject) parser.parse(networkInUtilizationStr); if (!networkInUtilizationjson.get("value").isJsonNull()) { utilization.put(NETWORK_IN, networkInUtilizationjson.get("value").getAsLong()); } else { utilization.put(NETWORK_IN, 0); } Object networkOutUtilizationObj = bucketdetails.get("Avg-NetworkOut"); String networkOutUtilizationStr = networkOutUtilizationObj.toString(); parser = new JsonParser(); JsonObject networkOutUtilizationjson = (JsonObject) parser.parse(networkOutUtilizationStr); if (!networkOutUtilizationjson.get("value").isJsonNull()) { utilization.put(NETWORK_OUT, networkOutUtilizationjson.get("value").getAsLong()); } else { utilization.put(NETWORK_OUT, 0); } Object diskReadUtilizationObj = bucketdetails.get("Avg-DiskReadinBytes"); String diskReadUtilizationStr = diskReadUtilizationObj.toString(); parser = new JsonParser(); JsonObject diskReadUtilizationjson = (JsonObject) parser.parse(diskReadUtilizationStr); if (!diskReadUtilizationjson.get("value").isJsonNull()) { utilization.put(DISK_READ_IN_BYTES, diskReadUtilizationjson.get("value").getAsLong()); } else { utilization.put(DISK_READ_IN_BYTES, 0); } Object diskRWriteUtilizationObj = bucketdetails.get("Avg-DiskWriteinBytes"); String diskRWriteUtilizationStr = diskRWriteUtilizationObj.toString(); parser = new JsonParser(); JsonObject diskRWriteUtilizationjson = (JsonObject) parser.parse(diskRWriteUtilizationStr); if (!diskRWriteUtilizationjson.get("value").isJsonNull()) { utilization.put(DISK_WRITE_IN_BYTES, diskRWriteUtilizationjson.get("value").getAsLong()); } else { utilization.put(DISK_WRITE_IN_BYTES, 0); } utilizationList.add(utilization); } return utilizationList; } catch (Exception e) { LOGGER.error(ERROR_RETRIEVING_INVENTORY_FROM_ES, e); throw e; } }  @SuppressWarnings("unchecked") @Deprecated List<Map<String, Object>> getDataFromES(String dataSource, String targetType, Map<String, Object> mustFilter,
			final Map<String, Object> mustNotFilter, final HashMultimap<String, Object> shouldFilter,
			List<String> fields, Map<String, Object> mustTermsFilter); @SuppressWarnings("unchecked") List<Map<String, Object>> getSortedDataFromES(String dataSource, String targetType,
			Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, Map<String, Object> mustTermsFilter,
			List<Map<String, Object>> sortFieldMapList); Map<String, Object> buildAggs(String distributionName, int size, String aggsName, Map<String, Object> nestedAggs); Map<String, Object> buildQuery(final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, final String searchText,
			final Map<String, Object> mustTermsFilter,Map<String, List<String>> matchPhrasePrefix); String buildScrollRequest(String scrollId, String esPageScrollTtl); String processResponseAndSendTheScrollBack(String responseDetails, List<Map<String, Object>> results); @SuppressWarnings("unchecked") long getTotalDocumentCountForIndexAndType(String index, String type, Map<String, Object> mustFilter,
			Map<String, Object> mustNotFilter, HashMultimap<String, Object> shouldFilter, String searchText,
			Map<String, Object> mustTermsFilter); Map<String, Long> getTotalDistributionForIndexAndType(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String aggsFilter, int size, Map<String, Object> mustTermsFilter); String buildAggsURL(String url, String index, String type); List<Map<String, Object>> getUtilizationByAssetGroup(String asseGroup); @Deprecated List<Map<String, Object>> getDataFromESBySize(String dataSource, String targetType,
			final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, int from, int size, String searchText,
			final Map<String, Object> mustTermsFilter); List<Map<String, Object>> getSortedDataFromESBySize(String dataSource, String targetType,
			final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, int from, int size, String searchText,
			final Map<String, Object> mustTermsFilter, List<Map<String, Object>> sortFieldMapList); @SuppressWarnings("unchecked") Boolean updatePartialDataToES(String dataSource, String targetType, String id, String routing, String parent,
			Map<String, Object> partialDocument); @SuppressWarnings("unchecked") Boolean saveExceptionDataToES(String dataSource, String routing, Map<String, Object> data); Map<String, Long> getTotalDistributionForIndexAndTypeBySize(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String aggsFilter, int size, int from, String searchText); Map<String, Long> getDateHistogramForIndexAndTypeByInterval(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String dateFieldName, String interval); Map<String, String> getAccountsByMultiAggs(String index, String type, Map<String, Object> filter,
			Map<String, Object> mustNotFilter, HashMultimap<String, Object> shouldFilter,
			Map<String, Object> aggsFilter, int size); List<LinkedHashMap<String, Object>> getDetailsFromESBySize(String dataSource, String targetType,
			final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, int from, int size, String searchText,
			final Map<String, Object> mustTermsFilter); @SuppressWarnings("unchecked") @Deprecated List<Map<String, Object>> getDataFromESWithMustNotTermsFilter(String dataSource, String targetType,
			Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, Map<String, Object> mustTermsFilter,
			Map<String, Object> mustNotTermsFilter, Map<String, Object> mustNotWildCardFilter); @SuppressWarnings("unchecked") List<Map<String, Object>> getSortedDataFromESWithMustNotTermsFilter(String dataSource, String targetType,
			Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, Map<String, Object> mustTermsFilter,
			Map<String, Object> mustNotTermsFilter, Map<String, Object> mustNotWildCardFilter,
			List<Map<String, Object>> sortFieldMapList); long getTotalDocumentCountForIndexAndTypeWithMustNotTermsFilter(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String searchText, Map<String, Object> mustTermsFilter,
			Map<String, Object> mustNotTermsFilter, Map<String, Object> mustNotWildCardFilter); @SuppressWarnings("unchecked") long getTotalDistributionForIndexAndTypeWithMatchPhrase(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String searchText,
			Map<String, Object> mustTermsFilter,
			Map<String, List<String>> matchPhrasePrefix); String buildESURL(String url, String index, String type, int size, int from); Map<String, Object> getEnvAndTotalDistributionForIndexAndType(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String aggsFilter, Map<String, Object> nestedaggs, int size, Map<String, Object> mustTermsFilter);  }