Exemplo n.º 1
0
        private void WrapAggregationAction(TInput row)
        {
            object key = GroupingFunc?.Invoke(row) ?? string.Empty;

            if (!AggregationData.ContainsKey(key))
            {
                AddRecordToDict(key);
            }

            TOutput currentAgg = AggregationData[key];

            AggregationAction.Invoke(row, currentAgg);
        }
Exemplo n.º 2
0
        private void WrapAggregationAction(TInput row)
        {
            object key = ClassificationProperty?.Invoke(row) ?? "AggregateAll";

            if (!AggregationData.ContainsKey(key))
            {
                AddRecordToDict(key);
            }

            TOutput currentAgg = AggregationData[key];

            AggregationAction.Invoke(row, currentAgg);
        }
Exemplo n.º 3
0
        private void WrapAggregationAction(TInput row)
        {
            try
            {
                object key = GroupingFunc?.Invoke(row) ?? string.Empty;

                if (!AggregationData.ContainsKey(key))
                {
                    AddRecordToDict(key);
                }

                TOutput currentAgg = AggregationData[key];
                AggregationAction.Invoke(row, currentAgg);
            }
            catch (Exception e)
            {
                ThrowOrRedirectError(e, ErrorSource.ConvertErrorData <TInput>(row));
                return;
            }
        }
Exemplo n.º 4
0
        private static void AssertAggregationResult(AggregationResult[] results, AggregationAction action, params TestFact[] orderedFacts)
        {
            Assert.Equal(1, results.Length);

            var result = results[0];

            Assert.Equal(action, result.Action);

            var aggregate = (IEnumerable <TestFact>)result.Aggregate;

            Assert.Equal(action == AggregationAction.Added ? null : aggregate, result.Previous);

            var actualAggregateFacts = aggregate.ToArray();
            var actualSourceFacts    = result.Source.Select(f => (TestFact)f.Value).ToArray();

            Assert.Equal(orderedFacts.Length, actualSourceFacts.Length);
            for (var i = 0; i < orderedFacts.Length; i++)
            {
                Assert.Equal(orderedFacts[i], actualAggregateFacts[i]);
                Assert.Equal(orderedFacts[i], actualSourceFacts[i]);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Handles key repetition
 /// </summary>
 private bool ShouldHandle(Func <TInputHelper, bool> script, AggregationAction aggrAction, double timeDelta)
 {
     if (script(inputHelper))
     {
         scriptHasEvaluated = true;
         if (this.aggrAction != aggrAction)
         {
             keyRepeatTimer  = KeyRepetition.KeyRepeatStartDuration;
             this.aggrAction = aggrAction;
             return(true);
         }
         if (this.aggrAction == aggrAction)
         {
             keyRepeatTimer -= timeDelta;
             if (keyRepeatTimer <= 0.0f)
             {
                 keyRepeatTimer += KeyRepetition.KeyRepeatDuration;
                 return(true);
             }
             return(false);
         }
     }
     return(false);
 }
Exemplo n.º 6
0
        private static void AssertAggregationResult <TKey>(AggregationResult[] results, AggregationAction action, Func <TestFact, TKey> keySelector, params TKey[] orderedKeys)
        {
            Assert.Equal(1, results.Length);

            var result       = results[0];
            var distinctKeys = orderedKeys.Distinct().ToArray();

            Assert.Equal(action, result.Action);

            var aggregate = (IEnumerable <TestFact>)result.Aggregate;

            Assert.Equal(action == AggregationAction.Added ? null : aggregate, result.Previous);

            var actualAggregateKeys = aggregate.Select(keySelector).ToArray();
            var actualSourceKeys    = result.Source.Select(f => keySelector((TestFact)f.Value)).ToArray();

            Assert.Equal(orderedKeys.Length, actualSourceKeys.Length);
            for (var i = 0; i < orderedKeys.Length; i++)
            {
                Assert.Equal(orderedKeys[i], actualAggregateKeys[i]);
                Assert.Equal(orderedKeys[i], actualSourceKeys[i]);
            }
        }
Exemplo n.º 7
0
 private static void AssertAggregationResult(AggregationResult[] results, AggregationAction action, params string[] orderedKeys)
 {
     AssertAggregationResult(results, action, f => f.Value, orderedKeys);
 }
Exemplo n.º 8
0
 private static void AssertAggregationResult(AggregationResult[] results, AggregationAction action, params int[] orderedKeys)
 {
     AssertAggregationResult(results, action, f => f.Id, orderedKeys);
 }
Exemplo n.º 9
0
 private AggregationResult(AggregationAction action, object aggregate)
 {
     Action    = action;
     Aggregate = aggregate;
 }
Exemplo n.º 10
0
        public static ICube Pivot(this ICube cube, string[] fieldsToAggregateBy, AggregationAction aggregationAction)
        {
            //for now, aggregate only works on numerical fields and performs a sum
            foreach (var fieldToAggregateBy in fieldsToAggregateBy)
            {
                if (!cube.DataTypes.ContainsKey(fieldToAggregateBy))
                {
                    throw new Exception($"Cannot aggregate on field {fieldToAggregateBy} as it is not present");
                }
            }

            var types = cube.DataTypes.Keys.ToList();
            var ixs   = fieldsToAggregateBy.Select(f => types.IndexOf(f)).ToArray();

            var rows           = cube.GetAllRows();
            var distinctValues = rows.Select(x => string.Join("~", ixs.Select(ix => x.MetaData[ix]?.ToString() ?? string.Empty))).Distinct();

            var outCube = new ResultCube();
            var oT      = new Dictionary <string, Type>();

            foreach (var fieldToAggregateBy in fieldsToAggregateBy)
            {
                oT.Add(fieldToAggregateBy, cube.DataTypes[fieldToAggregateBy]);
            }
            outCube.Initialize(oT);
            var aggData      = new Dictionary <string, double>();
            var aggDataCount = new Dictionary <string, int>();
            var metaDict     = new Dictionary <string, object[]>();

            foreach (var row in rows)
            {
                var rowKey = string.Join("~", ixs.Select(i => row.MetaData[i]?.ToString() ?? string.Empty));
                if (!aggData.ContainsKey(rowKey))
                {
                    if (aggregationAction == AggregationAction.Min)
                    {
                        aggData[rowKey] = double.MaxValue;
                    }
                    else if (aggregationAction == AggregationAction.Max)
                    {
                        aggData[rowKey] = double.MinValue;
                    }
                    else
                    {
                        aggData[rowKey] = 0;
                    }
                    aggDataCount[rowKey] = 0;
                    var filetedMetaData = new object[ixs.Length];
                    for (var i = 0; i < ixs.Length; i++)
                    {
                        filetedMetaData[i] = row.MetaData[ixs[i]];
                    }
                    metaDict[rowKey] = filetedMetaData;
                }
                switch (aggregationAction)
                {
                case AggregationAction.Sum:
                    aggData[rowKey] += row.Value;
                    break;

                case AggregationAction.Average:
                    aggData[rowKey] += row.Value;
                    aggDataCount[rowKey]++;
                    break;

                case AggregationAction.Min:
                    aggData[rowKey] = System.Math.Min(aggData[rowKey], row.Value);
                    break;

                case AggregationAction.Max:
                    aggData[rowKey] = System.Math.Max(aggData[rowKey], row.Value);
                    break;
                }
            }

            //final post-processing for average
            foreach (var rowKey in aggData.Keys.ToList())
            {
                var rowDict = new Dictionary <string, object>();
                if (aggregationAction == AggregationAction.Average)
                {
                    aggData[rowKey] /= aggDataCount[rowKey];
                }

                outCube.AddRow(metaDict[rowKey], aggData[rowKey]);
            }


            return(outCube);
        }
Exemplo n.º 11
0
 public static ICube Pivot(this ICube cube, string fieldToAggregateBy, AggregationAction aggregationAction)
 {
     return(cube.Pivot(new[] { fieldToAggregateBy }, aggregationAction));
 }
Exemplo n.º 12
0
 private AggregationResult(AggregationAction action, object aggregate)
 {
     _action = action;
     _aggregate = aggregate;
 }
Exemplo n.º 13
0
 private AggregationResult(AggregationAction action, object aggregate, object previous)
 {
     Action    = action;
     Aggregate = aggregate;
     Previous  = previous;
 }
Exemplo n.º 14
0
 public AggregateList(AggregationAction action)
 {
     Action = action;
 }
Exemplo n.º 15
0
 AggregationResult(AggregationAction action, object aggregate)
 {
     _action    = action;
     _aggregate = aggregate;
 }