Rest API support of strict data types in getTelemetry requests
This commit is contained in:
		
							parent
							
								
									e207545ce0
								
							
						
					
					
						commit
						4b0aae896d
					
				@ -183,11 +183,11 @@ public class TelemetryController extends BaseController {
 | 
				
			|||||||
    public DeferredResult<ResponseEntity> getLatestTimeseries(
 | 
					    public DeferredResult<ResponseEntity> getLatestTimeseries(
 | 
				
			||||||
            @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr,
 | 
					            @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr,
 | 
				
			||||||
            @RequestParam(name = "keys", required = false) String keysStr,
 | 
					            @RequestParam(name = "keys", required = false) String keysStr,
 | 
				
			||||||
            @RequestParam(name = "useStrictType", required = false, defaultValue = "false") Boolean useStrictType) throws ThingsboardException {
 | 
					            @RequestParam(name = "useStrictDataTypes", required = false, defaultValue = "false") Boolean useStrictDataTypes) throws ThingsboardException {
 | 
				
			||||||
        SecurityUser user = getCurrentUser();
 | 
					        SecurityUser user = getCurrentUser();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_TELEMETRY, entityType, entityIdStr,
 | 
					        return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_TELEMETRY, entityType, entityIdStr,
 | 
				
			||||||
                (result, tenantId, entityId) -> getLatestTimeseriesValuesCallback(result, user, entityId, keysStr, useStrictType));
 | 
					                (result, tenantId, entityId) -> getLatestTimeseriesValuesCallback(result, user, entityId, keysStr, useStrictDataTypes));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -202,7 +202,7 @@ public class TelemetryController extends BaseController {
 | 
				
			|||||||
            @RequestParam(name = "interval", defaultValue = "0") Long interval,
 | 
					            @RequestParam(name = "interval", defaultValue = "0") Long interval,
 | 
				
			||||||
            @RequestParam(name = "limit", defaultValue = "100") Integer limit,
 | 
					            @RequestParam(name = "limit", defaultValue = "100") Integer limit,
 | 
				
			||||||
            @RequestParam(name = "agg", defaultValue = "NONE") String aggStr,
 | 
					            @RequestParam(name = "agg", defaultValue = "NONE") String aggStr,
 | 
				
			||||||
            @RequestParam(name = "useStrictType", required = false, defaultValue = "false") Boolean useStrictType) throws ThingsboardException {
 | 
					            @RequestParam(name = "useStrictDataTypes", required = false, defaultValue = "false") Boolean useStrictDataTypes) throws ThingsboardException {
 | 
				
			||||||
        return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_TELEMETRY, entityType, entityIdStr,
 | 
					        return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_TELEMETRY, entityType, entityIdStr,
 | 
				
			||||||
                (result, tenantId, entityId) -> {
 | 
					                (result, tenantId, entityId) -> {
 | 
				
			||||||
                    // If interval is 0, convert this to a NONE aggregation, which is probably what the user really wanted
 | 
					                    // If interval is 0, convert this to a NONE aggregation, which is probably what the user really wanted
 | 
				
			||||||
@ -210,7 +210,7 @@ public class TelemetryController extends BaseController {
 | 
				
			|||||||
                    List<ReadTsKvQuery> queries = toKeysList(keys).stream().map(key -> new BaseReadTsKvQuery(key, startTs, endTs, interval, limit, agg))
 | 
					                    List<ReadTsKvQuery> queries = toKeysList(keys).stream().map(key -> new BaseReadTsKvQuery(key, startTs, endTs, interval, limit, agg))
 | 
				
			||||||
                            .collect(Collectors.toList());
 | 
					                            .collect(Collectors.toList());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    Futures.addCallback(tsService.findAll(tenantId, entityId, queries), getTsKvListCallback(result, useStrictType));
 | 
					                    Futures.addCallback(tsService.findAll(tenantId, entityId, queries), getTsKvListCallback(result, useStrictDataTypes));
 | 
				
			||||||
                });
 | 
					                });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -455,14 +455,14 @@ public class TelemetryController extends BaseController {
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void getLatestTimeseriesValuesCallback(@Nullable DeferredResult<ResponseEntity> result, SecurityUser user, EntityId entityId, String keys, Boolean useStrictType) {
 | 
					    private void getLatestTimeseriesValuesCallback(@Nullable DeferredResult<ResponseEntity> result, SecurityUser user, EntityId entityId, String keys, Boolean useStrictDataTypes) {
 | 
				
			||||||
        ListenableFuture<List<TsKvEntry>> future;
 | 
					        ListenableFuture<List<TsKvEntry>> future;
 | 
				
			||||||
        if (StringUtils.isEmpty(keys)) {
 | 
					        if (StringUtils.isEmpty(keys)) {
 | 
				
			||||||
            future = tsService.findAllLatest(user.getTenantId(), entityId);
 | 
					            future = tsService.findAllLatest(user.getTenantId(), entityId);
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            future = tsService.findLatest(user.getTenantId(), entityId, toKeysList(keys));
 | 
					            future = tsService.findLatest(user.getTenantId(), entityId, toKeysList(keys));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        Futures.addCallback(future, getTsKvListCallback(result, useStrictType));
 | 
					        Futures.addCallback(future, getTsKvListCallback(result, useStrictDataTypes));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void getAttributeValuesCallback(@Nullable DeferredResult<ResponseEntity> result, SecurityUser user, EntityId entityId, String scope, String keys) {
 | 
					    private void getAttributeValuesCallback(@Nullable DeferredResult<ResponseEntity> result, SecurityUser user, EntityId entityId, String scope, String keys) {
 | 
				
			||||||
@ -560,13 +560,13 @@ public class TelemetryController extends BaseController {
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private FutureCallback<List<TsKvEntry>> getTsKvListCallback(final DeferredResult<ResponseEntity> response, Boolean useStrictType) {
 | 
					    private FutureCallback<List<TsKvEntry>> getTsKvListCallback(final DeferredResult<ResponseEntity> response, Boolean useStrictDataTypes) {
 | 
				
			||||||
        return new FutureCallback<List<TsKvEntry>>() {
 | 
					        return new FutureCallback<List<TsKvEntry>>() {
 | 
				
			||||||
            @Override
 | 
					            @Override
 | 
				
			||||||
            public void onSuccess(List<TsKvEntry> data) {
 | 
					            public void onSuccess(List<TsKvEntry> data) {
 | 
				
			||||||
                Map<String, List<TsData>> result = new LinkedHashMap<>();
 | 
					                Map<String, List<TsData>> result = new LinkedHashMap<>();
 | 
				
			||||||
                for (TsKvEntry entry : data) {
 | 
					                for (TsKvEntry entry : data) {
 | 
				
			||||||
                    Object value = useStrictType ? getKvValue(entry) : entry.getValueAsString();
 | 
					                    Object value = useStrictDataTypes ? getKvValue(entry) : entry.getValueAsString();
 | 
				
			||||||
                    result.computeIfAbsent(entry.getKey(), k -> new ArrayList<>()).add(new TsData(entry.getTs(), value));
 | 
					                    result.computeIfAbsent(entry.getKey(), k -> new ArrayList<>()).add(new TsData(entry.getTs(), value));
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                response.setResult(new ResponseEntity<>(result, HttpStatus.OK));
 | 
					                response.setResult(new ResponseEntity<>(result, HttpStatus.OK));
 | 
				
			||||||
 | 
				
			|||||||
@ -98,6 +98,7 @@ import java.util.HashMap;
 | 
				
			|||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.Map;
 | 
					import java.util.Map;
 | 
				
			||||||
import java.util.Optional;
 | 
					import java.util.Optional;
 | 
				
			||||||
 | 
					import java.util.UUID;
 | 
				
			||||||
import java.util.concurrent.ExecutorService;
 | 
					import java.util.concurrent.ExecutorService;
 | 
				
			||||||
import java.util.concurrent.Executors;
 | 
					import java.util.concurrent.Executors;
 | 
				
			||||||
import java.util.concurrent.Future;
 | 
					import java.util.concurrent.Future;
 | 
				
			||||||
@ -1612,31 +1613,40 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public List<TsKvEntry> getLatestTimeseries(EntityId entityId, List<String> keys) {
 | 
					    public List<TsKvEntry> getLatestTimeseries(EntityId entityId, List<String> keys) {
 | 
				
			||||||
 | 
					        return getLatestTimeseries(entityId, keys, true);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public List<TsKvEntry> getLatestTimeseries(EntityId entityId, List<String> keys, boolean useStrictDataTypes) {
 | 
				
			||||||
        Map<String, List<JsonNode>> timeseries = restTemplate.exchange(
 | 
					        Map<String, List<JsonNode>> timeseries = restTemplate.exchange(
 | 
				
			||||||
                baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/timeseries?keys={keys}",
 | 
					                baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/timeseries?keys={keys}&useStrictDataTypes={useStrictDataTypes}",
 | 
				
			||||||
                HttpMethod.GET,
 | 
					                HttpMethod.GET,
 | 
				
			||||||
                HttpEntity.EMPTY,
 | 
					                HttpEntity.EMPTY,
 | 
				
			||||||
                new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {
 | 
					                new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                entityId.getEntityType().name(),
 | 
					                entityId.getEntityType().name(),
 | 
				
			||||||
                entityId.getId().toString(),
 | 
					                entityId.getId().toString(),
 | 
				
			||||||
                listToString(keys)).getBody();
 | 
					                listToString(keys),
 | 
				
			||||||
 | 
					                useStrictDataTypes).getBody();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return RestJsonConverter.toTimeseries(timeseries);
 | 
					        return RestJsonConverter.toTimeseries(timeseries);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    public List<TsKvEntry> getTimeseries(EntityId entityId, List<String> keys, Long interval, Aggregation agg, TimePageLink pageLink) {
 | 
					    public List<TsKvEntry> getTimeseries(EntityId entityId, List<String> keys, Long interval, Aggregation agg, TimePageLink pageLink) {
 | 
				
			||||||
 | 
					        return getTimeseries(entityId, keys, interval, agg, pageLink, true);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public List<TsKvEntry> getTimeseries(EntityId entityId, List<String> keys, Long interval, Aggregation agg, TimePageLink pageLink, boolean useStrictDataTypes) {
 | 
				
			||||||
        Map<String, String> params = new HashMap<>();
 | 
					        Map<String, String> params = new HashMap<>();
 | 
				
			||||||
        addPageLinkToParam(params, pageLink);
 | 
					 | 
				
			||||||
        params.put("entityType", entityId.getEntityType().name());
 | 
					        params.put("entityType", entityId.getEntityType().name());
 | 
				
			||||||
        params.put("entityId", entityId.getId().toString());
 | 
					        params.put("entityId", entityId.getId().toString());
 | 
				
			||||||
        params.put("keys", listToString(keys));
 | 
					        params.put("keys", listToString(keys));
 | 
				
			||||||
        params.put("interval", interval == null ? "0" : interval.toString());
 | 
					        params.put("interval", interval == null ? "0" : interval.toString());
 | 
				
			||||||
        params.put("agg", agg == null ? "NONE" : agg.name());
 | 
					        params.put("agg", agg == null ? "NONE" : agg.name());
 | 
				
			||||||
 | 
					        params.put("useStrictDataTypes", Boolean.toString(useStrictDataTypes));
 | 
				
			||||||
 | 
					        addPageLinkToParam(params, pageLink);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Map<String, List<JsonNode>> timeseries = restTemplate.exchange(
 | 
					        Map<String, List<JsonNode>> timeseries = restTemplate.exchange(
 | 
				
			||||||
                baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/timeseries?keys={keys}&interval={interval}&agg={agg}&" + getUrlParams(pageLink),
 | 
					                baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/timeseries?keys={keys}&interval={interval}&agg={agg}&useStrictDataTypes={useStrictDataTypes}&" + getUrlParams(pageLink),
 | 
				
			||||||
                HttpMethod.GET,
 | 
					                HttpMethod.GET,
 | 
				
			||||||
                HttpEntity.EMPTY,
 | 
					                HttpEntity.EMPTY,
 | 
				
			||||||
                new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {
 | 
					                new ParameterizedTypeReference<Map<String, List<JsonNode>>>() {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user