List of usage examples for org.joda.time LocalDateTime minusYears
public LocalDateTime minusYears(int years)
From source file:com.axelor.csv.script.ImportDateTime.java
License:Open Source License
public LocalDateTime updateYear(LocalDateTime dateTime, String year) { if (!Strings.isNullOrEmpty(year)) { Matcher matcher = patternYear.matcher(year); if (matcher.find()) { Integer years = Integer.parseInt(matcher.group()); if (year.startsWith("+")) dateTime = dateTime.plusYears(years); else if (year.startsWith("-")) dateTime = dateTime.minusYears(years); else/*from w w w . ja v a2 s . c o m*/ dateTime = dateTime.withYear(years); } } return dateTime; }
From source file:me.vertretungsplan.parser.ParserUtils.java
License:Mozilla Public License
static LocalDateTime parseDateTime(String string) { if (string == null) return null; reinitIfNeeded();/*from w w w. j av a2 s.c o m*/ string = string.replace("Stand:", "").replace("Import:", "").trim(); int i = 0; for (DateTimeFormatter f : dateTimeFormatters) { try { LocalDateTime dt = f.parseLocalDateTime(string); if (dateTimeFormats[i].contains("yyyy")) { return dt; } else { Duration currentYearDifference = abs(new Duration(DateTime.now(), dt.toDateTime())); Duration lastYearDifference = abs(new Duration(DateTime.now(), dt.minusYears(1).toDateTime())); Duration nextYearDifference = abs(new Duration(DateTime.now(), dt.plusYears(1).toDateTime())); if (lastYearDifference.isShorterThan(currentYearDifference)) { return DateTimeFormat.forPattern(dateTimeFormats[i]).withLocale(Locale.GERMAN) .withDefaultYear(f.getDefaultYear() - 1).parseLocalDateTime(string); } else if (nextYearDifference.isShorterThan(currentYearDifference)) { return DateTimeFormat.forPattern(dateTimeFormats[i]).withLocale(Locale.GERMAN) .withDefaultYear(f.getDefaultYear() + 1).parseLocalDateTime(string); } else { return dt; } } } catch (IllegalArgumentException e) { // Does not match this format, try the next one } i++; } // Does not match any known format :( return null; }
From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRestClientImpl.java
License:Open Source License
/** * This method gets the app usage data for invoking APIs * * @param tableName name of the required table in the database * @param idList Id list of applications * @return a collection containing the data related to App usage * @throws APIMgtUsageQueryServiceClientException if an error occurs while querying the database *///from ww w. j a v a2 s . c om private List<AppUsageDTO> getTopAppUsageData(String tableName, List<String> idList, String fromDate, String toDate, int limit) throws APIMgtUsageQueryServiceClientException { List<AppUsageDTO> topAppUsageDataList = new ArrayList<AppUsageDTO>(); 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) || (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) + "'"); } StringBuilder query = new StringBuilder("from " + tableName + " on " + idListQuery.toString() + " within " + getTimestamp(startDate) + "L, " + getTimestamp(endDate) + "L per '" + granularity + "' select " + APIUsageStatisticsClientConstants.APPLICATION_ID + ", " + APIUsageStatisticsClientConstants.USERNAME + ", sum(" + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT + ") as net_total_requests group by " + APIUsageStatisticsClientConstants.APPLICATION_ID + ", " + APIUsageStatisticsClientConstants.USERNAME + " order by net_total_requests DESC"); // limit enforced if (limit >= 0) { query.append(" limit" + limit); } query.append(";"); JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor( APIUsageStatisticsClientConstants.APIM_ACCESS_SUMMARY_SIDDHI_APP, query.toString()); String applicationId; String username; long requestCount; AppUsageDTO appUsageDTO; if (jsonObj != null) { JSONArray jArray = (JSONArray) jsonObj.get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER); for (Object record : jArray) { JSONArray recordArray = (JSONArray) record; if (recordArray.size() == 3) { applicationId = (String) recordArray.get(0); username = (String) recordArray.get(1); requestCount = (Long) recordArray.get(2); String appName = subscriberAppsMap.get(applicationId); boolean found = false; for (AppUsageDTO dto : topAppUsageDataList) { if (dto.getAppName().equals(appName)) { dto.addToUserCountArray(username, requestCount); found = true; break; } } if (!found) { appUsageDTO = new AppUsageDTO(); appUsageDTO.setAppName(appName); appUsageDTO.addToUserCountArray(username, requestCount); topAppUsageDataList.add(appUsageDTO); } } } } } } catch (APIManagementException e) { handleException("Error occurred while querying top app usage data from Stream Processor ", e); } return topAppUsageDataList; }
From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRestClientImpl.java
License:Open Source License
/** * This method gets the top user usage data for invoking APIs * * @param tableName name of the required table in the database * @param apiName API name/*from w ww. j a va 2 s . c om*/ * @param version version of the required API * @param fromDate Start date of the time span * @param toDate End date of time span * @return a collection containing the data related to Api usage * @throws APIMgtUsageQueryServiceClientException if an error occurs while querying the database */ private List<ApiTopUsersDTO> getTopApiUsers(String tableName, String apiName, String tenantDomain, String version, String fromDate, String toDate) throws APIMgtUsageQueryServiceClientException { List<ApiTopUsersDTO> apiTopUsersDataList = new ArrayList<ApiTopUsersDTO>(); try { StringBuilder topApiUserQuery; long totalRequestCount = getTotalRequestCountOfAPIVersion(tableName, apiName, tenantDomain, version, fromDate, toDate); 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; } topApiUserQuery = new StringBuilder("from " + APIUsageStatisticsClientConstants.API_USER_PER_APP_AGG + " on(" + APIUsageStatisticsClientConstants.API_CREATOR_TENANT_DOMAIN + "=='" + tenantDomain + "' AND " + APIUsageStatisticsClientConstants.API_NAME + "=='" + apiName); if (!APIUsageStatisticsClientConstants.FOR_ALL_API_VERSIONS.equals(version)) { topApiUserQuery.append("' AND " + APIUsageStatisticsClientConstants.API_VERSION + "=='" + version); } topApiUserQuery.append("') within " + getTimestamp(fromDate) + "L, " + getTimestamp(toDate) + "L per '" + granularity + "' select " + APIUsageStatisticsClientConstants.USERNAME + ", " + APIUsageStatisticsClientConstants.API_CREATOR + ", sum(" + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT + ") as net_total_requests group by " + APIUsageStatisticsClientConstants.USERNAME + ", " + APIUsageStatisticsClientConstants.API_CREATOR + " order by net_total_requests DESC;"); JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor( APIUsageStatisticsClientConstants.APIM_ACCESS_SUMMARY_SIDDHI_APP, topApiUserQuery.toString()); String username; Long requestCount; if (jsonObj != null) { JSONArray jArray = (JSONArray) jsonObj.get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER); for (Object record : jArray) { JSONArray recordArray = (JSONArray) record; if (recordArray.size() == 3) { String creator = (String) recordArray.get(1); if (creator != null && MultitenantUtils.getTenantDomain(creator).equals(tenantDomain)) { username = (String) recordArray.get(0); requestCount = (Long) recordArray.get(2); ApiTopUsersDTO apiTopUsersDTO = new ApiTopUsersDTO(); apiTopUsersDTO.setApiName(apiName); apiTopUsersDTO.setFromDate(fromDate); apiTopUsersDTO.setToDate(toDate); apiTopUsersDTO.setVersion(version); apiTopUsersDTO.setProvider(creator); //remove @carbon.super from super tenant users if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME .equals(MultitenantUtils.getTenantDomain(username))) { username = MultitenantUtils.getTenantAwareUsername(username); } apiTopUsersDTO.setUser(username); apiTopUsersDTO.setRequestCount(requestCount); apiTopUsersDTO.setTotalRequestCount(totalRequestCount); apiTopUsersDataList.add(apiTopUsersDTO); } } } } } catch (APIManagementException e) { handleException("Error occurred while querying top api users data from Stream Processor ", e); } return apiTopUsersDataList; }
From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRestClientImpl.java
License:Open Source License
/** * This method gets the API faulty invocation data * * @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 faulty invocations * @throws APIMgtUsageQueryServiceClientException if an error occurs while querying the database *//* ww w. j a v a2 s. c o m*/ private List<FaultCountDTO> getFaultAppUsageData(String tableName, List<String> idList, String fromDate, String toDate, int limit) throws APIMgtUsageQueryServiceClientException { List<FaultCountDTO> falseAppUsageDataList = new ArrayList<FaultCountDTO>(); try { if (!idList.isEmpty()) { String startDate = fromDate + ":00"; String endDate = toDate + ":00"; String granularity = APIUsageStatisticsClientConstants.MINUTES_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 || durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_WEEKS) > 0) { granularity = APIUsageStatisticsClientConstants.DAYS_GRANULARITY; } else if (durationBreakdown.get(APIUsageStatisticsClientConstants.DURATION_DAYS) > 0) { granularity = APIUsageStatisticsClientConstants.HOURS_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.APPLICATION_ID + ", " + APIUsageStatisticsClientConstants.API_NAME + ", " + APIUsageStatisticsClientConstants.API_CREATOR + ", sum(" + APIUsageStatisticsClientConstants.TOTAL_FAULT_COUNT + ") as total_faults group by " + APIUsageStatisticsClientConstants.APPLICATION_ID + ", " + APIUsageStatisticsClientConstants.API_CREATOR + ", " + APIUsageStatisticsClientConstants.API_NAME + ";"; JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor( APIUsageStatisticsClientConstants.APIM_FAULT_SUMMARY_SIDDHI_APP, query); String applicationId; String apiName; String apiCreator; long faultCount; FaultCountDTO faultCountDTO; if (jsonObj != null) { JSONArray jArray = (JSONArray) jsonObj.get(APIUsageStatisticsClientConstants.RECORDS_DELIMITER); for (Object record : jArray) { JSONArray recordArray = (JSONArray) record; if (recordArray.size() == 4) { applicationId = (String) recordArray.get(0); apiName = (String) recordArray.get(1); apiCreator = (String) recordArray.get(2); apiName = apiName + " (" + apiCreator + ")"; faultCount = (Long) recordArray.get(3); String appName = subscriberAppsMap.get(applicationId); boolean found = false; for (FaultCountDTO dto : falseAppUsageDataList) { if (dto.getAppName().equals(appName)) { dto.addToApiFaultCountArray(apiName, faultCount); found = true; break; } } if (!found) { faultCountDTO = new FaultCountDTO(); faultCountDTO.setAppName(appName); faultCountDTO.addToApiFaultCountArray(apiName, faultCount); falseAppUsageDataList.add(faultCountDTO); } } } } } } catch (APIManagementException e) { handleException("Error occurred while querying API faulty invocation data from Stream Processor ", e); } return falseAppUsageDataList; }
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 .java2 s .c om 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 *///from w ww . j a va2s.c o 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.com*/ * @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 ww w. j a v a2 s . co 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 ww w .ja va2 s . c o m * @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); } }