Search in sources :

Example 1 with BaseDeleteTsKvQuery

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

the class TelemetryController method deleteTimeseries.

private DeferredResult<ResponseEntity> deleteTimeseries(EntityId entityIdStr, String keysStr, boolean deleteAllDataForKeys, Long startTs, Long endTs, boolean rewriteLatestIfDeleted) throws ThingsboardException {
    List<String> keys = toKeysList(keysStr);
    if (keys.isEmpty()) {
        return getImmediateDeferredResult("Empty keys: " + keysStr, HttpStatus.BAD_REQUEST);
    }
    SecurityUser user = getCurrentUser();
    long deleteFromTs;
    long deleteToTs;
    if (deleteAllDataForKeys) {
        deleteFromTs = 0L;
        deleteToTs = System.currentTimeMillis();
    } else {
        if (startTs == null || endTs == null) {
            return getImmediateDeferredResult("When deleteAllDataForKeys is false, start and end timestamp values shouldn't be empty", HttpStatus.BAD_REQUEST);
        } else {
            deleteFromTs = startTs;
            deleteToTs = endTs;
        }
    }
    return accessValidator.validateEntityAndCallback(user, Operation.WRITE_TELEMETRY, entityIdStr, (result, tenantId, entityId) -> {
        List<DeleteTsKvQuery> deleteTsKvQueries = new ArrayList<>();
        for (String key : keys) {
            deleteTsKvQueries.add(new BaseDeleteTsKvQuery(key, deleteFromTs, deleteToTs, rewriteLatestIfDeleted));
        }
        tsSubService.deleteTimeseriesAndNotify(tenantId, entityId, keys, deleteTsKvQueries, new FutureCallback<>() {

            @Override
            public void onSuccess(@Nullable Void tmp) {
                logTimeseriesDeleted(user, entityId, keys, deleteFromTs, deleteToTs, null);
                result.setResult(new ResponseEntity<>(HttpStatus.OK));
            }

            @Override
            public void onFailure(Throwable t) {
                logTimeseriesDeleted(user, entityId, keys, deleteFromTs, deleteToTs, t);
                result.setResult(new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
            }
        });
    });
}
Also used : BaseDeleteTsKvQuery(org.thingsboard.server.common.data.kv.BaseDeleteTsKvQuery) DeleteTsKvQuery(org.thingsboard.server.common.data.kv.DeleteTsKvQuery) BaseDeleteTsKvQuery(org.thingsboard.server.common.data.kv.BaseDeleteTsKvQuery) ArrayList(java.util.ArrayList) ResponseEntity(org.springframework.http.ResponseEntity) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser)

Example 2 with BaseDeleteTsKvQuery

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

the class BaseTimeseriesService method removeLatest.

@Override
public ListenableFuture<List<TsKvLatestRemovingResult>> removeLatest(TenantId tenantId, EntityId entityId, Collection<String> keys) {
    validate(entityId);
    List<ListenableFuture<TsKvLatestRemovingResult>> futures = Lists.newArrayListWithExpectedSize(keys.size());
    for (String key : keys) {
        DeleteTsKvQuery query = new BaseDeleteTsKvQuery(key, 0, System.currentTimeMillis(), false);
        futures.add(timeseriesLatestDao.removeLatest(tenantId, entityId, query));
    }
    return Futures.allAsList(futures);
}
Also used : BaseDeleteTsKvQuery(org.thingsboard.server.common.data.kv.BaseDeleteTsKvQuery) DeleteTsKvQuery(org.thingsboard.server.common.data.kv.DeleteTsKvQuery) BaseDeleteTsKvQuery(org.thingsboard.server.common.data.kv.BaseDeleteTsKvQuery) ListenableFuture(com.google.common.util.concurrent.ListenableFuture)

Example 3 with BaseDeleteTsKvQuery

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

the class BaseTimeseriesServiceTest method testDeleteDeviceTsDataWithOverwritingLatest.

@Test
public void testDeleteDeviceTsDataWithOverwritingLatest() throws Exception {
    DeviceId deviceId = new DeviceId(Uuids.timeBased());
    saveEntries(deviceId, 10000);
    saveEntries(deviceId, 20000);
    saveEntries(deviceId, 30000);
    saveEntries(deviceId, 40000);
    tsService.remove(tenantId, deviceId, Collections.singletonList(new BaseDeleteTsKvQuery(STRING_KEY, 25000, 45000, true))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    List<TsKvEntry> list = tsService.findAll(tenantId, deviceId, Collections.singletonList(new BaseReadTsKvQuery(STRING_KEY, 5000, 45000, 10000, 10, Aggregation.NONE))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    Assert.assertEquals(2, list.size());
    List<TsKvEntry> latest = tsService.findLatest(tenantId, deviceId, Collections.singletonList(STRING_KEY)).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    Assert.assertEquals(20000, latest.get(0).getTs());
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BaseDeleteTsKvQuery(org.thingsboard.server.common.data.kv.BaseDeleteTsKvQuery) DeviceId(org.thingsboard.server.common.data.id.DeviceId) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest) Test(org.junit.Test)

Aggregations

BaseDeleteTsKvQuery (org.thingsboard.server.common.data.kv.BaseDeleteTsKvQuery)3 DeleteTsKvQuery (org.thingsboard.server.common.data.kv.DeleteTsKvQuery)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 ResponseEntity (org.springframework.http.ResponseEntity)1 DeviceId (org.thingsboard.server.common.data.id.DeviceId)1 BaseReadTsKvQuery (org.thingsboard.server.common.data.kv.BaseReadTsKvQuery)1 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)1 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)1 AbstractServiceTest (org.thingsboard.server.dao.service.AbstractServiceTest)1 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)1