@Test public void getCostAggsTest_Exception() throws Exception { mockStatic(PacHttpUtils.class); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenThrow(new Exception()); assertThatThrownBy(() -> costRepository.getCostAggs(Arrays.asList("app"))) .isInstanceOf(Exception.class); }
public List<Map<String,Object>> getCostAggs(List<String> appNameList) throws DataException { List<Map<String,Object>> costTrend = new ArrayList<>(); Map<String, Integer> latestCostFinalised = findLatestCostFinalisedMonth(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append("aws-cost/monthly-cost").append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"terms\":{\"application.keyword\":"); requestBody.append((new Gson().toJson(appNameList))); requestBody.append("}}]}},\"aggs\":{\"YEAR\":{\"terms\":{\"field\":\"year\",\"size\":10000},\"aggs\":{\"MONTH\":{\"terms\":{\"field\":\"month\",\"size\":12}," + "\"aggs\":{\"COST\":{\"sum\":{\"field\":\"totalCost\"}}}}}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { throw new DataException(e); } int currentYear = LocalDate.now().getYear(); int currentMonth = LocalDate.now().getMonthValue(); JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); JsonArray yearBuckets = aggregations.get("YEAR").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (yearBuckets.size() > 0) { for (int i=0; i<yearBuckets.size();i++) { JsonObject yearObj = (JsonObject) yearBuckets.get(i); if (yearObj != null) { int year = yearObj.get("key").getAsInt(); JsonArray monthBucket = yearObj.get("MONTH").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); for(int j=0;j< monthBucket.size();j++) { Map<String,Object> trendMap = new HashMap<>(); trendMap.put("year",year); JsonObject monthObj = (JsonObject) monthBucket.get(j); trendMap.put("month",monthObj.get("key").getAsInt()); trendMap.put("cost",Math.round(monthObj.get("COST").getAsJsonObject().get("value").getAsDouble())); if(year==currentYear && currentMonth== monthObj.get("key").getAsInt()) { trendMap.put("costStatus","accumulated"); }else { trendMap.put("costStatus",checkForFinalised(latestCostFinalised.get("year"),latestCostFinalised.get("month"),year,monthObj.get("key").getAsInt())?"finalised":"estimated"); } costTrend.add(trendMap); } } } } return costTrend; }
CostRepository { public List<Map<String,Object>> getCostAggs(List<String> appNameList) throws DataException { List<Map<String,Object>> costTrend = new ArrayList<>(); Map<String, Integer> latestCostFinalised = findLatestCostFinalisedMonth(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append("aws-cost/monthly-cost").append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"terms\":{\"application.keyword\":"); requestBody.append((new Gson().toJson(appNameList))); requestBody.append("}}]}},\"aggs\":{\"YEAR\":{\"terms\":{\"field\":\"year\",\"size\":10000},\"aggs\":{\"MONTH\":{\"terms\":{\"field\":\"month\",\"size\":12}," + "\"aggs\":{\"COST\":{\"sum\":{\"field\":\"totalCost\"}}}}}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { throw new DataException(e); } int currentYear = LocalDate.now().getYear(); int currentMonth = LocalDate.now().getMonthValue(); JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); JsonArray yearBuckets = aggregations.get("YEAR").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (yearBuckets.size() > 0) { for (int i=0; i<yearBuckets.size();i++) { JsonObject yearObj = (JsonObject) yearBuckets.get(i); if (yearObj != null) { int year = yearObj.get("key").getAsInt(); JsonArray monthBucket = yearObj.get("MONTH").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); for(int j=0;j< monthBucket.size();j++) { Map<String,Object> trendMap = new HashMap<>(); trendMap.put("year",year); JsonObject monthObj = (JsonObject) monthBucket.get(j); trendMap.put("month",monthObj.get("key").getAsInt()); trendMap.put("cost",Math.round(monthObj.get("COST").getAsJsonObject().get("value").getAsDouble())); if(year==currentYear && currentMonth== monthObj.get("key").getAsInt()) { trendMap.put("costStatus","accumulated"); }else { trendMap.put("costStatus",checkForFinalised(latestCostFinalised.get("year"),latestCostFinalised.get("month"),year,monthObj.get("key").getAsInt())?"finalised":"estimated"); } costTrend.add(trendMap); } } } } return costTrend; } }
CostRepository { public List<Map<String,Object>> getCostAggs(List<String> appNameList) throws DataException { List<Map<String,Object>> costTrend = new ArrayList<>(); Map<String, Integer> latestCostFinalised = findLatestCostFinalisedMonth(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append("aws-cost/monthly-cost").append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"terms\":{\"application.keyword\":"); requestBody.append((new Gson().toJson(appNameList))); requestBody.append("}}]}},\"aggs\":{\"YEAR\":{\"terms\":{\"field\":\"year\",\"size\":10000},\"aggs\":{\"MONTH\":{\"terms\":{\"field\":\"month\",\"size\":12}," + "\"aggs\":{\"COST\":{\"sum\":{\"field\":\"totalCost\"}}}}}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { throw new DataException(e); } int currentYear = LocalDate.now().getYear(); int currentMonth = LocalDate.now().getMonthValue(); JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); JsonArray yearBuckets = aggregations.get("YEAR").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (yearBuckets.size() > 0) { for (int i=0; i<yearBuckets.size();i++) { JsonObject yearObj = (JsonObject) yearBuckets.get(i); if (yearObj != null) { int year = yearObj.get("key").getAsInt(); JsonArray monthBucket = yearObj.get("MONTH").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); for(int j=0;j< monthBucket.size();j++) { Map<String,Object> trendMap = new HashMap<>(); trendMap.put("year",year); JsonObject monthObj = (JsonObject) monthBucket.get(j); trendMap.put("month",monthObj.get("key").getAsInt()); trendMap.put("cost",Math.round(monthObj.get("COST").getAsJsonObject().get("value").getAsDouble())); if(year==currentYear && currentMonth== monthObj.get("key").getAsInt()) { trendMap.put("costStatus","accumulated"); }else { trendMap.put("costStatus",checkForFinalised(latestCostFinalised.get("year"),latestCostFinalised.get("month"),year,monthObj.get("key").getAsInt())?"finalised":"estimated"); } costTrend.add(trendMap); } } } } return costTrend; }  }
CostRepository { public List<Map<String,Object>> getCostAggs(List<String> appNameList) throws DataException { List<Map<String,Object>> costTrend = new ArrayList<>(); Map<String, Integer> latestCostFinalised = findLatestCostFinalisedMonth(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append("aws-cost/monthly-cost").append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"terms\":{\"application.keyword\":"); requestBody.append((new Gson().toJson(appNameList))); requestBody.append("}}]}},\"aggs\":{\"YEAR\":{\"terms\":{\"field\":\"year\",\"size\":10000},\"aggs\":{\"MONTH\":{\"terms\":{\"field\":\"month\",\"size\":12}," + "\"aggs\":{\"COST\":{\"sum\":{\"field\":\"totalCost\"}}}}}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { throw new DataException(e); } int currentYear = LocalDate.now().getYear(); int currentMonth = LocalDate.now().getMonthValue(); JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); JsonArray yearBuckets = aggregations.get("YEAR").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (yearBuckets.size() > 0) { for (int i=0; i<yearBuckets.size();i++) { JsonObject yearObj = (JsonObject) yearBuckets.get(i); if (yearObj != null) { int year = yearObj.get("key").getAsInt(); JsonArray monthBucket = yearObj.get("MONTH").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); for(int j=0;j< monthBucket.size();j++) { Map<String,Object> trendMap = new HashMap<>(); trendMap.put("year",year); JsonObject monthObj = (JsonObject) monthBucket.get(j); trendMap.put("month",monthObj.get("key").getAsInt()); trendMap.put("cost",Math.round(monthObj.get("COST").getAsJsonObject().get("value").getAsDouble())); if(year==currentYear && currentMonth== monthObj.get("key").getAsInt()) { trendMap.put("costStatus","accumulated"); }else { trendMap.put("costStatus",checkForFinalised(latestCostFinalised.get("year"),latestCostFinalised.get("month"),year,monthObj.get("key").getAsInt())?"finalised":"estimated"); } costTrend.add(trendMap); } } } } return costTrend; }  List<Map<String,Object>> getCostAggs(List<String> appNameList); List<Map<String,Object>> getCostAggsWithTT(List<String> appNameList, List<String> tTypeList); Map<String,Integer> findLatestCostFinalisedMonth(); List<String> fetchApplicationMasterList(); }
CostRepository { public List<Map<String,Object>> getCostAggs(List<String> appNameList) throws DataException { List<Map<String,Object>> costTrend = new ArrayList<>(); Map<String, Integer> latestCostFinalised = findLatestCostFinalisedMonth(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append("aws-cost/monthly-cost").append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"terms\":{\"application.keyword\":"); requestBody.append((new Gson().toJson(appNameList))); requestBody.append("}}]}},\"aggs\":{\"YEAR\":{\"terms\":{\"field\":\"year\",\"size\":10000},\"aggs\":{\"MONTH\":{\"terms\":{\"field\":\"month\",\"size\":12}," + "\"aggs\":{\"COST\":{\"sum\":{\"field\":\"totalCost\"}}}}}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { throw new DataException(e); } int currentYear = LocalDate.now().getYear(); int currentMonth = LocalDate.now().getMonthValue(); JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); JsonArray yearBuckets = aggregations.get("YEAR").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (yearBuckets.size() > 0) { for (int i=0; i<yearBuckets.size();i++) { JsonObject yearObj = (JsonObject) yearBuckets.get(i); if (yearObj != null) { int year = yearObj.get("key").getAsInt(); JsonArray monthBucket = yearObj.get("MONTH").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); for(int j=0;j< monthBucket.size();j++) { Map<String,Object> trendMap = new HashMap<>(); trendMap.put("year",year); JsonObject monthObj = (JsonObject) monthBucket.get(j); trendMap.put("month",monthObj.get("key").getAsInt()); trendMap.put("cost",Math.round(monthObj.get("COST").getAsJsonObject().get("value").getAsDouble())); if(year==currentYear && currentMonth== monthObj.get("key").getAsInt()) { trendMap.put("costStatus","accumulated"); }else { trendMap.put("costStatus",checkForFinalised(latestCostFinalised.get("year"),latestCostFinalised.get("month"),year,monthObj.get("key").getAsInt())?"finalised":"estimated"); } costTrend.add(trendMap); } } } } return costTrend; }  List<Map<String,Object>> getCostAggs(List<String> appNameList); List<Map<String,Object>> getCostAggsWithTT(List<String> appNameList, List<String> tTypeList); Map<String,Integer> findLatestCostFinalisedMonth(); List<String> fetchApplicationMasterList();  }