Ejemplo n.º 1
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));
            }
        }
        private static async Task <List <IInfluxDatapoint> > CreateTestPoints(string MeasurementName, int size, TimePrecision?precision = null, InfluxRetentionPolicy retention = null)
        {
            var time     = DateTime.Now;
            var TestDate = time.ToShortDateString();
            var TestTime = time.ToShortTimeString();
            var points   = new List <IInfluxDatapoint>();

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

                var point = new InfluxDatapoint <long>();
                if (retention != null)
                {
                    point.Retention = retention;
                }
                point.UtcTimestamp    = DateTime.UtcNow.AddDays(-i);
                point.MeasurementName = MeasurementName;
                if (precision != null)
                {
                    point.Precision = precision.Value;
                }
                point.Tags.Add("TestDate", TestDate);
                point.Tags.Add("TestTime", TestTime);
                point.Fields.Add("Val", i);
                points.Add(point);
            }

            return(points);
        }
        public async Task TestPostPointAsyncNonDefaultRetention()
        {
            try
            {
                var client   = new InfluxDBClient(influxUrl, dbUName, dbpwd);
                var time     = DateTime.Now;
                var rand     = new Random();
                var valMixed = new InfluxDatapoint <InfluxValueField>();
                valMixed.UtcTimestamp = DateTime.UtcNow;
                valMixed.Tags.Add("TestDate", time.ToShortDateString());
                valMixed.Tags.Add("TestTime", time.ToShortTimeString());
                valMixed.Fields.Add("Doublefield", new InfluxValueField(rand.NextDouble()));
                valMixed.Fields.Add("Stringfield", new InfluxValueField(DataGen.RandomString()));
                valMixed.Fields.Add("Boolfield", new InfluxValueField(true));
                valMixed.Fields.Add("Int Field", new InfluxValueField(rand.Next()));

                valMixed.MeasurementName = measurementName;
                valMixed.Precision       = TimePrecision.Seconds;
                valMixed.Retention       = new InfluxRetentionPolicy()
                {
                    Duration = TimeSpan.FromHours(6)
                };

                var r = await client.PostPointAsync(dbName, valMixed);

                Assert.IsTrue(r && valMixed.Saved, "PostPointAsync retunred false");
            }
            catch (Exception e)
            {
                Assert.Fail($"Unexpected exception of type {e.GetType()} caught: {e.Message}");
                return;
            }
        }
        public async Task TestPostBooleanPointAsync()
        {
            try
            {
                var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);
                var time   = DateTime.Now;

                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.Seconds;
                var r = await client.PostPointAsync(dbName, valBool);

                Assert.IsTrue(r, "PostPointAsync retunred false");
            }
            catch (Exception e)
            {
                Assert.Fail("Unexpected exception of type {0} caught: {1}",
                            e.GetType(), e.Message);
                return;
            }
        }
Ejemplo n.º 5
0
        public async Task WriteTimeSeriesDoubleAsync(TimeSeries aSeries)
        {
            var client = GetConnection();

            string tagName         = aSeries.TagName;
            string retentionPolicy = aSeries.RetensionPolicy;
            string databaseName    = aSeries.DatabaseName;

            foreach (TimeSeriesPoint <double> pt in aSeries.TimeSeriesPoints)
            {
                var currentPoint = new InfluxDatapoint <InfluxValueField>();
                var quality      = pt.Quality;
                var value        = pt.Value;
                var timeSeries   = pt.TimeStamp_UTC;

                currentPoint.MeasurementName = tagName;
                currentPoint.UtcTimestamp    = timeSeries.ToUniversalTime();
                currentPoint.Precision       = TimePrecision.Seconds;

                currentPoint.Fields.Add("Values", new InfluxValueField(value));

                currentPoint.Tags.Add("Qualuty", quality);
                currentPoint.Retention = new InfluxRetentionPolicy()
                {
                    Name = retentionPolicy
                };

                using (client)
                {
                    var write = await client.PostPointAsync(databaseName, currentPoint);
                }
            }
        }
        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 TestPostDoublePointAsync()
        {
            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.Seconds;
                var r = await client.PostPointAsync(dbName, valDouble);

                Assert.IsTrue(r, "PostPointAsync retunred false");
            }
            catch (Exception e)
            {
                Assert.Fail("Unexpected exception of type {0} caught: {1}",
                            e.GetType(), 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;
            }
        }
Ejemplo n.º 9
0
        public async Task <bool> Record(RecordData data)
        {
            if (peristenceDataMap == null)
            {
                throw new HspiException("Collection not started");
            }
            if (peristenceDataMap.TryGetValue(data.DeviceRefId, out var peristenceData))
            {
                foreach (var value in peristenceData)
                {
                    var influxDatapoint = new InfluxDatapoint <InfluxValueField>()
                    {
                        MeasurementName = value.Measurement,
                        Precision       = TimePrecision.Seconds,
                        UtcTimestamp    = data.TimeStamp.ToUniversalTime(),
                    };

                    if (!string.IsNullOrWhiteSpace(value.Field))
                    {
                        double deviceValue = data.DeviceValue;

                        if (IsValidRange(value, deviceValue))
                        {
                            influxDatapoint.Fields.Add(value.Field, new InfluxValueField(deviceValue));
                        }
                        else
                        {
                            Trace.TraceInformation(Invariant($"Not Recording Value for {data.Name} as there is no it does not have valid ranged value at {deviceValue}"));
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(value.FieldString))
                    {
                        influxDatapoint.Fields.Add(value.FieldString, new InfluxValueField(data.DeviceString));
                    }

                    if (influxDatapoint.Fields.Count == 0)
                    {
                        Trace.TraceInformation(Invariant($"Not Recording Value for {data.Name} as there is no valid value to record."));
                        continue;
                    }

                    influxDatapoint.Tags.Add(PluginConfig.DeviceNameTag, data.Name);
                    influxDatapoint.Tags.Add(PluginConfig.DeviceRefIdTag, Convert.ToString(data.DeviceRefId, CultureInfo.InvariantCulture));

                    AddIfNotEmpty(influxDatapoint.Tags, PluginConfig.DeviceLocation1Tag, data.Location1);
                    AddIfNotEmpty(influxDatapoint.Tags, PluginConfig.DeviceLocation2Tag, data.Location2);

                    foreach (var tag in value.Tags)
                    {
                        AddIfNotEmpty(influxDatapoint.Tags, tag.Key, tag.Value);
                    }

                    await queue.EnqueueAsync(influxDatapoint, tokenSource.Token).ConfigureAwait(false);
                }
            }

            return(false);
        }
        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 void TestInfluxEscape()
        {
            var strPoint = new InfluxDatapoint <string>();

            strPoint.UtcTimestamp    = DateTime.UtcNow;
            strPoint.MeasurementName = "\"measurement with quo⚡️es and emoji\"";
            strPoint.Tags.Add("tag key with sp🚀ces", "tag,value,with\"commas\",");
            strPoint.Fields.Add("field_k\\ey", "string field value, only \" need be esc🍭ped");
            strPoint.Precision = TimePrecision.Milliseconds;
            Assert.IsTrue(strPoint.ConvertToInfluxLineProtocol().StartsWith("\"measurement\\ with\\ quo⚡️es\\ and\\ emoji\",tag\\ key\\ with\\ sp🚀ces=tag\\,value\\,with\"commas\"\\, field_k\\ey=\"string field value, only \\\" need be esc🍭ped\""));
        }
        /// <summary>
        /// Creates a DTO that contains Google Realtime data to be stored in the database.
        /// </summary>
        /// <param name="measurementName">The name of the measurement that the data is stored in.</param>
        /// <param name="fields">A dictionary containing the requested Google Realtime data.</param>
        /// <returns>An InfluxDatapoint object containing the Google Realtime data.</returns>
        public static InfluxDatapoint <InfluxValueField> CreateInfluxDatapoint(string measurementName, Dictionary <string, string> fields)
        {
            InfluxDatapoint <InfluxValueField> dp = new InfluxDatapoint <InfluxValueField>();

            dp.UtcTimestamp = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day,
                                           DateTime.UtcNow.Hour, DateTime.UtcNow.Minute, 0).Subtract(new TimeSpan(0, Convert.ToInt32(fields["rt:minutesAgo"]), 0));
            dp.MeasurementName = measurementName;

            dp.Fields.Add("rt:pageviews", new InfluxValueField(Convert.ToInt32(fields["rt:pageviews"])));

            return(dp);
        }
Ejemplo n.º 14
0
        private static IInfluxDatapoint ConvertinfluxDatapoint(HourlyActualRevenue revenue)
        {
            var      valMixed = new InfluxDatapoint <InfluxValueField>();
            DateTime dt       = revenue.Createdt.AddHours(revenue.Hour);

            valMixed.UtcTimestamp = dt.AddHours(-9);
            valMixed.Tags.Add("siteId", revenue.Siteid.ToString());
            valMixed.Tags.Add("rcc", revenue.Rcc.ToString());
            valMixed.Fields.Add("revenue", new InfluxValueField(revenue.Revenue));
            valMixed.MeasurementName = "revenue_1h";
            valMixed.Precision       = TimePrecision.Hours;
            return(valMixed);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Creates a DTO that contains SmartAlarm connector data to be stored in the database.
        /// </summary>
        /// <param name="measurementName">The name of the measurement that the data is stored in.</param>
        /// <param name="name">The name of the connector.</param>
        /// <param name="hasHeartBeat">A value indicating whether the connector has heartbeat or not.</param>
        /// <param name="events">A value indicating whether the connector is sending events or not.</param>
        /// <returns>An InfluxDatapoint object containing the SmartAlarm connector data.</returns>
        public static InfluxDatapoint <InfluxValueField> CreateInfluxDatapoint(string measurementName, string name, bool hasHeartBeat, bool hasEvents)
        {
            InfluxDatapoint <InfluxValueField> dp = new InfluxDatapoint <InfluxValueField>();

            dp.UtcTimestamp    = DateTime.UtcNow;
            dp.MeasurementName = measurementName;

            dp.Tags.Add("Name", name);
            dp.Fields.Add("HeartBeat", new InfluxValueField(Convert.ToInt32(hasHeartBeat)));
            dp.Fields.Add("Events", new InfluxValueField(Convert.ToInt32(hasEvents)));

            return(dp);
        }
        /// <summary>
        /// Creates a DTO that contains SmartAlarm Analytics data to be stored in the database.
        /// </summary>
        /// <param name="measurementName">The name of the measurement that the data is stored in.</param>
        /// <param name="timestamp">The timestamp describing the aggregation of the data.</param>
        /// <param name="visits">The amount of visits for the given timestamp.</param>
        /// <param name="actions">The amount of actions for the given timestamp.</param>
        /// <param name="users">The amount of users for the given timestamp.</param>
        /// <returns>An InfluxDatapoint object containing the SmartAlarm Analytics data.</returns>
        public static InfluxDatapoint <InfluxValueField> CreateInfluxDatapoint(string measurementName, DateTime timestamp, int visits, int actions, int users)
        {
            InfluxDatapoint <InfluxValueField> dp = new InfluxDatapoint <InfluxValueField>();

            dp.UtcTimestamp    = timestamp;
            dp.MeasurementName = measurementName;

            dp.Fields.Add("Visits", new InfluxValueField(visits));
            dp.Fields.Add("Actions", new InfluxValueField(actions));
            dp.Fields.Add("Users", new InfluxValueField(users));

            return(dp);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Creates a DTO that contains Google Analytics data to be stored in the database.
        /// </summary>
        /// <param name="measurementName">The name of the measurement that the data is stored in.</param>
        /// <param name="fields">A dictionary containing the requested Google Analytics data.</param>
        /// <returns>An InfluxDatapoint object containing the Google Analytics data.</returns>
        public static InfluxDatapoint <InfluxValueField> CreateInfluxDatapoint(string measurementName, Dictionary <string, string> fields)
        {
            InfluxDatapoint <InfluxValueField> dp = new InfluxDatapoint <InfluxValueField>();

            dp.UtcTimestamp    = new DateTime(Convert.ToInt32(fields["ga:year"]), Convert.ToInt32(fields["ga:month"]), Convert.ToInt32(fields["ga:day"]), 0, 0, 0);
            dp.MeasurementName = measurementName;

            dp.Fields.Add("ga:pageviews", new InfluxValueField(Convert.ToInt32(fields["ga:pageviews"])));
            dp.Fields.Add("ga:sessions", new InfluxValueField(Convert.ToInt32(fields["ga:sessions"])));
            dp.Fields.Add("ga:users", new InfluxValueField(Convert.ToInt32(fields["ga:users"])));

            return(dp);
        }
Ejemplo n.º 18
0
        public async Task <int> AggregateAsync(string metricKey)
        {
            var redisDb = connectionMultiplexer.GetDatabase();

            // Read newly added values
            var metricValues = await redisDb.SortedSetRangeByRankWithScoresAsync <MetricRedisValue>(metricKey, 0, -1);

            // Create the metric if it doesn't exist
            var metricId = "0";

            var timestamp = metricValues.FirstOrDefault().TimestampGranulated;

            var aggregatedMetricData = new AggregatedMetricData
            {
                Measurement = "m_" + metricId,
                MetricId    = metricId,
                Sum         = metricValues.Sum(x => x.Value),
                Average     = metricValues.Average(x => x.Value),
                Min         = metricValues.Min(x => x.Value),
                Max         = metricValues.Max(x => x.Value),
                Count       = metricValues.Length,
                Timestamp   = timestamp,
                Precision   = TimePrecision.Seconds,
            };

            aggregatedMetricData.Time = DateTimeOffset.FromUnixTimeSeconds(aggregatedMetricData.Timestamp).UtcDateTime;

            // Insert this metric to InfluxDb
            var datapoint = new InfluxDatapoint <InfluxValueField>
            {
                MeasurementName = "m_" + aggregatedMetricData.MetricId,
                UtcTimestamp    = DateTimeOffset.FromUnixTimeSeconds(aggregatedMetricData.Timestamp).UtcDateTime,
                Precision       = TimePrecision.Seconds
            };

            datapoint.Fields.Add("sum", new InfluxValueField(aggregatedMetricData.Sum));
            datapoint.Fields.Add("average", new InfluxValueField(aggregatedMetricData.Average));
            datapoint.Fields.Add("min", new InfluxValueField(aggregatedMetricData.Min));
            datapoint.Fields.Add("max", new InfluxValueField(aggregatedMetricData.Max));
            datapoint.Fields.Add("count", new InfluxValueField(aggregatedMetricData.Count));

            var result = await influxDBClient.PostPointAsync("myDatabase", datapoint);

            // Remove the metric's sorted set
            await redisDb.KeyDeleteAsync(metricKey);

            return(metricValues.Length);
        }
        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));
        }
Ejemplo n.º 20
0
        public async Task InsertAsync(InfluxDbBase dbBase)
        {
            var valMixed = new InfluxDatapoint <InfluxValueField>();

            valMixed.UtcTimestamp = DateTime.UtcNow;
            valMixed.Tags.Add("Date", DateTime.UtcNow.ToShortDateString());
            valMixed.Tags.Add("Time", DateTime.UtcNow.ToShortTimeString());
            var list = JsonConvert.DeserializeObject <Dictionary <string, string> >(JsonConvert.SerializeObject(dbBase));

            foreach (var item in list)
            {
                valMixed.Fields.Add($"{item.Key}", new InfluxValueField(item.Value));
            }

            var r = await InfluxDBConnection.PostPointAsync(DbName, valMixed);
        }
        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 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;
            }
        }
Ejemplo n.º 23
0
        public async Task AddDataPoint(DockerStatDataModel model)
        {
            var valMixed = new InfluxDatapoint <InfluxValueField>();

            valMixed.UtcTimestamp = model.Read.UtcDateTime;
            valMixed.Tags.Add("ContainerId", model.Id);
            valMixed.Tags.Add("ContainerName", model.Name);
            valMixed.Fields.Add("CpuPercent", new InfluxValueField(CpuCycleConverter.CalculateCPUPercentWindows(model)));
            valMixed.Fields.Add("MemoryUsage", new InfluxValueField(BinaryConverter.ConvertBytesToMegabytes(model.MemoryStats.Privateworkingset)));
            valMixed.Fields.Add("DiskInput", new InfluxValueField(BinaryConverter.ConvertBytesToMegabytes(model.StorageStats.WriteSizeBytes)));
            valMixed.Fields.Add("DiskOutput", new InfluxValueField(BinaryConverter.ConvertBytesToMegabytes(model.StorageStats.ReadSizeBytes)));

            valMixed.MeasurementName = _settings.MeasurementName;
            valMixed.Precision       = TimePrecision.Seconds;

            var r = await _dbClient.PostPointAsync(_settings.DbName, valMixed);
        }
        /// <summary>
        /// Creates a DTO that contains MailChimp campaign data to be stored in the database.
        /// </summary>
        /// <param name="measurementName">The name of the measurement that the data is stored in.</param>
        /// <param name="report">The MailChimp report whose data shall be stored.</param>
        /// <returns>An InfluxDatapoint object containing the MailChimp report data.</returns>
        public static InfluxDatapoint <InfluxValueField> CreateInfluxDatapoint(string measurementName, MailChimp.Net.Models.Report report)
        {
            InfluxDatapoint <InfluxValueField> dp = new InfluxDatapoint <InfluxValueField>();

            dp.UtcTimestamp    = DateTime.UtcNow;
            dp.MeasurementName = measurementName;

            dp.Tags.Add("CampaignID", report.Id);
            dp.Fields.Add("Bounces", new InfluxValueField(report.Bounces.SoftBounces + report.Bounces.HardBounces));
            dp.Fields.Add("EMailsSent", new InfluxValueField(report.EmailsSent));
            dp.Fields.Add("Forwards", new InfluxValueField(report.Forwards.ForwardsCount));
            dp.Fields.Add("UniqueClicks", new InfluxValueField(report.Clicks.UniqueSubscriberClicks));
            dp.Fields.Add("UniqueOpens", new InfluxValueField(report.Opens.UniqueOpens));
            dp.Fields.Add("Unsubscribed", new InfluxValueField(report.Unsubscribed));

            return(dp);
        }
        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;
            }
        }
Ejemplo n.º 26
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 TestPostPointAsync_InvalidReq()
        {
            var client = new InfluxDBClient(influxUrl, dbUName, dbpwd);
            var time   = DateTime.Now;
            var today  = DateTime.Now.ToShortDateString();
            var now    = DateTime.Now.ToShortTimeString();

            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;

            var r = await AssertEx.ThrowsAsync <InfluxDBException>(() => client.PostPointAsync(dbName, valDouble));
        }
Ejemplo n.º 28
0
        public async Task <bool> addMeasure(string measureName, string spaceID, double value)
        {
            if (dbExists == null)
            {
                if ((bool)(dbExists = await checkIfDBExists()) == false)
                {
                    dbExists = await CreateDB();
                }
            }
            var point = new InfluxDatapoint <InfluxValueField>();

            point.UtcTimestamp = DateTime.UtcNow;
            point.Tags.Add("spaceID", spaceID);
            point.Fields.Add("value", new InfluxValueField(value));
            point.MeasurementName = measureName;
            log.Info($"Add measure point: {{MeasureName: {measureName}, spaceID: {spaceID}, value: {value} }} to {database}");

            return(await influx.PostPointAsync(database, point));
        }
        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;
            }
        }
Ejemplo n.º 30
0
        private IEnumerable <IInfluxDatapoint> ConvertEntries(InfluxDbEntry[] entries, string retentionPoliciy)
        {
            foreach (var entry in entries)
            {
                var point = new InfluxDatapoint <InfluxValueField>();
                point.MeasurementName = entry.Measurement;

                if (entry.Time.HasValue)
                {
                    point.UtcTimestamp = entry.Time.Value;
                }

                foreach (var tag in entry.Tags)
                {
                    point.Tags.Add(tag.Name, tag.Value.ToString());
                }

                foreach (var value in entry.Fields)
                {
                    if (value.Value is IComparable)
                    {
                        point.Fields.Add(value.Name, new InfluxValueField((IComparable)value.Value));
                    }
                    else
                    {
                        _logger.Error("Value for InfluxDB write provided that could not be uploaded");
                    }
                }

                if (!string.IsNullOrEmpty(retentionPoliciy))
                {
                    point.Retention = new InfluxRetentionPolicy()
                    {
                        Name = retentionPoliciy
                    };
                }

                yield return(point);
            }
        }
        public async Task TestPostBooleanPointAsync()
        {
            try
            {
                var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
                var time = DateTime.Now;

                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.Seconds;
                var r = await client.PostPointAsync (dbName, valBool);
                Assert.IsTrue (r, "PostPointAsync retunred false");
            }
            catch ( Exception e )
            {

                Assert.Fail ("Unexpected exception of type {0} caught: {1}",
                            e.GetType (), e.Message);
                return;
            }
        }
Ejemplo n.º 32
0
        private InfluxDatapoint<InfluxValueField> ProcessGenericLine (string line, List<GenericColumn> columnHeaders)
        {

            var columns = pattern.Split (line);
            var columnCount = columns.Count ();
            var content = columns[settings.GenericFile.TimeColumn - 1].Replace ("\"", "");

            InfluxDatapoint<InfluxValueField> point = new InfluxDatapoint<InfluxValueField> ();
            point.Precision = settings.GenericFile.Precision;
            point.MeasurementName = settings.InfluxDB.Measurement;


            point.InitializeTags (defaultTags);

            var pointData = new Dictionary<GenericColumn, string> ();

            foreach (var c in columnHeaders)
            {

                content = columns[c.ColumnIndex].Replace ("\"", "");

                if (c.HasAutoGenColumns)
                {
                    pointData.AddRange (c.SplitData (content));
                }
                else
                {
                    pointData.Add (c, content);
                }
            }

            foreach (var d in pointData)
            {
                content = d.Value;
                if (d.Key.HasTransformations && d.Key.CanTransform (content))
                    content = d.Key.Transform (d.Value);

                if (String.IsNullOrWhiteSpace (content)) continue;

                if (d.Key.ColumnIndex == settings.GenericFile.TimeColumn - 1)
                {
                    DateTime timeStamp;
                    if (!DateTime.TryParseExact (content, settings.GenericFile.TimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out timeStamp))
                        throw new FormatException ("Couldn't parse " + content + " using format " + settings.GenericFile.TimeFormat + ", check -timeformat argument");
                    point.UtcTimestamp = timeStamp.AddMinutes (settings.GenericFile.UtcOffset);
                }
                else
                {
                    double value = double.NaN; bool boolVal = false;
                    if (d.Key.Type == ColumnDataType.NumericalField)
                    {
                        if (!Double.TryParse (content, out value) || double.IsNaN (value))
                            throw new InvalidDataException (d.Key.ColumnHeader + " has inconsistent data, Unable to parse \"" + content + "\" as number");
                        point.Fields.Add (d.Key.ColumnHeader, new InfluxValueField (Math.Round (value, 2)));
                    }
                    else if (d.Key.Type == ColumnDataType.StringField)
                    {
                        point.Fields.Add (d.Key.ColumnHeader, new InfluxValueField (content));
                    }
                    else if (d.Key.Type == ColumnDataType.BooleanField)
                    {
                        if (!Boolean.TryParse (content, out boolVal))
                            throw new InvalidDataException (d.Key.ColumnHeader + " has inconsistent data, Unable to parse \"" + content + "\" as Boolean");
                        point.Fields.Add (d.Key.ColumnHeader, new InfluxValueField (boolVal));
                    }
                    else if (d.Key.Type == ColumnDataType.Tag)
                        point.Tags.Add (d.Key.ColumnHeader, content.Replace (settings.InfluxDB.InfluxReserved.ReservedCharecters.ToCharArray (), settings.InfluxDB.InfluxReserved.ReplaceReservedWith));
                }
            }


            if (point.Fields.Count == 0)
                throw new InvalidDataException ("No values found on the row to post to Influx");

            return point;
        }
        public async Task TestPostDoublePointAsync()
        {
            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.Seconds;
                var r = await client.PostPointAsync (dbName, valDouble);
                Assert.IsTrue (r, "PostPointAsync retunred false");
            }
            catch ( Exception e )
            {

                Assert.Fail ("Unexpected exception of type {0} caught: {1}",
                            e.GetType (), e.Message);
                return;
            }
        }
        public async Task TestPostStringPointAsync()
        {
            try
            {
                var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
                var time = DateTime.Now;
                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", @"j0,Oox7ju6lJvA0\");//DataGen.RandomString ());
                valString.MeasurementName = measurementName;
                valString.Precision = TimePrecision.Seconds;
                var r = await client.PostPointAsync (dbName, valString);
                Assert.IsTrue (r, "PostPointAsync retunred false");
            }
            catch ( Exception e )
            {

                Assert.Fail ("Unexpected exception of type {0} caught: {1}",
                            e.GetType (), e.Message);
                return;
            }
        }
        public async Task TestBachingPostPointsAsync()
        {
            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 valDouble = new InfluxDatapoint<double> ();
                    valDouble.UtcTimestamp = DataGen.RandomDate (start, end);
                    valDouble.Tags.Add ("TestDate", today);
                    valDouble.Tags.Add ("TestTime", now);
                    valDouble.Fields.Add ("Doublefield", DataGen.RandomDouble ());
                    valDouble.Fields.Add ("Doublefield2", DataGen.RandomDouble ());
                    valDouble.MeasurementName = measurementName;
                    valDouble.Precision = (TimePrecision) ( DataGen.RandomInt () % 6 ) + 1;

                    var valInt = new InfluxDatapoint<int> ();
                    valInt.UtcTimestamp = DataGen.RandomDate (start, end);
                    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) ( DataGen.RandomInt () % 6 ) + 1;

                    var valBool = new InfluxDatapoint<bool> ();
                    valBool.UtcTimestamp = DataGen.RandomDate (start, end);
                    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) ( DataGen.RandomInt () % 6 ) + 1;

                    var valString = new InfluxDatapoint<string> ();
                    valString.UtcTimestamp = DataGen.RandomDate (start, end);
                    valString.Tags.Add ("TestDate", today);
                    valString.Tags.Add ("TestTime", now);
                    valString.Fields.Add ("Stringfield", DataGen.RandomString ());
                    valString.MeasurementName = measurementName;
                    valString.Precision = (TimePrecision) ( DataGen.RandomInt () % 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;
            }
        }
        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 client.PostPointsAsync (dbName, points);
            Assert.IsTrue (r, "PostPointsAsync retunred false");

        }
        public async Task TestPostPointAsync_InvalidReq()
        {
            var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
            var time = DateTime.Now;
            var today = DateTime.Now.ToShortDateString ();
            var now = DateTime.Now.ToShortTimeString ();

            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;

            var r = await client.PostPointAsync (dbName, valDouble);
            Assert.IsTrue (r, "PostPointsAsync retunred false");

        }
Ejemplo n.º 38
0
        private List<IInfluxDatapoint> ProcessPerfmonLogLine (string line, IEnumerable<IGrouping<string, PerfmonCounter>> perfGroup)
        {
            var columns = pattern.Split (line.Replace ("\"", ""));
            var columnCount = columns.Count ();

            DateTime timeStamp;
            if (!DateTime.TryParseExact (columns[0], settings.PerfmonFile.TimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out timeStamp))
                throw new FormatException ("Couldn't parse " + columns[0] + " using format " + settings.PerfmonFile.TimeFormat + ", check -timeformat argument");
            var utcTime = timeStamp.AddMinutes (minOffset);

            var points = new List<IInfluxDatapoint> ();

            foreach (var performanceObject in perfGroup)
            {
                foreach (var hostGrp in performanceObject.GroupBy (p => p.Host))
                {
                    if (settings.PerfmonFile.MultiMeasurements)
                    {
                        var point = new InfluxDatapoint<double> ();
                        if (defaultTags.Count > 0) point.InitializeTags (defaultTags);
                        point.Tags.Add ("Host", hostGrp.Key);
                        point.Precision = TimePrecision.Seconds;
                        point.Retention = policy;
                        point.MeasurementName = performanceObject.Key;
                        point.UtcTimestamp = utcTime;

                        double value = 0.0;

                        foreach (var counter in hostGrp)
                        {
                            if (!String.IsNullOrWhiteSpace (columns[counter.ColumnIndex]) && Double.TryParse (columns[counter.ColumnIndex], out value))
                            {
                                //Perfmon file can have duplicate columns!!
                                if (point.Fields.ContainsKey (counter.CounterName))
                                    point.Fields[counter.CounterName] = value;
                                else
                                    point.Fields.Add (counter.CounterName, value);
                            }
                        }
                        if (point.Fields.Count > 0)
                            points.Add (point);
                    }
                    else
                    {
                        foreach (var counter in hostGrp)
                        {
                            double value = 0.0;
                            if (!String.IsNullOrWhiteSpace (columns[counter.ColumnIndex]) && Double.TryParse (columns[counter.ColumnIndex], out value))
                            {
                                var point = new InfluxDatapoint<double> ();
                                point.Precision = TimePrecision.Seconds;
                                point.Retention = policy;
                                if (defaultTags.Count > 0) point.InitializeTags (defaultTags);
                                point.Tags.Add ("Host", hostGrp.Key);
                                point.MeasurementName = settings.InfluxDB.Measurement;
                                point.UtcTimestamp = utcTime;
                                point.Tags.Add ("PerformanceObject", counter.PerformanceObject);
                                point.Tags.Add ("PerformanceCounter", counter.CounterName);
                                point.Fields.Add ("CounterValue", value);
                                points.Add (point);
                            }
                        }
                    }
                }
            }
            return points;
        }
        public async Task TestPostPointsAsync()
        {
            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 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", DataGen.RandomDouble ());
                valDouble.MeasurementName = measurementName;
                valDouble.Precision = TimePrecision.Nanoseconds;
                points.Add (valDouble);

                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", DataGen.RandomDouble ());
                valDouble.MeasurementName = measurementName;
                valDouble.Precision = TimePrecision.Microseconds;
                points.Add (valDouble);

                var valInt = new InfluxDatapoint<int> ();
                valInt.UtcTimestamp = DateTime.UtcNow;
                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.Milliseconds;
                points.Add (valInt);

                valInt = new InfluxDatapoint<int> ();
                valInt.UtcTimestamp = DateTime.UtcNow;
                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);

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

                var valString = new InfluxDatapoint<string> ();
                valString.UtcTimestamp = DateTime.UtcNow;
                valString.Tags.Add ("TestDate", today);
                valString.Tags.Add ("TestTime", now);
                valString.Fields.Add ("Stringfield", DataGen.RandomString ());
                valString.MeasurementName = measurementName;
                valString.Precision = TimePrecision.Hours;
                points.Add (valString);


                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;
            }
        }