Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Web UI] Filter entities by given subscription #1656

Merged
merged 10 commits into from
Jun 18, 2018
48 changes: 48 additions & 0 deletions backend/apid/graphql/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package graphql
import (
"errors"
"sort"
"strings"

"github.com/sensu/sensu-go/backend/apid/actions"
"github.com/sensu/sensu-go/backend/apid/graphql/globalid"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/sensu/sensu-go/graphql"
"github.com/sensu/sensu-go/types"
"github.com/sensu/sensu-go/util/eval"
string_utils "github.com/sensu/sensu-go/util/strings"
)

var _ schema.EnvironmentFieldResolvers = (*envImpl)(nil)
Expand Down Expand Up @@ -283,3 +285,49 @@ func (r *envImpl) CheckHistory(p schema.EnvironmentCheckHistoryFieldResolverPara
limit := clampInt(p.Args.Limit, 0, len(history))
return history[0:limit], nil
}

// Subscriptions implements response to request for 'subscriptions' field.
func (r *envImpl) Subscriptions(p schema.EnvironmentSubscriptionsFieldResolverParams) (interface{}, error) {
set := string_utils.OccurrenceSet{}
env := p.Source.(*types.Environment)
ctx := types.SetContextFromResource(p.Context, env)

entities, err := r.entityCtrl.Query(ctx)
if err != nil {
return set, err
}
for _, entity := range entities {
newSet := occurrencesOfSubscriptions(entity)
set.Merge(newSet)
}

checks, err := r.checksCtrl.Query(ctx)
if err != nil {
return set, err
}
for _, check := range checks {
newSet := occurrencesOfSubscriptions(check)
set.Merge(newSet)
}

// If specified omit subscriptions prefix'd with 'entity:'
if p.Args.OmitEntity {
for _, subscription := range set.Values() {
if strings.HasPrefix(subscription, "entity:") {
set.Remove(subscription)
}
}
}

// Sort entries
subscriptionSet := newSubscriptionSet(set)
if p.Args.OrderBy == schema.SubscriptionSetOrders.ALPHA_DESC {
subscriptionSet.sortByAlpha(false)
} else if p.Args.OrderBy == schema.SubscriptionSetOrders.ALPHA_ASC {
subscriptionSet.sortByAlpha(true)
} else if p.Args.OrderBy == schema.SubscriptionSetOrders.OCCURRENCES {
subscriptionSet.sortByOccurrence()
}

return subscriptionSet, nil
}
158 changes: 134 additions & 24 deletions backend/apid/graphql/schema/environment.gql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading