Example usage for org.joda.time LocalDateTime minusYears

List of usage examples for org.joda.time LocalDateTime minusYears

Introduction

In this page you can find the example usage for org.joda.time LocalDateTime minusYears.

Prototype

public LocalDateTime minusYears(int years) 

Source Link

Document

Returns a copy of this datetime minus the specified number of years.

Usage

From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRestClientImpl.java

License:Open Source License

/**
 * This method finds the API Destination usage of APIs
 *
 * @param tableName Name of the table where the data exist
 * @param providerName Name of the provider
 * @param fromDate  starting date// w w  w .jav a  2s.  c o  m
 * @param toDate    ending date
 * @return list of APIUsageByDestination
 * @throws APIMgtUsageQueryServiceClientException throws if error occurred
 */
private List<APIUsageByDestination> getAPIUsageByDestinationData(String tableName, String providerName,
        String fromDate, String toDate) throws APIMgtUsageQueryServiceClientException {

    List<APIUsageByDestination> usageByDestination = new ArrayList<APIUsageByDestination>();
    String tenantDomain = MultitenantUtils.getTenantDomain(providerName);
    try {
        String granularity = APIUsageStatisticsClientConstants.HOURS_GRANULARITY;//default granularity

        Map<String, Integer> durationBreakdown = this.getDurationBreakdown(fromDate, toDate);
        LocalDateTime currentDate = LocalDateTime.now(DateTimeZone.UTC);
        DateTimeFormatter formatter = DateTimeFormat
                .forPattern(APIUsageStatisticsClientConstants.TIMESTAMP_PATTERN);
        LocalDateTime fromLocalDateTime = LocalDateTime.parse(fromDate, formatter);//GMT time

        if (fromLocalDateTime.isBefore(currentDate.minusYears(1))) {
            granularity = APIUsageStatisticsClientConstants.MONTHS_GRANULARITY;
        } else if ((durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_MONTHS) > 0)
                || (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_DAYS) > 0)) {
            granularity = APIUsageStatisticsClientConstants.DAYS_GRANULARITY;
        }

        String query = "from " + tableName + " on("
                + APIUsageStatisticsClientConstants.API_CREATOR_TENANT_DOMAIN + "=='" + tenantDomain
                + "') within " + getTimestamp(fromDate) + "L, " + getTimestamp(toDate) + "L per '" + granularity
                + "' select " + APIUsageStatisticsClientConstants.API_NAME + ", "
                + APIUsageStatisticsClientConstants.API_VERSION + ", "
                + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                + APIUsageStatisticsClientConstants.API_CONTEXT + ", "
                + APIUsageStatisticsClientConstants.DESTINATION + ", " + "sum("
                + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT + ") as total_request_count group by "
                + APIUsageStatisticsClientConstants.API_NAME + ", "
                + APIUsageStatisticsClientConstants.API_VERSION + ", "
                + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                + APIUsageStatisticsClientConstants.API_CONTEXT + ", "
                + APIUsageStatisticsClientConstants.DESTINATION + ";";
        JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(
                APIUsageStatisticsClientConstants.APIM_ACCESS_SUMMARY_SIDDHI_APP, query);
        String apiName;
        String version;
        String context;
        String destination;
        Long requestCount;
        APIUsageByDestination apiUsageByDestination;

        if (jsonObj != null) {
            JSONArray jArray = (JSONArray) jsonObj.get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER);
            for (Object record : jArray) {
                JSONArray recordArray = (JSONArray) record;
                if (recordArray.size() == 6) {
                    apiName = (String) recordArray.get(0);
                    version = (String) recordArray.get(1);
                    context = (String) recordArray.get(3);//omitting apiCreator
                    destination = (String) recordArray.get(4);
                    requestCount = (Long) recordArray.get(5);
                    apiUsageByDestination = new APIUsageByDestination(apiName, version, context, destination,
                            requestCount);
                    usageByDestination.add(apiUsageByDestination);
                }
            }
        }
        return usageByDestination;
    } catch (APIManagementException e) {
        log.error("Error occurred while querying from stream processor " + e.getMessage(), e);
        throw new APIMgtUsageQueryServiceClientException("Error occurred while querying from stream processor",
                e);
    }
}

From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRestClientImpl.java

License:Open Source License

/**
 * This method find the API version wise usage
 *
 * @param tableName Name of the table data exist
 * @param fromDate  starting data//from w ww. j  ava  2 s  . c  om
 * @param toDate    ending date
 * @param apiName   API name
 * @return list of APIUsage
 * @throws APIMgtUsageQueryServiceClientException throws if error occurred
 */
private List<APIUsage> getUsageByAPIVersionsData(String tableName, String tenantDomain, String fromDate,
        String toDate, String apiName) throws APIMgtUsageQueryServiceClientException {

    List<APIUsage> usageDataList = new ArrayList<APIUsage>();
    try {
        String query;
        if (fromDate != null && toDate != null) {
            String granularity = APIUsageStatisticsClientConstants.HOURS_GRANULARITY;//default granularity

            Map<String, Integer> durationBreakdown = this.getDurationBreakdown(fromDate, toDate);
            LocalDateTime currentDate = LocalDateTime.now(DateTimeZone.UTC);
            DateTimeFormatter formatter = DateTimeFormat
                    .forPattern(APIUsageStatisticsClientConstants.TIMESTAMP_PATTERN);
            LocalDateTime fromLocalDateTime = LocalDateTime.parse(fromDate, formatter);//GMT time

            if (fromLocalDateTime.isBefore(currentDate.minusYears(1))) {
                granularity = APIUsageStatisticsClientConstants.MONTHS_GRANULARITY;
            } else if (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_MONTHS) > 0) {
                granularity = APIUsageStatisticsClientConstants.DAYS_GRANULARITY;
            } else if (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_DAYS) > 0) {
                granularity = APIUsageStatisticsClientConstants.DAYS_GRANULARITY;
            }

            query = "from " + tableName + " on(" + APIUsageStatisticsClientConstants.API_CREATOR_TENANT_DOMAIN
                    + "=='" + tenantDomain + "' AND " + APIUsageStatisticsClientConstants.API_NAME + "=='"
                    + apiName + "') within " + getTimestamp(fromDate) + "L, " + getTimestamp(toDate) + "L per '"
                    + granularity + "' select " + APIUsageStatisticsClientConstants.API_NAME + ", "
                    + APIUsageStatisticsClientConstants.API_VERSION + ", "
                    + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                    + APIUsageStatisticsClientConstants.API_CONTEXT + ", sum("
                    + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT
                    + ") as total_request_count group by " + APIUsageStatisticsClientConstants.API_NAME + ", "
                    + APIUsageStatisticsClientConstants.API_VERSION + ", "
                    + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                    + APIUsageStatisticsClientConstants.API_CONTEXT + ";";
        } else {
            query = "from " + APIUsageStatisticsClientConstants.API_USER_PER_APP_AGG + " on("
                    + APIUsageStatisticsClientConstants.API_CREATOR_TENANT_DOMAIN + "=='" + tenantDomain
                    + "' AND " + APIUsageStatisticsClientConstants.API_NAME + "=='" + apiName + "') within " + 0
                    + "L, " + new Date().getTime() + "L per 'months' select "
                    + APIUsageStatisticsClientConstants.API_NAME + ", "
                    + APIUsageStatisticsClientConstants.API_VERSION + ", "
                    + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                    + APIUsageStatisticsClientConstants.API_CONTEXT + ", sum("
                    + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT
                    + ") as total_request_count group by " + APIUsageStatisticsClientConstants.API_NAME + ", "
                    + APIUsageStatisticsClientConstants.API_VERSION + ", "
                    + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                    + APIUsageStatisticsClientConstants.API_CONTEXT + ";";
        }

        JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(
                APIUsageStatisticsClientConstants.APIM_ACCESS_SUMMARY_SIDDHI_APP, query);

        String apiContext;
        String apiVersion;
        Long requestCount;
        if (jsonObj != null) {
            JSONArray jArray = (JSONArray) jsonObj.get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER);
            for (Object record : jArray) {
                JSONArray recordArray = (JSONArray) record;
                if (recordArray.size() == 5) {
                    apiVersion = (String) recordArray.get(1);
                    apiContext = (String) recordArray.get(3);
                    requestCount = (Long) recordArray.get(4);
                    usageDataList.add(new APIUsage(apiName, apiContext, apiVersion, requestCount));
                }
            }
        }
        return usageDataList;
    } catch (Exception e) {
        log.error("Error occurred while querying from Stream Processor " + e.getMessage(), e);
        throw new APIMgtUsageQueryServiceClientException("Error occurred while querying from Stream Processor ",
                e);
    }
}

From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRestClientImpl.java

License:Open Source License

/**
 * This method find the api usage count and its subscribers
 *
 * @param providerName logged API publisher
 * @param fromDate     starting date/*from   w  w w. ja va  2s . com*/
 * @param toDate       ending date
 * @param limit        result to be limited
 * @return list of APIUsageByUserName
 * @throws APIMgtUsageQueryServiceClientException throws if error occurred
 */
private List<APIUsageByUserName> getAPIUsageByUserData(String providerName, String fromDate, String toDate,
        Integer limit) throws APIMgtUsageQueryServiceClientException {

    String tenantDomain = MultitenantUtils.getTenantDomain(providerName);
    try {
        String query;
        String filter;
        if (providerName.contains(APIUsageStatisticsClientConstants.ALL_PROVIDERS)) {
            filter = APIUsageStatisticsClientConstants.API_CREATOR_TENANT_DOMAIN + "=='" + tenantDomain + "'";
        } else {
            filter = APIUsageStatisticsClientConstants.API_CREATOR + "=='" + providerName + "'";
        }

        if (fromDate != null && toDate != null) {
            String granularity = APIUsageStatisticsClientConstants.HOURS_GRANULARITY;//default granularity

            Map<String, Integer> durationBreakdown = this.getDurationBreakdown(fromDate, toDate);
            LocalDateTime currentDate = LocalDateTime.now(DateTimeZone.UTC);
            DateTimeFormatter formatter = DateTimeFormat
                    .forPattern(APIUsageStatisticsClientConstants.TIMESTAMP_PATTERN);
            LocalDateTime fromLocalDateTime = LocalDateTime.parse(fromDate, formatter);//GMT time

            if (fromLocalDateTime.isBefore(currentDate.minusYears(1))) {
                granularity = APIUsageStatisticsClientConstants.MONTHS_GRANULARITY;
            } else if ((durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_MONTHS) > 0)
                    || (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_DAYS) > 0)) {
                granularity = APIUsageStatisticsClientConstants.DAYS_GRANULARITY;
            }

            query = "from " + APIUsageStatisticsClientConstants.API_USER_PER_APP_AGG + " on " + filter
                    + " within " + getTimestamp(fromDate) + "L, " + getTimestamp(toDate) + "L per '"
                    + granularity + "' select " + APIUsageStatisticsClientConstants.API_NAME + ", "
                    + APIUsageStatisticsClientConstants.API_VERSION + ", "
                    + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                    + APIUsageStatisticsClientConstants.USERNAME + ", sum("
                    + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT + ") as total_request_count, "
                    + APIUsageStatisticsClientConstants.API_CONTEXT + " group by "
                    + APIUsageStatisticsClientConstants.API_NAME + ", "
                    + APIUsageStatisticsClientConstants.API_VERSION + ", "
                    + APIUsageStatisticsClientConstants.USERNAME + ", "
                    + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                    + APIUsageStatisticsClientConstants.API_CONTEXT + " order by total_request_count DESC;";
        } else {
            query = "from " + APIUsageStatisticsClientConstants.API_USER_PER_APP_AGG + " on " + filter
                    + " select " + APIUsageStatisticsClientConstants.API_NAME + ", "
                    + APIUsageStatisticsClientConstants.API_VERSION + ", "
                    + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                    + APIUsageStatisticsClientConstants.USERNAME + ", sum("
                    + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT + ") as total_request_count, "
                    + APIUsageStatisticsClientConstants.API_CONTEXT + " group by "
                    + APIUsageStatisticsClientConstants.API_NAME + ", "
                    + APIUsageStatisticsClientConstants.API_VERSION + ", "
                    + APIUsageStatisticsClientConstants.USERNAME + ", "
                    + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                    + APIUsageStatisticsClientConstants.API_CONTEXT + " order by total_request_count DESC;";
        }

        JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(
                APIUsageStatisticsClientConstants.APIM_ACCESS_SUMMARY_SIDDHI_APP, query);
        String apiName;
        String apiVersion;
        String apiContext;
        String username;
        Long requestCount;
        String creator;
        List<APIUsageByUserName> usageByName = new ArrayList<APIUsageByUserName>();

        if (jsonObj != null) {
            JSONArray jArray = (JSONArray) jsonObj.get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER);
            for (Object record : jArray) {
                JSONArray recordArray = (JSONArray) record;
                if (recordArray.size() == 6) {
                    apiName = (String) recordArray.get(0);
                    apiVersion = (String) recordArray.get(1);
                    creator = (String) recordArray.get(2);
                    username = (String) recordArray.get(3);
                    requestCount = (Long) recordArray.get(4);
                    apiContext = (String) recordArray.get(5);
                    if (creator != null) {
                        APIUsageByUserName usage = new APIUsageByUserName(apiName, apiVersion, apiContext,
                                username, creator, requestCount);
                        usageByName.add(usage);
                    }
                }
            }
        }
        return usageByName;
    } catch (APIManagementException e) {
        log.error("Error occurred while querying from Stream Processor " + e.getMessage(), e);
        throw new APIMgtUsageQueryServiceClientException("Error occurred while querying from Stream Processor ",
                e);
    }
}

From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRestClientImpl.java

License:Open Source License

/**
 * Given API name and Application, returns throttling request counts over time for a given time span.
 *
 * @param apiName  Name of the API/*from ww w.  j a va2s  .  c o m*/
 * @param provider Provider name
 * @param appName  Application name
 * @param fromDate Start date of the time span
 * @param toDate   End date of time span
 * @param groupBy  Group by parameter. Supported parameters are :day,hour
 * @return Throttling counts over time
 * @throws APIMgtUsageQueryServiceClientException throws when there is an error
 */
@Override
public List<APIThrottlingOverTimeDTO> getThrottleDataOfAPIAndApplication(String apiName, String provider,
        String appName, String fromDate, String toDate, String groupBy)
        throws APIMgtUsageQueryServiceClientException {
    try {
        List<APIThrottlingOverTimeDTO> throttlingData = new ArrayList<APIThrottlingOverTimeDTO>();
        String tenantDomain = MultitenantUtils.getTenantDomain(provider);
        String granularity = APIUsageStatisticsClientConstants.MINUTES_GRANULARITY;//default granularity

        Map<String, Integer> durationBreakdown = this.getDurationBreakdown(fromDate, toDate);
        LocalDateTime currentDate = LocalDateTime.now(DateTimeZone.UTC);
        DateTimeFormatter formatter = DateTimeFormat
                .forPattern(APIUsageStatisticsClientConstants.TIMESTAMP_PATTERN);
        LocalDateTime fromLocalDateTime = LocalDateTime.parse(fromDate, formatter);//GMT time

        if (fromLocalDateTime.isBefore(currentDate.minusYears(1))) {
            granularity = APIUsageStatisticsClientConstants.MONTHS_GRANULARITY;
        } else if (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_MONTHS) > 0
                || durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_WEEKS) > 0) {
            granularity = APIUsageStatisticsClientConstants.DAYS_GRANULARITY;
        } else if (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_DAYS) > 0) {
            granularity = APIUsageStatisticsClientConstants.HOURS_GRANULARITY;
        }
        StringBuilder query = new StringBuilder("from " + APIUsageStatisticsClientConstants.APIM_REQ_COUNT_AGG
                + " on(" + APIUsageStatisticsClientConstants.API_CREATOR_TENANT_DOMAIN + "=='" + tenantDomain
                + "' AND " + APIUsageStatisticsClientConstants.API_NAME + "=='" + apiName + "'");
        if (!provider.startsWith(APIUsageStatisticsClientConstants.ALL_PROVIDERS)) {
            query.append(" AND " + APIUsageStatisticsClientConstants.API_CREATOR + "=='" + provider + "'");
        }
        if (!StringUtils.isEmpty(appName)) {
            query.append(" AND " + APIUsageStatisticsClientConstants.APPLICATION_NAME + "=='" + appName + "'");
        }
        query.append(") within " + getTimestamp(fromDate) + "L, " + getTimestamp(toDate) + "L per '"
                + granularity + "' select " + APIUsageStatisticsClientConstants.TIME_STAMP + ", sum("
                + APIUsageStatisticsClientConstants.SUCCESS_COUNT + ") as success_request_count, sum("
                + APIUsageStatisticsClientConstants.THROTTLE_COUNT + ") as throttled_out_count group by "
                + APIUsageStatisticsClientConstants.TIME_STAMP + " order by "
                + APIUsageStatisticsClientConstants.TIME_STAMP + " ASC;");
        JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(
                APIUsageStatisticsClientConstants.APIM_THROTTLED_OUT_SUMMARY_SIDDHI_APP, query.toString());
        Long timeStamp;
        String time;
        long successRequestCount;
        long throttledOutCount;
        if (jsonObj != null) {
            JSONArray jArray = (JSONArray) jsonObj.get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER);
            for (Object record : jArray) {
                JSONArray recordArray = (JSONArray) record;
                if (recordArray.size() == 3) {
                    timeStamp = (Long) recordArray.get(0);
                    time = new DateTime(timeStamp).withZone(DateTimeZone.UTC).toString(formatter);
                    successRequestCount = (Long) recordArray.get(1);
                    throttledOutCount = (Long) recordArray.get(2);
                    throttlingData.add(new APIThrottlingOverTimeDTO(apiName, provider,
                            (int) successRequestCount, (int) throttledOutCount, time));
                }
            }
        }
        return throttlingData;
    } catch (APIManagementException e) {
        log.error("Error occurred while querying from Stream Processor " + e.getMessage(), e);
        throw new APIMgtUsageQueryServiceClientException("Error occurred while querying from Stream Processor ",
                e);
    }
}

From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRestClientImpl.java

License:Open Source License

/**
 * Given Application name and the provider, returns throttle data for the APIs of the provider invoked by the
 * given application./* w  w w. ja  v  a 2s .co  m*/
 *
 * @param appName  Application name
 * @param provider Provider name
 * @param fromDate Start date of the time span
 * @param toDate   End date of time span
 * @return Throttling counts of APIs of the provider invoked by the given app
 * @throws APIMgtUsageQueryServiceClientException
 */
@Override
public List<APIThrottlingOverTimeDTO> getThrottleDataOfApplication(String appName, String provider,
        String fromDate, String toDate) throws APIMgtUsageQueryServiceClientException {
    try {
        List<APIThrottlingOverTimeDTO> throttlingData = new ArrayList<APIThrottlingOverTimeDTO>();
        String tenantDomain = MultitenantUtils.getTenantDomain(provider);

        String granularity = APIUsageStatisticsClientConstants.MINUTES_GRANULARITY;//default granularity

        Map<String, Integer> durationBreakdown = this.getDurationBreakdown(fromDate, toDate);

        LocalDateTime currentDate = LocalDateTime.now(DateTimeZone.UTC);
        DateTimeFormatter formatter = DateTimeFormat
                .forPattern(APIUsageStatisticsClientConstants.TIMESTAMP_PATTERN);
        LocalDateTime fromLocalDateTime = LocalDateTime.parse(fromDate, formatter);//GMT time

        if (fromLocalDateTime.isBefore(currentDate.minusYears(1))) {
            granularity = APIUsageStatisticsClientConstants.MONTHS_GRANULARITY;
        } else if (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_MONTHS) > 0
                || durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_WEEKS) > 0) {
            granularity = APIUsageStatisticsClientConstants.DAYS_GRANULARITY;
        } else if (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_DAYS) > 0) {
            granularity = APIUsageStatisticsClientConstants.HOURS_GRANULARITY;
        }

        StringBuilder query = new StringBuilder("from " + APIUsageStatisticsClientConstants.APIM_REQ_COUNT_AGG
                + " on (" + APIUsageStatisticsClientConstants.API_CREATOR_TENANT_DOMAIN + "=='" + tenantDomain
                + "' AND " + APIUsageStatisticsClientConstants.APPLICATION_NAME + "=='" + appName + "'");
        if (!provider.startsWith(APIUsageStatisticsClientConstants.ALL_PROVIDERS)) {
            query.append("AND " + APIUsageStatisticsClientConstants.API_CREATOR + "=='" + provider + "'");
        }
        query.append(") within " + getTimestamp(fromDate) + "L, " + getTimestamp(toDate) + "L per '"
                + granularity + "' select " + APIUsageStatisticsClientConstants.API_NAME + ", "
                + APIUsageStatisticsClientConstants.API_CREATOR + ", sum("
                + APIUsageStatisticsClientConstants.SUCCESS_COUNT + ") as success_request_count, sum("
                + APIUsageStatisticsClientConstants.THROTTLE_COUNT + ") as throttleout_count group by "
                + APIUsageStatisticsClientConstants.API_NAME + ", "
                + APIUsageStatisticsClientConstants.API_CREATOR + " order by "
                + APIUsageStatisticsClientConstants.API_NAME + " ASC;");
        JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(
                APIUsageStatisticsClientConstants.APIM_THROTTLED_OUT_SUMMARY_SIDDHI_APP, query.toString());

        String apiName;
        String apiCreator;
        long successRequestCount;
        long throttledOutCount;

        if (jsonObj != null) {
            JSONArray jArray = (JSONArray) jsonObj.get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER);
            for (Object record : jArray) {
                JSONArray recordArray = (JSONArray) record;
                if (recordArray.size() == 4) {
                    apiName = (String) recordArray.get(0);
                    apiCreator = (String) recordArray.get(1);
                    successRequestCount = (Long) recordArray.get(2);
                    throttledOutCount = (Long) recordArray.get(3);
                    throttlingData.add(new APIThrottlingOverTimeDTO(apiName, apiCreator,
                            (int) successRequestCount, (int) throttledOutCount, null));
                }
            }
        }
        return throttlingData;
    } catch (APIManagementException e) {
        log.error("Error occurred while querying from Stream Processor " + e.getMessage(), e);
        throw new APIMgtUsageQueryServiceClientException("Error occurred while querying from Stream Processor ",
                e);
    }
}

From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRestClientImpl.java

License:Open Source License

@Override
public List<Result<ExecutionTimeOfAPIValues>> getExecutionTimeByAPI(String apiName, String version,
        String tenantDomain, String fromDate, String toDate, String drillDown, String mediationType)
        throws APIMgtUsageQueryServiceClientException {
    List<Result<ExecutionTimeOfAPIValues>> result = new ArrayList<Result<ExecutionTimeOfAPIValues>>();
    try {/* w w  w. j a  v a2 s  .c om*/
        StringBuilder query = new StringBuilder(
                "from " + APIUsageStatisticsClientConstants.API_EXECUTION_TIME_AGG + " on("
                        + APIUsageStatisticsClientConstants.API_NAME + "=='" + apiName + "'");
        if (version != null) {
            query.append(" AND " + APIUsageStatisticsClientConstants.API_VERSION + "=='" + version + "'");
        }
        if (tenantDomain != null) {
            query.append(" AND " + APIUsageStatisticsClientConstants.API_CREATOR_TENANT_DOMAIN + "=='"
                    + tenantDomain + "'");
        }
        if (fromDate != null && toDate != null) {
            String granularity = APIUsageStatisticsClientConstants.SECONDS_GRANULARITY;

            Map<String, Integer> durationBreakdown = this.getDurationBreakdown(fromDate, toDate);
            LocalDateTime currentDate = LocalDateTime.now(DateTimeZone.UTC);
            DateTimeFormatter formatter = DateTimeFormat
                    .forPattern(APIUsageStatisticsClientConstants.TIMESTAMP_PATTERN);
            LocalDateTime fromLocalDateTime = LocalDateTime.parse(fromDate, formatter);//GMT time

            if (fromLocalDateTime.isBefore(currentDate.minusYears(1))) {
                granularity = APIUsageStatisticsClientConstants.MONTHS_GRANULARITY;
            } else if (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_MONTHS) > 0
                    || durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_WEEKS) > 0) {
                granularity = APIUsageStatisticsClientConstants.DAYS_GRANULARITY;
            } else if (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_DAYS) > 0) {
                granularity = APIUsageStatisticsClientConstants.HOURS_GRANULARITY;
            } else if (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_HOURS) > 0) {
                granularity = APIUsageStatisticsClientConstants.MINUTES_GRANULARITY;
            }
            query.append(") within " + getTimestamp(fromDate) + "L, " + getTimestamp(toDate) + "L per '"
                    + granularity + "'");
        } else {
            query.append(") within " + 0 + "L, " + new Date().getTime() + "L per 'months'");
        }
        query.append(" select " + APIUsageStatisticsClientConstants.API_NAME + ", "
                + APIUsageStatisticsClientConstants.API_CONTEXT + ", "
                + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                + APIUsageStatisticsClientConstants.API_VERSION + ", "
                + APIUsageStatisticsClientConstants.TIME_STAMP + ", "
                + APIUsageStatisticsClientConstants.RESPONSE_TIME + ", "
                + APIUsageStatisticsClientConstants.SECURITY_LATENCY + ", "
                + APIUsageStatisticsClientConstants.THROTTLING_LATENCY + ", "
                + APIUsageStatisticsClientConstants.REQUEST_MEDIATION_LATENCY + ", "
                + APIUsageStatisticsClientConstants.RESPONSE_MEDIATION_LATENCY + ", "
                + APIUsageStatisticsClientConstants.BACKEND_LATENCY + ", "
                + APIUsageStatisticsClientConstants.OTHER_LATENCY + ";");
        JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(
                APIUsageStatisticsClientConstants.APIM_ACCESS_SUMMARY_SIDDHI_APP, query.toString());
        long timeStamp;
        if (jsonObj != null) {
            JSONArray jArray = (JSONArray) jsonObj.get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER);
            for (Object record : jArray) {
                JSONArray recordArray = (JSONArray) record;
                if (recordArray.size() == 12) {
                    Result<ExecutionTimeOfAPIValues> result1 = new Result<ExecutionTimeOfAPIValues>();
                    ExecutionTimeOfAPIValues executionTimeOfAPIValues = new ExecutionTimeOfAPIValues();
                    executionTimeOfAPIValues.setApi((String) recordArray.get(0));
                    executionTimeOfAPIValues.setContext((String) recordArray.get(1));
                    executionTimeOfAPIValues.setApiPublisher((String) recordArray.get(2));
                    executionTimeOfAPIValues.setVersion((String) recordArray.get(3));
                    timeStamp = (Long) recordArray.get(4);
                    DateTime time = new DateTime(timeStamp).withZone(DateTimeZone.UTC);
                    executionTimeOfAPIValues.setYear(time.getYear());
                    executionTimeOfAPIValues.setMonth(time.getMonthOfYear());
                    executionTimeOfAPIValues.setDay(time.getDayOfMonth());
                    executionTimeOfAPIValues.setHour(time.getHourOfDay());
                    executionTimeOfAPIValues.setMinutes(time.getMinuteOfHour());
                    executionTimeOfAPIValues.setSeconds(time.getSecondOfMinute());
                    executionTimeOfAPIValues.setApiResponseTime((Long) recordArray.get(5));
                    executionTimeOfAPIValues.setSecurityLatency((Long) recordArray.get(6));
                    executionTimeOfAPIValues.setThrottlingLatency((Long) recordArray.get(7));
                    executionTimeOfAPIValues.setRequestMediationLatency((Long) recordArray.get(8));
                    executionTimeOfAPIValues.setResponseMediationLatency((Long) recordArray.get(9));
                    executionTimeOfAPIValues.setBackendLatency((Long) recordArray.get(10));
                    executionTimeOfAPIValues.setOtherLatency((Long) recordArray.get(11));
                    result1.setValues(executionTimeOfAPIValues);
                    result1.setTableName(APIUsageStatisticsClientConstants.API_EXECUTION_TIME_AGG);
                    result1.setTimestamp(RestClientUtil.longToDate(new Date().getTime()));
                    result.add(result1);
                }
            }
        }
        if (!result.isEmpty() && fromDate != null && toDate != null) {
            insertZeroElementsAndSort(result, drillDown, getDateToLong(fromDate), getDateToLong(toDate));
        }
    } catch (APIManagementException e) {
        handleException("Error occurred while querying from Stream Processor ", e);
    } catch (ParseException e) {
        handleException("Couldn't parse the date", e);
    }
    return result;
}

From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRestClientImpl.java

License:Open Source License

@Override
public List<Result<PerGeoLocationUsageCount>> getGeoLocationsByApi(String apiName, String version,
        String tenantDomain, String fromDate, String toDate, String drillDown)
        throws APIMgtUsageQueryServiceClientException {
    List<Result<PerGeoLocationUsageCount>> result = new ArrayList<Result<PerGeoLocationUsageCount>>();
    try {//from   w ww  . j av  a  2  s. c  o  m
        StringBuilder query = new StringBuilder("from " + APIUsageStatisticsClientConstants.GEO_LOCATION_AGG
                + " on(" + APIUsageStatisticsClientConstants.API_NAME + "=='" + apiName + "'");
        if (version != null && !"ALL".equals(version)) {
            query.append(" AND " + APIUsageStatisticsClientConstants.API_VERSION + "=='" + version + "'");
        }
        if (tenantDomain != null) {
            query.append(" AND " + APIUsageStatisticsClientConstants.API_CREATOR_TENANT_DOMAIN + "=='"
                    + tenantDomain + "'");
        }
        if (!"ALL".equals(drillDown)) {
            query.append(" AND " + APIUsageStatisticsClientConstants.COUNTRY + "=='" + drillDown + "'");
        }
        if (fromDate != null && toDate != null) {
            String granularity = APIUsageStatisticsClientConstants.HOURS_GRANULARITY;//default granularity

            Map<String, Integer> durationBreakdown = this.getDurationBreakdown(fromDate, toDate);
            LocalDateTime currentDate = LocalDateTime.now(DateTimeZone.UTC);
            DateTimeFormatter formatter = DateTimeFormat
                    .forPattern(APIUsageStatisticsClientConstants.TIMESTAMP_PATTERN);
            LocalDateTime fromLocalDateTime = LocalDateTime.parse(fromDate, formatter);//GMT time

            if (fromLocalDateTime.isBefore(currentDate.minusYears(1))) {
                granularity = APIUsageStatisticsClientConstants.MONTHS_GRANULARITY;
            } else if ((durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_MONTHS) > 0)
                    || (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_DAYS) > 0)) {
                granularity = APIUsageStatisticsClientConstants.DAYS_GRANULARITY;
            }
            query.append(") within " + getTimestamp(fromDate) + "L, " + getTimestamp(toDate) + "L per '"
                    + granularity + "' select sum(" + APIUsageStatisticsClientConstants.TOTAL_COUNT);
        } else {
            query.append(") within " + 0 + "L, " + new Date().getTime() + "L per 'months' select sum("
                    + APIUsageStatisticsClientConstants.TOTAL_COUNT);
        }
        query.append(") as count, " + APIUsageStatisticsClientConstants.COUNTRY);
        if (!"ALL".equals(drillDown)) {
            query.append(", " + APIUsageStatisticsClientConstants.CITY);
        }
        query.append(" group by " + APIUsageStatisticsClientConstants.COUNTRY);
        if (!"ALL".equals(drillDown)) {
            query.append(", " + APIUsageStatisticsClientConstants.CITY);
        }
        query.append(";");
        JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(
                APIUsageStatisticsClientConstants.APIM_ACCESS_SUMMARY_SIDDHI_APP, query.toString());
        long count;
        String country;
        String city;
        if (jsonObj != null) {
            JSONArray jArray = (JSONArray) jsonObj.get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER);
            for (Object record : jArray) {
                JSONArray recordArray = (JSONArray) record;
                if (recordArray.size() >= 2) {
                    Result<PerGeoLocationUsageCount> result1 = new Result<PerGeoLocationUsageCount>();
                    count = (Long) recordArray.get(0);
                    country = (String) recordArray.get(1);
                    List<String> facetValues = new ArrayList<String>();
                    facetValues.add(country);
                    if (!"ALL".equals(drillDown)) {
                        city = (String) recordArray.get(2);
                        facetValues.add(city);
                    }
                    PerGeoLocationUsageCount perGeoLocationUsageCount = new PerGeoLocationUsageCount(
                            (int) count, facetValues);
                    result1.setValues(perGeoLocationUsageCount);
                    result1.setTableName(APIUsageStatisticsClientConstants.GEO_LOCATION_AGG);
                    result1.setTimestamp(RestClientUtil.longToDate(new Date().getTime()));
                    result.add(result1);
                }
            }
        }
    } catch (APIManagementException e) {
        handleException("Error occurred while querying from Stream Processor ", e);
    }
    return result;

}

From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRestClientImpl.java

License:Open Source License

@Override
public List<Result<UserAgentUsageCount>> getUserAgentUsageByAPI(String apiName, String version,
        String tenantDomain, String fromDate, String toDate, String drillDown)
        throws APIMgtUsageQueryServiceClientException {
    List<Result<UserAgentUsageCount>> result = new ArrayList<Result<UserAgentUsageCount>>();
    try {/* www .  j av a 2 s .  c  om*/
        StringBuilder query = new StringBuilder("from " + APIUsageStatisticsClientConstants.API_USER_BROWSER_AGG
                + " on(" + APIUsageStatisticsClientConstants.API_NAME + "=='" + apiName + "'");
        if (version != null && !"ALL".equals(version)) {
            query.append(" AND " + APIUsageStatisticsClientConstants.API_VERSION + "=='" + version + "'");
        }
        if (tenantDomain != null) {
            query.append(" AND " + APIUsageStatisticsClientConstants.API_CREATOR_TENANT_DOMAIN + "=='"
                    + tenantDomain + "'");
        }
        if (!"ALL".equals(drillDown)) {
            query.append(
                    " AND " + APIUsageStatisticsClientConstants.OPERATING_SYSTEM + "=='" + drillDown + "'");
        }
        if (fromDate != null && toDate != null) {
            String granularity = APIUsageStatisticsClientConstants.HOURS_GRANULARITY;//default granularity

            Map<String, Integer> durationBreakdown = this.getDurationBreakdown(fromDate, toDate);
            LocalDateTime currentDate = LocalDateTime.now(DateTimeZone.UTC);
            DateTimeFormatter formatter = DateTimeFormat
                    .forPattern(APIUsageStatisticsClientConstants.TIMESTAMP_PATTERN);
            LocalDateTime fromLocalDateTime = LocalDateTime.parse(fromDate, formatter);//GMT time

            if (fromLocalDateTime.isBefore(currentDate.minusYears(1))) {
                granularity = APIUsageStatisticsClientConstants.MONTHS_GRANULARITY;
            } else if ((durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_MONTHS) > 0)
                    || (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_DAYS) > 0)) {
                granularity = APIUsageStatisticsClientConstants.DAYS_GRANULARITY;
            }
            query.append(") within " + getTimestamp(fromDate) + "L, " + getTimestamp(toDate) + "L per '"
                    + granularity + "' select sum(" + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT);
        } else {
            query.append(") within " + 0 + "L, " + new Date().getTime() + "L per 'months' select sum("
                    + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT);
        }
        query.append(") as count, " + APIUsageStatisticsClientConstants.OPERATING_SYSTEM + ", "
                + APIUsageStatisticsClientConstants.BROWSER + " group by "
                + APIUsageStatisticsClientConstants.OPERATING_SYSTEM + ", "
                + APIUsageStatisticsClientConstants.BROWSER + ";");

        JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(
                APIUsageStatisticsClientConstants.APIM_ACCESS_SUMMARY_SIDDHI_APP, query.toString());
        long count;
        String operatingSystem;
        String browser;
        if (jsonObj != null) {
            JSONArray jArray = (JSONArray) jsonObj.get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER);
            for (Object record : jArray) {
                JSONArray recordArray = (JSONArray) record;
                if (recordArray.size() == 3) {
                    Result<UserAgentUsageCount> result1 = new Result<UserAgentUsageCount>();
                    count = (Long) recordArray.get(0);
                    operatingSystem = (String) recordArray.get(1);
                    browser = (String) recordArray.get(2);
                    List<String> facetValues = new ArrayList<String>();
                    facetValues.add(operatingSystem);
                    facetValues.add(browser);
                    UserAgentUsageCount perUserAgentUsageCount = new UserAgentUsageCount((int) count,
                            facetValues);
                    result1.setValues(perUserAgentUsageCount);
                    result1.setTableName(APIUsageStatisticsClientConstants.API_USER_BROWSER_AGG);
                    result1.setTimestamp(RestClientUtil.longToDate(new Date().getTime()));
                    result.add(result1);
                }
            }
        }
    } catch (APIManagementException e) {
        handleException("Error occurred while querying from Stream Processor ", e);
    }
    return result;
}

From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRestClientImpl.java

License:Open Source License

@Override
public List<Result<APIUsageByApplication>> getAPIUsageByApplications(String apiName, String apiVersion,
        String fromDate, String toDate, String providerName) throws APIMgtUsageQueryServiceClientException {
    String tenantDomain = null;/*from   w w w.  j  a v a2s .  co m*/
    if (providerName != null) {
        tenantDomain = MultitenantUtils.getTenantDomain(providerName);
    }
    List<Result<APIUsageByApplication>> apiUsageByApplicationsResultList = new ArrayList<Result<APIUsageByApplication>>();
    try {
        // Build the query.
        StringBuilder apiUsageByAppQuery = new StringBuilder(
                "from " + APIUsageStatisticsClientConstants.API_VERSION_PER_APP_AGG + " on("
                        + APIUsageStatisticsClientConstants.API_CREATOR_TENANT_DOMAIN + "=='" + tenantDomain
                        + "' AND " + APIUsageStatisticsClientConstants.API_NAME + "=='" + apiName);

        if (!APIUsageStatisticsClientConstants.FOR_ALL_API_VERSIONS.equals(apiVersion)) {
            apiUsageByAppQuery
                    .append("' AND " + APIUsageStatisticsClientConstants.API_VERSION + "=='" + apiVersion);
        }
        if (fromDate != null && toDate != null) {
            String granularity = APIUsageStatisticsClientConstants.SECONDS_GRANULARITY;
            Map<String, Integer> durationBreakdown = this.getDurationBreakdown(fromDate, toDate);
            LocalDateTime currentDate = LocalDateTime.now(DateTimeZone.UTC);
            DateTimeFormatter formatter = DateTimeFormat
                    .forPattern(APIUsageStatisticsClientConstants.TIMESTAMP_PATTERN);
            LocalDateTime fromLocalDateTime = LocalDateTime.parse(fromDate, formatter);//GMT time

            if (fromLocalDateTime.isBefore(currentDate.minusYears(1))) {
                granularity = APIUsageStatisticsClientConstants.MONTHS_GRANULARITY;
            } else if (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_MONTHS) > 0
                    || durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_WEEKS) > 0) {
                granularity = APIUsageStatisticsClientConstants.DAYS_GRANULARITY;
            } else if (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_DAYS) > 0) {
                granularity = APIUsageStatisticsClientConstants.HOURS_GRANULARITY;
            } else if (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_HOURS) > 0) {
                granularity = APIUsageStatisticsClientConstants.MINUTES_GRANULARITY;
            }
            apiUsageByAppQuery.append("') within " + getTimestamp(fromDate) + "L, " + getTimestamp(toDate)
                    + "L per '" + granularity + "' select " + APIUsageStatisticsClientConstants.API_NAME + ", "
                    + APIUsageStatisticsClientConstants.API_VERSION + ", "
                    + APIUsageStatisticsClientConstants.APPLICATION_NAME + ", sum("
                    + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT
                    + ") as total_request_count group by " + APIUsageStatisticsClientConstants.API_NAME + ", "
                    + APIUsageStatisticsClientConstants.API_VERSION + ", "
                    + APIUsageStatisticsClientConstants.APPLICATION_NAME + ";");
        }
        // Invoke the rest api and get the results.
        JSONObject apiUsageByAppResult = APIUtil.executeQueryOnStreamProcessor(
                APIUsageStatisticsClientConstants.APIM_ACCESS_SUMMARY_SIDDHI_APP,
                apiUsageByAppQuery.toString());

        // Create the api usage object and return.
        long requestCount;
        String api;
        String version;
        String appName;
        if (apiUsageByAppResult != null) {
            JSONArray jArray = (JSONArray) apiUsageByAppResult
                    .get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER);
            for (Object record : jArray) {
                JSONArray recordArray = (JSONArray) record;
                if (recordArray.size() == 4) {
                    Result<APIUsageByApplication> result = new Result<APIUsageByApplication>();
                    api = (String) recordArray.get(0);
                    version = (String) recordArray.get(1);
                    appName = (String) recordArray.get(2);
                    requestCount = (Long) recordArray.get(3);

                    APIUsageByApplication apiUsageByApplication = new APIUsageByApplication();
                    apiUsageByApplication.setApiName(api);
                    apiUsageByApplication.setApiVersion(version);
                    apiUsageByApplication.setApplicationName(appName);
                    apiUsageByApplication.setRequstCount(requestCount);

                    result.setValues(apiUsageByApplication);
                    result.setTableName(APIUsageStatisticsClientConstants.API_VERSION_PER_APP_AGG);
                    result.setTimestamp(RestClientUtil.longToDate(new Date().getTime()));
                    apiUsageByApplicationsResultList.add(result);
                }
            }
        }
        return apiUsageByApplicationsResultList;
    } catch (APIManagementException e) {
        handleException("Error occurred while querying from Stream Processor.", e);
    }
    return new ArrayList<Result<APIUsageByApplication>>();
}