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;
}
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;
}
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;
}
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());
}
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);
}
Aggregations