Search in sources :

Example 1 with APISubscriptionCount

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;
}
Also used : APISubscriptionCountListDTO(org.wso2.carbon.apimgt.rest.api.analytics.dto.APISubscriptionCountListDTO) APISubscriptionCount(org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount) APISubscriptionCountDTO(org.wso2.carbon.apimgt.rest.api.analytics.dto.APISubscriptionCountDTO) ArrayList(java.util.ArrayList)

Example 2 with APISubscriptionCount

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;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APISubscriptionCount(org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 3 with APISubscriptionCount

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);
}
Also used : AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO) APISubscriptionCount(org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount) Instant(java.time.Instant) API(org.wso2.carbon.apimgt.core.models.API) Application(org.wso2.carbon.apimgt.core.models.Application) Test(org.testng.annotations.Test)

Example 4 with APISubscriptionCount

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");
    }
}
Also used : AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APISubscriptionCount(org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount) ArrayList(java.util.ArrayList) Analyzer(org.wso2.carbon.apimgt.core.api.Analyzer) Test(org.testng.annotations.Test)

Example 5 with APISubscriptionCount

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());
    }
}
Also used : APISubscriptionCountListDTO(org.wso2.carbon.apimgt.rest.api.analytics.dto.APISubscriptionCountListDTO) APISubscriptionCount(org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Aggregations

APISubscriptionCount (org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount)7 ArrayList (java.util.ArrayList)4 Test (org.testng.annotations.Test)3 APISubscriptionCountListDTO (org.wso2.carbon.apimgt.rest.api.analytics.dto.APISubscriptionCountListDTO)3 Analyzer (org.wso2.carbon.apimgt.core.api.Analyzer)2 AnalyticsDAO (org.wso2.carbon.apimgt.core.dao.AnalyticsDAO)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Instant (java.time.Instant)1 Random (java.util.Random)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1 API (org.wso2.carbon.apimgt.core.models.API)1 Application (org.wso2.carbon.apimgt.core.models.Application)1 APISubscriptionCountDTO (org.wso2.carbon.apimgt.rest.api.analytics.dto.APISubscriptionCountDTO)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1