class CostRepository {

	public List<Map<String,Object>> getCostAggsWithTT(List<String> appNameList, List<String> tTypeList) 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\":{\"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},");
		requestBody.append( "\"aggs\":{\"COST\":{\"sum\":{\"script\":{\"inline\":\" double cost =0.0 ; for(int i=0; i<params._source.costInfo.length; i++){ if(params._source.costInfo[i].type=='");
		requestBody.append(tTypeList.get(0));
		requestBody.append("' ){ cost = params._source.costInfo[i].cost ; break;}} return cost;\"}}}}}}}}}");
		
		String responseDetails;
		try {
			responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString());
		} catch (Exception e) {
			throw new DataException(e);
		}

        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();
        
        int currentYear = LocalDate.now().getYear();
        int currentMonth = LocalDate.now().getMonthValue();
        
        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;
	}

}