Exemple #1
0
        public PivotLevel ComputePivot(OrQuery filter, IEnumerable <int> axis, IEnumerable <int> values)
        {
            var result = new PivotLevel();

            try
            {
                Parallel.ForEach(CacheClients, client =>
                {
                    var pivot = client.ComputePivot(filter, axis, values);
                    lock (result)
                    {
                        result.MergeWith(pivot);
                    }
                });
            }
            catch (AggregateException e)
            {
                if (e.InnerException != null)
                {
                    throw e.InnerException;
                }
            }

            return(result);
        }
        public void ComputePivotWithMultipleAxis()
        {
            var schema = TypedSchemaFactory.FromType(typeof(Order));

            var order1 = new Order
            {
                Amount   = 123.45, Date = DateTimeOffset.Now, Category = "geek", ClientId = 101, ProductId = 401,
                Id       = Guid.NewGuid(),
                Quantity = 2
            };

            var order2 = new Order
            {
                Amount   = 123.45, Date = DateTimeOffset.Now, Category = "sf", ClientId = 101, ProductId = 401,
                Id       = Guid.NewGuid(),
                Quantity = 2
            };

            var order3 = new Order
            {
                Amount   = 14.5, Date = DateTimeOffset.Now, Category = "geek", ClientId = 101, ProductId = 402,
                Id       = Guid.NewGuid(),
                Quantity = 2
            };

            var packed1 = PackedObject.Pack(order1, schema);
            var packed2 = PackedObject.Pack(order2, schema);
            var packed3 = PackedObject.Pack(order3, schema);


            // first test with one single axis (Category index = 3)
            var pivot = new PivotLevel();

            pivot.AggregateOneObject(packed1, new List <int> {
                3
            }, new List <int> {
                1, 2
            });
            pivot.AggregateOneObject(packed2, new List <int> {
                3
            }, new List <int> {
                1, 2
            });
            pivot.AggregateOneObject(packed3, new List <int> {
                3
            }, new List <int> {
                1, 2
            });


            // Amount and Quantity should be aggregated
            Assert.AreEqual(2, pivot.AggregatedValues.Count);

            var agg = pivot.AggregatedValues.First(v => v.ColumnName == "Amount");

            Assert.AreEqual(3, agg.Count);
            Assert.AreEqual(order1.Amount + order2.Amount + order3.Amount, agg.Sum);

            Assert.IsTrue(pivot.Children.Keys.All(k => k.KeyName == "Category"));
            Assert.IsTrue(pivot.Children.Values.All(v => v.AxisValue.KeyName == "Category"));

            var geek = pivot.Children.Values.First(p => p.AxisValue.StringValue == "geek");

            Assert.AreEqual(2, geek.AggregatedValues.Count);

            // then with two axis

            pivot = new PivotLevel();

            pivot.AggregateOneObject(packed1, new List <int> {
                3, 4
            }, new List <int> {
                1, 2
            });
            pivot.AggregateOneObject(packed2, new List <int> {
                3, 4
            }, new List <int> {
                1, 2
            });
            pivot.AggregateOneObject(packed3, new List <int> {
                3, 4
            }, new List <int> {
                1, 2
            });

            Console.WriteLine(pivot.ToString());

            var geek1 = pivot.Children.Values.First(p => p.AxisValue.StringValue == "geek");

            Assert.AreEqual(2, geek1.AggregatedValues.Count);
            Assert.AreEqual(2, geek1.Children.Count);


            // check pivot merging

            // a new category
            var order4 = new Order
            {
                Amount   = 66.5, Date = DateTimeOffset.Now, Category = "student", ClientId = 101, ProductId = 405,
                Id       = Guid.NewGuid(),
                Quantity = 1
            };

            var packed4 = PackedObject.Pack(order4, schema);

            var pivot1 = new PivotLevel();

            pivot1.AggregateOneObject(packed1, new List <int> {
                3, 4
            }, new List <int> {
                1, 2
            });
            pivot1.AggregateOneObject(packed2, new List <int> {
                3, 4
            }, new List <int> {
                1, 2
            });
            pivot1.AggregateOneObject(packed3, new List <int> {
                3, 4
            }, new List <int> {
                1, 2
            });

            var pivot2 = new PivotLevel();

            pivot2.AggregateOneObject(packed1, new List <int> {
                3, 4
            }, new List <int> {
                1, 2
            });
            pivot2.AggregateOneObject(packed3, new List <int> {
                3, 4
            }, new List <int> {
                1, 2
            });
            pivot2.AggregateOneObject(packed4, new List <int> {
                3, 4
            }, new List <int> {
                1, 2
            });

            pivot1.MergeWith(pivot2);

            Console.WriteLine(pivot1);

            // check that an aggregate is equal to the sum of the children
            var sum1 = pivot1.AggregatedValues.First(v => v.ColumnName == "Amount").Sum;
            var sum2 = pivot1.Children.Sum(c => c.Value.AggregatedValues.First(v => v.ColumnName == "Amount").Sum);

            Assert.AreEqual(sum1, sum2);
        }