Ejemplo n.º 1
0
        /// <summary>
        /// Writes a list of measurements to the InfluxDB database
        /// </summary>
        /// <param name="listOfMeasurements">The list of measurements to write.  Each measurement must have at least one field specified</param>
        /// <param name="retention">The specific retention policy name to write to.</param>
        /// <returns>An awaitable Task containing the HttpResponseMessage returned from the InfluxDB server</returns>
        async public Task <HttpResponseMessage> Write(List <Measurement> listOfMeasurements, String retention = "")
        {
            //  The default response message:
            HttpResponseMessage retval = new HttpResponseMessage(HttpStatusCode.InternalServerError);

            //  Create our url to post data to
            string url = string.Format("{0}/write?db={1}", _baseUrl, _database);

            if (retention.Length > 0)
            {
                url += string.Format("&rp={0}", retention);
            }

            //  Our string to build:
            StringBuilder sb = new StringBuilder();

            foreach (var m in listOfMeasurements)
            {
                //  Make sure the measurement has at least one field:
                if (!(m.BooleanFields.Any() ||
                      m.FloatFields.Any() ||
                      m.IntegerFields.Any() ||
                      m.StringFields.Any()))
                {
                    string error = string.Format("Measurement '{0}' needs at least one field value", m.Name);
                    Trace.TraceError(error);

                    LogError(new ArgumentException(error), "Measurement '{0}' needs at least one field value", m.Name);
                }

                sb.AppendFormat("{0}\n", LineProtocol.Format(m));
            }

            //  If we had some measurements...
            if (listOfMeasurements.Any())
            {
                //  Remove the last trailing newline
                sb.Remove(sb.Length - 1, 1);
            }

            //  Create our data to post:
            HttpContent content = new StringContent(sb.ToString());

            try {
                //  Make an async call to get the response
                using (HttpClient client = new HttpClient()) {
                    if (CredentialsHaveBeenSet())
                    {
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", GetHttpBasicAuthCredentials());
                    }
                    retval = await client.PostAsync(url, content);
                }
            } catch (Exception ex) {
                Trace.TraceError("Write (list) {0} caused an exception: {1}", url, ex.Message);

                LogError(ex, "Write (list) {0} caused an exception: {1}", url, ex.Message);
            }

            return(retval);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Write a measurement to the InfluxDB database
        /// </summary>
        /// <param name="m">The measurement to write.  It must have at least one field specified</param>
        /// <returns>An awaitable Task containing the HttpResponseMessage returned from the InfluxDB server</returns>
        async public Task <HttpResponseMessage> Write(Measurement m)
        {
            //  The default response message:
            HttpResponseMessage retval = new HttpResponseMessage(HttpStatusCode.InternalServerError);

            //  Make sure the measurement has at least one field:
            if (!(m.BooleanFields.Any() ||
                  m.FloatFields.Any() ||
                  m.IntegerFields.Any() ||
                  m.StringFields.Any()))
            {
                string error = string.Format("Measurement '{0}' needs at least one field value", m.Name);
                Trace.TraceError(error);

                LogError(new ArgumentException(error), error);
            }

            //  Create our url to post data to
            string url = string.Format("{0}/write?db={1}", _baseUrl, _database);

            //  Create our data to post:
            HttpContent content = new StringContent(LineProtocol.Format(m));

            try
            {
                //  Make an async call to get the response
                using (HttpClient client = new HttpClient())
                {
                    if (CredentialsHaveBeenSet())
                    {
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", GetHttpBasicAuthCredentials());
                    }
                    retval = await client.PostAsync(url, content);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Write {0} caused an exception: {1}", url, ex.Message);

                LogError(ex, "Write {0} caused an exception: {1}", url, ex.Message);
            }

            return(retval);
        }