Example usage for org.joda.time LocalDateTime parse

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

Introduction

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

Prototype

public static LocalDateTime parse(String str, DateTimeFormatter formatter) 

Source Link

Document

Parses a LocalDateTime from the specified string using a formatter.

Usage

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

License:Open Source License

/**
 * This method gets the API usage data per API call type
 *
 * @param tableName name of the required table in the database
 * @param idList    Ids List of applications
 * @return a collection containing the data related to API call types
 * @throws APIMgtUsageQueryServiceClientException if an error occurs while querying the database
 *//*from  w  w w.  j  a v a 2s .com*/
private List<AppCallTypeDTO> getAPICallTypeUsageData(String tableName, List<String> idList, String fromDate,
        String toDate, int limit) throws APIMgtUsageQueryServiceClientException {
    List<AppCallTypeDTO> appApiCallTypeList = new ArrayList<AppCallTypeDTO>();
    try {
        if (!idList.isEmpty()) {
            String startDate = fromDate + ":00";
            String endDate = toDate + ":00";
            String granularity = APIUsageStatisticsClientConstants.HOURS_GRANULARITY;//default granularity

            Map<String, Integer> durationBreakdown = this.getDurationBreakdown(startDate, endDate);
            LocalDateTime currentDate = LocalDateTime.now(DateTimeZone.UTC);
            DateTimeFormatter formatter = DateTimeFormat
                    .forPattern(APIUsageStatisticsClientConstants.TIMESTAMP_PATTERN);
            LocalDateTime fromLocalDateTime = LocalDateTime.parse(startDate, 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;
            }
            StringBuilder idListQuery = new StringBuilder();
            for (int i = 0; i < idList.size(); i++) {
                if (i > 0) {
                    idListQuery.append(" or ");
                }
                idListQuery.append(APIUsageStatisticsClientConstants.APPLICATION_ID + "==");
                idListQuery.append("'" + idList.get(i) + "'");
            }
            String query = "from " + tableName + " on " + idListQuery.toString() + " within "
                    + getTimestamp(startDate) + "L, " + getTimestamp(endDate) + "L per '" + granularity
                    + "' select " + APIUsageStatisticsClientConstants.API_NAME + ", "
                    + APIUsageStatisticsClientConstants.API_VERSION + ", "
                    + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                    + APIUsageStatisticsClientConstants.API_METHOD + ", "
                    + APIUsageStatisticsClientConstants.APPLICATION_ID + ", "
                    + APIUsageStatisticsClientConstants.API_RESOURCE_TEMPLATE + ", " + "sum("
                    + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT
                    + ") as total_request_count group by " + APIUsageStatisticsClientConstants.APPLICATION_ID
                    + ", " + APIUsageStatisticsClientConstants.API_NAME + ", "
                    + APIUsageStatisticsClientConstants.API_VERSION + ", "
                    + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                    + APIUsageStatisticsClientConstants.API_METHOD + ", "
                    + APIUsageStatisticsClientConstants.API_RESOURCE_TEMPLATE + ";";
            JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(
                    APIUsageStatisticsClientConstants.APIM_ACCESS_SUMMARY_SIDDHI_APP, query);
            String apiName;
            String apiVersion;
            String apiCreator;
            String callType;
            String applicationId;
            String apiResourceTemplate;
            AppCallTypeDTO appCallTypeDTO;
            if (jsonObj != null) {
                JSONArray jArray = (JSONArray) jsonObj.get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER);
                for (Object record : jArray) {
                    JSONArray recordArray = (JSONArray) record;
                    if (recordArray.size() == 7) {
                        apiName = (String) recordArray.get(0);
                        apiVersion = (String) recordArray.get(1);
                        apiCreator = (String) recordArray.get(2);
                        apiName = apiName + " (" + apiCreator + ")";
                        callType = (String) recordArray.get(3);
                        applicationId = (String) recordArray.get(4);
                        apiResourceTemplate = (String) recordArray.get(5);
                        List<String> callTypeList = new ArrayList<String>();
                        callTypeList.add(apiResourceTemplate + " (" + callType + ")");
                        List<Integer> hitCountList = new ArrayList<Integer>();
                        long hitCount = (Long) recordArray.get(6);
                        hitCountList.add((int) hitCount);
                        String appName = subscriberAppsMap.get(applicationId);
                        boolean found = false;
                        for (AppCallTypeDTO dto : appApiCallTypeList) {
                            if (dto.getAppName().equals(appName)) {
                                dto.addToApiCallTypeArray(apiName, apiVersion, callTypeList, hitCountList);
                                found = true;
                                break;
                            }
                        }
                        if (!found) {
                            appCallTypeDTO = new AppCallTypeDTO();
                            appCallTypeDTO.setAppName(appName);
                            appCallTypeDTO.addToApiCallTypeArray(apiName, apiVersion, callTypeList,
                                    hitCountList);
                            appApiCallTypeList.add(appCallTypeDTO);
                        }
                    }
                }
            }
        }
    } catch (APIManagementException e) {
        handleException("Error occurred while querying API call type data from Stream Processor ", e);
    }
    return appApiCallTypeList;
}

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

License:Open Source License

/**
 * This method gets the API usage data per application
 *
 * @param tableName name of the required table in the database
 * @param idList    Ids list of applications
 * @return a collection containing the data related to per App API usage
 * @throws APIMgtUsageQueryServiceClientException if an error occurs while querying the database
 *//*www .  j  a va  2s  .co m*/
private List<PerAppApiCountDTO> getPerAppAPIUsageData(String tableName, List<String> idList, String fromDate,
        String toDate, int limit) throws APIMgtUsageQueryServiceClientException {
    List<PerAppApiCountDTO> perAppUsageDataList = new ArrayList<PerAppApiCountDTO>();
    try {
        if (!idList.isEmpty()) {
            String startDate = fromDate + ":00";
            String endDate = toDate + ":00";
            String granularity = APIUsageStatisticsClientConstants.HOURS_GRANULARITY;//default granularity
            Map<String, Integer> durationBreakdown = this.getDurationBreakdown(startDate, endDate);
            LocalDateTime currentDate = LocalDateTime.now(DateTimeZone.UTC);
            DateTimeFormatter formatter = DateTimeFormat
                    .forPattern(APIUsageStatisticsClientConstants.TIMESTAMP_PATTERN);
            LocalDateTime fromLocalDateTime = LocalDateTime.parse(startDate, 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;
            }
            StringBuilder idListQuery = new StringBuilder();
            for (int i = 0; i < idList.size(); i++) {
                if (i > 0) {
                    idListQuery.append(" or ");
                }
                idListQuery.append(APIUsageStatisticsClientConstants.APPLICATION_ID + "==");
                idListQuery.append("'" + idList.get(i) + "'");
            }
            String query = "from " + tableName + " on " + idListQuery.toString() + " within "
                    + getTimestamp(startDate) + "L, " + getTimestamp(endDate) + "L per '" + granularity
                    + "' select " + APIUsageStatisticsClientConstants.API_NAME + ", "
                    + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                    + APIUsageStatisticsClientConstants.APPLICATION_ID + ", sum("
                    + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT + ") as total_calls group by "
                    + APIUsageStatisticsClientConstants.API_NAME + ", "
                    + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                    + APIUsageStatisticsClientConstants.APPLICATION_ID + ";";
            JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(
                    APIUsageStatisticsClientConstants.APIM_ACCESS_SUMMARY_SIDDHI_APP, query);
            String apiName;
            String apiCreator;
            String applicationId;
            long requestCount;
            PerAppApiCountDTO apiUsageDTO;
            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);
                        apiName = apiName + " (" + apiCreator + ")";
                        applicationId = (String) recordArray.get(2);
                        requestCount = (Long) recordArray.get(3);
                        String appName = subscriberAppsMap.get(applicationId);
                        boolean found = false;
                        for (PerAppApiCountDTO dto : perAppUsageDataList) {
                            if (dto.getAppName().equals(appName)) {
                                dto.addToApiCountArray(apiName, requestCount);
                                found = true;
                                break;
                            }
                        }
                        if (!found) {
                            apiUsageDTO = new PerAppApiCountDTO();
                            apiUsageDTO.setAppName(appName);
                            apiUsageDTO.addToApiCountArray(apiName, requestCount);
                            perAppUsageDataList.add(apiUsageDTO);
                        }
                    }
                }
            }
        }
    } catch (APIManagementException e) {
        handleException("Error occurred while querying per App usage data from Stream Processor", e);
    }
    return perAppUsageDataList;
}

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

License:Open Source License

/**
 * This method gets the usage data for a given API across all versions
 *
 * @param tableName name of the table in the database
 * @param tenantDomain Tenant Domain/*  w  w  w .  j  av  a  2  s.  co  m*/
 * @param fromDate From date
 * @param toDate To date
 * @return a collection containing the API usage data
 * @throws APIMgtUsageQueryServiceClientException if an error occurs while querying the database
 */
private Collection<APIUsage> getAPIUsageData(String tableName, String tenantDomain, String fromDate,
        String toDate) throws APIMgtUsageQueryServiceClientException {

    Collection<APIUsage> usageDataList = new ArrayList<APIUsage>();
    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_CONTEXT + ", "
                + APIUsageStatisticsClientConstants.API_VERSION + ", sum("
                + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT + ") as aggregateSum group by "
                + APIUsageStatisticsClientConstants.API_NAME + ", "
                + APIUsageStatisticsClientConstants.API_CONTEXT + ", "
                + APIUsageStatisticsClientConstants.API_VERSION + ";";
        JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(
                APIUsageStatisticsClientConstants.APIM_ACCESS_SUMMARY_SIDDHI_APP, query);

        String apiName;
        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() == 4) {
                    apiName = (String) recordArray.get(0);
                    apiContext = (String) recordArray.get(1);
                    apiVersion = (String) recordArray.get(2);
                    requestCount = (Long) recordArray.get(3);
                    usageDataList.add(new APIUsage(apiName, apiContext, apiVersion, requestCount));
                }
            }
        }
    } catch (APIManagementException e) {
        handleException("Error occurred while querying API usage data from Stream Processor ", e);
    }
    return usageDataList;
}

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

License:Open Source License

/**
 * This method find the API fault count data
 *
 * @param tableName Name of the table data exist
 * @param tenantDomain Tenant Domain/*from   www .  j av a  2s.  c o m*/
 * @param fromDate  starting data
 * @param toDate    ending date
 * @return list of APIResponseFaultCount
 * @throws APIMgtUsageQueryServiceClientException throws if error occurred
 */
private List<APIResponseFaultCount> getAPIResponseFaultCountData(String tableName, String tenantDomain,
        String fromDate, String toDate) throws APIMgtUsageQueryServiceClientException {
    List<APIResponseFaultCount> faultUsage = new ArrayList<APIResponseFaultCount>();
    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 + ", sum("
                + APIUsageStatisticsClientConstants.TOTAL_FAULT_COUNT + ") as total_fault_count group by "
                + APIUsageStatisticsClientConstants.API_NAME + ", "
                + APIUsageStatisticsClientConstants.API_VERSION + ", "
                + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                + APIUsageStatisticsClientConstants.API_CONTEXT + "  order by "
                + APIUsageStatisticsClientConstants.API_NAME + " ASC ;";
        JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(
                APIUsageStatisticsClientConstants.APIM_FAULT_SUMMARY_SIDDHI_APP, query);
        String apiName;
        String apiVersion;
        String apiContext;
        long faultCount;
        APIResponseFaultCount apiResponseFaultCount;
        if (jsonObj != null) {
            JSONArray jArray = (JSONArray) jsonObj.get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER);
            for (Object record : jArray) {
                JSONArray recordArray = (JSONArray) record;
                if (recordArray.size() == 5) {
                    apiName = (String) recordArray.get(0);
                    apiVersion = (String) recordArray.get(1);
                    apiContext = (String) recordArray.get(3); //omitting the creator
                    faultCount = (Long) recordArray.get(4);
                    apiResponseFaultCount = new APIResponseFaultCount(apiName, apiVersion, apiContext,
                            faultCount);
                    faultUsage.add(apiResponseFaultCount);
                }
            }
        }
        return faultUsage;
    } 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 finds the Resource path usage of APIs
 *
 * @param tableName Name of the aggregation where the data exist
 * @param providerName Name of the provider
 * @param fromDate  starting date//from www . j  a  va 2s . com
 * @param toDate    ending date
 * @return list of APIUsageByResourcePath
 * @throws APIMgtUsageQueryServiceClientException throws if error occurred
 */
private List<APIUsageByResourcePath> getAPIUsageByResourcePathData(String tableName, String providerName,
        String fromDate, String toDate) throws APIMgtUsageQueryServiceClientException {
    List<APIUsageByResourcePath> usage = new ArrayList<APIUsageByResourcePath>();
    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.API_METHOD + ", " + "sum("
                + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT + ") as total_request_count, "
                + APIUsageStatisticsClientConstants.API_RESOURCE_TEMPLATE + ", "
                + APIUsageStatisticsClientConstants.TIME_STAMP + " group by "
                + APIUsageStatisticsClientConstants.API_NAME + ", "
                + APIUsageStatisticsClientConstants.API_VERSION + ", "
                + APIUsageStatisticsClientConstants.API_CREATOR + ", "
                + APIUsageStatisticsClientConstants.API_CONTEXT + ", "
                + APIUsageStatisticsClientConstants.API_METHOD + ", "
                + APIUsageStatisticsClientConstants.API_RESOURCE_TEMPLATE + ";";
        JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(
                APIUsageStatisticsClientConstants.APIM_ACCESS_SUMMARY_SIDDHI_APP, query);
        String apiName;
        String version;
        String context;
        String method;
        Long hits;
        String resourcePaths;
        Long time;
        APIUsageByResourcePath apiUsageByResourcePath;
        if (jsonObj != null) {
            JSONArray jArray = (JSONArray) jsonObj.get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER);
            for (Object record : jArray) {
                JSONArray recordArray = (JSONArray) record;
                if (recordArray.size() == 8) {
                    apiName = (String) recordArray.get(0);
                    version = (String) recordArray.get(1);
                    context = (String) recordArray.get(3);//omitting apiCreator
                    method = (String) recordArray.get(4);
                    hits = (Long) recordArray.get(5);
                    resourcePaths = (String) recordArray.get(6);
                    time = (Long) recordArray.get(7);
                    DateTime date = new DateTime(time);
                    apiUsageByResourcePath = new APIUsageByResourcePath(apiName, version, method, context, hits,
                            date.toString(formatter), resourcePaths);
                    usage.add(apiUsageByResourcePath);
                }
            }
        }
        return usage;
    } 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 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.j av a  2  s.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 www. j  ava2s  .  com
 * @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 ww w.ja va 2 s . c o m
 * @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   w w  w .j a va  2s .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./*from  w w  w .ja va2 s .  c  om*/
 *
 * @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);
    }
}