Skip to content

Finding the unique ID of a time series

Doug Schmidt edited this page Sep 18, 2017 · 2 revisions

Most of the objects operated upon by the public APIs are identified by a unique ID (a .NET GUID) which never changes for the lifetime of the object.

A time-series identifier string shown in the browser as "Stage.Logger@Location1" on Tuesday might be renamed on Wednesday to appear as "Stage.DataLogger@Riverbed". The unique ID for the time-series will remain unchanged at "74d837a46f914b138798fb6dcf35da13".

While the browser does have a way of showing the unique ID of a time-series, many AQTS users will simply be familiar with the identifier string, with its familiar <Parameter>.<Label>@<Location> syntax.

getTimeSeriesUniqueId()

The getTimeSeriesUniqueId() method below will take a string identifier and return the unique ID for the time-series.

Basic logic:

  • If the string is actually a valid .NET GUID, just return it in GUID form. This will allow the method to avoid an HTTP request if the GUID was already known.
  • Use a regular expression to parse out the location indentifer from the string.
  • Ask the Publish API for the TimeSeriesDescriptions of all the time-series at the location.
  • Search the results for a TimeSeriesDescription whose Identifier property matches the desired text.
  • Return the UniqueId of the found time-series.
  • Throw an exception if something isn't found.

The example makes use of the Func library of LINQ-for-java included with the ServiceStack.Java package.

import com.aquaticinformatics.aquarius.sdk.timeseries.AquariusClient;
import com.aquaticinformatics.aquarius.sdk.timeseries.servicemodels.Publish.*;
import net.servicestack.func.Func;
import net.servicestack.func.Predicate;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

private AquariusClient _client;

public String getTimeSeriesUniqueId(String timeSeriesIdentifier) {

    if (DOT_NET_GUID_REGEX.matcher(timeSeriesIdentifier).matches())
        return timeSeriesIdentifier;

    String locationIdentifier = parseLocationIdentifier(timeSeriesIdentifier);

    TimeSeriesDescriptionListServiceResponse response = _client.Publish.get(new TimeSeriesDescriptionServiceRequest()
            .setLocationIdentifier(locationIdentifier));

    TimeSeriesDescription timeSeriesDescription = Func.first(
        response.TimeSeriesDescriptions,
        t -> t.Identifier.equals(timeSeriesIdentifier));

    if (timeSeriesDescription == null)
        throw new IllegalArgumentException("Can't find '" + timeSeriesIdentifier + "' at location '"+ locationIdentifier + "'");

    return timeSeriesDescription.UniqueId;
}

private String parseLocationIdentifier(String timeSeriesIdentifier) {

    Matcher matcher = IDENTIFIER_REGEX.matcher(timeSeriesIdentifier);

    if (!matcher.matches())
        throw new IllegalArgumentException("Can't parse '" + timeSeriesIdentifier + "' as time-series identifier. Expecting <Parameter>.<Label>@<Location");

    return matcher.group("location");
}

// AQTS GUIDs are serialized in "N" format: 32 hex characters with no dashes or braces
private static final Pattern DOT_NET_GUID_REGEX = Pattern.compile("^[0-9a-f]{32}$", Pattern.CASE_INSENSITIVE);

private static final Pattern IDENTIFIER_REGEX = Pattern.compile("^(?<parameter>[^.]+)\\.(?<label>[^@]+)@(?<location>.*)$");