Search in sources :

Example 6 with SubscriptionInfo

use of org.wso2.carbon.apimgt.core.models.analytics.SubscriptionInfo in project carbon-apimgt by wso2.

the class AnalyticsDAOImpl method getSubscriptionInfo.

/**
 * @see AnalyticsDAO#getSubscriptionInfo(Instant, Instant, String)
 */
@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<SubscriptionInfo> getSubscriptionInfo(Instant fromTimestamp, Instant toTimestamp, String createdBy) throws APIMgtDAOException {
    final String query;
    if (StringUtils.isNotEmpty(createdBy)) {
        query = "SELECT sub.UUID, api.NAME as API_NAME, api.VERSION as API_VERSION, app.NAME " + "as APP_NAME, app.DESCRIPTION, sub.CREATED_TIME , policy.DISPLAY_NAME , sub.SUB_STATUS " + "FROM AM_API api, AM_SUBSCRIPTION sub, AM_APPLICATION app, AM_SUBSCRIPTION_POLICY policy " + "WHERE (sub.CREATED_TIME BETWEEN ? AND ?) " + "AND api.UUID = sub.API_ID " + "AND sub.APPLICATION_ID = app.UUID " + "AND sub.CREATED_BY= ? " + "AND sub.TIER_ID = policy.UUID ";
    } else {
        query = "SELECT sub.UUID, api.NAME as API_NAME, api.VERSION as API_VERSION, app.NAME " + "as APP_NAME, app.DESCRIPTION, sub.CREATED_TIME , policy.DISPLAY_NAME , sub.SUB_STATUS " + "FROM AM_API api, AM_SUBSCRIPTION sub, AM_APPLICATION app, AM_SUBSCRIPTION_POLICY policy " + "WHERE (sub.CREATED_TIME BETWEEN ? AND ?) " + "AND api.UUID = sub.API_ID " + "AND sub.APPLICATION_ID = app.UUID " + "AND sub.TIER_ID = policy.UUID ";
    }
    List<SubscriptionInfo> subscriptionInfoList = new ArrayList<>();
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setTimestamp(1, Timestamp.from(fromTimestamp));
        statement.setTimestamp(2, Timestamp.from(toTimestamp));
        if (StringUtils.isNotEmpty(createdBy)) {
            statement.setString(3, createdBy);
        }
        log.debug("Executing query: {} ", query);
        statement.execute();
        try (ResultSet rs = statement.getResultSet()) {
            while (rs.next()) {
                SubscriptionInfo subscriptionInfo = new SubscriptionInfo();
                subscriptionInfo.setId(rs.getString("UUID"));
                subscriptionInfo.setName(rs.getString("API_NAME"));
                subscriptionInfo.setVersion(rs.getString("API_VERSION"));
                subscriptionInfo.setAppName(rs.getString("APP_NAME"));
                subscriptionInfo.setDescription(rs.getString("DESCRIPTION"));
                subscriptionInfo.setCreatedTime(rs.getTimestamp("CREATED_TIME").getTime());
                subscriptionInfo.setSubscriptionStatus(rs.getString("SUB_STATUS"));
                subscriptionInfo.setSubscriptionTier(rs.getString("DISPLAY_NAME"));
                subscriptionInfoList.add(subscriptionInfo);
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error while creating database connection/prepared-statement", e);
    }
    return subscriptionInfoList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SubscriptionInfo(org.wso2.carbon.apimgt.core.models.analytics.SubscriptionInfo) PreparedStatement(java.sql.PreparedStatement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 7 with SubscriptionInfo

use of org.wso2.carbon.apimgt.core.models.analytics.SubscriptionInfo in project carbon-apimgt by wso2.

the class SubscriptionApiServiceImpl method subscriptionListGet.

/**
 * Get list of subscriptions info over time
 *
 * @param startTime Filter for start time stamp
 * @param endTime   Filter for end time stamp
 * @param createdBy Filter for createdBy
 * @param request   MSF4J request
 * @return Subscriptions information over time
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response subscriptionListGet(String startTime, String endTime, String createdBy, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        log.debug("Retrieving subscriptions info. [From: {}  To: {} Created By: {}]", startTime, endTime, createdBy);
        Analyzer analyzer = RestApiUtil.getAnalyzer(username);
        ZoneId requestTimezone = RestApiUtil.getRequestTimeZone(startTime);
        List<SubscriptionInfo> subscriptionInfoList = analyzer.getSubscriptionInfo(fromISO8601ToInstant(startTime), fromISO8601ToInstant(endTime), createdBy);
        SubscriptionInfoListDTO subscriptionInfoListDTO = AnalyticsMappingUtil.fromSubscriptionInfoListToDTO(subscriptionInfoList, requestTimezone);
        return Response.ok().entity(subscriptionInfoListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving subscription information";
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : ZoneId(java.time.ZoneId) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionInfoListDTO(org.wso2.carbon.apimgt.rest.api.analytics.dto.SubscriptionInfoListDTO) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) SubscriptionInfo(org.wso2.carbon.apimgt.core.models.analytics.SubscriptionInfo) Analyzer(org.wso2.carbon.apimgt.core.api.Analyzer)

Aggregations

SubscriptionInfo (org.wso2.carbon.apimgt.core.models.analytics.SubscriptionInfo)7 ArrayList (java.util.ArrayList)4 Test (org.testng.annotations.Test)3 SubscriptionInfoListDTO (org.wso2.carbon.apimgt.rest.api.analytics.dto.SubscriptionInfoListDTO)3 Analyzer (org.wso2.carbon.apimgt.core.api.Analyzer)2 AnalyticsDAO (org.wso2.carbon.apimgt.core.dao.AnalyticsDAO)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Instant (java.time.Instant)1 ZoneId (java.time.ZoneId)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1 API (org.wso2.carbon.apimgt.core.models.API)1 Application (org.wso2.carbon.apimgt.core.models.Application)1 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)1 SubscriptionInfoDTO (org.wso2.carbon.apimgt.rest.api.analytics.dto.SubscriptionInfoDTO)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1