class VulnerabilityRepository implements Constants {

	public List<Map<String, Object>> getVulnerabilityTrend(String assetGroup, Map<String, String> filter, Date from,
			Date to) throws Exception {
		List<Map<String, Object>> vulnTrendList = new ArrayList<>();
		try {

			StringBuilder urlToQuery = new StringBuilder(esUrl).append("/assetgroup_stats/count_vuln/_search");
			StringBuilder request = new StringBuilder(
					"{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"ag.keyword\":" + "\"" + assetGroup
							+ "\"}}");

			if (filter != null) {
				Set<String> filterkeys = filter.keySet();
				if (filterkeys.contains(TAGS_APPS)) {
					request.append(
							",{ \"match\": {\"tags.Application.keyword\": " + "\"" + filter.get(TAGS_APPS) + "\"}}");
				}
				if (filterkeys.contains("tags.Environment.keyword")) {
					request.append(",{ \"match\": {\"tags.Environment.keyword\": " + "\""
							+ filter.get("tags.Environment.keyword") + "\"}}");
				}
			}
			String gte = null;
			String lte = null;

			if (from != null) {
				gte = "\"gte\": \"" + new SimpleDateFormat("yyyy-MM-dd").format(from) + "\"";
			}
			if (to != null) {
				lte = "\"lte\": \"" + new SimpleDateFormat("yyyy-MM-dd").format(to) + "\"";
			}

			if (gte != null && lte != null) {
				request.append(",{ \"range\": {\"date\": {" + gte + "," + lte + "}}}");
			} else if (gte != null) {
				request.append(",{ \"range\": {\"date\": {" + gte + "}}}");
			} else {
				request.append(",{ \"range\": {\"date\": {" + lte + "}}}");
			}

			request.append(
					"]}},\"aggs\": {\"date\": {\"date_histogram\": {\"field\": \"date\",\"interval\": \"day\",\"format\": \"yyyy-MM-dd\"},\"aggs\": {\"vulns\": {\"sum\": {\"field\": \"count\"}}}}}}");

			String responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), request.toString());
			JsonParser jsonParser = new JsonParser();
			JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson);
			JsonArray buckets = resultJson.get(AGGREGATIONS).getAsJsonObject().get("date").getAsJsonObject()
					.get(BUCKETS).getAsJsonArray();
			if (buckets.size() > 0) {
				for (int i = 0; i < buckets.size(); i++) {
					Map<String, Object> trend = new HashMap<>();
					JsonObject bucket = (JsonObject) buckets.get(i);
					String date = bucket.get("key_as_string").getAsString();
					Long count = bucket.get(VULN).getAsJsonObject().get(VALUE).getAsLong();
					trend.put("date", date);
					trend.put(COUNT, count);
					vulnTrendList.add(trend);
				}
			}
		} catch (Exception e) {
			LOGGER.error("Error in getVulnerabilityTrend from ES", e);
			throw e;
		}

		return vulnTrendList;
	}

}