Search in sources :

Example 1 with TsKvEntity

use of org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity in project thingsboard by thingsboard.

the class AbstractChunkedAggregationTimeseriesDao method setFutures.

protected SettableFuture<TsKvEntity> setFutures(List<CompletableFuture<TsKvEntity>> entitiesFutures) {
    SettableFuture<TsKvEntity> listenableFuture = SettableFuture.create();
    CompletableFuture<List<TsKvEntity>> entities = CompletableFuture.allOf(entitiesFutures.toArray(new CompletableFuture[entitiesFutures.size()])).thenApply(v -> entitiesFutures.stream().map(CompletableFuture::join).collect(Collectors.toList()));
    entities.whenComplete((tsKvEntities, throwable) -> {
        if (throwable != null) {
            listenableFuture.setException(throwable);
        } else {
            TsKvEntity result = null;
            for (TsKvEntity entity : tsKvEntities) {
                if (entity.isNotEmpty()) {
                    result = entity;
                    break;
                }
            }
            listenableFuture.set(result);
        }
    });
    return listenableFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) AbstractTsKvEntity(org.thingsboard.server.dao.model.sql.AbstractTsKvEntity) TsKvEntity(org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity)

Example 2 with TsKvEntity

use of org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity in project thingsboard by thingsboard.

the class JpaPsqlTimeseriesDao method save.

@Override
public ListenableFuture<Integer> save(TenantId tenantId, EntityId entityId, TsKvEntry tsKvEntry, long ttl) {
    int dataPointDays = getDataPointDays(tsKvEntry, computeTtl(ttl));
    savePartitionIfNotExist(tsKvEntry.getTs());
    String strKey = tsKvEntry.getKey();
    Integer keyId = getOrSaveKeyId(strKey);
    TsKvEntity entity = new TsKvEntity();
    entity.setEntityId(entityId.getId());
    entity.setTs(tsKvEntry.getTs());
    entity.setKey(keyId);
    entity.setStrValue(tsKvEntry.getStrValue().orElse(null));
    entity.setDoubleValue(tsKvEntry.getDoubleValue().orElse(null));
    entity.setLongValue(tsKvEntry.getLongValue().orElse(null));
    entity.setBooleanValue(tsKvEntry.getBooleanValue().orElse(null));
    entity.setJsonValue(tsKvEntry.getJsonValue().orElse(null));
    log.trace("Saving entity: {}", entity);
    return Futures.transform(tsQueue.add(entity), v -> dataPointDays, MoreExecutors.directExecutor());
}
Also used : TsKvEntity(org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity)

Example 3 with TsKvEntity

use of org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity in project thingsboard by thingsboard.

the class BaseEntityServiceTest method saveLongTimeseries.

private ListenableFuture<Integer> saveLongTimeseries(EntityId entityId, String key, Double value) {
    TsKvEntity tsKv = new TsKvEntity();
    tsKv.setStrKey(key);
    tsKv.setDoubleValue(value);
    KvEntry telemetryValue = new DoubleDataEntry(key, value);
    BasicTsKvEntry timeseries = new BasicTsKvEntry(42L, telemetryValue);
    return timeseriesService.save(SYSTEM_TENANT_ID, entityId, timeseries);
}
Also used : DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) TsKvEntity(org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity)

Example 4 with TsKvEntity

use of org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity in project thingsboard by thingsboard.

the class JpaHsqlTimeseriesDao method save.

@Override
public ListenableFuture<Integer> save(TenantId tenantId, EntityId entityId, TsKvEntry tsKvEntry, long ttl) {
    int dataPointDays = getDataPointDays(tsKvEntry, computeTtl(ttl));
    String strKey = tsKvEntry.getKey();
    Integer keyId = getOrSaveKeyId(strKey);
    TsKvEntity entity = new TsKvEntity();
    entity.setEntityId(entityId.getId());
    entity.setTs(tsKvEntry.getTs());
    entity.setKey(keyId);
    entity.setStrValue(tsKvEntry.getStrValue().orElse(null));
    entity.setDoubleValue(tsKvEntry.getDoubleValue().orElse(null));
    entity.setLongValue(tsKvEntry.getLongValue().orElse(null));
    entity.setBooleanValue(tsKvEntry.getBooleanValue().orElse(null));
    entity.setJsonValue(tsKvEntry.getJsonValue().orElse(null));
    log.trace("Saving entity: {}", entity);
    return Futures.transform(tsQueue.add(entity), v -> dataPointDays, MoreExecutors.directExecutor());
}
Also used : TsKvEntity(org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity)

Example 5 with TsKvEntity

use of org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity in project thingsboard by thingsboard.

the class AbstractChunkedAggregationTimeseriesDao method init.

@PostConstruct
protected void init() {
    TbSqlBlockingQueueParams tsParams = TbSqlBlockingQueueParams.builder().logName("TS").batchSize(tsBatchSize).maxDelay(tsMaxDelay).statsPrintIntervalMs(tsStatsPrintIntervalMs).statsNamePrefix("ts").batchSortEnabled(batchSortEnabled).build();
    Function<TsKvEntity, Integer> hashcodeFunction = entity -> entity.getEntityId().hashCode();
    tsQueue = new TbSqlBlockingQueueWrapper<>(tsParams, hashcodeFunction, tsBatchThreads, statsFactory);
    tsQueue.init(logExecutor, v -> insertRepository.saveOrUpdate(v), Comparator.comparing((Function<TsKvEntity, UUID>) AbstractTsKvEntity::getEntityId).thenComparing(AbstractTsKvEntity::getKey).thenComparing(AbstractTsKvEntity::getTs));
}
Also used : MoreExecutors(com.google.common.util.concurrent.MoreExecutors) java.util(java.util) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbSqlBlockingQueueWrapper(org.thingsboard.server.dao.sql.TbSqlBlockingQueueWrapper) Autowired(org.springframework.beans.factory.annotation.Autowired) CompletableFuture(java.util.concurrent.CompletableFuture) SettableFuture(com.google.common.util.concurrent.SettableFuture) StatsFactory(org.thingsboard.server.common.stats.StatsFactory) Function(java.util.function.Function) TenantId(org.thingsboard.server.common.data.id.TenantId) Value(org.springframework.beans.factory.annotation.Value) PreDestroy(javax.annotation.PreDestroy) AbstractTsKvEntity(org.thingsboard.server.dao.model.sql.AbstractTsKvEntity) TsKvEntity(org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity) TbSqlBlockingQueueParams(org.thingsboard.server.dao.sql.TbSqlBlockingQueueParams) EntityId(org.thingsboard.server.common.data.id.EntityId) Sort(org.springframework.data.domain.Sort) InsertTsRepository(org.thingsboard.server.dao.sqlts.insert.InsertTsRepository) TsKvRepository(org.thingsboard.server.dao.sqlts.ts.TsKvRepository) Aggregation(org.thingsboard.server.common.data.kv.Aggregation) PageRequest(org.springframework.data.domain.PageRequest) Collectors(java.util.stream.Collectors) DeleteTsKvQuery(org.thingsboard.server.common.data.kv.DeleteTsKvQuery) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) TimeseriesDao(org.thingsboard.server.dao.timeseries.TimeseriesDao) PostConstruct(javax.annotation.PostConstruct) DaoUtil(org.thingsboard.server.dao.DaoUtil) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) Function(java.util.function.Function) AbstractTsKvEntity(org.thingsboard.server.dao.model.sql.AbstractTsKvEntity) TsKvEntity(org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity) AbstractTsKvEntity(org.thingsboard.server.dao.model.sql.AbstractTsKvEntity) TbSqlBlockingQueueParams(org.thingsboard.server.dao.sql.TbSqlBlockingQueueParams) PostConstruct(javax.annotation.PostConstruct)

Aggregations

TsKvEntity (org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity)5 CompletableFuture (java.util.concurrent.CompletableFuture)2 AbstractTsKvEntity (org.thingsboard.server.dao.model.sql.AbstractTsKvEntity)2 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 java.util (java.util)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 PostConstruct (javax.annotation.PostConstruct)1 PreDestroy (javax.annotation.PreDestroy)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Value (org.springframework.beans.factory.annotation.Value)1 PageRequest (org.springframework.data.domain.PageRequest)1 Sort (org.springframework.data.domain.Sort)1 EntityId (org.thingsboard.server.common.data.id.EntityId)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1 Aggregation (org.thingsboard.server.common.data.kv.Aggregation)1