public void Build <T>(QueryBuilderContext <T> ctx) where T : class, new()
        {
            var softDeletesQuery = ctx.GetSourceAs <ISoftDeletesQuery>();

            if (softDeletesQuery == null || softDeletesQuery.IncludeSoftDeletes)
            {
                return;
            }

            var idsQuery = ctx.GetSourceAs <IIdentityQuery>();
            var opt      = ctx.GetOptionsAs <IElasticQueryOptions>();

            if (opt == null || !opt.SupportsSoftDeletes || (idsQuery != null && idsQuery.Ids.Count > 0))
            {
                return;
            }

            var missingFilter = new MissingFilter {
                Field = Deleted
            };
            var termFilter = new TermFilter {
                Field = Deleted, Value = softDeletesQuery.IncludeSoftDeletes
            };

            ctx.Filter &= (new FilterContainer(missingFilter) || new FilterContainer(termFilter));
        }
Esempio n. 2
0
        public void Build <T>(QueryBuilderContext <T> ctx) where T : class, new()
        {
            var sfq = ctx.GetSourceAs <IExceptionlessSystemFilterQuery>();

            if (sfq == null)
            {
                return;
            }

            var allowedOrganizations = sfq.Organizations.Where(o => o.HasPremiumFeatures || (!o.HasPremiumFeatures && !sfq.UsesPremiumFeatures)).ToList();

            if (allowedOrganizations.Count == 0)
            {
                ctx.Filter &= Filter <T> .Term("organization", "none");

                return;
            }

            string field = GetDateField(ctx.GetOptionsAs <IElasticQueryOptions>());

            if (sfq.Stack != null)
            {
                var organization = sfq.Organizations.Single(o => o.Id == sfq.Stack.OrganizationId);
                ctx.Filter &= (Filter <T> .Term("stack", sfq.Stack.Id) && GetRetentionFilter <T>(field, organization.RetentionDays));
                return;
            }

            FilterContainer container = null;

            if (sfq.Projects?.Count > 0)
            {
                foreach (var project in sfq.Projects)
                {
                    var organization = sfq.Organizations.Single(o => o.Id == project.OrganizationId);
                    container |= (Filter <T> .Term("project", project.Id) && GetRetentionFilter <T>(field, organization.RetentionDays));
                }

                ctx.Filter &= container;
                return;
            }

            if (sfq.Organizations?.Count > 0)
            {
                foreach (var organization in sfq.Organizations)
                {
                    container |= (Filter <T> .Term("organization", organization.Id) && GetRetentionFilter <T>(field, organization.RetentionDays));
                }

                ctx.Filter &= container;
            }
        }
Esempio n. 3
0
        public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new()
        {
            if (ctx.Type != ContextType.SystemFilter)
            {
                return(Task.CompletedTask);
            }

            var sfq = ctx.GetSourceAs <IExceptionlessSystemFilterQuery>() ?? ctx.GetSourceAs <ISystemFilterQuery>()?.SystemFilter as IExceptionlessSystemFilterQuery;

            if (sfq == null)
            {
                return(Task.CompletedTask);
            }

            var allowedOrganizations = sfq.Organizations.Where(o => o.HasPremiumFeatures || (!o.HasPremiumFeatures && !sfq.UsesPremiumFeatures)).ToList();

            if (allowedOrganizations.Count == 0)
            {
                ctx.Filter &= Query <T> .Term(_organizationIdFieldName, "none");

                return(Task.CompletedTask);
            }

            string field = GetDateField(ctx.GetOptionsAs <IElasticQueryOptions>());

            if (sfq.Stack != null)
            {
                var organization = allowedOrganizations.SingleOrDefault(o => o.Id == sfq.Stack.OrganizationId);
                if (organization != null)
                {
                    ctx.Filter &= (Query <T> .Term(_stackIdFieldName, sfq.Stack.Id) && GetRetentionFilter <T>(field, organization, sfq.Stack.FirstOccurrence));
                }
                else
                {
                    ctx.Filter &= Query <T> .Term(_stackIdFieldName, "none");
                }

                return(Task.CompletedTask);
            }

            QueryContainer container = null;

            if (sfq.Projects?.Count > 0)
            {
                var allowedProjects = sfq.Projects.ToDictionary(p => p, p => allowedOrganizations.SingleOrDefault(o => o.Id == p.OrganizationId)).Where(kvp => kvp.Value != null).ToList();
                if (allowedProjects.Count > 0)
                {
                    foreach (var project in allowedProjects)
                    {
                        container |= (Query <T> .Term(_projectIdFieldName, project.Key.Id) && GetRetentionFilter <T>(field, project.Value, project.Key.CreatedUtc.SafeSubtract(TimeSpan.FromDays(3))));
                    }

                    ctx.Filter &= container;
                    return(Task.CompletedTask);
                }

                ctx.Filter &= (Query <T> .Term(_projectIdFieldName, "none"));
                return(Task.CompletedTask);
            }

            foreach (var organization in allowedOrganizations)
            {
                container |= (Query <T> .Term(_organizationIdFieldName, organization.Id) && GetRetentionFilter <T>(field, organization));
            }

            ctx.Filter &= container;
            return(Task.CompletedTask);
        }