Example #1
0
 /// <summary>
 /// Writes the record to the InfluxDB server. If batching is used, the record will be added to the
 /// batch buffer but will not immediately be written to the server. If the number of buffered records
 /// is greater than or equal to the BatchSize, then the batch will be flushed to the underlying writer.
 /// </summary>
 /// <param name="record">The record to write.</param>
 public virtual void Write(InfluxRecord record)
 {
     if (record == null)
     {
         throw new ArgumentNullException(nameof(record));
     }
     batch.Add(record);
     if (batchSize > 0 && batch.Count >= batchSize)
     {
         Flush();                 // flush if batch is full
     }
 }
        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);
        }