Ejemplo n.º 1
0
        /// <summary>
        /// Posts an InfluxDataPoint to given measurement
        /// </summary>
        /// <param name="dbName">InfluxDB database name</param>
        /// <param name="point">Influx data point to be written</param>
        /// <returns>True:Success, False:Failure</returns>
        ///<exception cref="UnauthorizedAccessException">When Influx needs authentication, and no user name password is supplied or auth fails</exception>
        ///<exception cref="HttpRequestException">all other HTTP exceptions</exception>
        public async Task <bool> PostPointAsync(string dbName, IInfluxDatapoint point)
        {
            ByteArrayContent requestContent = new ByteArrayContent(Encoding.UTF8.GetBytes(point.ConvertToInfluxLineProtocol()));
            var endPoint = new Dictionary <string, string>()
            {
                { "db", dbName },
                { "precision", precisionLiterals[(int)point.Precision] }
            };

            if (!String.IsNullOrWhiteSpace(point.Retention?.Name))
            {
                endPoint.Add("rp", point.Retention?.Name);
            }
            HttpResponseMessage response = await PostAsync(endPoint, requestContent);

            if (response.StatusCode == HttpStatusCode.BadRequest)
            {
                throw InfluxDBException.ProcessInfluxDBError(await response.Content.ReadAsStringAsync());
            }
            else if (response.StatusCode == HttpStatusCode.NoContent)
            {
                point.Saved = true;
                return(true);
            }
            else
            {
                point.Saved = false;
                return(false);
            }
        }
        /// <summary>
        /// Posts an InfluxDataPoint to given measurement
        /// </summary>
        /// <param name="dbName">InfluxDB database name</param>
        /// <param name="point">Influx data point to be written</param>
        /// <returns>True:Success, False:Failure</returns>
        ///<exception cref="UnauthorizedAccessException">When Influx needs authentication, and no user name password is supplied or auth fails</exception>
        ///<exception cref="HttpRequestException">all other HTTP exceptions</exception>   
        public async Task<bool> PostPointAsync(string dbName, IInfluxDatapoint point)
        {
            var influxAddress = new Uri (String.Format ("{0}/write?", InfluxUrl));
            var builder = new UriBuilder (influxAddress);
            builder.Query = await new FormUrlEncodedContent (new[] { 
                    new KeyValuePair<string, string>("db", dbName) ,
                    new KeyValuePair<string, string>("precision", precisionLiterals[(int) point.Precision])
                    }).ReadAsStringAsync ();

            ByteArrayContent requestContent = new ByteArrayContent (Encoding.UTF8.GetBytes (point.ConvertToInfluxLineProtocol ()));
            HttpResponseMessage response = await PostAsync (builder, requestContent);

            if ( response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.BadGateway || ( response.StatusCode == HttpStatusCode.InternalServerError && response.ReasonPhrase == "INKApi Error" ) ) //502 Connection refused
                throw new UnauthorizedAccessException ("InfluxDB needs authentication. Check uname, pwd parameters");
            //if(response.StatusCode==HttpStatusCode.NotFound)
            else if ( response.StatusCode == HttpStatusCode.BadRequest )
            {
                var content = await response.Content.ReadAsStringAsync ();
                //regex assumes error text from https://github.com/influxdata/influxdb/blob/master/models/points.go ParsePointsWithPrecision
                //fmt.Sprintf("'%s': %v", string(block[start:len(block)])
                List<string> parts; bool partialWrite;
                if ( content.Contains ("partial write") )
                {
                    if ( content.Contains ("\\n") )
                        parts = Regex.Matches (content.Substring (content.IndexOf ("partial write:\\n") + 16), @"([\P{Cc}].*?) '([\P{Cc}].*?)':([\P{Cc}].*?)\\n").ToList ();
                    else
                        parts = Regex.Matches (content.Substring (content.IndexOf ("partial write:\\n") + 16), @"([\P{Cc}].*?) '([\P{Cc}].*?)':([\P{Cc}].*?)").ToList ();
                    partialWrite = true;
                }
                else
                {
                    parts = Regex.Matches (content, @"{\""error"":""([9\P{Cc}]+) '([\P{Cc}]+)':([a-zA-Z0-9 ]+)").ToList ();
                    partialWrite = false;
                }
                throw new InfluxDBException (partialWrite ? "Partial Write" : "Failed to Write", String.Format ("{0}: {1} due to {2}", partialWrite ? "Partial Write" : "Failed to Write", parts[0], parts[2]), point);
                return false;
            }
            else if ( response.StatusCode == HttpStatusCode.NoContent )
            {
                point.Saved = true;
                return true;
            }
            else
            {
                point.Saved = false;
                return false;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Posts an InfluxDataPoint to given measurement
        /// </summary>
        /// <param name="dbName">InfluxDB database name</param>
        /// <param name="point">Influx data point to be written</param>
        /// <returns>True:Success, False:Failure</returns>
        ///<exception cref="UnauthorizedAccessException">When Influx needs authentication, and no user name password is supplied or auth fails</exception>
        ///<exception cref="HttpRequestException">all other HTTP exceptions</exception>
        public async Task <bool> PostPointAsync(string dbName, IInfluxDatapoint point)
        {
            var influxAddress = new Uri(String.Format("{0}/write?", InfluxUrl));
            var builder       = new UriBuilder(influxAddress);

            builder.Query = await new FormUrlEncodedContent(new[] {
                new KeyValuePair <string, string>("db", dbName),
                new KeyValuePair <string, string>("precision", precisionLiterals[(int)point.Precision])
            }).ReadAsStringAsync();

            ByteArrayContent    requestContent = new ByteArrayContent(Encoding.UTF8.GetBytes(point.ConvertToInfluxLineProtocol()));
            HttpResponseMessage response       = await PostAsync(builder, requestContent);

            if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.BadGateway || (response.StatusCode == HttpStatusCode.InternalServerError && response.ReasonPhrase == "INKApi Error"))     //502 Connection refused
            {
                throw new UnauthorizedAccessException("InfluxDB needs authentication. Check uname, pwd parameters");
            }
            //if(response.StatusCode==HttpStatusCode.NotFound)
            else if (response.StatusCode == HttpStatusCode.NoContent)
            {
                point.Saved = true;
                return(true);
            }
            else
            {
                point.Saved = false;
                return(false);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Posts an InfluxDataPoint to given measurement
        /// </summary>
        /// <param name="dbName">InfluxDB database name</param>
        /// <param name="point">Influx data point to be written</param>
        /// <returns>True:Success, False:Failure</returns>
        ///<exception cref="UnauthorizedAccessException">When Influx needs authentication, and no user name password is supplied or auth fails</exception>
        ///<exception cref="HttpRequestException">all other HTTP exceptions</exception>   
        public async Task<bool> PostPointAsync (string dbName, IInfluxDatapoint point)
        {

            ByteArrayContent requestContent = new ByteArrayContent (Encoding.UTF8.GetBytes (point.ConvertToInfluxLineProtocol ()));
            var endPoint = new Dictionary<string, string> () {
               { "db", dbName },
               { "precision", precisionLiterals[(int)point.Precision] }};
            if (!String.IsNullOrWhiteSpace (point.Retention?.Name))
                endPoint.Add ("rp", point.Retention?.Name);
            HttpResponseMessage response = await PostAsync (endPoint, requestContent);

            if (response.StatusCode == HttpStatusCode.BadRequest)
            {
                throw InfluxDBException.ProcessInfluxDBError (await response.Content.ReadAsStringAsync ());
            }
            else if (response.StatusCode == HttpStatusCode.NoContent)
            {
                point.Saved = true;
                return true;
            }
            else
            {
                point.Saved = false;
                return false;
            }
        }