public IAmbientStatistic GetOrAddStatistic(bool timeBased, string id, string description, bool resetIfAlreadyExists , long initialValue = 0 , AggregationTypes temporalAggregationTypes = AggregationTypes.Min | AggregationTypes.Average | AggregationTypes.Max , AggregationTypes spatialAggregationTypes = AggregationTypes.Min | AggregationTypes.Average | AggregationTypes.Max , AggregationTypes preferredTemporalAggregationType = AggregationTypes.Average , AggregationTypes preferredSpatialAggregationType = AggregationTypes.Average , MissingSampleHandling missingSampleHandling = MissingSampleHandling.LinearEstimation ) { IAmbientStatistic?statistic; if (resetIfAlreadyExists) { statistic = new Statistic(() => _statistics.TryRemove(id, out _), timeBased, id, description, initialValue, temporalAggregationTypes, spatialAggregationTypes, preferredTemporalAggregationType, preferredSpatialAggregationType, missingSampleHandling); _statistics.AddOrUpdate(id, statistic, (k, v) => statistic); } else { statistic = new Statistic(() => _statistics.TryRemove(id, out _), timeBased, id, description, initialValue, temporalAggregationTypes, spatialAggregationTypes, preferredTemporalAggregationType, preferredSpatialAggregationType, missingSampleHandling); statistic = _statistics.GetOrAdd(id, statistic) as IAmbientStatistic; // this *could* return something that is only an IAmbientStatisticReader! if (statistic == null) { throw new InvalidOperationException("The specified statistic identifier is already in use by a read-only statistic!"); } } return(statistic); }
private long _sampleValue; // interlocked public Statistic(Action removeRegistration, bool timeBased, string id, string description, long initialValue = 0 , AggregationTypes temporalAggregationTypes = AggregationTypes.Min | AggregationTypes.Average | AggregationTypes.Max , AggregationTypes spatialAggregationTypes = AggregationTypes.Min | AggregationTypes.Average | AggregationTypes.Max , AggregationTypes preferredTemporalAggregationType = AggregationTypes.Average , AggregationTypes preferredSpatialAggregationType = AggregationTypes.Average , MissingSampleHandling missingSampleHandling = MissingSampleHandling.LinearEstimation ) { _removeRegistration = removeRegistration; _timeBased = timeBased; _id = id; _description = description; _sampleValue = initialValue; TemporalAggregationTypes = temporalAggregationTypes; SpatialAggregationTypes = spatialAggregationTypes; PreferredTemporalAggregationType = preferredTemporalAggregationType; PreferredSpatialAggregationType = preferredSpatialAggregationType; MissingSampleHandling = missingSampleHandling; }
protected TNumber GetNumericValue <TModel, TNumber>(ODataQueryOptions QueryOptions, AggregationTypes aggregationType) where TNumber : IComparable { string keyWord = string.Empty; switch (aggregationType) { case AggregationTypes.Count: keyWord = "COUNT"; break; case AggregationTypes.Sum: keyWord = "SUM"; break; case AggregationTypes.Max: keyWord = "MAX"; break; default: break; } string column = this.GetSelection(QueryOptions); string condition = this.GetFilter(QueryOptions); var count = db.Database.SqlQuery <TNumber>("Select " + keyWord + "(" + column + ") From " + typeof(TModel).Name + " " + condition); return((TNumber)count.FirstOrDefault()); }