All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.keen.client.java.SavedQueryPut Maven / Gradle / Ivy

package io.keen.client.java;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import io.keen.client.java.http.HttpMethods;

/**
 * Represents PUT requests performed against the Saved/Cached Queries API.
 *
 * @author masojus
 */
class SavedQueryPut extends SavedQueryRequest {
    private final KeenQueryRequest query;
    private final int refreshRate;

    private final Map miscProperties;


    SavedQueryPut(String queryName,
                  String displayName,
                  KeenQueryRequest query,
                  int refreshRate,
                  Map miscProperties) {
        super(HttpMethods.PUT, true /* needsMasterKey */, queryName, displayName);

        if (null == query && (null == miscProperties || miscProperties.isEmpty())) {
            throw new IllegalArgumentException("If a query definition is not provided, then " +
                                               "miscProperties should define the Saved/Cached " +
                                               "Query.");
        }

        this.query = query;

        // Validate refreshRate range. We can use -1 to indicate it is unset since negative values
        // aren't valid.
        if (0 < refreshRate) {
            RefreshRate.validateRefreshRate(refreshRate);
        }

        this.refreshRate = refreshRate;
        this.miscProperties = (null == miscProperties ?
                new HashMap() : new HashMap(miscProperties));
    }

    // For updates, mostly, or special super advanced usage.
    SavedQueryPut(String queryName, Map miscProperties) {
        this(queryName, null, null, -1, miscProperties);
    }

    @Override
    Map constructRequestArgs() {
        Map requestArgs = super.constructRequestArgs();

        if (null == requestArgs) {
            throw new IllegalStateException("Request args should not be null.");
        }

        // If there isn't a KeenQueryRequest then hopefully this is an update and the entire
        // new query definition had better be in miscProperties.
        if (null != query) {
            // Make sure there isn't already a "query" key
            SavedQueryPut.expectNotContainsKey(requestArgs, KeenQueryConstants.QUERY);
            Map queryArgs = query.constructRequestArgs();

            // The "analysis_type" goes on the "query" object
            queryArgs.put(KeenQueryConstants.ANALYSIS_TYPE, query.getAnalysisType());

            requestArgs.put(KeenQueryConstants.QUERY, queryArgs);
        }

        if (0 <= refreshRate) {
            // Make sure there isn't already a "refresh_rate" key first
            SavedQueryPut.expectNotContainsKey(requestArgs, KeenQueryConstants.REFRESH_RATE);
            requestArgs.put(KeenQueryConstants.REFRESH_RATE, refreshRate);
        }

        String displayName = getDisplayName();

        // NOTE : For now, metadata and therefore display_name is undocumented for Saved Queries.
        if (null != displayName) {
            // Make sure there isn't already a "metadata" key
            SavedQueryPut.expectNotContainsKey(requestArgs, KeenQueryConstants.METADATA);
            Map metadata = new HashMap();
            metadata.put(KeenQueryConstants.DISPLAY_NAME, displayName);

            requestArgs.put(KeenQueryConstants.METADATA, metadata);
        }

        // Make sure miscProperties wouldn't overwrite any of the other properties.
        for (String key : miscProperties.keySet()) {
            SavedQueryPut.expectNotContainsKey(requestArgs, key);
        }

        requestArgs.putAll(miscProperties);

        return requestArgs;
    }

    private static void expectNotContainsKey(Map map, Object key) {
        if (map.containsKey(key)) {
            throw new IllegalStateException(String.format(Locale.US,
                                                          "The key '%s' already exists.",
                                                          key));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy