Example #1
0
        AggregatedValue CalculateOld(KeyValuePair <AggregationKey, ConcurrentBag <double> > kvp, DateTime d)
        {
            //int countBefore = kvp.Value.Count;
            try
            {
                if (kvp.Key.Date > d)
                {
                    foreach (var value in kvp.Value)
                    {
                        _events.GetOrAdd(kvp.Key, new ConcurrentBag <double>()).Add(value);
                    }
                    return(null);
                }

                var           av        = new AggregatedValue(kvp.Key);
                List <double> rawValues = kvp.Value.ToList();
                foreach (var aggregationOperation in _aggregationOperations)
                {
                    try
                    {
                        av.AddResult(aggregationOperation.Do(rawValues));
                    }
                    catch (InvalidOperationException)
                    {
                    }
                }
                //TODO dirty hack
                if (
                    _aggregationOperations.Any(
                        op =>
                        op.GetType() == typeof(PercentileAggregationOperation) ||
                        op.GetType() == typeof(DistributionGroupAggregationOperation)))
                {
                    av.AddRawValues(rawValues);
                }
                return(av);
                //_onAggregateCompleteAction(av);

                /*ThreadPool.QueueUserWorkItem(state =>
                 *                               {
                 *                                   Thread.Sleep(500);
                 *                                   int countAfter = kvp.Value.Count;
                 *                                   int i;
                 *                                   if (countBefore != countAfter)
                 *                                       i = 1;
                 *                               });
                 */
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
            return(null);
        }
 private static AggregatedValue ParseAggregatedValue(StoredCounter entry)
 {
     DateTime date = entry.Date;
     Dictionary<string, string> properties = entry.Props;
     AggregationKey ak = new AggregationKey(date, properties);
     AggregatedValue val = new AggregatedValue(ak);
     if (entry.Data.Sum != null) val.AddResult(new AggregationOperationResult(AggregationType.Sum, entry.Data.Sum.Value));
     if (entry.Data.Count != null) val.AddResult(new AggregationOperationResult(AggregationType.Count, entry.Data.Count.Value));
     if (entry.Data.Min != null) val.AddResult(new AggregationOperationResult(AggregationType.Min, entry.Data.Min.Min()));
     if (entry.Data.Max != null) val.AddResult(new AggregationOperationResult(AggregationType.Max, entry.Data.Max.Max()));
     if (entry.Data.Avg != null) val.AddResult(new AggregationOperationResult(AggregationType.Avg, entry.Data.Avg.Average()));
     if (entry.Data.RawValues != null) val.AddRawValues(entry.Data.RawValues);
     return val;
 }
        public static IEnumerable <AggregatedValue> Compact(this IEnumerable <AggregatedValue> source)
        {
            Dictionary <AggregationKey, List <AggregatedValue> > dic = new Dictionary <AggregationKey, List <AggregatedValue> >();

            foreach (var aggregatedValue in source)
            {
                var aggregationKey = new AggregationKey(aggregatedValue.Date, aggregatedValue.Props);
                if (!dic.ContainsKey(aggregationKey))
                {
                    dic[aggregationKey] = new List <AggregatedValue>();
                }
                dic[aggregationKey].Add(aggregatedValue);
            }
            foreach (var kvp in dic)
            {
                AggregatedValue result = new AggregatedValue(kvp.Key);
                if (kvp.Value.Any(v => v.Sum.HasValue))
                {
                    result.AddResult(new AggregationOperationResult(AggregationType.Sum, kvp.Value.Where(v => v.Sum.HasValue).Sum(v => v.Sum.Value)));
                }
                if (kvp.Value.Any(v => v.Count.HasValue))
                {
                    result.AddResult(new AggregationOperationResult(AggregationType.Count, kvp.Value.Where(v => v.Count.HasValue).Sum(v => v.Count.Value)));
                }
                if (kvp.Value.Any(v => v.Min.HasValue))
                {
                    result.AddResult(new AggregationOperationResult(AggregationType.Min, kvp.Value.Where(v => v.Min.HasValue).Min(v => v.Min.Value)));
                }
                if (kvp.Value.Any(v => v.Max.HasValue))
                {
                    result.AddResult(new AggregationOperationResult(AggregationType.Max, kvp.Value.Where(v => v.Max.HasValue).Max(v => v.Max.Value)));
                }
                if (kvp.Value.Any(v => v.Avg.HasValue))
                {
                    result.AddResult(new AggregationOperationResult(AggregationType.Avg, kvp.Value.Where(v => v.Avg.HasValue).Average(v => v.Avg.Value)));
                }
                if (result.Count != 0 && (result.Max == 0.0))
                {
                    Console.WriteLine("!");
                }

                yield return(result);
            }
        }
        AggregatedValue CalculateOld(KeyValuePair<AggregationKey, ConcurrentBag<double>> kvp, DateTime d)
        {
            //int countBefore = kvp.Value.Count;
            try
            {
                if (kvp.Key.Date > d)
                {
                    foreach (var value in kvp.Value)
                    {
                        _events.GetOrAdd(kvp.Key, new ConcurrentBag<double>()).Add(value);
                    }
                    return null;
                }

                var av = new AggregatedValue(kvp.Key);
                List<double> rawValues = kvp.Value.ToList();
                foreach (var aggregationOperation in _aggregationOperations)
                {
                    try
                    {
                        av.AddResult(aggregationOperation.Do(rawValues));
                    }
                    catch (InvalidOperationException)
                    {
                    }

                }
                //TODO dirty hack
                if (
                    _aggregationOperations.Any(
                        op =>
                        op.GetType() == typeof (PercentileAggregationOperation) ||
                        op.GetType() == typeof (DistributionGroupAggregationOperation)))
                    av.AddRawValues(rawValues);
                return av;
                //_onAggregateCompleteAction(av);

                /*ThreadPool.QueueUserWorkItem(state =>
                                                 {
                                                     Thread.Sleep(500);
                                                     int countAfter = kvp.Value.Count;
                                                     int i;
                                                     if (countBefore != countAfter)
                                                         i = 1;
                                                 });
*/

            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
            return null;
        }
 private static AggregatedValue ParseAggregatedValue(BsonDocument entry)
 {
     DateTime date = entry["date"].AsDateTime;
     Dictionary<string, string> properties = !entry.Contains("props")? new Dictionary<string, string>() : 
         entry["props"].AsBsonDocument.Where(kvp => kvp.Name != "pCnt").ToDictionary(kvp => kvp.Name,
                                                                                     kvp => kvp.Value.AsString);
     AggregationKey ak = new AggregationKey(date, properties);
     AggregatedValue val = new AggregatedValue(ak);
     foreach (var dataElement in entry["data"].AsBsonDocument)
     {
         AggregationType at;
         if (AggregationTypeParser.TryParse(dataElement.Name, out at))
         {
             switch (at)
             {
                 case AggregationType.Sum:
                 case AggregationType.Count:
                     val.AddResult(new AggregationOperationResult(at, dataElement.Value.AsDouble));
                     break;
                 case AggregationType.Min:
                     val.AddResult(new AggregationOperationResult(at,
                                                                  dataElement.Value.AsBsonArray.Select(
                                                                      v => v.AsDouble).Min()));
                     break;
                 case AggregationType.Max:
                     val.AddResult(new AggregationOperationResult(at,
                                                                  dataElement.Value.AsBsonArray.Select(
                                                                      v => v.AsDouble).Max()));
                     break;
                 case AggregationType.Avg:
                     val.AddResult(new AggregationOperationResult(at,
                                                                  dataElement.Value.AsBsonArray.Select(
                                                                      v => v.AsDouble).Average()));
                     break;
             }
         }
         else
         {
             if (dataElement.Name == "Raw" && dataElement.Value.AsBsonArray.Count != 0)
                 val.AddRawValues(dataElement.Value.AsBsonArray.Select(v => v.AsDouble).ToList());
         }
     }
     return val;
 }
        public void Save (AggregatedValue val)
        {
            MongoCollection<BsonDocument> items = MongoDb.GetCollection("countersData");
            UpdateBuilder updateBuilder = new UpdateBuilder();
            if (val.Count.HasValue)
                updateBuilder.Inc("data.Count", val.Count.Value);
            if (val.Sum.HasValue)
                updateBuilder.Inc("data.Sum", val.Sum.Value);
            if (val.Min.HasValue)
                updateBuilder.Push("data.Min", val.Min.Value);
            if (val.Max.HasValue)
                updateBuilder.Push("data.Max", val.Max.Value);
            if (val.Avg.HasValue)
                updateBuilder.Push("data.Avg", val.Avg.Value);
            if (val.Percentiles!=null || val.DistributionGroups!=null)
                updateBuilder.PushAll("data.Raw", val.RawValues.Select(v =>new BsonDouble(v)));

            IMongoQuery q = Query.And(Query.EQ("date", val.Date), Query.EQ("props.pCnt", val.Props.Count));
            foreach (var prop in val.Props)
            {
                q = Query.And(q, Query.EQ("props."+prop.Key, prop.Value));
            }
            items.Update(q, updateBuilder,UpdateFlags.Upsert,SafeMode.True);
        }