Ejemplo n.º 1
0
        public void PivotData_3D()
        {
            var pvtData = new PivotData(new string[] { "a", "year", "month" },
                                        new CountAggregatorFactory());

            pvtData.ProcessData(SampleGenerator(100000), GetRecordValue);

            Assert.Equal(50000, Convert.ToInt32(
                             pvtData[pvtData.GetDimensionKeys()[0][0], Key.Empty, Key.Empty].Value));

            Assert.Equal(100000, Convert.ToInt32(
                             pvtData[Key.Empty, pvtData.GetDimensionKeys()[1][0], Key.Empty].Value));

            Assert.Equal(30240, Convert.ToInt32(
                             pvtData[Key.Empty, Key.Empty, pvtData.GetDimensionKeys()[2][0]].Value));

            Assert.Equal(100000, Convert.ToInt32(
                             pvtData[Key.Empty, Key.Empty, Key.Empty].Value));

            // incremental processing
            pvtData.ProcessData(SampleGenerator(10000), GetRecordValue);
            Assert.Equal(55000, Convert.ToInt32(
                             pvtData[pvtData.GetDimensionKeys()[0][0], Key.Empty, Key.Empty].Value));
            Assert.Equal(40240, Convert.ToInt32(
                             pvtData[Key.Empty, Key.Empty, pvtData.GetDimensionKeys()[2][0]].Value));

            Assert.Equal(110000, Convert.ToInt32(
                             pvtData[Key.Empty, Key.Empty, Key.Empty].Value));
        }
Ejemplo n.º 2
0
        public void PivotData_GetDimensionKeys()
        {
            var testData = generateData();
            var pvtData  = new PivotData(new string[] { "name", "qty", "i" }, new CountAggregatorFactory(), testData);

            var dimKeys = pvtData.GetDimensionKeys();

            Assert.Equal(3, dimKeys.Length);

            // check "i" dim keys
            Assert.Equal(1, dimKeys[2].Length);
            Assert.Equal(2, dimKeys[2][0]);

            // check "i" dim keys
            Assert.Equal(10, dimKeys[1].Length);

            var onlyOneDimKeys = pvtData.GetDimensionKeys(new [] { "i" });

            Assert.Equal(1, onlyOneDimKeys.Length);
            Assert.Equal(1, onlyOneDimKeys[0].Length);

            // check custom comparers
            var qtyDimKeys = pvtData.GetDimensionKeys(new [] { "i", "qty" },
                                                      new IComparer <object> [] {
                null,                          //use default for i
                NaturalSortKeyComparer.ReverseInstance
            });

            Assert.Equal(9, qtyDimKeys[1][0]);
        }
Ejemplo n.º 3
0
        public void PivotData_Merge()
        {
            var pvtData = new PivotData(new string[] { "a", "year", "month" },
                                        new AverageAggregatorFactory("i"));

            var pvtData1 = new PivotData(new string[] { "a", "year", "month" },
                                         new AverageAggregatorFactory("i"));
            var pvtData2 = new PivotData(new string[] { "a", "year", "month" },
                                         new AverageAggregatorFactory("i"));
            var pvtData3 = new PivotData(new string[] { "a", "b" }, new CountAggregatorFactory());

            Assert.Throws <ArgumentException>(() => {
                pvtData1.Merge(pvtData3);
            });

            pvtData.ProcessData(SampleGenerator(20000), GetRecordValue);
            pvtData1.ProcessData(SampleGenerator(10000), GetRecordValue);
            pvtData2.ProcessData(SampleGenerator(10000, 10000), GetRecordValue);

            pvtData1.Merge(pvtData2);

            foreach (var v in pvtData.AllValues)
            {
                var aggr       = pvtData[v.Key];
                var aggrMerged = pvtData1[v.Key];
                Assert.Equal(aggr.Count, aggrMerged.Count);
                Assert.Equal(aggr.Value, aggrMerged.Value);
            }
            Assert.Equal(pvtData.GetDimensionKeys()[0].Length, pvtData1.GetDimensionKeys()[0].Length);
            Assert.Equal(pvtData.GetDimensionKeys()[1].Length, pvtData1.GetDimensionKeys()[1].Length);
            Assert.Equal(pvtData.GetDimensionKeys()[2].Length, pvtData1.GetDimensionKeys()[2].Length);
        }
Ejemplo n.º 4
0
        public void PivotData_2D()
        {
            var testData = generateData();

            var pvtData1 = new PivotData(new string[] { "name", "qty" }, new CountAggregatorFactory(), testData);

            Assert.Equal(2, pvtData1.GetDimensionKeys()[0].Length);
            Assert.Equal(10, pvtData1.GetDimensionKeys()[1].Length);
            foreach (var cKey in pvtData1.GetDimensionKeys()[0])
            {
                foreach (var rKey in pvtData1.GetDimensionKeys()[1])
                {
                    var v = pvtData1[cKey, rKey];
                    if (v.Count > 0)
                    {
                        Assert.Equal(100, Convert.ToInt32(v.Value));
                    }
                }
            }


            var pvtData = new PivotData(new string[] { "name", "date" }, new SumAggregatorFactory("i"), testData);

            Assert.Equal(2, pvtData.GetDimensionKeys()[0].Length);
            Assert.Equal(42, pvtData.GetDimensionKeys()[1].Length);

            var rowTest0Totals = new ValueKey("Test0", Key.Empty);

            Assert.Equal(1000M, pvtData[rowTest0Totals].Value);

            // calc test
            var calcData = new object[5][] {
                new object[] { "A", 10, 50 },
                new object[] { "A", 15, 40 },
                new object[] { "B", 20, 50 },
                new object[] { "B", 25, 60 },
                new object[] { "C", 10, 0 }
            };
            Func <object, string, object> getVal = (r, f) => {
                return(((object[])r)[Convert.ToInt32(f)]);
            };
            var countPvtData = new PivotData(new string[] { "0", "1" }, new CountAggregatorFactory());

            countPvtData.ProcessData(calcData, getVal);
            Assert.Equal(2, Convert.ToInt32(countPvtData["A", Key.Empty].Value));
            Assert.Equal(1, Convert.ToInt32(countPvtData["C", Key.Empty].Value));

            var avgPvtData = new PivotData(new string[] { "0", "1" }, new AverageAggregatorFactory("2"));

            avgPvtData.ProcessData(calcData, getVal);
            Assert.Equal(45M, avgPvtData["A", Key.Empty].Value);
            Assert.Equal(0M, avgPvtData["C", Key.Empty].Value);
            Assert.Equal(25M, avgPvtData[Key.Empty, 10].Value);
        }
Ejemplo n.º 5
0
        protected void Page_Load(object sender, EventArgs e)
        {
            PvtData = GetDataCube();

            if (!IsPostBack)
            {
                // bind years filter items
                var yearKeys = PvtData.GetDimensionKeys(new[] { "Order Year" })[0];
                filterYear.DataSource = yearKeys.Select(y => new ListItem(y.ToString(), y.ToString())).ToArray();
                filterYear.DataBind();
            }
        }
Ejemplo n.º 6
0
        public void PerfTest()
        {
            var sw = new Stopwatch();

            sw.Start();
            var pvtData = new PivotData(
                new string[] { "year", "month", "a" },
                new CompositeAggregatorFactory(new CountAggregatorFactory(), new SumAggregatorFactory("i")),
                SampleGenerator(5000000),
                GetRecordValue, true);

            sw.Stop();
            Console.WriteLine("ProcessData: {0}", sw.Elapsed);
            sw.Restart();
            var dimKeys = pvtData.GetDimensionKeys();

            sw.Stop();
            Console.WriteLine("GetDimensionKeys: {0}", sw.Elapsed);

            Assert.Equal(2500000, Convert.ToInt32(pvtData[Key.Empty, Key.Empty, dimKeys[2][0]].AsComposite().Aggregators[0].Value));
            Assert.Equal(470880, Convert.ToInt32(pvtData[2015, Key.Empty, Key.Empty].AsComposite().Aggregators[0].Value));
        }
Ejemplo n.º 7
0
        //This takes a DataTable and returns a PivotTable on a given field (var_to_process) by a list of Rows and Cols
        public static PivotTable DataTable_to_PivotTable(DataTable dtStacked, List <string> pivotRows, List <string> pivotCols, string fldname, List <AggrFx> requested_stats)
        {
            if (dtStacked.Rows.Count > 0)
            {
                List <string> keepCols = new List <string>();

                keepCols.AddRange(pivotRows);
                keepCols.AddRange(pivotCols);
                keepCols.Add(fldname);


                IAggregatorFactory[] aggs = new IAggregatorFactory[requested_stats.Count];

                CountAggregatorFactory       aggrN       = new CountAggregatorFactory();
                AverageAggregatorFactory     aggrM       = new AverageAggregatorFactory(fldname);
                VarianceAggregatorFactory    aggrSD      = new VarianceAggregatorFactory(fldname, VarianceAggregatorValueType.StandardDeviation);
                MinAggregatorFactory         aggrMin     = new MinAggregatorFactory(fldname);
                MaxAggregatorFactory         aggrMax     = new MaxAggregatorFactory(fldname);
                SumAggregatorFactory         aggrSum     = new SumAggregatorFactory(fldname);
                ConcatAggregatorFactory      aggrConcat  = new ConcatAggregatorFactory(fldname);
                CountUniqueAggregatorFactory aggrNunq    = new CountUniqueAggregatorFactory(fldname);
                Concat2AggregatorFactory     aggrConcat2 = new Concat2AggregatorFactory(fldname);


                int counter = 0;
                foreach (AggrFx s in requested_stats)
                {
                    if (s == AggrFx.M)
                    {
                        aggs[counter] = aggrM;
                    }
                    else if (s == AggrFx.SD)
                    {
                        aggs[counter] = aggrSD;
                    }
                    else if (s == AggrFx.N)
                    {
                        aggs[counter] = aggrN;
                    }
                    else if (s == AggrFx.Nunq)
                    {
                        aggs[counter] = aggrNunq;
                    }
                    else if (s == AggrFx.Min)
                    {
                        aggs[counter] = aggrMin;
                    }
                    else if (s == AggrFx.Max)
                    {
                        aggs[counter] = aggrMax;
                    }
                    else if (s == AggrFx.Sum)
                    {
                        aggs[counter] = aggrSum;
                    }
                    else if (s == AggrFx.Concat)
                    {
                        aggs[counter] = aggrConcat;
                    }
                    else if (s == AggrFx.Concat2)
                    {
                        aggs[counter] = aggrConcat2;
                    }
                    counter++;
                }


                if (aggs.Length > 1)
                {
                    CompositeAggregatorFactory compositeFactory = new CompositeAggregatorFactory(aggs);

                    var cube = new PivotData(keepCols.ToArray(), compositeFactory);
                    cube.ProcessData(new DataTableReader(dtStacked));

                    var allKeys = cube.GetDimensionKeys(); // returns array of keys for each dimension

                    var pivotTable = new PivotTable(
                        pivotRows.ToArray(), // row dimension(s)
                        pivotCols.ToArray(), // column dimension(s)
                        cube);
                    return(pivotTable);
                }
                else
                {
                    var cube = new PivotData(keepCols.ToArray(), aggs[0]);
                    cube.ProcessData(new DataTableReader(dtStacked));

                    var allKeys = cube.GetDimensionKeys(); // returns array of keys for each dimension

                    var pivotTable = new PivotTable(
                        pivotRows.ToArray(), // row dimension(s)
                        pivotCols.ToArray(), // column dimension(s)
                        cube);
                    return(pivotTable);
                }
            }
            else
            {
                return(null);
            }
        }