Ejemplo n.º 1
0
        private string CreateQueryUrl(string commandOrQuery, string db, InfluxQueryOptions options)
        {
            var epochPart     = options.Epoch.HasValue ? $"&epoch={options.Precision.GetQueryParameter()}" : "";
            var chunkPart     = options.ChunkSize.HasValue ? $"&chunk_size={options.ChunkSize.Value}" : "";
            var precisionPart = $"&precision={options.Precision.GetQueryParameter()}";
            var queryPart     = $"q={Uri.EscapeDataString(commandOrQuery)}";
            var dbPart        = string.IsNullOrEmpty(db) ? "" : $"db={Uri.EscapeDataString(db)}&";

            return("query?" + dbPart + queryPart + precisionPart + chunkPart + epochPart);
        }
Ejemplo n.º 2
0
 private string CreateQueryUrl(string commandOrQuery, string db, InfluxQueryOptions options)
 {
     if (options.ChunkSize.HasValue)
     {
         return($"query?db={Uri.EscapeDataString( db )}&q={Uri.EscapeDataString( commandOrQuery )}&precision={options.Precision.GetQueryParameter()}&chunk_size={options.ChunkSize.Value}");
     }
     else
     {
         return($"query?db={Uri.EscapeDataString( db )}&q={Uri.EscapeDataString( commandOrQuery )}&precision={options.Precision.GetQueryParameter()}");
     }
 }
Ejemplo n.º 3
0
        private string CreateQueryUrl(string commandOrQuery, string db, InfluxQueryOptions options)
        {
            var query = $"query?db={Uri.EscapeDataString( db )}&q={Uri.EscapeDataString( commandOrQuery )}";

            if (options.Precision.HasValue)
            {
                query += $"&epoch={options.Precision.Value.GetQueryParameter()}";
            }

            if (options.ChunkSize.HasValue)
            {
                query += $"&chunk_size={options.ChunkSize.Value}";
            }

            return(query);
        }
Ejemplo n.º 4
0
        private InfluxClient(Uri endpoint, string username, string password, HttpClient client, bool disposeHttpClient)
        {
            _disposeHttpClientHandler = disposeHttpClient;
            _seriesMetaCache          = new Dictionary <DatabaseMeasurementInfoKey, DatabaseMeasurementInfo>();
            _endpoint = endpoint;
            _client   = client;

            DefaultWriteOptions     = new InfluxWriteOptions();
            DefaultQueryOptions     = new InfluxQueryOptions();
            TimestampParserRegistry = new DefaultTimestampParserRegistry();

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                var encoding               = Encoding.GetEncoding("ISO-8859-1");
                var credentials            = username + ":" + password;
                var encodedCredentialBytes = encoding.GetBytes(credentials);
                var encodedCredentials     = Convert.ToBase64String(encodedCredentialBytes);
                _authzHeader = new AuthenticationHeaderValue("Basic", encodedCredentials);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Constructs an InfluxClient that uses the specified credentials.
        /// </summary>
        /// <param name="endpoint"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public InfluxClient(Uri endpoint, string username, string password)
        {
            _handler = new HttpClientHandler();
            _handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

            _client             = new HttpClient(_handler, false);
            _client.BaseAddress = endpoint;

            _seriesMetaCache = new Dictionary <DatabaseMeasurementInfoKey, DatabaseMeasurementInfo>();

            DefaultWriteOptions = new InfluxWriteOptions();
            DefaultQueryOptions = new InfluxQueryOptions();

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                var encoding               = Encoding.GetEncoding("ISO-8859-1");
                var credentials            = username + ":" + password;
                var encodedCredentialBytes = encoding.GetBytes(credentials);
                var encodedCredentials     = Convert.ToBase64String(encodedCredentialBytes);
                _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encodedCredentials);
            }
        }
Ejemplo n.º 6
0
        private async Task <InfluxResultSet <TInfluxRow> > ExecuteQueryInternalAsync <TInfluxRow>(string query, string db, bool isTimeSeriesQuery, bool forcePost, object parameters, InfluxQueryOptions options)
            where TInfluxRow : new()
        {
            List <QueryResult> queryResults = await PerformQueryInternal(query, db, forcePost, isTimeSeriesQuery, false, parameters, options).ConfigureAwait(false);

            return(await ResultSetFactory.CreateAsync <TInfluxRow>(this, queryResults, db, isTimeSeriesQuery, options).ConfigureAwait(false));
        }
Ejemplo n.º 7
0
        private async Task <InfluxChunkedResultSet <TInfluxRow> > ExecuteQueryByObjectIteratorInternalAsync <TInfluxRow>(string query, string db, bool isTimeSeriesQuery, bool forcePost, object parameters, InfluxQueryOptions options)
            where TInfluxRow : new()
        {
            var iterator = await PerformQueryInternal <TInfluxRow>(query, db, forcePost, isTimeSeriesQuery, true, parameters, options).ConfigureAwait(false);

            return(new InfluxChunkedResultSet <TInfluxRow>(iterator, this, options, db));
        }
Ejemplo n.º 8
0
        private string CreateQueryUrl(string commandOrQuery, string db, bool isTimeSeriesQuery, bool requireChunking, object parameters, InfluxQueryOptions options)
        {
            var  query     = "query";
            char seperator = '?';

            if (!string.IsNullOrEmpty(db))
            {
                query    += $"{seperator}db={UriHelper.SafeEscapeDataString(db)}";
                seperator = '&';
            }

            if (!string.IsNullOrEmpty(commandOrQuery))
            {
                query    += $"{seperator}q={UriHelper.SafeEscapeDataString(commandOrQuery)}";
                seperator = '&';
            }
            if (parameters != null)
            {
                query    += $"{seperator}params={UriHelper.SafeEscapeDataString(ParamsConverter.GetParams(parameters))}";
                seperator = '&';
            }

            if (options != null)
            {
                if (options.Precision.HasValue && isTimeSeriesQuery)
                {
                    query    += $"{seperator}epoch={options.Precision.Value.GetQueryParameter()}";
                    seperator = '&';
                }

                if (options.ChunkSize.HasValue)
                {
                    query    += $"{seperator}chunked=true&chunk_size={options.ChunkSize.Value}";
                    seperator = '&';
                }
            }

            // add chunking if it is not already set
            if (requireChunking && options?.ChunkSize.HasValue != true)
            {
                query += $"{seperator}chunked=true";
            }

            return(new Uri(_endpoint, query).ToString());
        }
Ejemplo n.º 9
0
        private LongFormUrlEncodedContent CreateQueryPostContent(string commandOrQuery, string db, bool isTimeSeriesQuery, bool requireChunking, object parameters, InfluxQueryOptions options)
        {
            List <KeyValuePair <string, string> > param = new List <KeyValuePair <string, string> >(5);

            if (!string.IsNullOrEmpty(db))
            {
                param.Add(new KeyValuePair <string, string>("db", db));
            }
            if (!string.IsNullOrEmpty(commandOrQuery))
            {
                param.Add(new KeyValuePair <string, string>("q", commandOrQuery));
            }
            if (parameters != null)
            {
                param.Add(new KeyValuePair <string, string>("params", ParamsConverter.GetParams(parameters)));
            }

            if (options != null)
            {
                if (options.Precision.HasValue && isTimeSeriesQuery)
                {
                    param.Add(new KeyValuePair <string, string>("epoch", options.Precision.Value.GetQueryParameter()));
                }
                if (options.ChunkSize.HasValue)
                {
                    param.Add(new KeyValuePair <string, string>("chunked", "true"));
                    param.Add(new KeyValuePair <string, string>("chunk_size", options.ChunkSize.Value.ToString(CultureInfo.InvariantCulture)));
                }
            }

            // add chunking if it is not already set
            if (requireChunking && options?.ChunkSize.HasValue != true)
            {
                param.Add(new KeyValuePair <string, string>("chunked", "true"));
            }

            return(new LongFormUrlEncodedContent(param));
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Executes the query and returns the result with the specified query options.
 /// </summary>
 /// <typeparam name="TInfluxRow">The type of the influx row.</typeparam>
 /// <param name="db">The database.</param>
 /// <param name="query">The query.</param>
 /// <param name="parameters">The parameters.</param>
 /// <param name="options">The options.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns></returns>
 public Task <InfluxResultSet <TInfluxRow> > ReadAsync <TInfluxRow>(string db, string query, object parameters, InfluxQueryOptions options, CancellationToken cancellationToken = default)
     where TInfluxRow : new()
 {
     return(ExecuteQueryInternalAsync <TInfluxRow>(query, db, true, false, parameters, options, cancellationToken));
 }
Ejemplo n.º 11
0
        private async Task <List <QueryResult> > PerformQueryInternal(string query, string db, bool forcePost, bool isTimeSeriesQuery, bool requireChunking, object parameters, InfluxQueryOptions options)
        {
            List <QueryResult> queryResults;

            if (options.UsePost)
            {
                queryResults = await ExecuteHttpAsync(HttpMethod.Post, new Uri(_endpoint, "query").ToString(), CreateQueryPostContent(query, db, isTimeSeriesQuery, requireChunking, parameters, options)).ConfigureAwait(false);
            }
            else
            {
                if (forcePost)
                {
                    queryResults = await ExecuteHttpAsync(HttpMethod.Post, CreateQueryUrl(query, db, isTimeSeriesQuery, requireChunking, parameters, options)).ConfigureAwait(false);
                }
                else
                {
                    queryResults = await ExecuteHttpAsync(HttpMethod.Get, CreateQueryUrl(query, db, isTimeSeriesQuery, requireChunking, parameters, options)).ConfigureAwait(false);
                }
            }

            return(queryResults);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Executes the query and returns the result with the specified query options.
 /// </summary>
 /// <typeparam name="TInfluxRow"></typeparam>
 /// <param name="query"></param>
 /// <param name="db"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public Task <InfluxResultSet <TInfluxRow> > ReadAsync <TInfluxRow>(string db, string query, InfluxQueryOptions options)
     where TInfluxRow : new()
 {
     return(ExecuteQueryInternalAsync <TInfluxRow>(query, db, options));
 }
 internal InfluxChunkedResultSet(ContextualQueryResultIterator <TInfluxRow> contextualIterator, InfluxClient client, InfluxQueryOptions options, string db)
 {
     _iterator = contextualIterator;
 }
 /// <summary>
 /// Executes the query and returns a deferred result that can be iterated over as they
 /// are returned by the database.
 ///
 /// It does not make sense to use this method unless you are returning a big payload and
 /// have enabled chunking through InfluxQueryOptions.
 /// </summary>
 /// <typeparam name="TInfluxRow"></typeparam>
 /// <param name="client">The IInfluxClient that performs operation.</param>
 /// <param name="db"></param>
 /// <param name="query"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static Task <InfluxChunkedResultSet <TInfluxRow> > ReadChunkedAsync <TInfluxRow>(this IInfluxClient client, string db, string query, InfluxQueryOptions options)
     where TInfluxRow : new()
 {
     return(client.ReadChunkedAsync <TInfluxRow>(db, query, null, options));
 }
Ejemplo n.º 15
0
        private async Task <InfluxResultSet> ExecuteQueryInternalAsync(string query, string db, bool forcePost, object parameters, InfluxQueryOptions options)
        {
            List <QueryResult> queryResults = await PerformQueryInternal(query, db, forcePost, false, false, parameters, options).ConfigureAwait(false);

            return(ResultSetFactory.Create(queryResults));
        }
Ejemplo n.º 16
0
        private async Task <ContextualQueryResultIterator <TInfluxRow> > PerformQueryInternal <TInfluxRow>(string query, string db, bool forcePost, bool isTimeSeriesQuery, bool requireChunking, object parameters, InfluxQueryOptions options)
            where TInfluxRow : new()
        {
            ContextualQueryResultIterator <TInfluxRow> iterator;

            if (options.UsePost)
            {
                iterator = await ExecuteHttpAsync <TInfluxRow>(HttpMethod.Post, new Uri(_endpoint, "query").ToString(), db, options, CreateQueryPostContent(query, db, isTimeSeriesQuery, requireChunking, parameters, options)).ConfigureAwait(false);
            }
            else
            {
                if (forcePost)
                {
                    iterator = await ExecuteHttpAsync <TInfluxRow>(HttpMethod.Post, CreateQueryUrl(query, db, isTimeSeriesQuery, requireChunking, parameters, options), db, options).ConfigureAwait(false);
                }
                else
                {
                    iterator = await ExecuteHttpAsync <TInfluxRow>(HttpMethod.Get, CreateQueryUrl(query, db, isTimeSeriesQuery, requireChunking, parameters, options), db, options).ConfigureAwait(false);
                }
            }

            return(iterator);
        }
Ejemplo n.º 17
0
        private async Task <InfluxResultSet <TInfluxRow> > ExecuteQueryInternalAsync <TInfluxRow>(string query, string db, InfluxQueryOptions options)
            where TInfluxRow : new()
        {
            var queryResult = await GetInternalAsync(CreateQueryUrl( query, db, options ), true).ConfigureAwait(false);

            return(await ResultSetFactory.CreateAsync <TInfluxRow>(this, queryResult, db, false).ConfigureAwait(false));
        }
Ejemplo n.º 18
0
        private async Task <ContextualQueryResultIterator <TInfluxRow> > ExecuteHttpAsync <TInfluxRow>(HttpMethod method, string url, string db, InfluxQueryOptions options, HttpContent content = null)
            where TInfluxRow : new()
        {
            try
            {
                using (var request = new HttpRequestMessage(method, url)
                {
                    Content = (method == HttpMethod.Get ? null : (content == null ? new StringContent("") : content))
                })
                {
                    request.Headers.Authorization = _authzHeader;

                    var response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
                    await EnsureSuccessCode(response).ConfigureAwait(false);

                    var objectIterator = await response.Content.GetObjectIteratorAsync().ConfigureAwait(false);

                    var iterator           = new QueryResultIterator <TInfluxRow>(response, objectIterator, this, options, db);
                    var contextualIterator = new ContextualQueryResultIterator <TInfluxRow>(iterator);
                    return(contextualIterator);
                }
            }
            catch (HttpRequestException e)
            {
                throw new InfluxException(Errors.UnknownError, e);
            }
        }
Ejemplo n.º 19
0
 /// <summary>
 /// Executes the query and returns a deferred result that can be iterated over as they
 /// are returned by the database.
 ///
 /// It does not make sense to use this method unless you are returning a big payload and
 /// have enabled chunking through InfluxQueryOptions.
 /// </summary>
 /// <typeparam name="TInfluxRow"></typeparam>
 /// <param name="db"></param>
 /// <param name="query"></param>
 /// <param name="options"></param>
 /// <param name="parameters"></param>
 /// <returns></returns>
 public Task <InfluxChunkedResultSet <TInfluxRow> > ReadChunkedAsync <TInfluxRow>(string db, string query, object parameters, InfluxQueryOptions options)
     where TInfluxRow : new()
 {
     return(ExecuteQueryByObjectIteratorInternalAsync <TInfluxRow>(query, db, true, false, parameters, options));
 }