class VulnerabilityRepository implements Constants {

	public Map<String, Long> getAssetsAffectedCount(String assetGroup, Map<String, String> filter, String parentType) {

		Map<String, Long> assetsAffectedCount = new HashMap<>();
		Map<String, String> filterForQuery = new HashMap<>();
		if (!CollectionUtils.isEmpty(filter)) {
			filterForQuery = new HashMap<>(filter);
		}

		StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup);
		urlToQuery.append("/").append(VULN_INFO);
		urlToQuery.append("/").append(SEARCH);
		String responseJson = "";
		try {
			String severity = SEVERITY_LEVELS;
			if (filterForQuery.containsKey(SEVEITY_LEVEL)) {
				severity = filterForQuery.get(SEVEITY_LEVEL);
				filterForQuery.remove(SEVEITY_LEVEL);
			}

			StringBuilder requestBody = new StringBuilder(
					"{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}},{\"terms\":{\"severitylevel.keyword\":[");
			requestBody.append(severity + "]}},");
			requestBody.append("{\"has_parent\":{\"parent_type\":\"" + parentType
					+ "\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}}");

			if (!filterForQuery.isEmpty()) {
				requestBody.append(",{\"match\":");
				requestBody.append(new GsonBuilder().create().toJson(filterForQuery));
				requestBody.append("}");
			}
			requestBody.append("]}}}}]}},\"aggs\":{\"qid\":{\"terms\":{\"field\":\"qid.keyword\",\"size\":");
			requestBody.append(ES_PAGE_SIZE);
			requestBody.append("}}}}");
			responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString());
		} catch (Exception e) {
			LOGGER.error("Error in getAssetsAffectedCount from ES", e);
		}
		JsonParser jsonParser = new JsonParser();
		if (StringUtils.isNotEmpty(responseJson)) {
			JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson);
			JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString());
			JsonArray outerBuckets = aggsJson.getAsJsonObject("qid").getAsJsonArray(BUCKETS);
			if (outerBuckets.size() > 0) {
				for (int i = 0; i < outerBuckets.size(); i++) {
					assetsAffectedCount.put(
							String.valueOf(outerBuckets.get(i).getAsJsonObject().get("key").getAsLong()),
							outerBuckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong());
				}
			}
		}
		return assetsAffectedCount;
	}

}