private static async Task ExecuteHourlyRevenue()
        {
            List <IInfluxDatapoint> influxDatapoints = new List <IInfluxDatapoint>();

            using (var session = dataAccessor.SessionFactory.OpenSession())
                using (var trans = session.BeginTransaction())
                {
                    PeiuPlatformMethodsExecutor methodsExecutor = new PeiuPlatformMethodsExecutor(session);
                    var results = await session.CreateCriteria <HourlyAccmofMeasurement>().ListAsync <HourlyAccmofMeasurement>();

                    foreach (HourlyAccmofMeasurement row in results)
                    {
                        HourlyActualRevenue hourlyRevenue = new HourlyActualRevenue();
                        hourlyRevenue.Createdt = row.Createdt;
                        hourlyRevenue.Hour     = row.Hour;
                        hourlyRevenue.Rcc      = row.Rcc;
                        hourlyRevenue.Siteid   = row.Siteid;

                        double pvPower = row.Sumofpvgeneration;
                        double dhg     = row.Sumofdischarge;

                        int?  rec = methodsExecutor.GetRec(row.Createdt);
                        float?smp = methodsExecutor.GetSmpPrice(row.Rcc == 16  ?  1 : 0, row.Createdt);

                        //hourlyRevenue.Rec = rec.HasValue ? rec.Value : 0;
                        //hourlyRevenue.Smp = smp.HasValue ? smp.Value : 0;
                        if (rec.HasValue && smp.HasValue)
                        {
                            double sumOfChg = row.Sumofcharge * -1;
                            double exp1     = ((pvPower - sumOfChg) * 1 * rec.Value / 1000) + (dhg * 5 * rec.Value / 1000);
                            double exp2     = (((pvPower - sumOfChg) * 1) + (dhg * 5)) * (rec.Value / 1000);
                            hourlyRevenue.Revenue = exp1 + exp2;
                        }
                        else
                        {
                            hourlyRevenue.Revenue = 0;
                        }


                        await session.SaveOrUpdateAsync(hourlyRevenue);

                        IInfluxDatapoint datapoint = ConvertinfluxDatapoint(hourlyRevenue);
                        influxDatapoints.Add(datapoint);
                    }
                    await trans.CommitAsync();
                }
            logger.Info("Complete record to mysql");
            await influxDBClient.PostPointsAsync("statistics", influxDatapoints, influxDatapoints.Count);

            logger.Info("Completed record to influxdb");
        }
        public async Task TestPostPointsAsync_FromObjects()
        {
            try
            {
                var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);
                var time   = DateTime.Now;

                var points = Enumerable.Range(0, 3).Select(i =>
                                                           new PointObjectWithStringRetention
                {
                    Time        = DateTime.UtcNow,
                    Measurement = measurementName,
                    Precision   = TimePrecision.Milliseconds,
                    StringTag   = "FromObject",
                    IntTag      = DataGen.RandomInt(),
                    StringField = DataGen.RandomString(),
                    DoubleField = DataGen.RandomDouble(),
                    IntField    = DataGen.RandomInt(),
                    BoolField   = i % 2 == 0,
                }
                                                           );

                var r = await client.PostPointsAsync(dbName, points);

                Assert.IsTrue(r, "PostPointsAsync returned false");
            }
            catch (Exception e)
            {
                Assert.Fail($"Unexpected exception of type {e.GetType()} caught: {e.Message}");
            }
        }
        public async Task TestPostPointsAsync_FromObject_AutogenRetention()
        {
            try
            {
                var client    = new InfluxDBClient(influxUrl, dbUName, dbpwd);
                var retention = new InfluxRetentionPolicy()
                {
                    Name = "autogen", DBName = dbName, Duration = TimeSpan.FromMinutes(0), IsDefault = true
                };
                var points = Enumerable.Range(0, 10).Select(i =>
                                                            new PointObjectWithObjectRetention
                {
                    Measurement = "RetentionTest",
                    Time        = DateTime.UtcNow.AddDays(-i),
                    Retention   = retention,
                    Precision   = TimePrecision.Milliseconds,
                    StringTag   = "FromObject",
                    IntTag      = DataGen.RandomInt(),
                    StringField = DataGen.RandomString(),
                    DoubleField = DataGen.RandomDouble(),
                    IntField    = DataGen.RandomInt(),
                    BoolField   = i % 2 == 0,
                });

                var r = await client.PostPointsAsync(dbName, points);

                Assert.IsTrue(r, "PostPointsAsync returned false");
            }
            catch (Exception e)
            {
                Assert.Fail($"Unexpected exception of type {e.GetType()} caught: {e.Message}");
                return;
            }
        }
        public async Task TestPostPointsAsync_DefaultTimePrecision()
        {
            try
            {
                var client   = new InfluxDBClient(influxUrl, dbUName, dbpwd);
                var time     = DateTime.Now;
                var TestDate = time.ToShortDateString();
                var TestTime = time.ToShortTimeString();
                var points   = new List <IInfluxDatapoint>();
                for (var i = 0; i < 10; i++)
                {
                    await Task.Delay(1);

                    var point = new InfluxDatapoint <long>();
                    point.MeasurementName = "DefaultPrecisionTest";
                    point.Tags.Add("TestDate", TestDate);
                    point.Tags.Add("TestTime", TestTime);
                    point.Fields.Add("Val", i);
                    points.Add(point);
                }

                var r = await client.PostPointsAsync(dbName, points);

                Assert.IsTrue(r, "PostPointsAsync retunred false");
            }
            catch (Exception e)
            {
                Assert.Fail($"Unexpected exception of type {e.GetType()} caught: {e.Message}");
                return;
            }
        }
Example #5
0
        public async Task <bool> AddRangeAsync(IEnumerable <StockSymbol> stockSymbols)
        {
            using (InfluxDBClient client = await influxContext.GetDatabaseClient())
            {
                List <InfluxDatapoint <InfluxValueField> > dataPoints = new List <InfluxDatapoint <InfluxValueField> >();
                foreach (StockSymbol stockSymbol in stockSymbols)
                {
                    InfluxDatapoint <InfluxValueField> point = new InfluxDatapoint <InfluxValueField>();
                    point.UtcTimestamp    = stockSymbol.Date;
                    point.MeasurementName = MeasureName;
                    point.Precision       = TimePrecision.Hours;
                    point.Tags.Add("SymbolName", stockSymbol.Name);
                    point.Fields.Add("Date", new InfluxValueField(stockSymbol.Date.ToString("MM-dd-yyyy")));
                    point.Fields.Add("UserName", new InfluxValueField(stockSymbol.UserName));
                    point.Fields.Add("SymbolName", new InfluxValueField(stockSymbol.Name));
                    point.Fields.Add("Open", new InfluxValueField(stockSymbol.Open));
                    point.Fields.Add("Close", new InfluxValueField(stockSymbol.Close));
                    point.Fields.Add("High", new InfluxValueField(stockSymbol.High));
                    point.Fields.Add("Low", new InfluxValueField(stockSymbol.Low));
                    point.Fields.Add("Volume", new InfluxValueField(stockSymbol.Volume));
                    dataPoints.Add(point);
                }

                return(await client.PostPointsAsync(influxContext.DatabaseName, dataPoints));
            }
        }
        public async Task TestPostPointsAsync_DifferentTypeFailure()
        {
            var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);

            var points = new List <IInfluxDatapoint>();

            var firstPoint = new InfluxDatapoint <int>();

            firstPoint.UtcTimestamp = DateTime.UtcNow;
            firstPoint.Fields.Add("value", 1);
            firstPoint.MeasurementName = "SameKeyDifferentType";
            firstPoint.Precision       = TimePrecision.Milliseconds;
            points.Add(firstPoint);


            var secondPoint = new InfluxDatapoint <double>();

            secondPoint.UtcTimestamp = DateTime.UtcNow;
            secondPoint.Fields.Add("value", 123.1234);
            secondPoint.MeasurementName = "SameKeyDifferentType";
            secondPoint.Precision       = TimePrecision.Milliseconds;
            points.Add(secondPoint);


            var r = await AssertEx.ThrowsAsync <InfluxDBException>(() => client.PostPointsAsync(dbName, points));
        }
        public async Task TestPostPointsAsync()
        {
            try
            {
                var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);
                var time   = DateTime.Now;
                var rand   = new Random();

                var valDouble = new InfluxDatapoint <double> ();
                valDouble.UtcTimestamp = DateTime.UtcNow;
                valDouble.Tags.Add("TestDate", time.ToShortDateString());
                valDouble.Tags.Add("TestTime", time.ToShortTimeString());
                valDouble.Fields.Add("Doublefield", rand.NextDouble());
                valDouble.Fields.Add("Doublefield2", rand.NextDouble());
                valDouble.MeasurementName = measurementName;
                valDouble.Precision       = TimePrecision.Milliseconds;

                var valInt = new InfluxDatapoint <int> ();
                valInt.UtcTimestamp = DateTime.UtcNow;
                valInt.Tags.Add("TestDate", time.ToShortDateString());
                valInt.Tags.Add("TestTime", time.ToShortTimeString());
                valInt.Fields.Add("Intfield", rand.Next());
                valInt.Fields.Add("Intfield2", rand.Next());
                valInt.MeasurementName = measurementName;
                valInt.Precision       = TimePrecision.Seconds;

                var valBool = new InfluxDatapoint <bool> ();
                valBool.UtcTimestamp = DateTime.UtcNow;
                valBool.Tags.Add("TestDate", time.ToShortDateString());
                valBool.Tags.Add("TestTime", time.ToShortTimeString());
                valBool.Fields.Add("Booleanfield", time.Ticks % 2 == 0);
                valBool.MeasurementName = measurementName;
                valBool.Precision       = TimePrecision.Minutes;

                var valString = new InfluxDatapoint <string> ();
                valString.UtcTimestamp = DateTime.UtcNow;
                valString.Tags.Add("TestDate", time.ToShortDateString());
                valString.Tags.Add("TestTime", time.ToShortTimeString());
                valString.Fields.Add("Stringfield", GenerateRandomString());
                valString.MeasurementName = measurementName;
                valString.Precision       = TimePrecision.Hours;

                var points = new List <IInfluxDatapoint> ();
                points.Add(valString);
                points.Add(valInt);
                points.Add(valDouble);
                points.Add(valBool);
                var r = await client.PostPointsAsync(dbName, points);

                Assert.IsTrue(r, "PostPointsAsync retunred false");
            }
            catch (Exception e)
            {
                Assert.Fail("Unexpected exception of type {0} caught: {1}",
                            e.GetType(), e.Message);
                return;
            }
        }
        public async Task TestPostPointsAsync_DifferentPrecisions()
        {
            try
            {
                var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);
                var time   = DateTime.Now;
                var today  = DateTime.Now.ToShortDateString();
                var now    = DateTime.Now.ToShortTimeString();

                var points = new List <IInfluxDatapoint>();

                foreach (TimePrecision precision in Enum.GetValues(typeof(TimePrecision)))
                {
                    var point = new InfluxDatapoint <long>();
                    point.UtcTimestamp    = DateTime.UtcNow;
                    point.MeasurementName = $"Precision{precision.ToString()}";
                    point.Precision       = precision;
                    point.Tags.Add("Precision", precision.ToString());
                    point.Fields.Add("Ticks", point.UtcTimestamp.Ticks);
                    points.Add(point);
                }

                var r = await client.PostPointsAsync(dbName, points);

                Assert.IsTrue(r, "PostPointsAsync retunred false");

                var values = await client.QueryMultiSeriesAsync(dbName, "select * from /Precision[A-Za-z]/");

                foreach (var val in values)
                {
                    var           x = val?.Entries?.FirstOrDefault();
                    var           d = new DateTime(long.Parse(x.Ticks));
                    TimeSpan      t = d - x.Time;
                    TimePrecision p = Enum.Parse(typeof(TimePrecision), x.Precision);
                    switch (p)
                    {
                    case TimePrecision.Hours: Assert.IsTrue(t.TotalHours < 1); break;

                    case TimePrecision.Minutes: Assert.IsTrue(t.TotalMinutes < 1); break;

                    case TimePrecision.Seconds: Assert.IsTrue(t.TotalSeconds < 1); break;

                    case TimePrecision.Milliseconds: Assert.IsTrue(t.TotalMilliseconds < 1); break;

                    case TimePrecision.Microseconds: Assert.IsTrue(t.Ticks < (TimeSpan.TicksPerMillisecond / 1000)); break;

                    case TimePrecision.Nanoseconds: Assert.IsTrue(t.Ticks < 1); break;
                    }
                }
            }
            catch (Exception e)
            {
                Assert.Fail($"Unexpected exception of type {e.GetType()} caught: {e.Message}");
                return;
            }
        }
        public async Task TestDropMeasurementAsyncError()
        {
            var measurement = "hara-kiri";
            var client      = new InfluxDBClient(influxUrl, dbUName, dbpwd);
            var points      = await CreateTestPoints(measurement, 10);

            var r = await client.PostPointsAsync(dbName, points);

            var d = new InfluxMeasurement(measurement);
            await AssertEx.ThrowsAsync <InfluxDBException>(() => client.DropMeasurementAsync(new InfluxDatabase(invalidDbName), d));
        }
        public async Task TestDeletePointsAsyncError()
        {
            var measurement = "hara-kiri-half";
            var client      = new InfluxDBClient(influxUrl, dbUName, dbpwd);
            var points      = await CreateTestPoints(measurement, 10);

            points.Skip(5).Select(p => { p.Tags.Add("purge", "'yes'"); return(true); });
            var r = await client.PostPointsAsync(dbName, points);

            await AssertEx.ThrowsAsync <InfluxDBException>(() => client.DeletePointsAsync(new InfluxDatabase(invalidDbName), new InfluxMeasurement(measurement), whereClause: new List <string>()
            {
                "purge = yes", $"time() > {DateTime.UtcNow.AddDays(-4).ToEpoch(TimePrecision.Hours)}"
            }));
        }
        public async Task TestDropMeasurementAsync()
        {
            var measurement = "hara-kiri";
            var client      = new InfluxDBClient(influxUrl, dbUName, dbpwd);
            var points      = await CreateTestPoints(measurement, 10);

            var r = await client.PostPointsAsync(dbName, points);

            var d = new InfluxMeasurement(measurement);

            r = await client.DropMeasurementAsync(new InfluxDatabase(dbName), d);

            Assert.IsTrue(r && d.Deleted, "DropMeasurementAsync retunred false");
        }
Example #12
0
        public void TestPostPoints()
        {
            try
            {
                var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);
                var time   = DateTime.Now;
                var today  = DateTime.Now.ToShortDateString();
                var now    = DateTime.Now.ToShortTimeString();

                var points = new List <IInfluxDatapoint>();

                var item = new CarBatteryPoint();
                item.DeviceNumber     = "";
                item.CustomerCode     = "";
                item.DeviceWays       = "";
                item.TransactionID    = "";
                item.VIN              = "";
                item.RVOL             = DataGen.RandomDouble();
                item.RCUR             = DataGen.RandomDouble();
                item.Ptemp            = DataGen.RandomDouble();
                item.Ntemp            = DataGen.RandomDouble();
                item.BATType          = DataGen.RandomDouble();
                item.BATPOW           = DataGen.RandomDouble();
                item.Capacity         = DataGen.RandomDouble();
                item.MaxCVol          = DataGen.RandomDouble();
                item.MaxCCur          = DataGen.RandomDouble();
                item.SigleMaxVol      = DataGen.RandomDouble();
                item.SigleMinVol      = DataGen.RandomDouble();
                item.StartSigleMaxVol = DataGen.RandomDouble();
                item.StartSigleMinVol = DataGen.RandomDouble();
                item.EndSigleMaxVol   = DataGen.RandomDouble();
                item.EndSigleMinVol   = DataGen.RandomDouble();
                item.MaxTemp          = DataGen.RandomDouble();
                item.StartVol         = DataGen.RandomDouble();
                item.CreateDate       = DateTime.UtcNow;
                item.MeasurementName  = measurementName;
                item.Precision        = TimePrecision.Nanoseconds;
                points.Add(item);



                var r = client.PostPointsAsync(dbName, points).Result;
                Assert.True(r, "PostPointsAsync retunred false");
            }
            catch (Exception e)
            {
                //Assert.Fail($"Unexpected exception of type {e.GetType()} caught: {e.Message}");
                return;
            }
        }
        public async Task TestPostPointsAsync_PartialWrite()
        {
            var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);
            var time   = DateTime.Now;
            var today  = DateTime.Now.ToShortDateString();
            var now    = DateTime.Now.ToShortTimeString();

            var points = new List <IInfluxDatapoint>();


            var valDouble = new InfluxDatapoint <double>();

            valDouble.UtcTimestamp = DateTime.UtcNow;
            valDouble.Tags.Add("TestDate", today);
            valDouble.Tags.Add("TestTime", now);
            valDouble.Fields.Add("Doublefield", DataGen.RandomDouble());
            valDouble.Fields.Add("Doublefield2", Double.NaN);
            valDouble.MeasurementName = measurementName;
            valDouble.Precision       = TimePrecision.Seconds;
            points.Add(valDouble);


            for (int i = 0; i < 5; i++)
            {
                var valInt = new InfluxDatapoint <int>();
                valInt.UtcTimestamp = DateTime.UtcNow.AddSeconds(-1 * DataGen.RandomInt(3600));
                valInt.Tags.Add("TestDate", today);
                valInt.Tags.Add("TestTime", now);
                valInt.Fields.Add("Intfield", DataGen.RandomInt());
                valInt.Fields.Add("Intfield2", DataGen.RandomInt());
                valInt.MeasurementName = measurementName;
                valInt.Precision       = TimePrecision.Seconds;
                points.Add(valInt);
            }

            valDouble = new InfluxDatapoint <double>();
            valDouble.UtcTimestamp = DateTime.UtcNow;
            valDouble.Tags.Add("TestDate", today);
            valDouble.Tags.Add("TestTime", now);
            valDouble.Fields.Add("Doublefield", DataGen.RandomDouble());
            valDouble.Fields.Add("Doublefield2", Double.NaN);
            valDouble.MeasurementName = measurementName;
            valDouble.Precision       = TimePrecision.Seconds;
            points.Add(valDouble);


            var r = await AssertEx.ThrowsAsync <InfluxDBException>(() => client.PostPointsAsync(dbName, points));
        }
        public async Task TestPartialResponseBecauseOfMaxRowLimit()
        {
            try
            {
                var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);

                var points = new List <IInfluxDatapoint>();

                var today       = DateTime.Now.ToShortDateString();
                var now         = DateTime.UtcNow;
                var nowString   = DateTime.Now.ToShortTimeString();
                var measurement = "Partialtest";

                for (int i = 0; i < 200000; i++)
                {
                    var valMixed = new InfluxDatapoint <InfluxValueField>();
                    valMixed.Tags.Add("TestDate", today);
                    valMixed.Tags.Add("TestTime", nowString);
                    valMixed.UtcTimestamp = now + TimeSpan.FromMilliseconds(i * 100);
                    valMixed.Fields.Add("Open", new InfluxValueField(DataGen.RandomDouble()));
                    valMixed.Fields.Add("High", new InfluxValueField(DataGen.RandomDouble()));
                    valMixed.Fields.Add("Low", new InfluxValueField(DataGen.RandomDouble()));
                    valMixed.Fields.Add("Close", new InfluxValueField(DataGen.RandomDouble()));
                    valMixed.Fields.Add("Volume", new InfluxValueField(DataGen.RandomDouble()));

                    valMixed.MeasurementName = measurement;
                    valMixed.Precision       = TimePrecision.Nanoseconds;
                    points.Add(valMixed);
                }


                Assert.IsTrue(await client.PostPointsAsync(dbName, points, 25000), "PostPointsAsync retunred false");

                var r = await client.QueryMultiSeriesAsync(dbName, "SELECT * FROM Partialtest");

                Assert.IsTrue(r.All(s => s.Partial), "Not all influx series returned by the query contained the flag 'partial=true'");

                r = await client.QueryMultiSeriesAsync(dbName, "SELECT * FROM Partialtest limit 50000");

                Assert.IsTrue(!r.Any(s => s.Partial), "At least one of the influx series returned by the query contained the flag 'partial=true'");
            }
            catch (Exception e)
            {
                Assert.Fail($"Unexpected exception of type {e.GetType()} caught: {e.Message}");
                return;
            }
        }
        public async Task TestQueryMultiSeriesAsync_ToObject()
        {
            var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);

            var tagId = DataGen.RandomString();

            var points = Enumerable.Range(0, 3).Select(i =>
                                                       new PointObjectWithStringRetention
            {
                Time        = DateTime.UtcNow,
                Measurement = measurementName,
                Precision   = TimePrecision.Nanoseconds,
                StringTag   = tagId,
                IntTag      = i,
                StringField = DataGen.RandomString(),
                DoubleField = DataGen.RandomDouble(),
                IntField    = DataGen.RandomInt(),
                BoolField   = i % 2 == 0,
            }
                                                       ).ToList();

            var postResult = await client.PostPointsAsync(dbName, points);

            Assert.IsTrue(postResult, "PostPointsAsync returned false");

            Stopwatch s = new Stopwatch();

            s.Start();
            var queryResult = await client.QueryMultiSeriesAsync <PointObjectWithStringRetention>(dbName, $"SELECT * from {measurementName} WHERE StringTag='{tagId}'");

            s.Stop();
            Debug.WriteLine($"Elapsed {s.ElapsedMilliseconds}ms");

            Assert.IsTrue(queryResult != null, "QueryMultiSeriesAsync returned null or invalid data");
            Assert.IsTrue(queryResult.Count == 1, "QueryMultiSeriesAsync returned invalid number of series");
            foreach (var point in points)
            {
                var matching = queryResult[0].Entries.FirstOrDefault(e => e.IntTag == point.IntTag);

                Assert.IsTrue(matching != null, $"Missing record corresponding to record {point.IntTag}");

                Assert.IsTrue(matching.StringField == point.StringField, $"Mismatching string field on record {point.IntTag}");
                Assert.IsTrue(Math.Abs(matching.DoubleField - point.DoubleField) < .0000001, $"Mismatching double field on record {point.IntTag}");
                Assert.IsTrue(matching.IntField == point.IntField, $"Mismatching int field on record {point.IntTag}");
                Assert.IsTrue(matching.BoolField == point.BoolField, $"Mismatching bool field on record {point.IntTag}");
            }
        }
        public async Task TestPerformance()
        {
            try
            {
                var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);

                var points = new List <IInfluxDatapoint>();

                var today       = DateTime.Now.ToShortDateString();
                var now         = DateTime.Now.ToShortTimeString();
                var start       = DateTime.Now.AddDays(-5);
                var end         = DateTime.Now;
                var measurement = "Perftest";

                for (int i = 0; i < 1000000; i++)
                {
                    var valMixed = new InfluxDatapoint <InfluxValueField>();
                    valMixed.Tags.Add("TestDate", today);
                    valMixed.Tags.Add("TestTime", now);
                    valMixed.UtcTimestamp = DateTime.UtcNow;
                    valMixed.Fields.Add("Open", new InfluxValueField(DataGen.RandomDouble()));
                    valMixed.Fields.Add("High", new InfluxValueField(DataGen.RandomDouble()));
                    valMixed.Fields.Add("Low", new InfluxValueField(DataGen.RandomDouble()));
                    valMixed.Fields.Add("Close", new InfluxValueField(DataGen.RandomDouble()));
                    valMixed.Fields.Add("Volume", new InfluxValueField(DataGen.RandomDouble()));

                    valMixed.MeasurementName = measurement;
                    valMixed.Precision       = TimePrecision.Nanoseconds;
                    points.Add(valMixed);
                }
                Stopwatch s = new Stopwatch();
                s.Start();

                var r = await client.PostPointsAsync(dbName, points, 10000);

                s.Stop();
                Debug.WriteLine($"Elapsed{s.ElapsedMilliseconds}");
                Assert.IsTrue(points.TrueForAll(p => p.Saved == true), "PostPointsAsync did not save all points");
                Assert.IsTrue(s.Elapsed.TotalSeconds < 120, "PostPointsAsync took more than 120 sec");
            }
            catch (Exception e)
            {
                Assert.Fail($"Unexpected exception of type {e.GetType()} caught: {e.Message}");
                return;
            }
        }
Example #17
0
 private DateTime _lastPrintErrTime = DateTime.Now.AddHours(-1);//记录一下上次出错后打印日志的时间
 /// <summary>
 /// 写入数据集合
 /// </summary>
 /// <param name="points"></param>
 /// <returns></returns>
 public async Task WriteAsync(List <InfluxDatapoint <InfluxValueField> > points)
 {
     try
     {
         //TODO: 添加错误log,通常错误是由于字段类型改变引起的
         await influxClient.PostPointsAsync(_influxDbName, points);
     }
     catch (Exception ex)
     {
         if (_lastPrintErrTime.AddMinutes(1) < DateTime.Now)
         {
             //1分钟之内重复收到消息只打1次日志
             _lastPrintErrTime = DateTime.Now;
             _logger.LogError("写入InfluxDB出错,原因:" + ex.Message + "\r\n" + ex.StackTrace);
         }
     }
 }
        public async Task TestPostPointsAsync_DefaultTimePrecision()
        {
            try
            {
                var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);
                var points = await CreateTestPoints("DefaultPrecisionTest", 10);

                var r = await client.PostPointsAsync(dbName, points);

                Assert.IsTrue(r, "PostPointsAsync retunred false");
            }
            catch (Exception e)
            {
                Assert.Fail($"Unexpected exception of type {e.GetType()} caught: {e.Message}");
                return;
            }
        }
        public async Task TestQueryMultiSeriesAsync_Chunking_BySeries()
        {
            try
            {
                var client    = new InfluxDBClient(influxUrl, dbUName, dbpwd);
                var time      = DateTime.Now;
                var TestDate  = time.ToShortDateString();
                var TestTime  = time.ToShortTimeString();
                var chunkSize = 10;
                var points    = new List <IInfluxDatapoint>();

                for (var i = 0; i < chunkSize * 10; i++)
                {
                    await Task.Delay(1);

                    var point = new InfluxDatapoint <long>();
                    point.UtcTimestamp    = DateTime.UtcNow;
                    point.MeasurementName = "ChunkTest";
                    point.Precision       = TimePrecision.Nanoseconds;
                    point.Tags.Add("ChunkSeries", point.UtcTimestamp.Ticks % 2 == 0 ? "Chunk0" : "Chunk1");
                    point.Tags.Add("TestDate", TestDate);
                    point.Tags.Add("TestTime", TestTime);
                    point.Fields.Add("Val", i);
                    points.Add(point);
                }

                var r = await client.PostPointsAsync(dbName, points);

                Assert.IsTrue(r, "PostPointsAsync retunred false");

                var values = await client.QueryMultiSeriesAsync(dbName, $"select sum(Val) from ChunkTest where TestTime ='{ TestTime}' group by ChunkSeries", chunkSize * 10);

                foreach (var val in values)
                {
                    var x = val?.Entries?.Count;
                    //the series should be smaller than the chunk size, each resultset will only one series
                    Assert.IsTrue(x == 1);
                }
            }
            catch (Exception e)
            {
                Assert.Fail($"Unexpected exception of type {e.GetType()} caught: {e.Message}");
                return;
            }
        }
Example #20
0
        //const string invalidInfluxUrl = "http://xyzerty:8089";
        public static void Test()
        {
            try
            {
                var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);

                bool isSuc = client.CreateDatabaseAsync(dbName).GetAwaiter().GetResult();

                long      timeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                int       totalNum  = 10;
                Stopwatch sw        = new Stopwatch();
                sw.Start();

                Random rdm    = new Random();
                var    points = new List <IInfluxDatapoint>();
                for (int i = 0; i < totalNum; i++)
                {
                    var firstPoint = new InfluxDatapoint <double>();
                    firstPoint.Timestamp = Convert.ToInt64(timeStamp.ToString() + i.ToString().PadLeft(6, '0'));
                    firstPoint.Tags.Add("exchangecode", "okex");
                    firstPoint.Tags.Add("pair1", "btc");
                    firstPoint.Tags.Add("pair2", "usdt");

                    firstPoint.Fields.Add("o", DataGen.RandomDouble());
                    firstPoint.Fields.Add("h", DataGen.RandomDouble());
                    firstPoint.Fields.Add("l", DataGen.RandomDouble());
                    firstPoint.Fields.Add("c", DataGen.RandomDouble());
                    firstPoint.MeasurementName = measurementName;
                    firstPoint.Precision       = TimePrecision.Nanoseconds;
                    points.Add(firstPoint);
                }


                client.PostPointsAsync(dbName, points).GetAwaiter().GetResult();

                Console.WriteLine(string.Format("批量插入{0}条数据耗时:{1}毫秒", totalNum, sw.ElapsedMilliseconds));

                sw.Stop();
            }
            catch (Exception ex)
            {
                Console.WriteLine("error " + ex.Message);
            }
        }
        public async Task TestPostPointsAsync_AutogenRetention()
        {
            try
            {
                var client    = new InfluxDBClient(influxUrl, dbUName, dbpwd);
                var time      = DateTime.Now;
                var TestDate  = time.ToShortDateString();
                var TestTime  = time.ToShortTimeString();
                var points    = new List <IInfluxDatapoint>();
                var retention = new InfluxRetentionPolicy()
                {
                    Name = "autogen", DBName = dbName, Duration = TimeSpan.FromMinutes(0), IsDefault = true
                };

                for (var i = 0; i < 10; i++)
                {
                    await Task.Delay(1);

                    var point = new InfluxDatapoint <long>();
                    point.Retention       = retention;
                    point.UtcTimestamp    = DateTime.UtcNow.AddDays(-i);
                    point.MeasurementName = "RetentionTest";
                    point.Precision       = TimePrecision.Nanoseconds;
                    point.Tags.Add("TestDate", TestDate);
                    point.Tags.Add("TestTime", TestTime);
                    point.Fields.Add("Val", i);
                    points.Add(point);
                }

                var r = await client.PostPointsAsync(dbName, points);

                Assert.IsTrue(r, "PostPointsAsync retunred false");

                Assert.IsTrue(points.Count(p => p.Saved) == 10, "PostPointsAsync failed with autogen default retention policy");
            }

            catch (Exception e)
            {
                Assert.Fail($"Unexpected exception of type {e.GetType()} caught: {e.Message}");
                return;
            }
        }
        public async Task TestPostPointsAsync_Batch()
        {
            try
            {
                var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);

                var points = new List <IInfluxDatapoint>();

                var today = DateTime.Now.ToShortDateString();
                var now   = DateTime.Now.ToShortTimeString();
                var start = DateTime.Now.AddDays(-5);
                var end   = DateTime.Now;

                for (int i = 0; i < 5000; i++)
                {
                    var valMixed = new InfluxDatapoint <InfluxValueField>();
                    valMixed.UtcTimestamp = DataGen.RandomDate(start, end);
                    valMixed.Tags.Add("TestDate", today);
                    valMixed.Tags.Add("TestTime", now);
                    valMixed.UtcTimestamp = DateTime.UtcNow;
                    valMixed.Fields.Add("Doublefield", new InfluxValueField(DataGen.RandomDouble()));
                    valMixed.Fields.Add("Stringfield", new InfluxValueField(DataGen.RandomString()));
                    valMixed.Fields.Add("Boolfield", new InfluxValueField(DateTime.Now.Ticks % 2 == 0));
                    valMixed.Fields.Add("Int Field", new InfluxValueField(DataGen.RandomInt()));
                    valMixed.MeasurementName = measurementName;
                    valMixed.Precision       = (TimePrecision)(DataGen.RandomInt() % 6) + 1;
                    points.Add(valMixed);
                }

                var r = await client.PostPointsAsync(dbName, points);

                Assert.IsTrue(points.TrueForAll(p => p.Saved == true), "PostPointsAsync did not save all points");
            }
            catch (Exception e)
            {
                Assert.Fail($"Unexpected exception of type {e.GetType()} caught: {e.Message}");
                return;
            }
        }
        public async Task TestDeletePointsAsync()
        {
            var measurement = "hara-kiri-half";
            var client      = new InfluxDBClient(influxUrl, dbUName, dbpwd);
            await client.CreateDatabaseAsync(dbName);

            var points = await CreateTestPoints(measurement, 10);

            points.Skip(5).Select(p => { p.Tags.Add("purge", "'yes'"); return(true); });
            var r = await client.PostPointsAsync(dbName, points);

            r = await client.DeletePointsAsync(
                new InfluxDatabase(dbName),
                new InfluxMeasurement(measurement),
                whereClause : new List <string>()
            {
                "purge = yes",
                $"time() > {DateTime.UtcNow.AddDays(-4).ToEpoch(TimePrecision.Hours)}"
            });

            Assert.IsTrue(r, "DropMeasurementAsync retunred false");
        }
        public async Task TestPostPointsAsync_OlderthanRetention()
        {
            try
            {
                var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);
                InfluxRetentionPolicy retention = new InfluxRetentionPolicy()
                {
                    Duration = TimeSpan.FromHours(1)
                };
                var points = await CreateTestPoints("RetentionTest", 10, TimePrecision.Nanoseconds, retention);

                var r = await client.PostPointsAsync(dbName, points);

                Assert.IsTrue(r, "PostPointsAsync retunred false");

                Assert.IsTrue(points.Count(p => p.Saved) == 1, "PostPointsAsync saved points older than retention policy");
            }
            catch (Exception e)
            {
                Assert.Fail($"Unexpected exception of type {e.GetType()} caught: {e.Message}");
                return;
            }
        }
        public async Task TestPostPointsAsync_AutogenRetention()
        {
            try
            {
                var client    = new InfluxDBClient(influxUrl, dbUName, dbpwd);
                var retention = new InfluxRetentionPolicy()
                {
                    Name = "autogen", DBName = dbName, Duration = TimeSpan.FromMinutes(0), IsDefault = true
                };
                var points = await CreateTestPoints("RetentionTest", 10, TimePrecision.Nanoseconds, retention);

                var r = await client.PostPointsAsync(dbName, points);

                Assert.IsTrue(r, "PostPointsAsync retunred false");

                Assert.IsTrue(points.Count(p => p.Saved) == 10, "PostPointsAsync failed with autogen default retention policy");
            }

            catch (Exception e)
            {
                Assert.Fail($"Unexpected exception of type {e.GetType()} caught: {e.Message}");
                return;
            }
        }
Example #26
0
        public async Task <ProcessStatus> ProcessPerfMonLog(string InputFileName, InfluxDBClient client)
        {
            ProcessStatus result         = new ProcessStatus();
            int           linesProcessed = 0;
            int           failedLines    = 0;

            int failedReqCount = 0;

            try
            {
                var failureReasons = new Dictionary <Type, FailureTracker>();

                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                var firstLine = File.ReadLines(InputFileName).FirstOrDefault();

                var firstCol = firstLine.Substring(0, firstLine.IndexOf(','));
                if (!firstCol.Contains("PDH-CSV"))
                {
                    throw new InvalidDataException("Input file is not a Standard Perfmon csv file");
                }
                var x = Regex.Matches(firstCol, "([-0-9]+)");
                if (x.Count > 0)
                {
                    minOffset = int.Parse(x[3].ToString());
                }

                //get the column headers
                List <PerfmonCounter> pecrfCounters;
                try
                {
                    pecrfCounters = ParsePerfMonFileHeader(firstLine);
                }
                catch (Exception ex)
                {
                    throw new InvalidDataException("Unable to parse file headers", ex);
                }

                IInfluxDatabase dbStructure;
                IEnumerable <IGrouping <string, PerfmonCounter> > perfGroup;
                if (settings.PerfmonFile.Filter != Filters.None)
                {
                    var filterColumns = ParsePerfMonFileHeader(settings.PerfmonFile.ColumnsFilter.ToString(), false);
                    dbStructure = await client.GetInfluxDBStructureAsync(settings.InfluxDB.DatabaseName);

                    perfGroup = FilterPerfmonLogColumns(pecrfCounters, filterColumns, dbStructure as InfluxDatabase).GroupBy(p => p.PerformanceObject);
                }
                else
                {
                    perfGroup = pecrfCounters.GroupBy(p => p.PerformanceObject);
                }

                List <IInfluxDatapoint> points = null, retryQueue = new List <IInfluxDatapoint>();

                if (settings.InfluxDB.RetentionDuration != 0 || !String.IsNullOrWhiteSpace(settings.InfluxDB.RetentionPolicy))
                {
                    var policies = await client.GetRetentionPoliciesAsync(settings.InfluxDB.DatabaseName);

                    //if duraiton is specified that takes precidence
                    if (settings.InfluxDB.RetentionDuration != 0)
                    {
                        policy = policies.FirstOrDefault(p => p.Duration.TotalMinutes == settings.InfluxDB.RetentionDuration);

                        if (policy == null)
                        {
                            policy = new InfluxRetentionPolicy()
                            {
                                Name      = String.IsNullOrWhiteSpace(settings.InfluxDB.RetentionPolicy) ? $"InfluxerRetention_{settings.InfluxDB.RetentionDuration}min" : settings.InfluxDB.RetentionPolicy,
                                DBName    = settings.InfluxDB.DatabaseName,
                                Duration  = TimeSpan.FromMinutes(settings.InfluxDB.RetentionDuration),
                                IsDefault = false,
                                ReplicaN  = 1
                            };
                            if (!await client.CreateRetentionPolicyAsync(policy))
                            {
                                throw new InvalidOperationException("Unable to create retention policy");
                            }
                        }
                    }
                    else if (!String.IsNullOrWhiteSpace(settings.InfluxDB.RetentionPolicy))
                    {
                        policy = policies.FirstOrDefault(p => p.Name == settings.InfluxDB.RetentionPolicy);
                        if (policy == null)
                        {
                            throw new ArgumentException("No Retention policy with Name {0} was found, and duration is not specified to create a new one!!", settings.InfluxDB.RetentionPolicy);
                        }
                    }
                }
                //Parallel.ForEach (File.ReadLines (inputFileName).Skip (1), (string line) =>
                foreach (var line in File.ReadLines(InputFileName).Skip(1))
                {
                    try
                    {
                        var linePoints = ProcessPerfmonLogLine(line, perfGroup);
                        linesProcessed++;

                        if (linePoints == null || linePoints.Count == 0)
                        {
                            failedLines++;
                        }
                        else
                        {
                            result.PointsFound += linePoints.Count;

                            if (points == null)
                            {
                                points = linePoints;
                            }
                            else
                            {
                                points.AddRange(linePoints);
                            }

                            if (points.Count >= settings.InfluxDB.PointsInSingleBatch)
                            {
                                bool postresult = false;
                                try
                                {
                                    postresult = await client.PostPointsAsync(settings.InfluxDB.DatabaseName, points);
                                }
                                catch (ServiceUnavailableException)
                                {
                                    postresult = false;
                                }
                                if (postresult)
                                {
                                    failedReqCount          = 0;
                                    result.PointsProcessed += points.Count(p => p.Saved);
                                }
                                else
                                {
                                    //add failed to retry queue
                                    retryQueue.AddRange(points.Where(p => p.Saved != true));
                                    result.PointsProcessed += points.Count(p => p.Saved);
                                    //avoid failing on too many points
                                    if (++failedReqCount > 4)
                                    {
                                        break;
                                    }
                                }
                                //a point will be either posted to Influx or in retry queue
                                points = null;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        failedLines++;
                        var type = e.GetType();
                        if (!failureReasons.ContainsKey(type))
                        {
                            failureReasons.Add(type, new FailureTracker()
                            {
                                ExceptionType = type, Message = e.Message
                            });
                        }
                        failureReasons[type].LineNumbers.Add(linesProcessed);
                    }

                    if (failedLines > 0 || retryQueue.Count > 0)
                    {
                        Logger.Log(LogLevel.Verbose, "\r{0} Processed:- {1} lines, {2} points, Failed:- {3} lines, {4} points     ", stopwatch.Elapsed.ToString(@"hh\:mm\:ss"), linesProcessed, result.PointsFound, failedLines, retryQueue.Count);
                    }
                    else
                    {
                        Logger.Log(LogLevel.Verbose, "\r{0} Processed:- {1} lines, {2} points", stopwatch.Elapsed.ToString(@"hh\:mm\:ss"), linesProcessed, result.PointsFound);
                    }
                }

                //if we reached here due to repeated failures
                if (retryQueue.Count >= settings.InfluxDB.PointsInSingleBatch * 3 || failedReqCount > 3)
                {
                    throw new InvalidOperationException("InfluxDB is not able to accept points!! Please check InfluxDB logs for error details!");
                }

                //finally few points may be left out which were not processed (say 10 points left, but we check for 100 points in a batch)
                if (points != null && points.Count > 0)
                {
                    if (await client.PostPointsAsync(settings.InfluxDB.DatabaseName, points))
                    {
                        result.PointsProcessed += points.Count;
                        points.Clear();
                    }
                    else
                    {
                        failedReqCount++;
                        //add failed to retry queue
                        retryQueue.AddRange(points.Where(p => p.Saved != true));
                        result.PointsProcessed += points.Count(p => p.Saved);
                    }
                }

                if (retryQueue.Count > 0)
                {
                    Logger.LogLine(LogLevel.Info, "\n {0} Retrying {1} failed points", stopwatch.Elapsed.ToString(@"hh\:mm\:ss"), retryQueue.Count);
                    if (await client.PostPointsAsync(settings.InfluxDB.DatabaseName, retryQueue))
                    {
                        result.PointsProcessed += retryQueue.Count;
                        retryQueue.Clear();
                    }
                    else if (retryQueue.Count >= settings.InfluxDB.PointsInSingleBatch * 3 || ++failedReqCount > 4)
                    {
                        throw new InvalidOperationException("InfluxDB is not able to accept points!! Please check InfluxDB logs for error details!");
                    }
                    else
                    {
                        result.PointsFailed += retryQueue.Count;
                    }
                }

                pecrfCounters.Clear();
                stopwatch.Stop();
                if (failedLines > 0 || retryQueue.Count > 0)
                {
                    Logger.Log(LogLevel.Verbose, "\r{0} Done!! Processed:- {1} lines, {2} points, Failed:- {3} lines, {4} points", stopwatch.Elapsed.ToString(@"hh\:mm\:ss"), linesProcessed, result.PointsFound, failedLines, retryQueue.Count);
                    Logger.LogLine(LogLevel.Error, "Process Started {0}, Input {1}, Processed:- {2} lines, {3} points, Failed:- {4} lines, {5} points", (DateTime.Now - stopwatch.Elapsed), InputFileName, linesProcessed, result.PointsFound, failedLines, retryQueue.Count);
                    foreach (var f in failureReasons.Values)
                    {
                        Logger.LogLine(LogLevel.Error, "{0} lines ({1}) failed due to {2} ({3})", f.Count, String.Join(",", f.LineNumbers), f.ExceptionType, f.Message);
                    }
                    if (failedLines == linesProcessed || result.PointsFound == retryQueue.Count)
                    {
                        result.ExitCode = ExitCode.UnableToProcess;
                    }
                    else
                    {
                        result.ExitCode = ExitCode.ProcessedWithErrors;
                    }
                }
                else
                {
                    result.ExitCode = ExitCode.Success;
                    Logger.LogLine(LogLevel.Info, "\n Done!! Processed:- {0} lines, {1} points", linesProcessed, result.PointsFound);
                }
            }
            catch (Exception e)
            {
                Logger.LogLine(LogLevel.Error, "Failed to process {0}", InputFileName);
                Logger.LogLine(LogLevel.Error, "\r\nError!! {0}:{1} - {2}", e.GetType().Name, e.Message, e.StackTrace);
                result.ExitCode = ExitCode.UnknownError;
            }

            return(result);
        }
Example #27
0
        public async Task <ProcessStatus> ProcessGenericFile(string InputFileName, InfluxDBClient client)
        {
            ProcessStatus result = new ProcessStatus();

            int failedReqCount = 0;

            try
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                var r = GetFileLayout(InputFileName);
                if (r.ExitCode != ExitCode.Success)
                {
                    return(r);
                }

                IInfluxDatabase dbStructure;
                if (settings.GenericFile.Filter != Filters.None)
                {
                    var filterColumns = new List <GenericColumn>();
                    if (settings.GenericFile.Filter == Filters.Columns)
                    {
                        if (settings.GenericFile.ColumnLayout != null && settings.GenericFile.ColumnLayout.Count > 0)
                        {
                            Logger.LogLine(LogLevel.Info, "Column Filtering is not applicable when columns are defined in Config file. Use the Skip attribute on each column to filter them");
                        }
                        else
                        {
                            filterColumns = ParseGenericColumns(settings.GenericFile.ColumnsFilter.ToString());
                        }
                    }

                    dbStructure = await client.GetInfluxDBStructureAsync(settings.InfluxDB.DatabaseName);

                    ColumnHeaders = FilterGenericColumns(ColumnHeaders, filterColumns, dbStructure as InfluxDatabase);
                }

                var validity = ValidateData(InputFileName);

                var failureReasons = new Dictionary <Type, FailureTracker>();

                List <IInfluxDatapoint> points = new List <IInfluxDatapoint>(), retryQueue = new List <IInfluxDatapoint>();
                IInfluxRetentionPolicy  policy = null;

                if (settings.InfluxDB.RetentionDuration != 0 || !String.IsNullOrWhiteSpace(settings.InfluxDB.RetentionPolicy))
                {
                    var policies = await client.GetRetentionPoliciesAsync(settings.InfluxDB.DatabaseName);

                    //if duraiton is specified that takes precidence
                    if (settings.InfluxDB.RetentionDuration != 0)
                    {
                        policy = policies.FirstOrDefault(p => p.Duration.TotalMinutes == settings.InfluxDB.RetentionDuration);

                        if (policy == null)
                        {
                            policy = new InfluxRetentionPolicy()
                            {
                                Name      = String.IsNullOrWhiteSpace(settings.InfluxDB.RetentionPolicy) ? $"InfluxerRetention_{settings.InfluxDB.RetentionDuration}min" : settings.InfluxDB.RetentionPolicy,
                                DBName    = settings.InfluxDB.DatabaseName,
                                Duration  = TimeSpan.FromMinutes(settings.InfluxDB.RetentionDuration),
                                IsDefault = false,
                                ReplicaN  = 1
                            };
                            if (!await client.CreateRetentionPolicyAsync(policy))
                            {
                                throw new InvalidOperationException("Unable to create retention policy");
                            }
                        }
                    }
                    else if (!String.IsNullOrWhiteSpace(settings.InfluxDB.RetentionPolicy))
                    {
                        policy = policies.FirstOrDefault(p => p.Name == settings.InfluxDB.RetentionPolicy);
                        if (policy == null)
                        {
                            throw new ArgumentException("No Retention policy with Name {0} was found, and duration is not specified to create a new one!!", settings.InfluxDB.RetentionPolicy);
                        }
                    }
                }

                foreach (var line in File.ReadLines(InputFileName).Skip(settings.GenericFile.HeaderRow + settings.GenericFile.SkipRows))
                {
                    if (String.IsNullOrWhiteSpace(line) || (!String.IsNullOrEmpty(settings.GenericFile.CommentMarker) && line.StartsWith(settings.GenericFile.CommentMarker)))
                    {
                        continue;
                    }
                    try
                    {
                        result.PointsFound++;
                        var point = ProcessGenericLine(line, ColumnHeaders);
                        if (point == null)
                        {
                            result.PointsFailed++;
                        }
                        else
                        {
                            point.Retention = policy;
                            points.Add(point);
                        }

                        if (points.Count >= settings.InfluxDB.PointsInSingleBatch)
                        {
                            bool postresult = false;
                            try
                            {
                                postresult = await client.PostPointsAsync(settings.InfluxDB.DatabaseName, points);
                            }
                            catch (ServiceUnavailableException)
                            {
                                postresult = false;
                            }

                            if (postresult)
                            {
                                failedReqCount          = 0;
                                result.PointsProcessed += points.Count(p => p.Saved);
                            }
                            else
                            {
                                //add failed to retry queue
                                retryQueue.AddRange(points.Where(p => p.Saved != true));
                                result.PointsProcessed += points.Count(p => p.Saved);
                                //avoid failing on too many points
                                if (++failedReqCount > 3)
                                {
                                    break;
                                }
                            }
                            //a point will be either posted to Influx or in retry queue
                            points.Clear();
                        }
                    }
                    catch (Exception e)
                    {
                        result.PointsFailed++;
                        var type = e.GetType();
                        if (e is InfluxDBException)
                        {
                            if ((e as InfluxDBException).Reason == "Partial Write")
                            {
                                retryQueue.AddRange(points.Where(p => p.Saved != true));
                                result.PointsProcessed += points.Count(p => p.Saved);
                                points.Clear();
                            }
                        }
                        if (!failureReasons.ContainsKey(type))
                        {
                            failureReasons.Add(type, new FailureTracker()
                            {
                                ExceptionType = type, Message = e.Message
                            });
                        }
                        failureReasons[type].LineNumbers.Add(result.PointsFound + settings.GenericFile.HeaderRow + settings.GenericFile.SkipRows + 1);

                        //avoid too many failures, may be config is wrong
                        if (!settings.GenericFile.IgnoreErrors && result.PointsFailed > settings.InfluxDB.PointsInSingleBatch * 3)
                        {
                            Logger.LogLine(LogLevel.Info, "\n Too many failed points, refer to error info. Aborting!!");
                            Logger.LogLine(LogLevel.Error, "\n Too many failed points, refer to error info. Aborting!!");
                            break;
                        }
                    }

                    if (result.PointsFailed > 0 || retryQueue.Count > 0)
                    {
                        Logger.Log(LogLevel.Verbose, "\r{0} Processed {1}, Failed {2}, Queued {3}                        ", stopwatch.Elapsed.ToString(@"hh\:mm\:ss"), result.PointsFound, result.PointsFailed, retryQueue.Count);
                    }
                    else
                    {
                        Logger.Log(LogLevel.Verbose, "\r{0} Processed {1}                          ", stopwatch.Elapsed.ToString(@"hh\:mm\:ss"), result.PointsFound);
                    }
                }

                //if we reached here due to repeated failures
                if (retryQueue.Count >= settings.InfluxDB.PointsInSingleBatch * 3 || failedReqCount > 3)
                {
                    throw new InvalidOperationException("InfluxDB is not able to accept points!! Please check InfluxDB logs for error details!");
                }

                //finally few points may be left out which were not processed (say 10 points left, but we check for 100 points in a batch)
                if (points != null && points.Count > 0)
                {
                    if (await client.PostPointsAsync(settings.InfluxDB.DatabaseName, points))
                    {
                        result.PointsProcessed += points.Count;
                        points.Clear();
                    }
                    else
                    {
                        failedReqCount++;
                        //add failed to retry queue
                        retryQueue.AddRange(points.Where(p => p.Saved != true));
                        result.PointsProcessed += points.Count(p => p.Saved);
                    }
                }

                //retry all previously failed points
                if (retryQueue.Count > 0)
                {
                    Logger.LogLine(LogLevel.Verbose, "\n {0} Retrying {1} failed points", stopwatch.Elapsed.ToString(@"hh\:mm\:ss"), retryQueue.Count);
                    try
                    {
                        if (await client.PostPointsAsync(settings.InfluxDB.DatabaseName, retryQueue))
                        {
                            result.PointsProcessed += retryQueue.Count;
                            retryQueue.Clear();
                        }
                        else
                        {
                            result.PointsFailed += retryQueue.Count;
                            if (retryQueue.Count >= settings.InfluxDB.PointsInSingleBatch * 3 || ++failedReqCount > 4)
                            {
                                throw new InvalidOperationException("InfluxDB is not able to accept points!! Please check InfluxDB logs for error details!");
                            }
                        }
                    }
                    catch (InfluxDBException e)
                    {
                        if (e.Reason == "Partial Write")
                        {
                        }
                    }
                }

                stopwatch.Stop();
                if (result.PointsFailed > 0)
                {
                    Logger.LogLine(LogLevel.Error, "Process Started {0}, Input {1}, Processed{2}, Failed:{3}", (DateTime.Now - stopwatch.Elapsed), InputFileName, result.PointsFound, result.PointsFailed);
                    foreach (var f in failureReasons.Values)
                    {
                        Logger.LogLine(LogLevel.Error, "{0} lines (e.g. {1}) failed due to {2} ({3})", f.Count, String.Join(",", f.LineNumbers.Take(5)), f.ExceptionType, f.Message);
                    }
                    if (result.PointsFailed == result.PointsFound)
                    {
                        result.ExitCode = ExitCode.UnableToProcess;
                    }
                    else
                    {
                        result.ExitCode = ExitCode.ProcessedWithErrors;
                    }
                }
                else
                {
                    result.ExitCode = ExitCode.Success;
                    Logger.LogLine(LogLevel.Info, "\n Done!! Processed:- {0} points", result.PointsFound);
                }
            }
            catch (Exception e)
            {
                Logger.LogLine(LogLevel.Error, "Failed to process {0}", InputFileName);
                Logger.LogLine(LogLevel.Error, "\r\nError!! {0}:{1} - {2}", e.GetType().Name, e.Message, e.StackTrace);
                result.ExitCode = ExitCode.UnableToProcess;
            }
            return(result);
        }
Example #28
0
        /// <summary>
        /// Executes the sender reading data from valuesToBeSent.
        /// </summary>
        public async Task Send()
        {
            InfluxDBClient client = new InfluxDBClient(url);
            var            vals   = new List <InfluxDatapoint <InfluxValueField> >();

            foreach (var item in valuesToBeSent)
            {
                var valMixed = new InfluxDatapoint <InfluxValueField>();
                valMixed.UtcTimestamp = DateTime.UtcNow;
                valMixed.Tags.Add("HOSTNAME", Environment.MachineName);
                valMixed.MeasurementName = MeasurementName;
                foreach (var fld in Fields)
                {
                    try
                    {
                        var val = (IComparable)item.Values[fld];
                        valMixed.Fields.Add(fld, new InfluxValueField(val));
                    }
                    catch (InvalidCastException ex)
                    {
                        //do nothing - field is not icomparable
                    }
                }
                foreach (var tag in Tags)
                {
                    var val = item.Values[tag];
                    valMixed.Tags.Add(tag, val?.ToString());
                }
                vals.Add(valMixed);
            }



            try
            {
                //TODO: log values to be sent
                if (vals.Count > 0)
                {
                    var r = await client.PostPointsAsync(db, vals);

                    await Task.Delay(1000);

                    if (!r)
                    {
                        string message = "error influx post points";
                        //@class.Log(LogLevel.Error, 0, $"{message}", null, null);
                        message += "";
                    }
                }
                else
                {
                    string message = "no values to be sent";
                    //@class.Log(LogLevel.Information, 0, $"sender influx {message}", null, null);
                    message += "";
                }
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                //@class.Log(LogLevel.Error, 0, $"sender influx error {message}", ex, null);
                message += "";
                throw;
            }
        }
        public async Task TestBachingPostPointsAsync()
        {
            try
            {
                var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);

                var rand   = new Random();
                var points = new List <IInfluxDatapoint> ();

                var today = DateTime.Now.ToShortDateString();
                var now   = DateTime.Now.ToShortTimeString();

                for (int i = 0; i < 500; i++)
                {
                    var utcTime   = DateTime.UtcNow;
                    var valDouble = new InfluxDatapoint <double> ();
                    valDouble.UtcTimestamp = utcTime;
                    valDouble.Tags.Add("TestDate", today);
                    valDouble.Tags.Add("TestTime", now);
                    valDouble.Fields.Add("Doublefield", rand.NextDouble());
                    valDouble.Fields.Add("Doublefield2", rand.NextDouble());
                    valDouble.MeasurementName = measurementName;
                    valDouble.Precision       = (TimePrecision)(rand.Next() % 6) + 1;

                    var valInt = new InfluxDatapoint <int> ();
                    valInt.UtcTimestamp = utcTime;
                    valInt.Tags.Add("TestDate", today);
                    valInt.Tags.Add("TestTime", now);
                    valInt.Fields.Add("Intfield", rand.Next());
                    valInt.Fields.Add("Intfield2", rand.Next());
                    valInt.MeasurementName = measurementName;
                    valInt.Precision       = (TimePrecision)(rand.Next() % 6) + 1;

                    var valBool = new InfluxDatapoint <bool> ();
                    valBool.UtcTimestamp = utcTime;
                    valBool.Tags.Add("TestDate", today);
                    valBool.Tags.Add("TestTime", now);
                    valBool.Fields.Add("Booleanfield", DateTime.Now.Ticks % 2 == 0);
                    valBool.MeasurementName = measurementName;
                    valBool.Precision       = (TimePrecision)(rand.Next() % 6) + 1;

                    var valString = new InfluxDatapoint <string> ();
                    valString.UtcTimestamp = utcTime;
                    valString.Tags.Add("TestDate", today);
                    valString.Tags.Add("TestTime", now);
                    valString.Fields.Add("Stringfield", GenerateRandomString());
                    valString.MeasurementName = measurementName;
                    valString.Precision       = (TimePrecision)(rand.Next() % 6) + 1;


                    points.Add(valString);
                    points.Add(valInt);
                    points.Add(valDouble);
                    points.Add(valBool);
                }

                var r = await client.PostPointsAsync(dbName, points);

                Assert.IsTrue(points.TrueForAll(p => p.Saved == true), "PostPointsAsync did not save all points");
            }
            catch (Exception e)
            {
                Assert.Fail("Unexpected exception of type {0} caught: {1}",
                            e.GetType(), e.Message);
                return;
            }
        }
Example #30
0
 public void Write(InfluxDbEntry[] entries, string database, string retentionPoliciy)
 => _client.PostPointsAsync(database, ConvertEntries(entries, retentionPoliciy)).Wait();