protected async Task <ActionResult <CountResult> > GetCountImplAsync(ExceptionlessSystemFilter sf, TimeInfo ti, string filter = null, string aggregations = null)
        {
            var pr = await _validator.ValidateQueryAsync(filter);

            if (!pr.IsValid)
            {
                return(BadRequest(pr.Message));
            }

            var far = await _validator.ValidateAggregationsAsync(aggregations);

            if (!far.IsValid)
            {
                return(BadRequest(far.Message));
            }

            sf.UsesPremiumFeatures = pr.UsesPremiumFeatures || far.UsesPremiumFeatures;
            var query = new RepositoryQuery <TModel>()
                        .SystemFilter(ShouldApplySystemFilter(sf, filter) ? sf : null)
                        .DateRange(ti.Range.UtcStart, ti.Range.UtcEnd, ti.Field)
                        .Index(ti.Range.UtcStart, ti.Range.UtcEnd);

            CountResult result;

            try {
                result = await _repository.CountBySearchAsync(query, filter, aggregations);
            } catch (Exception ex) {
                using (_logger.BeginScope(new ExceptionlessState().Property("Search Filter", new { SystemFilter = sf, UserFilter = filter, Time = ti, Aggregations = aggregations }).Tag("Search").Identity(CurrentUser.EmailAddress).Property("User", CurrentUser).SetHttpContext(HttpContext)))
                    _logger.LogError(ex, "An error has occurred. Please check your filter or aggregations.");

                return(BadRequest("An error has occurred. Please check your search filter."));
            }

            return(Ok(result));
        }
Esempio n. 2
0
        private async Task <ICollection <StackSummaryModel> > GetStackSummariesAsync(ICollection <Stack> stacks, AppFilter eventSystemFilter, TimeInfo ti)
        {
            if (stacks.Count == 0)
            {
                return(new List <StackSummaryModel>());
            }

            var systemFilter = new RepositoryQuery <PersistentEvent>().AppFilter(eventSystemFilter).DateRange(ti.Range.UtcStart, ti.Range.UtcEnd, (PersistentEvent e) => e.Date).Index(ti.Range.UtcStart, ti.Range.UtcEnd);
            var stackTerms   = await _eventRepository.CountAsync(q => q.SystemFilter(systemFilter).FilterExpression(String.Join(" OR ", stacks.Select(r => $"stack:{r.Id}"))).AggregationsExpression($"terms:(stack_id~{stacks.Count} cardinality:user sum:count~1 min:date max:date)"));

            return(await GetStackSummariesAsync(stacks, stackTerms.Aggregations.Terms <string>("terms_stack_id").Buckets, eventSystemFilter, ti));
        }
Esempio n. 3
0
        private async Task <ICollection <StackSummaryModel> > GetStackSummariesAsync(ICollection <Stack> stacks, IReadOnlyCollection <KeyedBucket <string> > stackTerms, AppFilter sf, TimeInfo ti)
        {
            if (stacks.Count == 0)
            {
                return(new List <StackSummaryModel>(0));
            }

            var totalUsers = await GetUserCountByProjectIdsAsync(stacks, sf, ti.Range.UtcStart, ti.Range.UtcEnd);

            return(stacks.Join(stackTerms, s => s.Id, tk => tk.Key, (stack, term) => {
                var data = _formattingPluginManager.GetStackSummaryData(stack);
                var summary = new StackSummaryModel {
                    TemplateKey = data.TemplateKey,
                    Data = data.Data,
                    Id = stack.Id,
                    Title = stack.Title,
                    FirstOccurrence = term.Aggregations.Min <DateTime>("min_date").Value,
                    LastOccurrence = term.Aggregations.Max <DateTime>("max_date").Value,
                    Total = (long)(term.Aggregations.Sum("sum_count").Value ?? term.Total.GetValueOrDefault()),

                    Users = term.Aggregations.Cardinality("cardinality_user").Value.GetValueOrDefault(),
                    TotalUsers = totalUsers.GetOrDefault(stack.ProjectId)
                };

                return summary;
            }).ToList());
        }
Esempio n. 4
0
        private async Task <ActionResult <IReadOnlyCollection <Stack> > > GetInternalAsync(AppFilter sf, TimeInfo ti, string filter = null, string sort = null, string mode = null, int page = 1, int limit = 10)
        {
            page  = GetPage(page);
            limit = GetLimit(limit);
            int skip = GetSkip(page, limit);

            if (skip > MAXIMUM_SKIP)
            {
                return(Ok(EmptyModels));
            }

            var pr = await _validator.ValidateQueryAsync(filter);

            if (!pr.IsValid)
            {
                return(BadRequest(pr.Message));
            }

            sf.UsesPremiumFeatures = pr.UsesPremiumFeatures;

            try {
                var results = await _repository.FindAsync(q => q.AppFilter(ShouldApplySystemFilter(sf, filter) ? sf : null).FilterExpression(filter).SortExpression(sort).DateRange(ti.Range.UtcStart, ti.Range.UtcEnd, ti.Field), o => o.PageNumber(page).PageLimit(limit));

                var stacks = results.Documents.Select(s => s.ApplyOffset(ti.Offset)).ToList();
                if (!String.IsNullOrEmpty(mode) && String.Equals(mode, "summary", StringComparison.OrdinalIgnoreCase))
                {
                    return(OkWithResourceLinks(await GetStackSummariesAsync(stacks, sf, ti), results.HasMore && !NextPageExceedsSkipLimit(page, limit), page));
                }

                return(OkWithResourceLinks(stacks, results.HasMore && !NextPageExceedsSkipLimit(page, limit), page));
            } catch (ApplicationException ex) {
                using (_logger.BeginScope(new ExceptionlessState().Property("Search Filter", new { SystemFilter = sf, UserFilter = filter, Time = ti, Page = page, Limit = limit }).Tag("Search").Identity(CurrentUser.EmailAddress).Property("User", CurrentUser).SetHttpContext(HttpContext)))
                    _logger.LogError(ex, "An error has occurred. Please check your search filter.");

                return(BadRequest("An error has occurred. Please check your search filter."));
            }
        }