Search in sources :

Example 1 with EmptyStatsResult

use of org.openkilda.testing.service.otsdb.model.EmptyStatsResult in project open-kilda by telstra.

the class OtsdbQueryServiceImpl method query.

@Override
public StatsResult query(Date start, Date end, Aggregator aggregator, String metric, Map<String, Object> tags) {
    UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString("/api/query");
    uriBuilder.queryParam("start", start.getTime());
    uriBuilder.queryParam("end", end.getTime());
    List<String> tagParts = tags.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(toList());
    String tagsString = "{" + String.join(",", tagParts) + "}";
    uriBuilder.queryParam("m", String.format("%s:%s{tags}", aggregator.toString(), metric));
    try {
        StatsResult[] result = restTemplate.exchange(uriBuilder.build().toString(), HttpMethod.GET, new HttpEntity<>(new HttpHeaders()), StatsResult[].class, tagsString).getBody();
        return result != null && result.length > 0 ? result[0] : new EmptyStatsResult();
    } catch (HttpClientErrorException e) {
        if (e.getStatusCode() == HttpStatus.BAD_REQUEST && e.getResponseBodyAsString().contains("net.opentsdb.tsd.BadRequestException: No such name for ")) {
            return new EmptyStatsResult();
        }
        throw e;
    }
}
Also used : StatsResult(org.openkilda.testing.service.otsdb.model.StatsResult) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) HttpHeaders(org.springframework.http.HttpHeaders) Date(java.util.Date) HttpMethod(org.springframework.http.HttpMethod) Aggregator(org.openkilda.testing.service.otsdb.model.Aggregator) Autowired(org.springframework.beans.factory.annotation.Autowired) EmptyStatsResult(org.openkilda.testing.service.otsdb.model.EmptyStatsResult) HttpStatus(org.springframework.http.HttpStatus) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Collectors.toList(java.util.stream.Collectors.toList) Slf4j(lombok.extern.slf4j.Slf4j) HttpEntity(org.springframework.http.HttpEntity) List(java.util.List) Service(org.springframework.stereotype.Service) Map(java.util.Map) Qualifier(org.springframework.beans.factory.annotation.Qualifier) RestTemplate(org.springframework.web.client.RestTemplate) HttpHeaders(org.springframework.http.HttpHeaders) EmptyStatsResult(org.openkilda.testing.service.otsdb.model.EmptyStatsResult) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) StatsResult(org.openkilda.testing.service.otsdb.model.StatsResult) EmptyStatsResult(org.openkilda.testing.service.otsdb.model.EmptyStatsResult)

Aggregations

Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 Collectors.toList (java.util.stream.Collectors.toList)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Aggregator (org.openkilda.testing.service.otsdb.model.Aggregator)1 EmptyStatsResult (org.openkilda.testing.service.otsdb.model.EmptyStatsResult)1 StatsResult (org.openkilda.testing.service.otsdb.model.StatsResult)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Qualifier (org.springframework.beans.factory.annotation.Qualifier)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpMethod (org.springframework.http.HttpMethod)1 HttpStatus (org.springframework.http.HttpStatus)1 Service (org.springframework.stereotype.Service)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1 RestTemplate (org.springframework.web.client.RestTemplate)1 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)1