use of org.wso2.carbon.apimgt.core.models.analytics.APICount in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createRandomAPICountObject.
/**
* Create Random APICount Object.
*
* @return Random APICount Object
*/
public static APICount createRandomAPICountObject() {
APICount apiCount = new APICount();
apiCount.setCount(ThreadLocalRandom.current().nextLong());
apiCount.setTimestamp(ThreadLocalRandom.current().nextLong());
return apiCount;
}
use of org.wso2.carbon.apimgt.core.models.analytics.APICount in project carbon-apimgt by wso2.
the class AnalyticsMappingUtilTestCase method fromAPICountToListDTOTest.
@Test
public void fromAPICountToListDTOTest() {
List<APICount> apiCountList = new ArrayList<>();
apiCountList.add(SampleTestObjectCreator.createRandomAPICountObject());
apiCountList.add(SampleTestObjectCreator.createRandomAPICountObject());
apiCountList.add(SampleTestObjectCreator.createRandomAPICountObject());
APICountListDTO apiCountListDTO = AnalyticsMappingUtil.fromAPICountToListDTO(apiCountList, ZoneOffset.UTC);
Assert.assertEquals(apiCountList.size(), apiCountListDTO.getList().size());
for (int i = 0; i < apiCountList.size(); i++) {
Assert.assertEquals(epochToISO8601DateTime(apiCountList.get(i).getTimestamp(), ZoneOffset.UTC), apiCountListDTO.getList().get(i).getTime());
}
}
use of org.wso2.carbon.apimgt.core.models.analytics.APICount in project carbon-apimgt by wso2.
the class AnalyticsDAOImpl method getAPICount.
/**
* @see AnalyticsDAO#getAPICount(Instant, Instant, String)
*/
@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<APICount> getAPICount(Instant fromTime, Instant toTime, String createdBy) throws APIMgtDAOException {
final String query;
if (StringUtils.isNotEmpty(createdBy)) {
query = "SELECT COUNT(UUID) AS count, CREATED_TIME AS time " + "FROM AM_API " + "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_API " + "WHERE (CREATED_TIME BETWEEN ? AND ?) " + "GROUP BY CREATED_TIME " + "ORDER BY CREATED_TIME ASC";
}
List<APICount> apiInfoList = new ArrayList<>();
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setTimestamp(1, Timestamp.from(fromTime));
statement.setTimestamp(2, Timestamp.from(toTime));
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()) {
APICount apiCount = new APICount();
count += rs.getLong("count");
apiCount.setTimestamp(rs.getTimestamp("time").getTime());
apiCount.setCount(count);
apiInfoList.add(apiCount);
}
}
} catch (SQLException e) {
throw new APIMgtDAOException("Error while creating database connection/prepared-statement", e);
}
return apiInfoList;
}
use of org.wso2.carbon.apimgt.core.models.analytics.APICount in project carbon-apimgt by wso2.
the class AnalyzerImplTestCase method testGetAPICount.
@Test(description = "Get API count test")
public void testGetAPICount() throws APIManagementException {
AnalyticsDAO analyticsDAO = Mockito.mock(AnalyticsDAO.class);
APICount apiCount1 = new APICount();
APICount apiCount2 = new APICount();
List<APICount> apiCountList = new ArrayList<>();
apiCountList.add(apiCount1);
apiCountList.add(apiCount2);
Analyzer analyzer = getAnalyzerImpl(analyticsDAO);
when(analyticsDAO.getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenReturn(apiCountList);
List<APICount> apiCountListFromDB = analyzer.getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
Assert.assertNotNull(apiCountListFromDB);
verify(analyticsDAO, Mockito.times(1)).getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
// Error path
Mockito.when(analyticsDAO.getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenThrow(APIMgtDAOException.class);
try {
analyzer.getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while fetching API count information");
}
}
use of org.wso2.carbon.apimgt.core.models.analytics.APICount in project carbon-apimgt by wso2.
the class AnalyticsMappingUtil method fromAPICountToDTO.
private static APICountDTO fromAPICountToDTO(APICount apiCount, ZoneId zoneId) {
APICountDTO apiCountDTO = new APICountDTO();
apiCountDTO.setTime(epochToISO8601DateTime(apiCount.getTimestamp(), zoneId));
apiCountDTO.setCount(apiCount.getCount());
return apiCountDTO;
}
Aggregations