use of org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount in project carbon-apimgt by wso2.
the class AnalyticsMappingUtil method fromAPISubscriptionCountListToDTO.
/**
* Converts and APISubscriptionCountList to APISubscriptionCountListDTO.
*
* @param apiSubscriptionCountList list of APISubscriptionCount objects
* @return corresponding APISubscriptionCountListDTO object
*/
public static APISubscriptionCountListDTO fromAPISubscriptionCountListToDTO(List<APISubscriptionCount> apiSubscriptionCountList) {
APISubscriptionCountListDTO apiSubscriptionListDTO = new APISubscriptionCountListDTO();
List<APISubscriptionCountDTO> apiSubscriptionDTOList = new ArrayList<>();
apiSubscriptionListDTO.setCount(apiSubscriptionCountList.size());
for (APISubscriptionCount apiSubscriptionCount : apiSubscriptionCountList) {
APISubscriptionCountDTO apiSubscriptionDTO = new APISubscriptionCountDTO();
apiSubscriptionDTO.setId(apiSubscriptionCount.getId());
apiSubscriptionDTO.setName(apiSubscriptionCount.getName());
apiSubscriptionDTO.setVersion(apiSubscriptionCount.getVersion());
apiSubscriptionDTO.setProvider(apiSubscriptionCount.getProvider());
apiSubscriptionDTO.setCount(apiSubscriptionCount.getCount());
apiSubscriptionDTOList.add(apiSubscriptionDTO);
}
apiSubscriptionListDTO.setList(apiSubscriptionDTOList);
return apiSubscriptionListDTO;
}
use of org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount in project carbon-apimgt by wso2.
the class AnalyticsDAOImpl method getAPISubscriptionCount.
/**
* @see AnalyticsDAO#getAPISubscriptionCount(Instant, Instant, String)
*/
@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<APISubscriptionCount> getAPISubscriptionCount(Instant fromTime, Instant toTime, String apiId) throws APIMgtDAOException {
final String query;
if (StringUtils.isNotEmpty(apiId)) {
query = "SELECT api.UUID,api.NAME,api.VERSION,api.PROVIDER,count(subs.UUID) as COUNT " + "FROM AM_SUBSCRIPTION subs,AM_API api " + "WHERE api.UUID=subs.API_ID " + "AND (subs.CREATED_TIME BETWEEN ? AND ?) " + "AND subs.SUB_STATUS = 'ACTIVE' " + "AND api.UUID=? " + "GROUP BY api.UUID,api.NAME,api.VERSION,api.PROVIDER";
} else {
query = "SELECT api.UUID,api.NAME,api.VERSION,api.PROVIDER,count(subs.UUID) as COUNT " + "FROM AM_SUBSCRIPTION subs,AM_API api " + "WHERE api.UUID=subs.API_ID " + "AND (subs.CREATED_TIME BETWEEN ? AND ?) " + "AND subs.SUB_STATUS = 'ACTIVE' " + "GROUP BY api.UUID,api.NAME,api.VERSION,api.PROVIDER";
}
List<APISubscriptionCount> apiSubscriptionCountList = 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(apiId)) {
statement.setString(3, apiId);
}
log.debug("Executing query: {} ", query);
statement.execute();
try (ResultSet rs = statement.getResultSet()) {
while (rs.next()) {
APISubscriptionCount apiSubscriptionCount = new APISubscriptionCount();
apiSubscriptionCount.setId(rs.getString("UUID"));
apiSubscriptionCount.setName(rs.getString("NAME"));
apiSubscriptionCount.setVersion(rs.getString("VERSION"));
apiSubscriptionCount.setProvider(rs.getString("PROVIDER"));
apiSubscriptionCount.setCount(rs.getInt("COUNT"));
apiSubscriptionCountList.add(apiSubscriptionCount);
}
}
} catch (SQLException e) {
throw new APIMgtDAOException("Error while creating database connection/prepared-statement", e);
}
return apiSubscriptionCountList;
}
use of org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount in project carbon-apimgt by wso2.
the class AnalyticsDAOImplIT method testGetSubscriptionCountPerAPI.
@Test
public void testGetSubscriptionCountPerAPI() throws Exception {
Instant fromTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis());
API testAPI = TestUtil.addTestAPI();
API testAPI1 = TestUtil.addCustomAPI("TestAPI1", "1.0.0", "test1");
API testAPI2 = TestUtil.addCustomAPI("TestAPI2", "1.0.0", "test2");
Application testApplication = TestUtil.addTestApplication();
Application testApplication2 = TestUtil.addCustomApplication("APP2", "admin");
TestUtil.subscribeToAPI(testAPI, testApplication);
TestUtil.subscribeToAPI(testAPI, testApplication2);
TestUtil.subscribeToAPI(testAPI1, testApplication2);
TestUtil.subscribeToAPI(testAPI1, testApplication);
TestUtil.subscribeToAPI(testAPI2, testApplication2);
Instant toTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis() + DELAY_TIME);
AnalyticsDAO analyticsDAO = DAOFactory.getAnalyticsDAO();
List<APISubscriptionCount> subscriptionCount = analyticsDAO.getAPISubscriptionCount(fromTimeStamp, toTimeStamp, null);
Assert.assertEquals(subscriptionCount.size(), 3);
}
use of org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount in project carbon-apimgt by wso2.
the class AnalyzerImplTestCase method testGetAPISubscrptionCount.
@Test(description = "Get Subscription count for API test")
public void testGetAPISubscrptionCount() throws APIManagementException {
AnalyticsDAO analyticsDAO = Mockito.mock(AnalyticsDAO.class);
APISubscriptionCount apiSubscriptionCount = new APISubscriptionCount();
List<APISubscriptionCount> apiSubscriptionCountList = new ArrayList<>();
apiSubscriptionCountList.add(apiSubscriptionCount);
Analyzer analyzer = getAnalyzerImpl(analyticsDAO);
when(analyticsDAO.getAPISubscriptionCount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenReturn(apiSubscriptionCountList);
List<APISubscriptionCount> apiSubscriptionCountResult = analyzer.getAPISubscriptionCount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
Assert.assertNotNull(apiSubscriptionCountResult);
verify(analyticsDAO, Mockito.times(1)).getAPISubscriptionCount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
// Error path
Mockito.when(analyticsDAO.getAPISubscriptionCount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenThrow(APIMgtDAOException.class);
try {
analyzer.getAPISubscriptionCount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while fetching API subscription count information");
}
}
use of org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount in project carbon-apimgt by wso2.
the class AnalyticsMappingUtilTestCase method fromAPISubscriptionCountListToDTOTest.
@Test
public void fromAPISubscriptionCountListToDTOTest() {
List<APISubscriptionCount> apiSubscriptionCountList = new ArrayList<>();
apiSubscriptionCountList.add(SampleTestObjectCreator.createRandomAPISubscriptionCountObject());
apiSubscriptionCountList.add(SampleTestObjectCreator.createRandomAPISubscriptionCountObject());
apiSubscriptionCountList.add(SampleTestObjectCreator.createRandomAPISubscriptionCountObject());
APISubscriptionCountListDTO apiSubscriptionCountListDTO = AnalyticsMappingUtil.fromAPISubscriptionCountListToDTO(apiSubscriptionCountList);
Assert.assertEquals(apiSubscriptionCountList.size(), apiSubscriptionCountListDTO.getList().size());
for (int i = 0; i < apiSubscriptionCountList.size(); i++) {
Assert.assertEquals(apiSubscriptionCountList.get(i).getProvider(), apiSubscriptionCountListDTO.getList().get(i).getProvider());
Assert.assertEquals(apiSubscriptionCountList.get(i).getVersion(), apiSubscriptionCountListDTO.getList().get(i).getVersion());
Assert.assertEquals(apiSubscriptionCountList.get(i).getId(), apiSubscriptionCountListDTO.getList().get(i).getId());
}
}
Aggregations