public void InfluxBatch_FormatsTo_LineProtocol()
        {
            var testNow    = new DateTime(2016, 6, 1, 0, 0, 0, DateTimeKind.Utc);
            var testTags   = TagTestCases.Select(tc => tc.Tag);
            var testFields = FieldTestCases.Select(tc => tc.Field);
            var precision  = InfluxConfig.Default.Precision;
            var expTime    = InfluxLineProtocol.FormatTimestamp(testNow, precision);

            // test with empty batch
            InfluxBatch batch = new InfluxBatch();

            batch.ToLineProtocol(precision).Should().BeEmpty();

            // test with single record
            batch.Add(new InfluxRecord("test_name", new[] { new InfluxTag("tag1", "value1") }, new[] { new InfluxField("field1", 123456) }));
            batch.ToLineProtocol(precision).Should().NotEndWith("\n").And.Be(@"test_name,tag1=value1 field1=123456i");
            batch.Clear();

            // test with multiple records
            batch.Add(new InfluxRecord("test_name1", new[] { new InfluxTag("tag1", "value1") }, new[] { new InfluxField("field1", 123456) }));
            batch.Add(new InfluxRecord("test_name2", new[] { new InfluxTag("tag2", "value2") }, new[] { new InfluxField("field2", 234561) }));
            batch.Add(new InfluxRecord("test_name3", new[] { new InfluxTag("tag3", "value3") }, new[] { new InfluxField("field3", 345612) }, testNow));
            batch.Add(new InfluxRecord("test_name4", new[] { new InfluxTag("tag4", "value4") }, new[] { new InfluxField("field4", 456123) }, testNow));
            batch.Add(new InfluxRecord("test_name5", new[] { new InfluxTag("tag5", "value5") }, new[] { new InfluxField("field5", 561234) }, testNow));

            String expOutput = String.Join("\n",
                                           $@"test_name1,tag1=value1 field1=123456i",
                                           $@"test_name2,tag2=value2 field2=234561i",
                                           $@"test_name3,tag3=value3 field3=345612i {expTime}",
                                           $@"test_name4,tag4=value4 field4=456123i {expTime}",
                                           $@"test_name5,tag5=value5 field5=561234i {expTime}"
                                           );

            batch.ToLineProtocol(precision).Should().NotEndWith("\n").And.Be(expOutput);
        }
예제 #2
0
        /// <summary>
        /// Gets the byte representation of the <see cref="InfluxBatch"/> in LineProtocol syntax using UTF8 encoding.
        /// </summary>
        /// <param name="batch">The batch to get the bytes for.</param>
        /// <returns>The byte representation of the batch.</returns>
        protected override Byte[] GetBatchBytes(InfluxBatch batch)
        {
            var strBatch = batch.ToLineProtocol(config.Precision);
            var bytes    = Encoding.UTF8.GetBytes(strBatch);

            return(bytes);
        }
예제 #3
0
        protected override Byte[] WriteToTransport(Byte[] bytes)
        {
            var lastBatch = LastBatch = new InfluxBatch(Batch.ToArray());

            FlushHistory.Add(lastBatch);
            return(null);
        }
예제 #4
0
        /// <summary>
        /// Gets the byte representation of the <see cref="InfluxBatch"/> in JSON syntax using UTF8 encoding.
        /// </summary>
        /// <param name="batch">The batch to get the bytes for.</param>
        /// <returns>The byte representation of the batch.</returns>
        protected override Byte[] GetBatchBytes(InfluxBatch batch)
        {
            var strBatch = ToJson(batch);
            var bytes    = Encoding.UTF8.GetBytes(strBatch);

            System.Diagnostics.Debug.WriteLine($"[NEW JSON]:\n{strBatch}");
            return(bytes);
        }
예제 #5
0
        /// <summary>
        /// Gets the byte representation of the <see cref="InfluxBatch"/> in LineProtocol syntax using UTF8 encoding.
        /// </summary>
        /// <param name="batch">The batch to get the bytes for.</param>
        /// <returns>The byte representation of the batch.</returns>
        protected override Byte[] GetBatchBytes(InfluxBatch batch)
        {
            // UDP only supports ns precision
            var strBatch = batch.ToLineProtocol(InfluxPrecision.Nanoseconds);
            var bytes    = Encoding.UTF8.GetBytes(strBatch);

            return(bytes);
        }
예제 #6
0
        /// <summary>
        /// Creates a new instance of a <see cref="InfluxdbWriter"/>.
        /// </summary>
        /// <param name="batchSize">The maximum number of records to write per flush. Set to zero to write all records in a single flush. Negative numbers are not allowed.</param>
        public InfluxdbWriter(Int32 batchSize)
        {
            if (batchSize < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(batchSize), "Batch size cannot be negative.");
            }

            this.batch     = new InfluxBatch();
            this.batchSize = batchSize;
        }
예제 #7
0
        protected override Byte[] WriteToTransport(Byte[] bytes)
        {
            var lastBatch = LastBatch = new InfluxBatch(Batch.ToArray());

            FlushHistory.Add(lastBatch);

            Debug.WriteLine($"[JSON] InfluxDB LineProtocol Write (count={lastBatch.Count} bytes={formatSize(bytes.Length)})");
            Stopwatch sw = Stopwatch.StartNew();

            Byte[] res = base.WriteToTransport(bytes);
            Debug.WriteLine($"[JSON] Uploaded {lastBatch.Count} measurements to InfluxDB in {sw.ElapsedMilliseconds:n0}ms. :: Bytes written: {formatSize(bytes.Length)} - Response string ({formatSize(res.Length)}): {Encoding.UTF8.GetString(res)}");
            return(res);
        }
예제 #8
0
 /// <summary>
 /// Gets the byte representation of the <see cref="InfluxBatch"/>. This will convert the
 /// batch to the correct string syntax and then get the byte array for it in UTF8 encoding.
 /// </summary>
 /// <param name="batch">The batch to get the bytes for.</param>
 /// <returns>The byte representation of the batch.</returns>
 protected abstract Byte[] GetBatchBytes(InfluxBatch batch);
예제 #9
0
 private static String ToJson(InfluxBatch batch)
 {
     return(new CollectionJsonValue(batch.Select(r => ToJsonObject(r))).AsJson());
 }