use of org.wso2.carbon.apimgt.core.models.analytics.ApplicationCount 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.models.analytics.ApplicationCount in project carbon-apimgt by wso2.
the class AnalyticsMappingUtil method fromApplicationCountToListDTO.
/**
* Converts and ApplicationCountList to ApplicationCountListDTO.
*
* @param applicationCountList list of ApplicationCount objects
* @return corresponding ApplicationCountListDTO object
*/
public static ApplicationCountListDTO fromApplicationCountToListDTO(List<ApplicationCount> applicationCountList, ZoneId zoneId) {
ApplicationCountListDTO applicationCountListDTO = new ApplicationCountListDTO();
List<ApplicationCountDTO> applicationCountDTOList = new ArrayList<>();
applicationCountListDTO.setCount(applicationCountList.size());
for (ApplicationCount applicationCount : applicationCountList) {
applicationCountDTOList.add(fromApplicationCountToDTO(applicationCount, zoneId));
}
applicationCountListDTO.setList(applicationCountDTOList);
return applicationCountListDTO;
}
use of org.wso2.carbon.apimgt.core.models.analytics.ApplicationCount in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createRandomApplicationCountObject.
/**
* Create Random ApplicationCount Object.
*
* @return ApplicationCount object
*/
public static ApplicationCount createRandomApplicationCountObject() {
ApplicationCount applicationCount = new ApplicationCount();
applicationCount.setCount(ThreadLocalRandom.current().nextLong());
applicationCount.setTimestamp(ThreadLocalRandom.current().nextLong());
return applicationCount;
}
use of org.wso2.carbon.apimgt.core.models.analytics.ApplicationCount in project carbon-apimgt by wso2.
the class AnalyzerImplTestCase method testGetApplicationCount.
@Test(description = "Get application count test")
public void testGetApplicationCount() throws APIManagementException {
AnalyticsDAO analyticsDAO = Mockito.mock(AnalyticsDAO.class);
ApplicationCount applicationCount1 = new ApplicationCount();
ApplicationCount applicationCount2 = new ApplicationCount();
List<ApplicationCount> dummyApplicationCountList = new ArrayList<>();
dummyApplicationCountList.add(applicationCount1);
dummyApplicationCountList.add(applicationCount2);
Analyzer analyzer = getAnalyzerImpl(analyticsDAO);
when(analyticsDAO.getApplicationCount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenReturn(dummyApplicationCountList);
List<ApplicationCount> applicationCountListFromDB = analyzer.getApplicationCount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
Assert.assertNotNull(applicationCountListFromDB);
verify(analyticsDAO, Mockito.times(1)).getApplicationCount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
// Error path
Mockito.when(analyticsDAO.getApplicationCount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenThrow(APIMgtDAOException.class);
try {
analyzer.getApplicationCount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while fetching application count information");
}
}
use of org.wso2.carbon.apimgt.core.models.analytics.ApplicationCount in project carbon-apimgt by wso2.
the class AnalyticsDAOImplIT method testGetApplicationCount.
@Test
public void testGetApplicationCount() throws Exception {
Instant fromTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis());
TestUtil.addCustomApplication("app1", "john");
Instant toTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis() + DELAY_TIME);
AnalyticsDAO analyticsDAO = DAOFactory.getAnalyticsDAO();
List<ApplicationCount> applicationCountList = analyticsDAO.getApplicationCount(fromTimeStamp, toTimeStamp, null);
Assert.assertEquals(applicationCountList.size(), 1);
}
Aggregations