class VulnerabilityRepository implements Constants {

	public List<Map<String, Object>> getVulnerabilitiesDistribution(String assetGroup, String parentType)
			throws Exception {
		List<Map<String, Object>> vulnDistributions = new ArrayList<>();

		StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup);
		urlToQuery.append("/").append(parentType).append("/_search");
		String requestBody = "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}}]}},\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":1000},"
				+ "\"aggs\":{\"envs\":{\"terms\":{\"field\":\"tags.Environment.keyword\",\"size\":1000},\"aggs\":{\"vulns\":{\"children\":{\"type\":\"vulninfo\"},"
				+ "\"aggs\":{\"NAME\":{\"filters\":{\"filters\":{\"S3\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":3}},{\"match\":{\"latest\":true}}]}},\"S4\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":4}},{\"match\":{\"latest\":true}}]}},\"S5\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":5}},{\"match\":{\"latest\":true}}]}}}}}}}}}}}}}";
		String responseJson = "";
		try {
			responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody);
		} catch (Exception e) {
			LOGGER.error("Error in getVulnerabilitiesDistribution from ES", e);
			throw e;
		}

		JsonParser jsonParser = new JsonParser();
		JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson);
		JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString());
		JsonArray outerBuckets = aggsJson.getAsJsonObject("apps").getAsJsonArray(BUCKETS);
		if (outerBuckets.size() > 0) {
			for (int i = 0; i < outerBuckets.size(); i++) {
				Map<String, Object> applist = new HashMap<>();
				String appName = outerBuckets.get(i).getAsJsonObject().get("key").getAsString();
				JsonArray envs = outerBuckets.get(i).getAsJsonObject().getAsJsonObject("envs").getAsJsonArray(BUCKETS);
				List<Map<String, Object>> envDetails = new ArrayList<>();
				if (envs.size() > 0) {
					for (int j = 0; j < envs.size(); j++) {
						String envName = envs.get(j).getAsJsonObject().get("key").getAsString();
						List<Map<String, Object>> severityInfo = getSeverityInfo(envs.get(j).getAsJsonObject()
								.getAsJsonObject(VULN).getAsJsonObject("NAME").getAsJsonObject(BUCKETS), null);
						Map<String, Object> envSeverity = new HashMap<>();
						envSeverity.put("environment", envName);
						envSeverity.put(SEVERITY_INFO, severityInfo);
						envSeverity.put(VULNEREBILITIES,
								Integer.valueOf(severityInfo.get(0).get(COUNT).toString())
										+ Integer.valueOf(severityInfo.get(ONE).get(COUNT).toString())
										+ Integer.valueOf(severityInfo.get(TWO).get(COUNT).toString()));
						envDetails.add(envSeverity);
					}
				}
				applist.put(APPS, appName);
				applist.put("applicationInfo", envDetails);
				vulnDistributions.add(applist);
			}
		}
		return vulnDistributions;
	}

}