Search in sources :

Example 1 with AnalyticsDAO

use of org.wso2.carbon.apimgt.core.dao.AnalyticsDAO in project carbon-apimgt by wso2.

the class AnalyticsDAOImpl method getApplicationCount.

/**
 * @see AnalyticsDAO#getApplicationCount(Instant, Instant, String)
 */
@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<ApplicationCount> getApplicationCount(Instant fromTimestamp, Instant toTimestamp, String createdBy) throws APIMgtDAOException {
    final String query;
    if (StringUtils.isNotEmpty(createdBy)) {
        query = "SELECT COUNT(UUID) AS count, CREATED_TIME AS time " + "FROM AM_APPLICATION " + "WHERE (CREATED_TIME BETWEEN ? AND ?) " + "AND CREATED_BY = ?" + "GROUP BY CREATED_TIME " + "ORDER BY CREATED_TIME ASC";
    } else {
        query = "SELECT COUNT(UUID) AS count, CREATED_TIME AS time " + "FROM AM_APPLICATION " + "WHERE (CREATED_TIME BETWEEN ? AND ?) " + "GROUP BY CREATED_TIME " + "ORDER BY CREATED_TIME ASC";
    }
    List<ApplicationCount> applicationCountList = 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()) {
            long count = 0;
            while (rs.next()) {
                ApplicationCount applicationCount = new ApplicationCount();
                count += rs.getLong("count");
                applicationCount.setTimestamp(rs.getTimestamp("time").getTime());
                applicationCount.setCount(count);
                applicationCountList.add(applicationCount);
            }
        }
    } catch (SQLException e) {
        String errorMsg = "Error while creating database connection/prepared-statement";
        throw new APIMgtDAOException(errorMsg, e);
    }
    return applicationCountList;
}
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) PreparedStatement(java.sql.PreparedStatement) ApplicationCount(org.wso2.carbon.apimgt.core.models.analytics.ApplicationCount) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with AnalyticsDAO

use of org.wso2.carbon.apimgt.core.dao.AnalyticsDAO in project carbon-apimgt by wso2.

the class DAOFactory method getAnalyticsDAO.

/**
 * To get the AnalyticsDao object. Depends on different vendors.
 *
 * @return AnalyticsDAO object
 * @throws APIMgtDAOException if error during getting analytics database connection
 */
public static AnalyticsDAO getAnalyticsDAO() throws APIMgtDAOException {
    AnalyticsDAO analyticsDAO;
    boolean isAnalyticsEnabled = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getAnalyticsConfigurations().isEnabled();
    if (isAnalyticsEnabled) {
        try (Connection connection = DAOUtil.getAnalyticsConnection()) {
            analyticsDAO = getAnalyticsDaoImplForVendor(connection);
        } catch (SQLException e) {
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting FunctionDAO", e);
        }
    } else {
        // if analytics is not enabled create a normal AMDB data connection to check db driver
        try (Connection connection = DAOUtil.getConnection()) {
            analyticsDAO = getAnalyticsDaoImplForVendor(connection);
        } catch (SQLException e) {
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting FunctionDAO", e);
        }
    }
    return analyticsDAO;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 3 with AnalyticsDAO

use of org.wso2.carbon.apimgt.core.dao.AnalyticsDAO in project carbon-apimgt by wso2.

the class DAOFactory method getAnalyticsDaoImplForVendor.

private static AnalyticsDAO getAnalyticsDaoImplForVendor(Connection connection) throws SQLException, APIMgtDAOException {
    AnalyticsDAO analyticsDAO = null;
    String driverName = connection.getMetaData().getDriverName();
    if (driverName.contains(MYSQL) || driverName.contains(H2)) {
        analyticsDAO = new AnalyticsDAOImpl();
    } else if (driverName.contains(DB2)) {
    } else if (driverName.contains(MS_SQL) || driverName.contains(MICROSOFT)) {
        analyticsDAO = new AnalyticsDAOImpl();
    } else if (driverName.contains(POSTGRE)) {
        analyticsDAO = new AnalyticsDAOImpl();
    } else if (driverName.contains(ORACLE)) {
        analyticsDAO = new AnalyticsDAOImpl();
    } else {
        throw new APIMgtDAOException("Unhandled DB Type detected");
    }
    return analyticsDAO;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO)

Example 4 with AnalyticsDAO

use of org.wso2.carbon.apimgt.core.dao.AnalyticsDAO in project carbon-apimgt by wso2.

the class AnalyticsDAOImplIT method testGetSubscriptionList.

@Test
public void testGetSubscriptionList() throws Exception {
    Instant fromTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis());
    API testAPI = TestUtil.addTestAPI();
    Application testApplication = TestUtil.addTestApplication();
    Subscription subscription = TestUtil.subscribeToAPI(testAPI, testApplication);
    Instant toTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis() + DELAY_TIME);
    AnalyticsDAO analyticsDAO = DAOFactory.getAnalyticsDAO();
    List<SubscriptionInfo> subscriptionInfo = analyticsDAO.getSubscriptionInfo(fromTimeStamp, toTimeStamp, null);
    Assert.assertEquals(subscriptionInfo.size(), 1);
    SubscriptionInfo subscriptionInfoResult = subscriptionInfo.get(0);
    Assert.assertEquals(subscription.getId(), subscriptionInfoResult.getId());
    Assert.assertEquals(subscription.getApi().getName(), subscriptionInfoResult.getName());
    Assert.assertEquals(subscription.getApplication().getName(), subscriptionInfoResult.getAppName());
}
Also used : AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO) Instant(java.time.Instant) API(org.wso2.carbon.apimgt.core.models.API) SubscriptionInfo(org.wso2.carbon.apimgt.core.models.analytics.SubscriptionInfo) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) Test(org.testng.annotations.Test)

Example 5 with AnalyticsDAO

use of org.wso2.carbon.apimgt.core.dao.AnalyticsDAO in project carbon-apimgt by wso2.

the class AnalyticsDAOImplIT method testGetAPICount.

@Test
public void testGetAPICount() throws Exception {
    Instant fromTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis());
    TestUtil.addTestAPI();
    Instant toTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis() + DELAY_TIME);
    AnalyticsDAO analyticsDAO = DAOFactory.getAnalyticsDAO();
    List<APICount> applicationCountList = analyticsDAO.getAPICount(fromTimeStamp, toTimeStamp, null);
    Assert.assertEquals(applicationCountList.size(), 1);
}
Also used : APICount(org.wso2.carbon.apimgt.core.models.analytics.APICount) AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO) Instant(java.time.Instant) Test(org.testng.annotations.Test)

Aggregations

AnalyticsDAO (org.wso2.carbon.apimgt.core.dao.AnalyticsDAO)14 ArrayList (java.util.ArrayList)12 Test (org.testng.annotations.Test)12 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)8 Connection (java.sql.Connection)7 SQLException (java.sql.SQLException)7 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)6 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 Instant (java.time.Instant)6 Analyzer (org.wso2.carbon.apimgt.core.api.Analyzer)6 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 APISubscriptionCount (org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount)6 API (org.wso2.carbon.apimgt.core.models.API)4 Application (org.wso2.carbon.apimgt.core.models.Application)3 APICount (org.wso2.carbon.apimgt.core.models.analytics.APICount)3 APIInfo (org.wso2.carbon.apimgt.core.models.analytics.APIInfo)3 ApplicationCount (org.wso2.carbon.apimgt.core.models.analytics.ApplicationCount)3 SubscriptionCount (org.wso2.carbon.apimgt.core.models.analytics.SubscriptionCount)3 SubscriptionInfo (org.wso2.carbon.apimgt.core.models.analytics.SubscriptionInfo)3