class CertificateRepository implements Constants {

    public Map<String, Object> getCertificatesExpiryByApplication(
            String assetGroup) throws DataException {

        Map<String, Object> expiryRules = new HashMap<>();

        StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(
                assetGroup);
        urlToQuery.append(CERT_SEARCH);
        String requestBody = "{\"size\":0,\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":"
                + TEN_THOUSAND
                + "},"
                + "\"aggs\":{\"certs\":{\"children\":{\"type\":\"issue_cert\"},\"aggs\":{\"openfilter\":{\"filter\":{\"term\":{\"issueStatus\":\"open\"}},"
                + "\"aggs\":{\"rules\":{\"terms\":{\"field\":\"ruleId.keyword\",\"size\":10}}}}}}}}}}";

        String responseJson = "";
        try {
            responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(),
                    requestBody);
        } catch (Exception e) {
            LOGGER.error("Error in getVulnerabilitiesDistribution from ES", e);
            throw new DataException(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++) {
                String appName = outerBuckets.get(i).getAsJsonObject()
                        .get("key").getAsString();
                Map<String, Integer> expiryDetails = new HashMap<>();
                expiryDetails.put(EXP_IN_45_DAYS, 0);
                expiryDetails.put(EXP_IN_30_DAYS, 0);
                JsonArray rules = outerBuckets.get(i).getAsJsonObject()
                        .getAsJsonObject("certs").getAsJsonObject("openfilter")
                        .getAsJsonObject("rules").getAsJsonArray(BUCKETS);
                if (rules.size() > 0) {
                    for (int j = 0; j < rules.size(); j++) {
                        if (SSL_CERT_45_DAYS_EXP_RULE.equals(rules.get(j)
                                .getAsJsonObject().get("key").getAsString())) {
                            expiryDetails.put(
                                    EXP_IN_45_DAYS,
                                    Integer.valueOf(rules.get(j)
                                            .getAsJsonObject().get(DOC_COUNT)
                                            .toString()));
                        }
                        if (SSL_CERT_30_DAYS_EXP_RULE.equals(rules.get(j)
                                .getAsJsonObject().get("key").getAsString())) {
                            expiryDetails.put(
                                    EXP_IN_30_DAYS,
                                    Integer.valueOf(rules.get(j)
                                            .getAsJsonObject().get(DOC_COUNT)
                                            .toString()));
                        }
                    }
                }
                if (StringUtils.isNotBlank(appName)) {
                    expiryRules.put(appName, expiryDetails);
                }
            }
        }
        return expiryRules;
    }

}