com.cognite.client.servicesV1.response.TSPointsProtoResponseParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cdf-sdk-java Show documentation
Show all versions of cdf-sdk-java Show documentation
Java SDK for reading and writing from/to CDF resources.
/*
* Copyright (c) 2020 Cognite AS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cognite.client.servicesV1.response;
import com.cognite.client.Request;
import com.cognite.client.servicesV1.util.TSIterationUtilities;
import com.cognite.v1.timeseries.proto.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
@AutoValue
public abstract class TSPointsProtoResponseParser implements ResponseParser {
private static final ObjectMapper mapper = new ObjectMapper();
private static final int DEFAULT_PARAMETER_LIMIT = 10000;
private static final String END_KEY = "end";
private static final String GRANULARITY_KEY = "granularity";
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
// Logger identifier per instance
private final String randomIdString = RandomStringUtils.randomAlphanumeric(5);
private final ObjectWriter objectWriter = mapper.writer();
public static TSPointsProtoResponseParser.Builder builder() {
return new com.cognite.client.servicesV1.response.AutoValue_TSPointsProtoResponseParser.Builder()
.setRequest(Request.create());
}
public abstract TSPointsProtoResponseParser.Builder toBuilder();
public abstract Request getRequest();
public TSPointsProtoResponseParser withRequest(Request parameters) {
Preconditions.checkNotNull(parameters, "Request parameters cannot be null");
return toBuilder().setRequest(parameters).build();
}
/**
* Extracts the start timestamp for the next iteration of timestamp points.
*
* The cursor is a json object with each TS *externalId* (or id, if no externalId exists) mapped to the next
* *start* timestamp.
*
* @param payload The response body
* @return
* @throws Exception
*/
public Optional extractNextCursor(byte[] payload) throws Exception {
final String loggingPrefix = "Extracting next cursor [" + randomIdString + "] -";
LOG.debug(loggingPrefix + "Start extracting next cursor from TS data points payload.");
LOG.debug(loggingPrefix + "start parsing binary payload.");
DataPointListResponse response = DataPointListResponse.parseFrom(payload);
LOG.debug(loggingPrefix + "done parsing binary payload.");
List responseItems = response.getItemsList();
if (responseItems.isEmpty()) {
// No items to parse, so no cursor
LOG.debug(loggingPrefix + "Could not find any TS list items in the response payload.");
return Optional.empty();
}
LOG.debug(loggingPrefix + "Found {} TS list response items in the response payload", responseItems.size());
// Adjust the limit based on the number of TS in the request
int effectiveLimit = TSIterationUtilities.calculateLimit(
(Integer) getRequest().getRequestParameters().getOrDefault("limit", DEFAULT_PARAMETER_LIMIT),
responseItems.size());
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy