class RecommendationsRepository {

    public Map<String,Object> getSummaryByApplication(String assetGroup, String category) throws DataException {
    	
    	Map<String,Object> result = new HashMap<>();
		List<Map<String,Object>> summaryByApplication = new ArrayList<>();
		Double totalMonthlySavings = 0.0;
		
    	StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup).append("/").append(Constants.SEARCH);
		StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}},{\"match\":{\"_entity.keyword\":true}}]}},"
				+ "\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":100000},\"aggs\":{\"recommendations\":{\"children\":{\"type\":\"recommendation\"},"
				+ "\"aggs\":{\"latest\":{\"filter\":{\"match\":{\"latest\":true}},\"aggs\":{\"category\":{\"filter\":{\"match\":{\"category.keyword\":\"");
		requestBody.append(category);
		requestBody.append("\"}},\"aggs\":{\"savings\":{\"sum\":{\"field\":\"monthlysavings\"}}}}}}}}}}}}");
		String responseDetails;
		try {
			responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString());
		} catch (Exception e) {
			LOGGER.error("Error in getSummaryByApplication "+e);
			throw new DataException(e);
		}
        JsonParser parser = new JsonParser();
        JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject();
        JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject();
        JsonArray appsBuckets = aggregations.get("apps").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray();
        if (appsBuckets.size() > 0) {
            for (int i=0; i<appsBuckets.size();i++) {
            	JsonObject appObj = (JsonObject) appsBuckets.get(i);
                if (appObj != null) {
            		JsonObject recommendationObj = appObj.get("recommendations").getAsJsonObject();
            		if(recommendationObj.has("latest") && recommendationObj.get("latest").getAsJsonObject().has("category")) {
            			Map<String,Object> app = new HashMap<>();
                		app.put("application", appObj.get(Constants.KEY).getAsString());
            			JsonObject categoryObj = recommendationObj.get("latest").getAsJsonObject().get("category").getAsJsonObject();
                		app.put("recommendations", categoryObj.get(Constants.DOC_COUNT).getAsLong());
                		if("cost_optimizing".equals(category)) {
                			Double monthlySavings = categoryObj.get("savings").getAsJsonObject().get(Constants.VALUE).getAsDouble();
                    		totalMonthlySavings += monthlySavings;
                    		app.put("monthlySavings", Math.round(monthlySavings));
                		}
                		if( categoryObj.get(Constants.DOC_COUNT).getAsLong() > 0) {
                			summaryByApplication.add(app);
                		}
            		}
                }
            }
        }
		
		result.put("ag", assetGroup);
		result.put("category", category);
		if("cost_optimizing".equals(category)) {
			result.put("totalMonthlySavings", Math.round(totalMonthlySavings));
		}
		result.put("applications", summaryByApplication);
		return result;
    }

}