public async Task <HttpResponseMessage> GetCounterNames(string counterStorageName, int skip, int take)
        {
            var counter = await CountersLandlord.GetResourceInternal(counterStorageName).ConfigureAwait(false);

            if (counter == null)
            {
                return(GetMessageWithString(string.Format("Counter storage with name {0} not found.", counterStorageName), HttpStatusCode.NotFound));
            }

            using (var reader = counter.CreateReader())
            {
                var groupsAndNames = reader.GetCounterGroups(0, int.MaxValue)
                                     .SelectMany(group => reader.GetCounterSummariesByGroup(group.Name, 0, int.MaxValue)
                                                 .Select(x => new CounterNameGroupPair
                {
                    Name  = x.CounterName,
                    Group = group.Name
                }));

                return(GetMessageWithObject(new
                {
                    Stats = counter.CreateStats(),
                    HasMore = groupsAndNames.Count() > skip + take,
                    GroupsAndNames = groupsAndNames.Skip(skip).Take(take)
                }));
            }
        }
        public async Task <HttpResponseMessage> GetCounterMetrics(string counterStorageName)
        {
            var counter = await CountersLandlord.GetResourceInternal(counterStorageName).ConfigureAwait(false);

            if (counter == null)
            {
                return(GetMessageWithString(string.Format("Counter storage with name {0} not found.", counterStorageName), HttpStatusCode.NotFound));
            }

            return(GetMessageWithObject(counter.CreateMetrics()));
        }
Exemple #3
0
        public override async Task <HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
        {
            if (ForceClusterAwareness == false)
            {
                var  clusterAwareHeader = GetClusterHeader(controllerContext, Constants.Cluster.ClusterAwareHeader);
                bool clusterAware;
                if (clusterAwareHeader == null || bool.TryParse(clusterAwareHeader, out clusterAware) == false || clusterAware == false)
                {
                    return(await base.ExecuteAsync(controllerContext, cancellationToken).ConfigureAwait(false));
                }
            }

            InnerInitialization(controllerContext);

            if (CounterStorage == null || ClusterManager.IsActive() == false)
            {
                return(await base.ExecuteAsync(controllerContext, cancellationToken).ConfigureAwait(false));
            }

            if (CountersName != null && await CountersLandlord.GetResourceInternal(CountersName).ConfigureAwait(false) == null)
            {
                var msg = "Could not find a counter named: " + CountersName;
                return(GetMessageWithObject(new { Error = msg }, HttpStatusCode.ServiceUnavailable));
            }

            if (CounterStorage.IsClusterDatabase() == false)
            {
                return(await base.ExecuteAsync(controllerContext, cancellationToken).ConfigureAwait(false));
            }

            if (ClusterManager.IsLeader())
            {
                return(await base.ExecuteAsync(controllerContext, cancellationToken).ConfigureAwait(false));
            }

            if (IsReadRequest(controllerContext))
            {
                var clusterReadBehaviorHeader = GetClusterHeader(controllerContext, Constants.Cluster.ClusterReadBehaviorHeader);
                if (string.Equals(clusterReadBehaviorHeader, "All", StringComparison.OrdinalIgnoreCase))
                {
                    return(await base.ExecuteAsync(controllerContext, cancellationToken).ConfigureAwait(false));
                }
            }

            var  clusterFailoverBehaviorHeader = GetClusterHeader(controllerContext, Constants.Cluster.ClusterFailoverBehaviorHeader);
            bool clusterFailoverBehavior;

            if (bool.TryParse(clusterFailoverBehaviorHeader, out clusterFailoverBehavior) && clusterFailoverBehavior)
            {
                return(await base.ExecuteAsync(controllerContext, cancellationToken).ConfigureAwait(false));
            }

            return(RedirectToLeader(controllerContext.Request));
        }
        private async Task <HttpResponseMessage> GetNamesAndGroups(string id, int skip, int take)
        {
            MessageWithStatusCode nameFormateErrorMsg;

            if (IsValidName(id, Counters.Configuration.Counter.DataDirectory, out nameFormateErrorMsg) == false)
            {
                return(GetMessageWithObject(new
                {
                    Error = nameFormateErrorMsg.Message
                }, nameFormateErrorMsg.ErrorCode));
            }

            if (Authentication.IsLicensedForCounters == false)
            {
                return(GetMessageWithObject(new
                {
                    Error = "Your license does not allow the use of Counters!"
                }, HttpStatusCode.BadRequest));
            }

            var counterStorage = await CountersLandlord.GetResourceInternal(id).ConfigureAwait(false);

            if (counterStorage == null)
            {
                return(GetMessageWithObject(new
                {
                    Message = $"Didn't find counter storage (name = {id})"
                }, HttpStatusCode.NotFound));
            }

            var counterNameGroupPairs = new List <CounterNameGroupPair>();

            using (var reader = counterStorage.CreateReader())
            {
                var groupsAndNames = reader.GetCounterGroups(0, int.MaxValue)
                                     .SelectMany(group => reader.GetCounterSummariesByGroup(group.Name, 0, int.MaxValue)
                                                 .Select(x => new CounterNameGroupPair
                {
                    Name  = x.CounterName,
                    Group = group.Name
                })).Skip(skip).Take(take);

                counterNameGroupPairs.AddRange(groupsAndNames);
            }

            return(GetMessageWithObject(counterNameGroupPairs));
        }
        private async Task <HttpResponseMessage> GetSummary(string counterStorageId, int shouldSkip, int shouldTake)
        {
            MessageWithStatusCode nameFormateErrorMsg;

            if (IsValidName(counterStorageId, Counters.Configuration.Counter.DataDirectory, out nameFormateErrorMsg) == false)
            {
                return(GetMessageWithObject(new
                {
                    Error = nameFormateErrorMsg.Message
                }, nameFormateErrorMsg.ErrorCode));
            }

            if (Authentication.IsLicensedForCounters == false)
            {
                return(GetMessageWithObject(new
                {
                    Error = "Your license does not allow the use of Counters!"
                }, HttpStatusCode.BadRequest));
            }

            var counterStorage = await CountersLandlord.GetResourceInternal(counterStorageId).ConfigureAwait(false);

            if (counterStorage == null)
            {
                return(GetMessageWithObject(new
                {
                    Message = $"Didn't find counter storage (name = {counterStorageId})"
                }, HttpStatusCode.NotFound));
            }

            var counterSummaries = new List <CounterSummary>();

            using (var reader = counterStorage.CreateReader())
            {
                int skipped = 0;
                int taken   = 0;
                foreach (var group in reader.GetCounterGroups(0, int.MaxValue))
                {
                    int leftToSkip = (shouldSkip - skipped);
                    if (leftToSkip > group.Count)
                    {
                        skipped += (int)group.Count;
                        continue;
                    }

                    if (taken >= shouldTake)
                    {
                        break;
                    }

                    int toTake;
                    if (leftToSkip <= group.Count)
                    {
                        toTake = (int)group.Count - leftToSkip;
                        toTake = AdjustToTake(shouldTake, taken, toTake);

                        counterSummaries.AddRange(reader.GetCounterSummariesByGroup(group.Name, leftToSkip, toTake));
                        taken   += toTake;
                        skipped += leftToSkip;
                        continue;
                    }

                    toTake = (int)group.Count;
                    toTake = AdjustToTake(shouldTake, taken, toTake);

                    counterSummaries.AddRange(reader.GetCounterSummariesByGroup(group.Name, 0, toTake));
                    taken += toTake;
                }
            }

            return(GetMessageWithObject(counterSummaries));
        }