Esempio n. 1
0
        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;
 }
Esempio n. 4
0
 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();
 }
Esempio n. 5
0
 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);
     }));
 }
Esempio n. 6
0
 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);
 }
Esempio n. 7
0
 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);
 }
Esempio n. 8
0
 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);
                }
            }
        }
Esempio n. 10
0
 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);
 }
Esempio n. 11
0
 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);
     }
 }
Esempio n. 12
0
 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)));
 }
Esempio n. 13
0
 public RollingCount(ICountedEntity <TEntityType> entity, IRollingCountConfig config, TCountType measure, Func <DateTime> timeProvider) : base(entity, config, measure, timeProvider)
 {
     this.rollingCountConfig = config;
 }
Esempio n. 14
0
 public RollingCount(ICountedEntity <TEntityType> entity, IRollingCountConfig config, TCountType measure) : this(entity, config, measure, () => DateTime.UtcNow)
 {
 }
Esempio n. 15
0
 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));
 }
Esempio n. 17
0
 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);
 }
Esempio n. 19
0
 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;
 }
Esempio n. 21
0
 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)));
 }
Esempio n. 22
0
 public AbsoluteCount(ICountedEntity <TEntityType> entity, IAbsoluteCountConfig config, TCountType measure, Func <DateTime> timeProvider) : base(entity, config, measure, timeProvider)
 {
     this.absoluteConfig = config;
 }
Esempio n. 23
0
 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));
 }
Esempio n. 24
0
 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)));
 }
Esempio n. 25
0
 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);
 }
Esempio n. 26
0
 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");
 }
Esempio n. 27
0
 public EmptyCount(ICountedEntity <TEntityType> entity, TCountType measure)
 {
     ArgumentValidator.ThrowIfNull("entity", entity);
     this.Entity  = entity;
     this.Measure = measure;
 }
Esempio n. 28
0
 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));
 }
Esempio n. 29
0
 private EntityValue(ICountedEntity <TEntityType> entity, ICountTrackerDiagnostics <TEntityType, TCountType> perfCounters, Count <TEntityType, TCountType>[] measures, Func <DateTime> timeProvider) : this(entity, perfCounters, timeProvider)
 {
     this.measures = measures;
 }