Esempio n. 1
0
        public void DoubleConverter(object input, double?expected)
        {
            var converter = new DoubleConverter();
            var result    = converter.Convert(input);

            Assert.AreEqual(expected, result);
        }
Esempio n. 2
0
        public void DoubleConverterFromDecimal()
        {
            var converter = new DoubleConverter();
            var result    = converter.Convert(4m / 5m);

            Assert.AreEqual(System.Convert.ToDouble(4m / 5m), result);
        }
Esempio n. 3
0
 public void Convert_ThrowsException_WhenNotDouble()
 {
     var py = Python.Instance();
     var pyStr = py.Eval("\"omershelef\"");
     var converter = new DoubleConverter();
     Assert.Throws<ConversionException>(() => converter.Convert(pyStr));
 }
Esempio n. 4
0
 public void Convert_SanityCheck()
 {
     var py = Python.Instance();
     var pyFloat = py.Eval("123.456789");
     var converter = new DoubleConverter();
     Assert.AreEqual(123.456789, converter.Convert(pyFloat));
 }
        public void ConvertTest()
        {
            DoubleConverter converter = new DoubleConverter();
            var             value     = converter.Convert(10.25, typeof(object), null, CultureInfo.CurrentCulture);
            double          d         = 10.25;

            Assert.AreEqual(d, value);
        }
Esempio n. 6
0
        public void DoubleConverter_CorrectConversionTest()
        {
            Array[] data = new double[][]
            {
                new double[] { 0, 1 },
                new double[] { 0.4, 0.6 },
            };

            var converter = new DoubleConverter();
            var sut       = converter.Convert(data, 2, 2);

            var expectation = new ushort[] { 0, ushort.MaxValue, 26214, 39321 };

            sut.Should().BeEquivalentTo(expectation);
        }
Esempio n. 7
0
        public void Conversion()
        {
            // Arrange
            IConverter converter     = new DoubleConverter();
            var        value         = "42,35";
            var        expectedValue = 42.35;

            // Act
            using (new LangageSwitcher("fr-fr"))
            {
                var actualValue = converter.Convert(value, converter.TargetType);

                // Assert
                Assert.NotNull(actualValue);
                Assert.IsType <double>(actualValue);
                Assert.Equal(expectedValue, (Double)actualValue);
            }
        }
Esempio n. 8
0
        public List <TS> CreateEntities <T>(IEnumerable <string> resultsets, IDictionary <string, TsMetadata> metas, int period, bool skipLastPoint) where T : TS, new()
        {
            var deserializedResultsets = resultsets.Select(rs => JsonConvert.DeserializeObject <InfluxDbResultset>(rs)).ToList();

            var entities = new List <TS>();

            foreach (var resultset in deserializedResultsets)
            {
                foreach (var result in resultset.Results.Where(r => r.Series != null))
                {
                    var  serie = result.Series.Single();
                    var  timeseriesEntities = new List <TS>();
                    var  meta       = metas[serie.Tags.Tag];
                    bool valueFound = false;

                    foreach (var value in serie.Values)
                    {
                        var entity = new T
                        {
                            Timestamp = (DateTime)value[0],
                            AVG       = DoubleConverter.Convert(value[1]),
                            MAX       = DoubleConverter.Convert(value[2]),
                            MIN       = DoubleConverter.Convert(value[3]),
                            STD       = DoubleConverter.Convert(value[4]),
                            Id        = _tagToId[serie.Tags != null ? serie.Tags.Tag : (string)value[5]],
                            Period    = period
                        };

                        if (entity.AVG != null)
                        {
                            valueFound = true;
                        }
                        else if (!valueFound)
                        {
                            entity.AVG = meta.LastValueBeforeWatermark;
                            entity.MAX = meta.LastValueBeforeWatermark;
                            entity.MIN = meta.LastValueBeforeWatermark;
                        }
                        else
                        {
                            throw new Exception($"Tag has unexpected nulls: {serie.Tags.Tag}");
                        }

                        if ((meta.Watermark.HasValue && entity.Timestamp > meta.Watermark) ||
                            (entity.AVG != null))
                        {
                            timeseriesEntities.Add(entity);
                        }
                    }

                    if (skipLastPoint)
                    {
                        if (timeseriesEntities.Count == 1 &&
                            (timeseriesEntities.Single().Timestamp < DateTime.UtcNow.AddDays(-1) || !meta.Watermark.HasValue))
                        {
                            entities.Add(timeseriesEntities.Single());
                        }
                        else
                        {
                            // The last point is not based on a full interval, so it should be written to SQL later.
                            entities.AddRange(OrderByTimestamp(timeseriesEntities).Take(timeseriesEntities.Count - 1));
                        }
                    }
                    else
                    {
                        entities.AddRange(timeseriesEntities);
                    }
                }
            }

            return(entities);
        }
Esempio n. 9
0
        private static async Task <IDictionary <string, (DateTime timestamp, double value)> > GetOutliers(InfluxQueryExecutor influxQueryExecutor, List <TsMetadataDto> metas, string retentionPolicy,
                                                                                                          string measurement, TagTypes tagType, string defaultTime, bool doAdHocResampling, TraceWriter log,
                                                                                                          bool beforeWatermark, int batchSize = 256, int parallelism = 32)
        {
            var dict = new Dictionary <string, (DateTime timestamp, double value)>();
            int skip = 0;

            while (skip < metas.Count)
            {
                var tasks = new List <Task <string> >(parallelism);

                for (int a = 0; a < parallelism; a++)
                {
                    if (skip >= metas.Count)
                    {
                        break;
                    }

                    var batchBuilder = new StringBuilder();

                    var batch = metas.Skip(skip).Take(batchSize);
                    skip += batchSize;

                    foreach (var metaInBatch in batch)
                    {
                        if (beforeWatermark && !metaInBatch.Timestamp.HasValue)
                        {
                            continue;
                        }

                        string tag = TagCleaner.Clean(metaInBatch.Tag);

                        string query = beforeWatermark
                            ? QueryBuilder.CreateFindLastPointBeforeWatermarkQuery(retentionPolicy, measurement, tagType, metaInBatch.Timestamp.Value, defaultTime, tag)
                            : QueryBuilder.CreateFindLastPointQuery(retentionPolicy, measurement, tagType, metaInBatch.Timestamp, defaultTime, tag);

                        batchBuilder.Append(query);
                    }

                    string batchedQuery = batchBuilder.ToString();
                    batchBuilder.Clear();

                    if (!string.IsNullOrWhiteSpace(batchedQuery))
                    {
                        tasks.Add(influxQueryExecutor.Query(batchedQuery, log));
                    }
                }

                await Task.WhenAll(tasks);

                var deserializedResultsets = tasks.Select(t => JsonConvert.DeserializeObject <InfluxDbResultset>(t.Result)).ToList();

                foreach (var resultset in deserializedResultsets)
                {
                    foreach (var result in resultset.Results.Where(r => r.Series != null))
                    {
                        var serie = result.Series.Single();

                        foreach (var value in serie.Values)
                        {
                            var timestamp = (DateTime)value[0];
                            var val       = DoubleConverter.Convert(value[1]).Value;
                            var tag       = (string)value[2];

                            dict.Add(tag, (timestamp, val));
                        }
                    }
                }
            }

            return(dict);
        }
Esempio n. 10
0
        public void Convert_test(double value, string expectedResult)
        {
            string actualResult = doubleConverter.Convert(value);

            Assert.AreEqual(expectedResult, actualResult);
        }
Esempio n. 11
0
        public void BadValueConversion()
        {
            // Arrange
            IConverter converter = new DoubleConverter();
            var        value     = "Hello";
            var        expectedExceptionMessage      = Constants.ExceptionMessages.FormatConverterUnableConvert(value, typeof(double));
            var        expectedInnerExceptionMessage = "Input string was not in a correct format.";

            // Act
            using (new LangageSwitcher("en-us"))
            {
                var actualException = Assert.Throws <CommandLineParserException>(() => converter.Convert(value, converter.TargetType));

                // Assert
                Assert.Equal(expectedExceptionMessage, actualException.Message);
                Assert.NotNull(actualException.InnerException);
                var actualInnerExecption = Assert.IsAssignableFrom <FormatException>(actualException.InnerException);
                Assert.Equal(expectedInnerExceptionMessage, actualInnerExecption.Message);
            }
        }