Search in sources :

Example 6 with APICount

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;
}
Also used : APICount(org.wso2.carbon.apimgt.core.models.analytics.APICount)

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

Example 8 with APICount

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;
}
Also used : APICount(org.wso2.carbon.apimgt.core.models.analytics.APICount) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) 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 9 with APICount

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

Example 10 with APICount

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;
}
Also used : APICountDTO(org.wso2.carbon.apimgt.rest.api.analytics.dto.APICountDTO)

Aggregations

APICount (org.wso2.carbon.apimgt.core.models.analytics.APICount)7 ArrayList (java.util.ArrayList)5 Test (org.testng.annotations.Test)3 APICountListDTO (org.wso2.carbon.apimgt.rest.api.analytics.dto.APICountListDTO)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 APICountDTO (org.wso2.carbon.apimgt.rest.api.analytics.dto.APICountDTO)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 ZoneId (java.time.ZoneId)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)1