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); }
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); }