class RecommendationsRepository {

    public List<Map<String,Object>> getRecommendationSummary(String assetGroup, String application) throws DataException {
    	
    	List<Map<String,Object>> recommendationSummary = new ArrayList<>();
    	StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup).append("/").append(Constants.SEARCH);
		StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}}");
		if(StringUtils.isNotBlank(application)) {
			requestBody.append(",{\"match\":{\"tags.Application.keyword\":\"");
			requestBody.append(application);
			requestBody.append("\"}}");
		}
		requestBody.append("]}},\"aggs\":{\"recommendations\":{\"children\":{\"type\":\"recommendation\"},\"aggs\":{\"latest\":{\"filter\":{\"term\":{\"latest\":\"true\"}},"
				+ "\"aggs\":{\"category\":{\"terms\":{\"field\":\"category.keyword\",\"size\":1000},\"aggs\":{\"savings\":{\"sum\":{\"field\":\"monthlysavings\"}}}}}}}}}}");
		String responseDetails;
		try {
			responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString());
		} catch (Exception e) {
			LOGGER.error("Error while fetching recommendation summary from ES ", e);
			throw new DataException(e);
		}
        JsonParser parser = new JsonParser();
        JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject();
        JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject();
        boolean dataAvailable = false;
        if(aggregations != null) {
        	JsonObject recommendations = aggregations.get("recommendations").getAsJsonObject();
        	if(recommendations.has("latest") && recommendations.get("latest").getAsJsonObject().has("category")) {
				JsonArray categoryBuckets = recommendations.get("latest").getAsJsonObject().get("category")
						.getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray();
				if (categoryBuckets.size() > 0) {
					dataAvailable = true;
                    for (int i=0; i<categoryBuckets.size();i++) {
                        JsonObject categoryObj = (JsonObject) categoryBuckets.get(i);
                        if (categoryObj != null) {
                        	Map<String,Object> category = new HashMap<>();
                        	category.put("category", categoryObj.get("key").getAsString());
                        	category.put("recommendations", categoryObj.get("doc_count").getAsLong());
                        	JsonObject savingsObj = categoryObj.get("savings").getAsJsonObject();
                        	if(savingsObj.size() != 0) {
                        		long potentialMonthlySavings = Math.round(savingsObj.get("value").getAsDouble());
                        		if(potentialMonthlySavings > 0) {
                        			category.put("potentialMonthlySavings", potentialMonthlySavings);
                        		}
                        	}
                        	recommendationSummary.add(category);
                        }
                    }
                }
        	}
        	
        }
        if(!dataAvailable) {
    		//for azure there are no recommendations currently, so passing 0 values for azure asset group
    		String[] categories = recommendationCategories.split(",");
    		for(int i=0; i < categories.length; i++) {
    			Map<String,Object> category = new HashMap<>();
            	category.put("category", categories[i]);
            	category.put("recommendations", 0L);
            	recommendationSummary.add(category);
    		}
    	}
        
        return recommendationSummary;
    }

}