Search in sources :

Example 1 with EventEntity

use of org.thingsboard.server.dao.model.sql.EventEntity in project thingsboard by thingsboard.

the class JpaBaseEventDao method findEvents.

@Override
public List<Event> findEvents(UUID tenantId, EntityId entityId, String eventType, TimePageLink pageLink) {
    Specification<EventEntity> timeSearchSpec = JpaAbstractSearchTimeDao.getTimeSearchPageSpec(pageLink, "id");
    Specification<EventEntity> fieldsSpec = getEntityFieldsSpec(tenantId, entityId, eventType);
    Sort.Direction sortDirection = pageLink.isAscOrder() ? Sort.Direction.ASC : Sort.Direction.DESC;
    Pageable pageable = new PageRequest(0, pageLink.getLimit(), sortDirection, ID_PROPERTY);
    return DaoUtil.convertDataList(eventRepository.findAll(where(timeSearchSpec).and(fieldsSpec), pageable).getContent());
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) EventEntity(org.thingsboard.server.dao.model.sql.EventEntity) Sort(org.springframework.data.domain.Sort)

Example 2 with EventEntity

use of org.thingsboard.server.dao.model.sql.EventEntity in project thingsboard by thingsboard.

the class JpaBaseEventDao method init.

@PostConstruct
private void init() {
    TbSqlBlockingQueueParams params = TbSqlBlockingQueueParams.builder().logName("Events").batchSize(batchSize).maxDelay(maxDelay).statsPrintIntervalMs(statsPrintIntervalMs).statsNamePrefix("events").batchSortEnabled(batchSortEnabled).build();
    Function<EventEntity, Integer> hashcodeFunction = entity -> entity.getEntityId().hashCode();
    queue = new TbSqlBlockingQueueWrapper<>(params, hashcodeFunction, batchThreads, statsFactory);
    queue.init(logExecutor, v -> eventInsertRepository.save(v), Comparator.comparing((EventEntity eventEntity) -> eventEntity.getTs()));
}
Also used : Event(org.thingsboard.server.common.data.Event) JpaAbstractDao(org.thingsboard.server.dao.sql.JpaAbstractDao) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) EventEntity(org.thingsboard.server.dao.model.sql.EventEntity) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) StatisticsEventFilter(org.thingsboard.server.common.data.event.StatisticsEventFilter) TbSqlBlockingQueueWrapper(org.thingsboard.server.dao.sql.TbSqlBlockingQueueWrapper) Autowired(org.springframework.beans.factory.annotation.Autowired) ErrorEventFilter(org.thingsboard.server.common.data.event.ErrorEventFilter) ScheduledLogExecutorComponent(org.thingsboard.server.dao.sql.ScheduledLogExecutorComponent) CrudRepository(org.springframework.data.repository.CrudRepository) EventId(org.thingsboard.server.common.data.id.EventId) StatsFactory(org.thingsboard.server.common.stats.StatsFactory) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) TenantId(org.thingsboard.server.common.data.id.TenantId) Value(org.springframework.beans.factory.annotation.Value) LifeCycleEventFilter(org.thingsboard.server.common.data.event.LifeCycleEventFilter) EventFilter(org.thingsboard.server.common.data.event.EventFilter) PreDestroy(javax.annotation.PreDestroy) TbSqlBlockingQueueParams(org.thingsboard.server.dao.sql.TbSqlBlockingQueueParams) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) EventDao(org.thingsboard.server.dao.event.EventDao) EntityId(org.thingsboard.server.common.data.id.EntityId) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) PageRequest(org.springframework.data.domain.PageRequest) UUID(java.util.UUID) DebugEvent(org.thingsboard.server.common.data.event.DebugEvent) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) AttributeKvEntity(org.thingsboard.server.dao.model.sql.AttributeKvEntity) PageData(org.thingsboard.server.common.data.page.PageData) PostConstruct(javax.annotation.PostConstruct) DaoUtil(org.thingsboard.server.dao.DaoUtil) Optional(java.util.Optional) Comparator(java.util.Comparator) EventEntity(org.thingsboard.server.dao.model.sql.EventEntity) TbSqlBlockingQueueParams(org.thingsboard.server.dao.sql.TbSqlBlockingQueueParams) PostConstruct(javax.annotation.PostConstruct)

Example 3 with EventEntity

use of org.thingsboard.server.dao.model.sql.EventEntity in project thingsboard by thingsboard.

the class EventInsertRepository method save.

protected void save(List<EventEntity> entities) {
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            jdbcTemplate.batchUpdate(INSERT, new BatchPreparedStatementSetter() {

                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    EventEntity event = entities.get(i);
                    ps.setObject(1, event.getId());
                    ps.setLong(2, event.getCreatedTime());
                    ps.setString(3, replaceNullChars(event.getBody().toString()));
                    ps.setObject(4, event.getEntityId());
                    ps.setString(5, event.getEntityType().name());
                    ps.setString(6, event.getEventType());
                    ps.setString(7, event.getEventUid());
                    ps.setObject(8, event.getTenantId());
                    ps.setLong(9, event.getTs());
                }

                @Override
                public int getBatchSize() {
                    return entities.size();
                }
            });
        }
    });
}
Also used : BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) EventEntity(org.thingsboard.server.dao.model.sql.EventEntity) TransactionStatus(org.springframework.transaction.TransactionStatus) PreparedStatement(java.sql.PreparedStatement) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 4 with EventEntity

use of org.thingsboard.server.dao.model.sql.EventEntity in project thingsboard by thingsboard.

the class JpaBaseEventDao method saveAsync.

@Override
public ListenableFuture<Void> saveAsync(Event event) {
    log.debug("Save event [{}] ", event);
    if (event.getId() == null) {
        UUID timeBased = Uuids.timeBased();
        event.setId(new EventId(timeBased));
        event.setCreatedTime(Uuids.unixTimestamp(timeBased));
    } else if (event.getCreatedTime() == 0L) {
        UUID eventId = event.getId().getId();
        if (eventId.version() == 1) {
            event.setCreatedTime(Uuids.unixTimestamp(eventId));
        } else {
            event.setCreatedTime(System.currentTimeMillis());
        }
    }
    if (StringUtils.isEmpty(event.getUid())) {
        event.setUid(event.getId().toString());
    }
    return save(new EventEntity(event));
}
Also used : EventEntity(org.thingsboard.server.dao.model.sql.EventEntity) EventId(org.thingsboard.server.common.data.id.EventId) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) UUID(java.util.UUID)

Aggregations

EventEntity (org.thingsboard.server.dao.model.sql.EventEntity)4 UUID (java.util.UUID)2 PageRequest (org.springframework.data.domain.PageRequest)2 EventId (org.thingsboard.server.common.data.id.EventId)2 NULL_UUID (org.thingsboard.server.dao.model.ModelConstants.NULL_UUID)2 Uuids (com.datastax.oss.driver.api.core.uuid.Uuids)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 PreparedStatement (java.sql.PreparedStatement)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 PostConstruct (javax.annotation.PostConstruct)1 PreDestroy (javax.annotation.PreDestroy)1 Slf4j (lombok.extern.slf4j.Slf4j)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Value (org.springframework.beans.factory.annotation.Value)1 Pageable (org.springframework.data.domain.Pageable)1