class VulnerabilityRepository implements Constants {

	public Map<String, Long> getProdInfoByEnv(String assetGroup, String severitylevel) {

		Map<String, Long> prodInfo = new HashMap<>();
		StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup);
		urlToQuery.append("/").append(SEARCH);

		StringBuilder requestbody = new StringBuilder(
				"{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},");
		requestbody.append("{\"terms\":{\"severitylevel.keyword\":[%s]}}]}}}}],")
				.append("\"should\":[{\"prefix\":{\"tags.Environment.keyword\":\"Production\"}},")
				.append("{\"prefix\":{\"tags.Environment.keyword\":\"production\"}},")
				.append("{\"prefix\":{\"tags.Environment.keyword\":\"Prd\"}},")
				.append("{\"prefix\":{\"tags.Environment.keyword\":\"prd\"}},")
				.append("{\"prefix\":{\"tags.Environment.keyword\":\"PRD\"}},")
				.append("{\"prefix\":{\"tags.Environment.keyword\":\"Prod\"}},")
				.append("{\"prefix\":{\"tags.Environment.keyword\":\"PROD\"}}],")
				.append("\"minimum_should_match\":1}},")
				.append("\"aggs\":{\"NAME\":{\"children\":{\"type\":\"vulninfo\"},")
				.append("\"aggs\":{\"NAME\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}},")
				.append("\"aggs\":{\"NAME\":{\"cardinality\":{\"script\":\"doc['qid.keyword'].toString().replace('.0','')\",\"precision_threshold\": 40000}}}}}}}}");
		String requestJson = String.format(requestbody.toString(), severitylevel, severitylevel);
		String responseJson = "";
		try {
			responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestJson);
		} catch (Exception e) {
			LOGGER.error("Error in getProdInfoByEnv", e);
		}

		JsonParser jsonParser = new JsonParser();
		if (StringUtils.isNotEmpty(responseJson)) {
			JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson);
			JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get(HITS).toString());
			JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString());
			long totalVulnerableAssets = hitsJson.get(TOTAL).getAsLong();
			long vulnerabilities = aggsJson.getAsJsonObject(NAME.toUpperCase()).getAsJsonObject(NAME.toUpperCase())
					.get(DOC_COUNT).getAsLong();
			long uniqueVulnCount = aggsJson.getAsJsonObject(NAME.toUpperCase()).getAsJsonObject(NAME.toUpperCase())
					.getAsJsonObject(NAME.toUpperCase()).get(VALUE).getAsLong();

			prodInfo.put(TOTAL_VULN_ASSETS, totalVulnerableAssets);
			prodInfo.put(VULNEREBILITIES, vulnerabilities);
			prodInfo.put(UNIQUE_VULN_COUNT, uniqueVulnCount);
		}

		return prodInfo;

	}

}