Exemplo n.º 1
0
        public void Minimal()
        {
            var data = new[] {
                new Group {
                    items = new object[] { 1, 5 }
                },
                new Group {
                    items = new object[] { 7 }
                }
            };

            var calculator = new AggregateCalculator <int>(data, new DefaultAccessor <int>(),
                                                           new[] { new SummaryInfo {
                                                                       Selector = "this", SummaryType = "sum"
                                                                   } },
                                                           new[] { new SummaryInfo {
                                                                       Selector = "this", SummaryType = "sum"
                                                                   } }
                                                           );

            var totals = calculator.Run();

            Assert.Equal(13M, totals[0]);
            Assert.Equal(6M, data[0].summary[0]);
            Assert.Equal(7M, data[1].summary[0]);
        }
        public void SumFix()
        {
            var summary = Enumerable.Range(1, 4)
                          .Select(i => new SummaryInfo {
                SummaryType = "sum", Selector = "Item" + i
            })
                          .ToArray();

            var data = new[] {
                new Group {
                    items = new[] { new SumFixItem() }
                },
                new Group {
                    items = new object[] { null }
                },
                new Group {
                    items = Array.Empty <object>()
                }
            };

            var totals = new AggregateCalculator <SumFixItem>(data, new DefaultAccessor <SumFixItem>(), summary, summary).Run();

            foreach (var values in new[] {
                totals,
                data[0].summary,
                data[1].summary,
                data[2].summary
            })
            {
                Assert.Equal(0m, values[0]);
                Assert.Equal(0d, values[1]);
                Assert.Equal(default(TimeSpan), values[2]);
                Assert.Equal(0m, values[3]);
            }
        }
        public static RemoteGroupingResult Run(IEnumerable<IRemoteGroup> flatGroups, int groupCount, SummaryInfo[] totalSummary, SummaryInfo[] groupSummary) {
            var accessor = new RemoteGroupAccessor();

            List<Group> hierGroups = null;

            if(groupCount > 0) {
                hierGroups = new GroupHelper<IRemoteGroup>(accessor).Group(
                    flatGroups,
                    Enumerable.Range(0, groupCount).Select(i => new GroupingInfo { Selector = "K" + i }).ToArray()
                );
            }

            IEnumerable dataToAggregate = hierGroups;
            if(dataToAggregate == null)
                dataToAggregate = flatGroups;

            var transformedTotalSummary = TransformSummary(totalSummary, "T");
            var transformedGroupSummary = TransformSummary(groupSummary, "G");

            transformedTotalSummary.Add(new SummaryInfo { SummaryType = "remoteCount" });

            var totals = new AggregateCalculator<IRemoteGroup>(dataToAggregate, accessor, transformedTotalSummary, transformedGroupSummary).Run();
            var totalCount = (int)totals.Last();

            totals = totals.Take(totals.Length - 1).ToArray();
            if(totals.Length < 1)
                totals = null;

            return new RemoteGroupingResult {
                Groups = hierGroups,
                Totals = totals,
                TotalCount = totalCount
            };
        }
Exemplo n.º 4
0
        public void CustomAggregator()
        {
            CustomAggregatorsBarrier.Run(delegate {
                CustomAggregators.RegisterAggregator("comma", typeof(CommaAggregator <>));

                var data = new[] {
                    new Group {
                        items = new Tuple <int>[] { Tuple.Create(1), Tuple.Create(5) }
                    },
                    new Group {
                        items = new Tuple <int>[] { Tuple.Create(7) }
                    },
                    new Group {
                        items = new Tuple <int>[] { }
                    }
                };

                var calculator = new AggregateCalculator <Tuple <int> >(data, new DefaultAccessor <Tuple <int> >(),
                                                                        new[] { new SummaryInfo {
                                                                                    Selector = "Item1", SummaryType = "comma"
                                                                                } },
                                                                        new[] { new SummaryInfo {
                                                                                    Selector = "Item1", SummaryType = "comma"
                                                                                } }
                                                                        );

                var totals = calculator.Run();

                Assert.Equal("1,5,7", totals[0]);
                Assert.Equal("1,5", data[0].summary[0]);
                Assert.Equal("7", data[1].summary[0]);
                Assert.Equal(string.Empty, data[2].summary[0]);
            });
        }
        public void TimeSpanType()
        {
            var data = new[] {
                TimeSpan.FromMinutes(1),
                TimeSpan.FromMinutes(3)
            };

            var calculator = new AggregateCalculator <TimeSpan>(data, new DefaultAccessor <TimeSpan>(),
                                                                new[] {
                new SummaryInfo {
                    Selector = "this", SummaryType = "min"
                },
                new SummaryInfo {
                    Selector = "this", SummaryType = "max"
                },
                new SummaryInfo {
                    Selector = "this", SummaryType = "sum"
                },
                new SummaryInfo {
                    Selector = "this", SummaryType = "avg"
                }
            },
                                                                null
                                                                );

            Assert.Equal(
                new object[] {
                TimeSpan.FromMinutes(1),
                TimeSpan.FromMinutes(3),
                TimeSpan.FromMinutes(4),
                TimeSpan.FromMinutes(2)
            },
                calculator.Run()
                );
        }
Exemplo n.º 6
0
        public void NestedGroups()
        {
            var data = new[] {
                new Group {
                    items = new[] {
                        new Group {
                            items = new object[] { 1, 5 }
                        }
                    }
                }
            };

            var calculator = new AggregateCalculator <int>(data, new DefaultAccessor <int>(), null, new[] {
                new SummaryInfo {
                    Selector = "this", SummaryType = "sum"
                }
            });

            var totals = calculator.Run();

            Assert.Null(totals);

            Assert.Equal(6M, data[0].summary[0]);
            Assert.Equal(6M, (data[0].items[0] as Group).summary[0]);
        }
Exemplo n.º 7
0
        public void IgnoreGroupSummaryIfNoGroups()
        {
            var data = new object[] { 1 };

            var calculator = new AggregateCalculator <int>(data, new DefaultAccessor <int>(), null, new[] {
                new SummaryInfo {
                    Selector = "ignore me", SummaryType = "ignore me"
                }
            });

            calculator.Run();
        }
Exemplo n.º 8
0
            public void NoStrategy_ThrowsException()
            {
                // Arrange
                var calculations = Enumerable.Empty <IBinaryCalculation>();
                var calculator   = new AggregateCalculator(calculations);

                // Act
                Action run = () => calculator.Solve(new CalculationCommand());

                // Assert
                run.ShouldThrow <ArgumentException>();
            }
Exemplo n.º 9
0
        public static RemoteGroupingResult Run(IEnumerable <AnonType> flatGroups, int groupCount, ContosoRetail.SharedKernel.DataAccess.DataLoader.SummaryInfo[] totalSummary, ContosoRetail.SharedKernel.DataAccess.DataLoader.SummaryInfo[] groupSummary)
        {
            var accessor = new AnonTypeAccessor();

            var markup = new RemoteGroupTypeMarkup(
                groupCount,
                totalSummary != null ? totalSummary.Length : 0,
                groupSummary != null ? groupSummary.Length : 0
                );

            List <Group> hierGroups = null;

            if (groupCount > 0)
            {
                hierGroups = new GroupHelper <AnonType>(accessor).Group(
                    flatGroups,
                    Enumerable.Range(0, groupCount).Select(i => new ContosoRetail.SharedKernel.DataAccess.DataLoader.GroupingInfo {
                    Selector = AnonType.ITEM_PREFIX + (RemoteGroupTypeMarkup.KeysStartIndex + i)
                }).ToArray()
                    );
            }

            IEnumerable dataToAggregate = hierGroups;

            if (dataToAggregate == null)
            {
                dataToAggregate = flatGroups;
            }

            var transformedTotalSummary = TransformSummary(totalSummary, markup.TotalSummaryStartIndex);
            var transformedGroupSummary = TransformSummary(groupSummary, markup.GroupSummaryStartIndex);

            transformedTotalSummary.Add(new SummaryInfo {
                SummaryType = "remoteCount"
            });

            var totals     = new AggregateCalculator <AnonType>(dataToAggregate, accessor, transformedTotalSummary, transformedGroupSummary).Run();
            var totalCount = (int)totals.Last();

            totals = totals.Take(totals.Length - 1).ToArray();
            if (totals.Length < 1)
            {
                totals = null;
            }

            return(new RemoteGroupingResult
            {
                Groups = hierGroups,
                Totals = totals,
                TotalCount = totalCount
            });
        }
Exemplo n.º 10
0
        public void Issue147()
        {
            var data = Enumerable.Repeat(Double.MaxValue / 3, 2);

            var calculator = new AggregateCalculator <double>(data, new DefaultAccessor <double>(),
                                                              new[] { new SummaryInfo {
                                                                          Selector = "this", SummaryType = "sum"
                                                                      } },
                                                              null
                                                              );

            Assert.Equal(data.Sum(), calculator.Run()[0]);
        }
Exemplo n.º 11
0
            public void ValidStrategy_CallsStrategy()
            {
                // Arrange
                var calculation  = CreateMockCalculation();
                var calculations = Enumerable.Repeat(calculation, 1);
                var calculator   = new AggregateCalculator(calculations);

                // Act
                calculator.Solve(new CalculationCommand());

                // Assert
                calculation.Received().Apply(0, 0);
            }
Exemplo n.º 12
0
        void AssertCalculation(object[] data, object expectedSum, object expectedMin, object expectedMax, object expectedAvg, object expectedCount)
        {
            /*
             *  SQL script to validate
             *
             *  drop table if exists t1;
             *
             #create table t1 (a int);
             #insert into t1 (a) values (1), (3), (5);
             *
             #create table t1 (a varchar(32));
             #insert into t1 (a) values ('a'), ('z');
             *
             #insert into t1 (a) values (null);
             *
             *  select concat(
             *      " sum=",   coalesce(sum(a),   'N'),
             *      " min=",   coalesce(min(a),   'N'),
             *      " max=",   coalesce(max(a),   'N'),
             *      " avg=",   coalesce(avg(a),   'N'),
             *      " count=", coalesce(count(*), 'N')
             *  ) from t1;
             *
             */

            var summaries = new[] {
                new SummaryInfo {
                    Selector = "this", SummaryType = "sum"
                },
                new SummaryInfo {
                    Selector = "this", SummaryType = "min"
                },
                new SummaryInfo {
                    Selector = "this", SummaryType = "max"
                },
                new SummaryInfo {
                    Selector = "this", SummaryType = "avg"
                },
                new SummaryInfo {
                    SummaryType = "count"
                },
            };

            var totals = new AggregateCalculator <object>(data, new DefaultAccessor <object>(), summaries, null).Run();

            Assert.Equal(expectedSum, totals[0]);
            Assert.Equal(expectedMin, totals[1]);
            Assert.Equal(expectedMax, totals[2]);
            Assert.Equal(expectedAvg, totals[3]);
            Assert.Equal(expectedCount, totals[4]);
        }
        public void CustomAggregator()
        {
            try {
                CustomAggregators.RegisterAggregator("totalSales", typeof(TotalSalesAggregator <>));

                var data = new[] {
                    new Group {
                        items = new[] {
                            new CustomAggregatorDataItem {
                                UnitPrice = 4.04M, Quantity = 5, Discount = 0.10M
                            },
                            new CustomAggregatorDataItem {
                                UnitPrice = 10.10M, Quantity = 2, Discount = 0.20M
                            }
                        }
                    },
                    new Group {
                        items = new[] {
                            new CustomAggregatorDataItem {
                                UnitPrice = 15.15M, Quantity = 4, Discount = 0.30M
                            }
                        }
                    },
                    new Group {
                        items = new CustomAggregatorDataItem[] { }
                    }
                };

                var calculator = new AggregateCalculator <CustomAggregatorDataItem>(data, new DefaultAccessor <CustomAggregatorDataItem>(),
                                                                                    new[] { new SummaryInfo {
                                                                                                Selector = "this", SummaryType = "totalSales"
                                                                                            } },
                                                                                    new[] { new SummaryInfo {
                                                                                                Selector = "this", SummaryType = "totalSales"
                                                                                            } }
                                                                                    );

                var totals = calculator.Run();

                Assert.Equal(76.76M, totals[0]);
                Assert.Equal(34.34M, data[0].summary[0]);
                Assert.Equal(42.42M, data[1].summary[0]);
                Assert.Equal(0M, data[2].summary[0]);
            } finally {
                CustomAggregators.Clear();
            }
        }
Exemplo n.º 14
0
            public void MultipleStrategies_ShouldAggregate()
            {
                // Arrange
                var calculation = Substitute.For <IBinaryCalculation>();

                calculation.Operation.Returns((string)null);
                var calculations = Enumerable.Range(0, 10)
                                   .Select(x => x.ToString())
                                   .Select(CreateMockCalculation);
                var calculator = new AggregateCalculator(calculations);

                // Act
                var result = calculator.GetSupportedOperations();

                // Assert
                result.ShouldBe(Enumerable.Range(0, 10).Select(x => x.ToString()));
            }
Exemplo n.º 15
0
        public static RemoteGroupingResult Run(Type sourceItemType, IEnumerable <AnonType> flatGroups, int groupCount, SummaryInfo[] totalSummary, SummaryInfo[] groupSummary)
        {
            List <Group> hierGroups = null;

            if (groupCount > 0)
            {
                hierGroups = new GroupHelper <AnonType>(AnonTypeAccessor.Instance).Group(
                    flatGroups,
                    Enumerable.Range(0, groupCount).Select(i => new GroupingInfo {
                    Selector = AnonType.IndexToField(1 + i)
                }).ToArray()
                    );
            }

            IEnumerable dataToAggregate = hierGroups;

            if (dataToAggregate == null)
            {
                dataToAggregate = flatGroups;
            }

            var fieldIndex = 1 + groupCount;
            var transformedTotalSummary = TransformSummary(totalSummary, ref fieldIndex);
            var transformedGroupSummary = TransformSummary(groupSummary, ref fieldIndex);

            transformedTotalSummary = transformedTotalSummary ?? new List <SummaryInfo>();
            transformedTotalSummary.Add(new SummaryInfo {
                SummaryType = AggregateName.REMOTE_COUNT
            });

            var sumFix     = new SumFix(sourceItemType, totalSummary, groupSummary);
            var totals     = new AggregateCalculator <AnonType>(dataToAggregate, AnonTypeAccessor.Instance, transformedTotalSummary, transformedGroupSummary, sumFix).Run();
            var totalCount = (int)totals.Last();

            totals = totals.Take(totals.Length - 1).ToArray();
            if (totals.Length < 1)
            {
                totals = null;
            }

            return(new RemoteGroupingResult {
                Groups = hierGroups,
                Totals = totals,
                TotalCount = totalCount
            });
        }
Exemplo n.º 16
0
        public static RemoteGroupingResult Run(IEnumerable <IRemoteGroup> flatGroups, int groupCount, SummaryInfo[] totalSummary, SummaryInfo[] groupSummary)
        {
            var accessor = new RemoteGroupAccessor();

            List <Group> hierGroups = null;

            if (groupCount > 0)
            {
                hierGroups = new GroupHelper <IRemoteGroup>(accessor).Group(
                    flatGroups,
                    Enumerable.Range(0, groupCount).Select(i => new GroupingInfo {
                    Selector = "K" + i
                }).ToArray()
                    );
            }

            IEnumerable dataToAggregate = hierGroups;

            if (dataToAggregate == null)
            {
                dataToAggregate = flatGroups;
            }

            var transformedTotalSummary = TransformSummary(totalSummary, "T");
            var transformedGroupSummary = TransformSummary(groupSummary, "G");

            transformedTotalSummary.Add(new SummaryInfo {
                SummaryType = "remoteCount"
            });

            var totals     = new AggregateCalculator <IRemoteGroup>(dataToAggregate, accessor, transformedTotalSummary, transformedGroupSummary).Run();
            var totalCount = (int)totals.Last();

            totals = totals.Take(totals.Length - 1).ToArray();
            if (totals.Length < 1)
            {
                totals = null;
            }

            return(new RemoteGroupingResult {
                Groups = hierGroups,
                Totals = totals,
                TotalCount = totalCount
            });
        }
        public void Minimal() {
            var data = new[] {
                new Group {
                    items = new object[] { 1, 5 }
                },
                new Group {
                    items = new object[] { 7 }
                }
            };

            var calculator = new AggregateCalculator<int>(data, new DefaultAccessor<int>(),
                new[] { new SummaryInfo { Selector = "this", SummaryType = "sum" } },
                new[] { new SummaryInfo { Selector = "this", SummaryType = "sum" } }
            );

            var totals = calculator.Run();

            Assert.Equal(13M, totals[0]);
            Assert.Equal(6M, data[0].summary[0]);
            Assert.Equal(7M, data[1].summary[0]);
        }
        void AssertCalculation(object[] data, object expectedSum, object expectedMin, object expectedMax, object expectedAvg, object expectedCount) {
            /*
                SQL script to validate

                drop table if exists t1;

                #create table t1 (a int);
                #insert into t1 (a) values (1), (3), (5);

                #create table t1 (a varchar(32));
                #insert into t1 (a) values ('a'), ('z');

                #insert into t1 (a) values (null);

                select concat(
                    " sum=",   coalesce(sum(a),   'N'), 
                    " min=",   coalesce(min(a),   'N'),  
                    " max=",   coalesce(max(a),   'N'),  
                    " avg=",   coalesce(avg(a),   'N'),  
                    " count=", coalesce(count(*), 'N') 
                ) from t1;

            */

            var summaries = new[] {
                new SummaryInfo { Selector = "this", SummaryType = "sum" },
                new SummaryInfo { Selector = "this", SummaryType = "min" },
                new SummaryInfo { Selector = "this", SummaryType = "max" },
                new SummaryInfo { Selector = "this", SummaryType = "avg" },
                new SummaryInfo { SummaryType = "count" },
            };

            var totals = new AggregateCalculator<object>(data, new DefaultAccessor<object>(), summaries, null).Run();

            Assert.Equal(expectedSum, totals[0]);
            Assert.Equal(expectedMin, totals[1]);
            Assert.Equal(expectedMax, totals[2]);
            Assert.Equal(expectedAvg, totals[3]);
            Assert.Equal(expectedCount, totals[4]);
        }
        public void NestedGroups() {
            var data = new[] {
                new Group {
                    items = new[] {
                        new Group {
                            items = new object[] { 1, 5 }
                        }
                    }
                }
            };

            var calculator = new AggregateCalculator<int>(data, new DefaultAccessor<int>(), null, new[] {
                new SummaryInfo { Selector = "this", SummaryType = "sum" }
            });

            var totals = calculator.Run();
            Assert.Null(totals);

            Assert.Equal(6M, data[0].summary[0]);
            Assert.Equal(6M, (data[0].items[0] as Group).summary[0]);
        }
Exemplo n.º 20
0
        static void DoTest(TestCase test, string filePath)
        {
            List <DataValue> expectedValues = GetExpectedResults(test.ExpectedResultsPath, test.TestId);

            ArchiveItem item = new ArchiveItem(test.DataPath, Assembly.GetExecutingAssembly(), test.DataPath);

            DataFileReader reader = new DataFileReader();

            reader.LoadConfiguration(null, item);
            reader.LoadHistoryData(null, item);

            AggregateConfiguration configuration = new AggregateConfiguration();

            configuration.PercentDataBad                = 100;
            configuration.PercentDataGood               = 100;
            configuration.TreatUncertainAsBad           = test.TreatUncertainAsBad;
            configuration.UseSlopedExtrapolation        = test.UseSlopedExtrapolation;
            configuration.UseServerCapabilitiesDefaults = false;

            DateTime startTime = DateTime.UtcNow;

            startTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, 0, 0, DateTimeKind.Utc);

            AggregateCalculator calculator = new AggregateCalculator(
                test.AggregateId,
                startTime.AddSeconds(0),
                startTime.AddSeconds(100),
                5000,
                test.Stepped,
                configuration);

            StringBuilder    buffer = new StringBuilder();
            List <DataValue> values = new List <DataValue>();

            foreach (DataRowView row in item.DataSet.Tables[0].DefaultView)
            {
                DataValue rawValue = (DataValue)row.Row[2];

                if (!calculator.QueueRawValue(rawValue))
                {
                    Utils.Trace("Oops!");
                    continue;
                }

                DataValue processedValue = calculator.GetProcessedValue(false);

                if (processedValue != null)
                {
                    values.Add(processedValue);
                }
            }

            for (DataValue processedValue = calculator.GetProcessedValue(true); processedValue != null; processedValue = calculator.GetProcessedValue(true))
            {
                values.Add(processedValue);
            }

            for (int ii = 0; ii < values.Count && ii < expectedValues.Count; ii++)
            {
                if (values[ii].SourceTimestamp != expectedValues[ii].SourceTimestamp)
                {
                    Utils.Trace("Wrong Status Timestamp");
                    continue;
                }

                if (values[ii].StatusCode != expectedValues[ii].StatusCode)
                {
                    Utils.Trace("Wrong Status Code");
                    continue;
                }

                if (StatusCode.IsNotBad(values[ii].StatusCode))
                {
                    double value1 = Math.Round(Convert.ToDouble(values[ii].Value), 4);
                    double value2 = Math.Round(Convert.ToDouble(expectedValues[ii].Value), 4);

                    if (value1 != value2)
                    {
                        Utils.Trace("Wrong Value");
                        continue;
                    }
                }
            }

            foreach (DataValue processedValue in values)
            {
                buffer.Append(processedValue.SourceTimestamp.ToString("HH:mm:ss"));
                buffer.Append(", ");
                buffer.Append(processedValue.WrappedValue);
                buffer.Append(", ");
                buffer.Append(new StatusCode(processedValue.StatusCode.CodeBits));
                buffer.Append(", ");
                buffer.Append(processedValue.StatusCode.AggregateBits);
                buffer.Append("\r\n");
            }

            // write to the file.
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                writer.Write(buffer.ToString());
            }
        }
        public void IgnoreGroupSummaryIfNoGroups() {
            var data = new object[] { 1 };

            var calculator = new AggregateCalculator<int>(data, new DefaultAccessor<int>(), null, new[] {
                new SummaryInfo { Selector = "ignore me", SummaryType = "ignore me" }
            });

            calculator.Run();
        }