Search in sources :

Example 1 with ApplicationCount

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;
}
Also used : 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) ApplicationCount(org.wso2.carbon.apimgt.core.models.analytics.ApplicationCount) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with ApplicationCount

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

Example 3 with ApplicationCount

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

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

Example 5 with ApplicationCount

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

Aggregations

ApplicationCount (org.wso2.carbon.apimgt.core.models.analytics.ApplicationCount)7 ArrayList (java.util.ArrayList)4 Test (org.testng.annotations.Test)3 ApplicationCountListDTO (org.wso2.carbon.apimgt.rest.api.analytics.dto.ApplicationCountListDTO)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 ApplicationCountDTO (org.wso2.carbon.apimgt.rest.api.analytics.dto.ApplicationCountDTO)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 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 Application (org.wso2.carbon.apimgt.api.model.Application)1 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1