public void AddUsage(ICountedEntity <TEntityType> entity, TCountType measure, ICountedConfig countConfig, long increment) { ArgumentValidator.ThrowIfNull("entity", entity); ArgumentValidator.ThrowIfNull("countConfig", countConfig); CountTracker <TEntityType, TCountType> .EntityValue entityValue; if (this.entities.TryGetValue(entity.Name, out entityValue)) { entityValue.AddMeasure(measure, countConfig, increment); return; } if (!countConfig.IsPromotable) { this.AddEntityAndMeasure(entity, measure, countConfig, increment); return; } if (this.entities.Any((KeyValuePair <IEntityName <TEntityType>, CountTracker <TEntityType, TCountType> .EntityValue> e) => e.Value.Entity.GroupName.Equals(entity.GroupName))) { this.AddEntityAndMeasure(entity, measure, countConfig, increment); return; } CountTracker <TEntityType, TCountType> .EntityKey entityKey = new CountTracker <TEntityType, TCountType> .EntityKey(entity.GroupName, measure); long num = this.unpromotedMeasures.AddOrUpdate(entityKey, (CountTracker <TEntityType, TCountType> .EntityKey k) => increment, (CountTracker <TEntityType, TCountType> .EntityKey k, long v) => v + increment); if (num > (long)countConfig.MinActivityThreshold) { this.AddEntityAndMeasure(entity, measure, countConfig, increment); this.unpromotedMeasures.TryRemove(entityKey, out num); this.tracer.TraceDebug <IEntityName <TEntityType>, TCountType, int>((long)this.GetHashCode(), "Promoted entity {0} and measure {1} for crossing threshold {2}", entityKey.Entity, entityKey.Measure, countConfig.MinActivityThreshold); this.perfCounters.MeasurePromoted(measure); } }
public void RaiseEvent(ICountedEntity <TEntityType> countedEntity) { if (!this.measureBased && (this.entity == null || this.entity.Equals(countedEntity))) { this.entityDelegate(countedEntity); } }
public CountedEntityWrapper(ICountedEntity <TEntityType> entity, ICountTracker <TEntityType, TCountType> tracker) { ArgumentValidator.ThrowIfNull("entity", entity); ArgumentValidator.ThrowIfNull("tracker", tracker); this.Entity = entity; this.tracker = tracker; }
public EntityValue(ICountedEntity <TEntityType> entity, ICountTrackerDiagnostics <TEntityType, TCountType> perfCounters, Func <DateTime> timeProvider) { this.entity = entity; this.perfCounters = perfCounters; this.timeProvider = timeProvider; this.measures = new Count <TEntityType, TCountType> [CountTracker <TEntityType, TCountType> .EntityValue.countTypeToIndexMap.Count]; this.lastAccessTime = this.timeProvider(); }
public Task AddUsageAsync(ICountedEntity <TEntityType> entity, TCountType measure, ICountedConfig countConfig, long increment) { ArgumentValidator.ThrowIfNull("entity", entity); ArgumentValidator.ThrowIfNull("countConfig", countConfig); return(Task.Factory.StartNew(delegate() { this.AddUsage(entity, measure, countConfig, increment); })); }
private CountTracker <TEntityType, TCountType> .EntityValue AddEntity(ICountedEntity <TEntityType> entity) { CountTracker <TEntityType, TCountType> .EntityValue result = this.entities.AddOrUpdate(entity.Name, delegate(IEntityName <TEntityType> e) { this.perfCounters.EntityAdded(entity); return(new CountTracker <TEntityType, TCountType> .EntityValue(entity, this.perfCounters, this.timeProvider)); }, (IEntityName <TEntityType> e, CountTracker <TEntityType, TCountType> .EntityValue v) => v); this.needsEntityCleanup = (this.entities.Count > this.config.MaxEntityCount); return(result); }
public bool TryGetEntityObject(ICountedEntity <TEntityType> entity, out ICountedEntityWrapper <TEntityType, TCountType> wrapper) { ArgumentValidator.ThrowIfNull("entity", entity); if (this.entities.ContainsKey(entity.Name)) { wrapper = new CountedEntityWrapper <TEntityType, TCountType>(entity, this); return(true); } wrapper = null; return(false); }
public IEnumerable <ICount <TEntityType, TCountType> > GetAllUsages(ICountedEntity <TEntityType> entity) { ArgumentValidator.ThrowIfNull("entity", entity); CountTracker <TEntityType, TCountType> .EntityValue source; if (this.entities.TryGetValue(entity.Name, out source)) { return(from c in source select new CountWrapper <TEntityType, TCountType>(c)); } return(null); }
private void RaiseEvent(MeteringEvent evt, ICountedEntity <TEntityType> filter) { List <CountTrackerDiagnostics <TEntityType, TCountType> .Subscriber> collection; if (this.TryGetSubscriber(evt, out collection)) { List <CountTrackerDiagnostics <TEntityType, TCountType> .Subscriber> list = new List <CountTrackerDiagnostics <TEntityType, TCountType> .Subscriber>(collection); foreach (CountTrackerDiagnostics <TEntityType, TCountType> .Subscriber subscriber in list) { subscriber.RaiseEvent(filter); } } }
public ICountedConfig GetConfig(ICountedEntity <TEntityType> entity, TCountType measure) { ArgumentValidator.ThrowIfNull("entity", entity); CountTracker <TEntityType, TCountType> .EntityValue entityValue; if (this.entities.TryGetValue(entity.Name, out entityValue)) { ICount <TEntityType, TCountType> usage = entityValue.GetUsage(measure); if (usage != null) { return(usage.Config); } } return(null); }
public Count(ICountedEntity <TEntityType> entity, ICountedConfig config, TCountType measure, Func <DateTime> timeProvider) { ArgumentValidator.ThrowIfNull("entity", entity); ArgumentValidator.ThrowIfNull("config", config); ArgumentValidator.ThrowIfNull("measure", measure); ArgumentValidator.ThrowIfNull("timeProvider", timeProvider); this.Entity = entity; this.Config = config; this.Measure = measure; this.timeProvider = timeProvider; this.lastAccessTime = this.timeProvider(); if (this.Config.TimeToLive != TimeSpan.Zero) { this.expirationTime = this.timeProvider().Add(this.Config.TimeToLive); } }
public IDictionary <TCountType, ICount <TEntityType, TCountType> > GetUsage(ICountedEntity <TEntityType> entity, TCountType[] measures) { ArgumentValidator.ThrowIfNull("entity", entity); ArgumentValidator.ThrowIfNull("measures", measures); CountTracker <TEntityType, TCountType> .EntityValue entityValue; if (this.entities.TryGetValue(entity.Name, out entityValue)) { IDictionary <TCountType, ICount <TEntityType, TCountType> > dictionary = new Dictionary <TCountType, ICount <TEntityType, TCountType> >(); foreach (TCountType tcountType in measures) { ICount <TEntityType, TCountType> usage = entityValue.GetUsage(tcountType); if (usage != null) { dictionary.Add(tcountType, usage); } } return(dictionary); } return(measures.ToDictionary((TCountType countType) => countType, (TCountType countType) => new EmptyCount <TEntityType, TCountType>(entity, countType))); }
public RollingCount(ICountedEntity <TEntityType> entity, IRollingCountConfig config, TCountType measure, Func <DateTime> timeProvider) : base(entity, config, measure, timeProvider) { this.rollingCountConfig = config; }
public RollingCount(ICountedEntity <TEntityType> entity, IRollingCountConfig config, TCountType measure) : this(entity, config, measure, () => DateTime.UtcNow) { }
public Task <IEnumerable <ICount <TEntityType, TCountType> > > GetAllUsagesAsync(ICountedEntity <TEntityType> entity) { ArgumentValidator.ThrowIfNull("entity", entity); return(Task <IEnumerable <ICount <TEntityType, TCountType> > > .Factory.StartNew(() => this.GetAllUsages(entity))); }
public void SubscribeTo(MeteringEvent evt, ICountedEntity <TEntityType> entity, Action <ICountedEntity <TEntityType> > entityDelegate) { this.AddSubscriber(evt, new CountTrackerDiagnostics <TEntityType, TCountType> .Subscriber(entityDelegate, entity)); }
public bool Equals(ICountedEntity <TEntityType> other) { return(other != null && this.GroupName.Equals(other.GroupName) && this.Name.Equals(other.Name)); }
public void EntityRemoved(ICountedEntity <TEntityType> entity) { this.RaiseEvent(MeteringEvent.EntityRemoved, entity); }
public AbsoluteCount(ICountedEntity <TEntityType> entity, IAbsoluteCountConfig config, TCountType measure) : this(entity, config, measure, () => DateTime.UtcNow) { }
public Subscriber(Action <ICountedEntity <TEntityType> > entityDelegate, ICountedEntity <TEntityType> entity) { this.entityDelegate = entityDelegate; this.entity = entity; this.measureBased = false; }
public Task <bool> SetUsageAsync(ICountedEntity <TEntityType> entity, TCountType measure, long value) { ArgumentValidator.ThrowIfNull("entity", entity); return(Task <bool> .Factory.StartNew(() => this.TrySetUsage(entity, measure, value))); }
public AbsoluteCount(ICountedEntity <TEntityType> entity, IAbsoluteCountConfig config, TCountType measure, Func <DateTime> timeProvider) : base(entity, config, measure, timeProvider) { this.absoluteConfig = config; }
public bool TrySetUsage(ICountedEntity <TEntityType> entity, TCountType measure, long value) { ArgumentValidator.ThrowIfNull("entity", entity); CountTracker <TEntityType, TCountType> .EntityValue entityValue; return(this.entities.TryGetValue(entity.Name, out entityValue) && entityValue.TrySetMeasure(measure, value)); }
public Task <IDictionary <TCountType, ICount <TEntityType, TCountType> > > GetUsageAsync(ICountedEntity <TEntityType> entity, TCountType[] measures) { ArgumentValidator.ThrowIfNull("entity", entity); ArgumentValidator.ThrowIfNull("measures", measures); return(Task <IDictionary <TCountType, ICount <TEntityType, TCountType> > > .Factory.StartNew(() => this.GetUsage(entity, measures))); }
private void AddEntityAndMeasure(ICountedEntity <TEntityType> entity, TCountType measure, ICountedConfig countConfig, long increment) { CountTracker <TEntityType, TCountType> .EntityValue entityValue = this.AddEntity(entity); entityValue.AddMeasure(measure, countConfig, increment); }
public static Count <TEntityType, TCountType> CreateCount <TEntityType, TCountType>(ICountedEntity <TEntityType> entity, TCountType measure, ICountedConfig config, Func <DateTime> timeProvider) where TEntityType : struct, IConvertible where TCountType : struct, IConvertible { if (config is IRollingCountConfig) { return(new RollingCount <TEntityType, TCountType>(entity, (IRollingCountConfig)config, measure, timeProvider)); } if (config is IAbsoluteCountConfig) { return(new AbsoluteCount <TEntityType, TCountType>(entity, (IAbsoluteCountConfig)config, measure, timeProvider)); } throw new InvalidOperationException("Need to create a config of a subtype class of ICountedConfig"); }
public EmptyCount(ICountedEntity <TEntityType> entity, TCountType measure) { ArgumentValidator.ThrowIfNull("entity", entity); this.Entity = entity; this.Measure = measure; }
public static Count <TEntityType, TCountType> CreateCount <TEntityType, TCountType>(ICountedEntity <TEntityType> entity, TCountType measure, ICountedConfig config) where TEntityType : struct, IConvertible where TCountType : struct, IConvertible { return(CountFactory.CreateCount <TEntityType, TCountType>(entity, measure, config, () => DateTime.UtcNow)); }
private EntityValue(ICountedEntity <TEntityType> entity, ICountTrackerDiagnostics <TEntityType, TCountType> perfCounters, Count <TEntityType, TCountType>[] measures, Func <DateTime> timeProvider) : this(entity, perfCounters, timeProvider) { this.measures = measures; }