Example usage for org.joda.time LocalDateTime now

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

Introduction

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

Prototype

public static LocalDateTime now(Chronology chronology) 

Source Link

Document

Obtains a LocalDateTime set to the current system millisecond time using the specified chronology.

Usage

From source file:com.act.lcms.db.io.LoadStandardIonAnalysisTableIntoDB.java

License:Open Source License

public static void main(String[] args) throws Exception {
    Options opts = new Options();
    for (Option.Builder b : OPTION_BUILDERS) {
        opts.addOption(b.build());//from   www  . ja va 2  s  .  c om
    }

    CommandLine cl = null;

    try {
        CommandLineParser parser = new DefaultParser();
        cl = parser.parse(opts, args);
    } catch (ParseException e) {
        System.err.format("Argument parsing failed: %s\n", e.getMessage());
        HELP_FORMATTER.printHelp(LoadStandardIonAnalysisTableIntoDB.class.getCanonicalName(), HELP_MESSAGE,
                opts, null, true);
        System.exit(1);
    }

    if (cl.hasOption("help")) {
        HELP_FORMATTER.printHelp(LoadStandardIonAnalysisTableIntoDB.class.getCanonicalName(), HELP_MESSAGE,
                opts, null, true);
        return;
    }

    File inputFile = new File(cl.getOptionValue(OPTION_FILE_PATH));
    if (!inputFile.exists()) {
        System.err.format("Unable to find input file at %s\n", cl.getOptionValue(OPTION_FILE_PATH));
        new HelpFormatter().printHelp(LoadConstructAnalysisTableIntoDB.class.getCanonicalName(), opts, true);
        System.exit(1);
    }

    try (DB db = DB.openDBFromCLI(cl)) {
        db.getConn().setAutoCommit(false);

        TSVParser parser = new TSVParser();
        parser.parse(inputFile);

        for (Map<String, String> row : parser.getResults()) {
            Integer standardIonResultId = Integer.parseInt(row.get(
                    ExportStandardIonResultsFromDB.STANDARD_ION_HEADER_FIELDS.STANDARD_ION_RESULT_ID.name()));

            String dbValueOfMetlinIon = ExportStandardIonResultsFromDB.NULL_VALUE;
            StandardIonResult ionResult = StandardIonResult.getInstance().getById(db, standardIonResultId);
            if (ionResult.getManualOverrideId() != null) {
                // There is an existing manual override ion in the DB
                CuratedStandardMetlinIon curatedChemical = CuratedStandardMetlinIon.getBestMetlinIon(db,
                        ionResult.getManualOverrideId());
                dbValueOfMetlinIon = curatedChemical.getBestMetlinIon();
            }

            String manualPickOfMetlinIon = row
                    .get(ExportStandardIonResultsFromDB.STANDARD_ION_HEADER_FIELDS.MANUAL_PICK.name());

            // If the manual metlin ion pick row is not NULL and it is not the same as the value stored in the DB, then
            // we need to add a new entry to the curated metlin ion table.
            if (!manualPickOfMetlinIon.equals(ExportStandardIonResultsFromDB.NULL_VALUE)
                    && !manualPickOfMetlinIon.equals(dbValueOfMetlinIon)) {
                System.out.format("Manual override has been found, so updating the DB\n");
                // A manual entry was created.
                if (!MS1.VALID_MS1_IONS.contains(manualPickOfMetlinIon)) {
                    System.err.format("ERROR: found invalid chemical name: %s\n", manualPickOfMetlinIon);
                    System.exit(-1);
                }

                String note = row.get(ExportStandardIonResultsFromDB.STANDARD_ION_HEADER_FIELDS.NOTE.name());
                CuratedStandardMetlinIon result = CuratedStandardMetlinIon.insertCuratedStandardMetlinIonIntoDB(
                        db, LocalDateTime.now(CuratedStandardMetlinIon.utcDateTimeZone),
                        cl.getOptionValue(OPTION_AUTHOR), manualPickOfMetlinIon, note, standardIonResultId);

                if (result == null) {
                    System.err.format(
                            "WARNING: Could not insert curated entry to the curated metlin ion table\n",
                            manualPickOfMetlinIon);
                    System.exit(-1);
                } else {
                    StandardIonResult getIonResult = StandardIonResult.getInstance().getById(db,
                            standardIonResultId);
                    getIonResult.setManualOverrideId(result.getId());
                    if (!StandardIonResult.getInstance().update(db, getIonResult)) {
                        System.err.format(
                                "WARNING: Could not insert manual override id to the standard ion table\n",
                                manualPickOfMetlinIon);
                        System.exit(-1);
                    } else {
                        System.out.format(
                                "Successfully committed updates to the standard ion table and the curated metlin ion table\n");
                    }
                }
            }
        }

        db.getConn().commit();
    }
}

From source file:org.apache.gobblin.data.management.copy.TimeAwareRecursiveCopyableDataset.java

License:Apache License

public TimeAwareRecursiveCopyableDataset(FileSystem fs, Path rootPath, Properties properties, Path glob) {
    super(fs, rootPath, properties, glob);
    this.lookbackTime = properties.getProperty(LOOKBACK_TIME_KEY);
    PeriodFormatter periodFormatter = new PeriodFormatterBuilder().appendDays().appendSuffix("d").appendHours()
            .appendSuffix("h").toFormatter();
    this.lookbackPeriod = periodFormatter.parsePeriod(lookbackTime);
    this.datePattern = properties.getProperty(DATE_PATTERN_KEY);
    this.isPatternHourly = isDatePatternHourly(datePattern);
    this.currentTime = properties.containsKey(DATE_PATTERN_TIMEZONE_KEY)
            ? LocalDateTime.now(DateTimeZone.forID(DATE_PATTERN_TIMEZONE_KEY))
            : LocalDateTime.now(DateTimeZone.forID(DEFAULT_DATE_PATTERN_TIMEZONE));

    //Daily directories cannot have a "hourly" lookback pattern. But hourly directories can accept lookback pattern with days.
    if (!this.isPatternHourly) {
        Assert.assertTrue(isLookbackTimeStringDaily(this.lookbackTime),
                "Expected day format for lookback time; found hourly format");
    }/*  w  w  w . ja  va2s . c o m*/
}

From source file:org.openlmis.fulfillment.JaVersDateProvider.java

License:Open Source License

public LocalDateTime now() {
    return LocalDateTime.now(DATE_TIME_ZONE);
}

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
 */// w  w w  .  j  a  va 2s. co m
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  ww w.jav a2  s  .com*/
 * @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
 *//*from   w w  w  .j a  v  a  2 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 ww.j  ava2 s . co  m
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  w  w  . j  av a 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//from  w  w w  .ja v  a  2  s  .c  om
 * @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/*w  w w.  ja v a  2  s. com*/
 * @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);
    }
}