Search in sources :

Example 1 with JsonDataEntry

use of org.thingsboard.server.common.data.kv.JsonDataEntry in project thingsboard by thingsboard.

the class SequentialTimeseriesPersistenceTest method saveAssetTsEntry.

void saveAssetTsEntry(Asset asset, String key, long value, long ts) throws ExecutionException, InterruptedException, TimeoutException {
    Optional<String> tsKvEntryOpt = getTsKvLatest(asset.getId(), GENERIC_CUMULATIVE_OBJ).getJsonValue();
    TsKvEntry saveTsKvEntry = new BasicTsKvEntry(ts, new JsonDataEntry(GENERIC_CUMULATIVE_OBJ, getJsonObject(key, value, tsKvEntryOpt).toString()));
    saveTimeseries(asset.getId(), saveTsKvEntry);
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) JsonDataEntry(org.thingsboard.server.common.data.kv.JsonDataEntry)

Example 2 with JsonDataEntry

use of org.thingsboard.server.common.data.kv.JsonDataEntry in project thingsboard by thingsboard.

the class AggregatePartitionsFunction method processMinOrMaxResult.

private Optional<TsKvEntry> processMinOrMaxResult(AggregationResult aggResult) {
    if (aggResult.dataType == DataType.DOUBLE || aggResult.dataType == DataType.LONG) {
        if (aggResult.hasDouble) {
            double currentD = aggregation == Aggregation.MIN ? Optional.ofNullable(aggResult.dValue).orElse(Double.MAX_VALUE) : Optional.ofNullable(aggResult.dValue).orElse(Double.MIN_VALUE);
            double currentL = aggregation == Aggregation.MIN ? Optional.ofNullable(aggResult.lValue).orElse(Long.MAX_VALUE) : Optional.ofNullable(aggResult.lValue).orElse(Long.MIN_VALUE);
            return Optional.of(new BasicTsKvEntry(ts, new DoubleDataEntry(key, aggregation == Aggregation.MIN ? Math.min(currentD, currentL) : Math.max(currentD, currentL))));
        } else {
            return Optional.of(new BasicTsKvEntry(ts, new LongDataEntry(key, aggResult.lValue)));
        }
    } else if (aggResult.dataType == DataType.STRING) {
        return Optional.of(new BasicTsKvEntry(ts, new StringDataEntry(key, aggResult.sValue)));
    } else if (aggResult.dataType == DataType.JSON) {
        return Optional.of(new BasicTsKvEntry(ts, new JsonDataEntry(key, aggResult.jValue)));
    } else {
        return Optional.of(new BasicTsKvEntry(ts, new BooleanDataEntry(key, aggResult.bValue)));
    }
}
Also used : DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) BooleanDataEntry(org.thingsboard.server.common.data.kv.BooleanDataEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) JsonDataEntry(org.thingsboard.server.common.data.kv.JsonDataEntry)

Example 3 with JsonDataEntry

use of org.thingsboard.server.common.data.kv.JsonDataEntry in project thingsboard by thingsboard.

the class TelemetryController method extractRequestAttributes.

private List<AttributeKvEntry> extractRequestAttributes(JsonNode jsonNode) {
    long ts = System.currentTimeMillis();
    List<AttributeKvEntry> attributes = new ArrayList<>();
    jsonNode.fields().forEachRemaining(entry -> {
        String key = entry.getKey();
        JsonNode value = entry.getValue();
        if (entry.getValue().isObject() || entry.getValue().isArray()) {
            attributes.add(new BaseAttributeKvEntry(new JsonDataEntry(key, toJsonStr(value)), ts));
        } else if (entry.getValue().isTextual()) {
            if (maxStringValueLength > 0 && entry.getValue().textValue().length() > maxStringValueLength) {
                String message = String.format("String value length [%d] for key [%s] is greater than maximum allowed [%d]", entry.getValue().textValue().length(), key, maxStringValueLength);
                throw new UncheckedApiException(new InvalidParametersException(message));
            }
            attributes.add(new BaseAttributeKvEntry(new StringDataEntry(key, value.textValue()), ts));
        } else if (entry.getValue().isBoolean()) {
            attributes.add(new BaseAttributeKvEntry(new BooleanDataEntry(key, value.booleanValue()), ts));
        } else if (entry.getValue().isDouble()) {
            attributes.add(new BaseAttributeKvEntry(new DoubleDataEntry(key, value.doubleValue()), ts));
        } else if (entry.getValue().isNumber()) {
            if (entry.getValue().isBigInteger()) {
                throw new UncheckedApiException(new InvalidParametersException("Big integer values are not supported!"));
            } else {
                attributes.add(new BaseAttributeKvEntry(new LongDataEntry(key, value.longValue()), ts));
            }
        }
    });
    return attributes;
}
Also used : BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) InvalidParametersException(org.thingsboard.server.service.telemetry.exception.InvalidParametersException) UncheckedApiException(org.thingsboard.server.service.telemetry.exception.UncheckedApiException) BooleanDataEntry(org.thingsboard.server.common.data.kv.BooleanDataEntry) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) JsonDataEntry(org.thingsboard.server.common.data.kv.JsonDataEntry)

Example 4 with JsonDataEntry

use of org.thingsboard.server.common.data.kv.JsonDataEntry in project thingsboard by thingsboard.

the class TbSubscriptionUtils method getKvEntry.

private static KvEntry getKvEntry(KeyValueProto proto) {
    KvEntry entry = null;
    DataType type = DataType.values()[proto.getType().getNumber()];
    switch(type) {
        case BOOLEAN:
            entry = new BooleanDataEntry(proto.getKey(), proto.getBoolV());
            break;
        case LONG:
            entry = new LongDataEntry(proto.getKey(), proto.getLongV());
            break;
        case DOUBLE:
            entry = new DoubleDataEntry(proto.getKey(), proto.getDoubleV());
            break;
        case STRING:
            entry = new StringDataEntry(proto.getKey(), proto.getStringV());
            break;
        case JSON:
            entry = new JsonDataEntry(proto.getKey(), proto.getJsonV());
            break;
    }
    return entry;
}
Also used : BooleanDataEntry(org.thingsboard.server.common.data.kv.BooleanDataEntry) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) DataType(org.thingsboard.server.common.data.kv.DataType) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) JsonDataEntry(org.thingsboard.server.common.data.kv.JsonDataEntry)

Example 5 with JsonDataEntry

use of org.thingsboard.server.common.data.kv.JsonDataEntry in project thingsboard by thingsboard.

the class DefaultRuleEngineStatisticsService method reportQueueStats.

@Override
public void reportQueueStats(long ts, TbRuleEngineConsumerStats ruleEngineStats) {
    String queueName = ruleEngineStats.getQueueName();
    ruleEngineStats.getTenantStats().forEach((id, stats) -> {
        TenantId tenantId = TenantId.fromUUID(id);
        try {
            AssetId serviceAssetId = getServiceAssetId(tenantId, queueName);
            if (stats.getTotalMsgCounter().get() > 0) {
                List<TsKvEntry> tsList = stats.getCounters().entrySet().stream().map(kv -> new BasicTsKvEntry(ts, new LongDataEntry(kv.getKey(), (long) kv.getValue().get()))).collect(Collectors.toList());
                if (!tsList.isEmpty()) {
                    tsService.saveAndNotifyInternal(tenantId, serviceAssetId, tsList, CALLBACK);
                }
            }
        } catch (DataValidationException e) {
            if (!e.getMessage().equalsIgnoreCase("Asset is referencing to non-existent tenant!")) {
                throw e;
            }
        }
    });
    ruleEngineStats.getTenantExceptions().forEach((tenantId, e) -> {
        TsKvEntry tsKv = new BasicTsKvEntry(e.getTs(), new JsonDataEntry("ruleEngineException", e.toJsonString()));
        try {
            tsService.saveAndNotifyInternal(tenantId, getServiceAssetId(tenantId, queueName), Collections.singletonList(tsKv), CALLBACK);
        } catch (DataValidationException e2) {
            if (!e2.getMessage().equalsIgnoreCase("Asset is referencing to non-existent tenant!")) {
                throw e2;
            }
        }
    });
}
Also used : JacksonUtil(org.thingsboard.common.util.JacksonUtil) AssetId(org.thingsboard.server.common.data.id.AssetId) TelemetrySubscriptionService(org.thingsboard.server.service.telemetry.TelemetrySubscriptionService) TenantId(org.thingsboard.server.common.data.id.TenantId) ConcurrentMap(java.util.concurrent.ConcurrentMap) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) Service(org.springframework.stereotype.Service) TbRuleEngineComponent(org.thingsboard.server.queue.util.TbRuleEngineComponent) TbRuleEngineConsumerStats(org.thingsboard.server.service.queue.TbRuleEngineConsumerStats) TbServiceInfoProvider(org.thingsboard.server.queue.discovery.TbServiceInfoProvider) Nullable(javax.annotation.Nullable) AssetService(org.thingsboard.server.dao.asset.AssetService) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collectors(java.util.stream.Collectors) FutureCallback(com.google.common.util.concurrent.FutureCallback) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Lock(java.util.concurrent.locks.Lock) JsonDataEntry(org.thingsboard.server.common.data.kv.JsonDataEntry) Data(lombok.Data) Collections(java.util.Collections) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) Asset(org.thingsboard.server.common.data.asset.Asset) TenantId(org.thingsboard.server.common.data.id.TenantId) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) AssetId(org.thingsboard.server.common.data.id.AssetId) JsonDataEntry(org.thingsboard.server.common.data.kv.JsonDataEntry)

Aggregations

JsonDataEntry (org.thingsboard.server.common.data.kv.JsonDataEntry)6 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)5 LongDataEntry (org.thingsboard.server.common.data.kv.LongDataEntry)5 BooleanDataEntry (org.thingsboard.server.common.data.kv.BooleanDataEntry)4 DoubleDataEntry (org.thingsboard.server.common.data.kv.DoubleDataEntry)4 StringDataEntry (org.thingsboard.server.common.data.kv.StringDataEntry)4 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)4 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)2 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)2 KvEntry (org.thingsboard.server.common.data.kv.KvEntry)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Collectors (java.util.stream.Collectors)1