From e0af2685eeb8aa8b79d26933d4e4e2a979cacfb6 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sun, 2 Sep 2018 13:46:12 +0200 Subject: [PATCH] Logs: Moved filtering logic out of GraphQL query resolver to the Log model. --- graphql/resolver/queries/logs.go | 58 ++++---------------------------- graphql/schema/types/log.graphql | 4 +-- model/log/filter.go | 32 ++++++++++++++++++ model/log/log.go | 40 ++++++++++------------ 4 files changed, 59 insertions(+), 75 deletions(-) create mode 100644 model/log/filter.go diff --git a/graphql/resolver/queries/logs.go b/graphql/resolver/queries/logs.go index 91a2e21..e995317 100644 --- a/graphql/resolver/queries/logs.go +++ b/graphql/resolver/queries/logs.go @@ -11,65 +11,21 @@ import ( // LogsArgs is args for the logs query type LogsArgs struct { - Filter *struct { - Search *string - Characters *[]string - Channels *[]string - Events *[]string - Open *bool - Limit *int32 - } + Filter *log.Filter } // Logs resolves the logs query func (r *QueryResolver) Logs(ctx context.Context, args *LogsArgs) ([]*types.LogResolver, error) { var logs []log.Log - var err error filter := args.Filter + if filter == nil { + filter = log.NewFilter().WithLimit(100) + } - if filter != nil { - limit := 100 - search := "" - - if filter.Search != nil { - search = *filter.Search - limit = 0 - } - - channels := []string(nil) - if filter.Channels != nil { - channels = *filter.Channels - limit = 0 - } - - characters := []string(nil) - if filter.Characters != nil { - characters = *filter.Characters - limit = 0 - } - - events := []string(nil) - if filter.Events != nil { - events = *filter.Events - limit = 0 - } - - if filter.Limit != nil { - limit = int(*filter.Limit) - } - - open := filter.Open != nil && *filter.Open == true - - logs, err = log.ListSearch(search, channels, characters, events, open, limit) - if err != nil { - return nil, err - } - } else { - logs, err = log.List(100) - if err != nil { - return nil, err - } + logs, err := log.List(filter) + if err != nil { + return nil, err } if len(logs) >= 100 { diff --git a/graphql/schema/types/log.graphql b/graphql/schema/types/log.graphql index 97922dc..f233109 100644 --- a/graphql/schema/types/log.graphql +++ b/graphql/schema/types/log.graphql @@ -50,10 +50,10 @@ input LogsFilter { # Search post content search: String - # Limit to only open logs + # Limit by whether it's open or not open: Boolean - # Limit the amount of posts + # Limit the amount of results you get back limit: Int } diff --git a/model/log/filter.go b/model/log/filter.go new file mode 100644 index 0000000..95e86d9 --- /dev/null +++ b/model/log/filter.go @@ -0,0 +1,32 @@ +package log + +// Filter for the List() function +type Filter struct { + Search *string + Characters *[]string + Channels *[]string + Events *[]string + Open *bool + Limit *int32 +} + +// NewFilter makes a new filter +func NewFilter() *Filter { + return &Filter{} +} + +// WithLimit adds a max amount of results to be returned. +func (filter *Filter) WithLimit(limit int) *Filter { + limitPtr := int32(limit) + filter.Limit = &limitPtr + + return filter +} + +// WithOpen filters on whether a log is open. +func (filter *Filter) WithOpen(limit int) *Filter { + limitPtr := int32(limit) + filter.Limit = &limitPtr + + return filter +} diff --git a/model/log/log.go b/model/log/log.go index 64451e2..f4968c6 100644 --- a/model/log/log.go +++ b/model/log/log.go @@ -82,49 +82,45 @@ func FindID(id string) (Log, error) { } // List lists all logs -func List(limit int) ([]Log, error) { - return listLog(bson.M{}, limit) -} - -// ListSearch lists the logs matching the parameters. Empty/zero values means the parameter is ingored when -// building the query. This is the old aitelogs2 way, but with the addition of a text search. -// -// If a text search is specified, it will make two trips to the database. -func ListSearch(textSearch string, channels []string, characterIds []string, events []string, open bool, limit int) ([]Log, error) { - postMutex.RLock() - defer postMutex.RUnlock() - +func List(filter *Filter) ([]Log, error) { query := bson.M{} // Run a text search - if textSearch != "" { + if filter.Search != nil { searchResults := make([]string, 0, 32) - err := postCollection.Find(bson.M{"$text": bson.M{"$search": textSearch}}).Distinct("logId", &searchResults) + postMutex.RLock() + err := postCollection.Find(bson.M{"$text": bson.M{"$search": *filter.Search}}).Distinct("logId", &searchResults) if err != nil { return nil, err } + postMutex.RUnlock() // Posts always use shortId to refer to the log query["shortId"] = bson.M{"$in": searchResults} } // Find logs including any of the specified events and channels - if len(channels) > 0 { - query["channel"] = bson.M{"$in": channels} + if filter.Channels != nil { + query["channel"] = bson.M{"$in": *filter.Channels} } - if len(events) > 0 { - query["events"] = bson.M{"$in": channels} + if filter.Events != nil { + query["event"] = bson.M{"$in": *filter.Events} } // Find logs including all of the specified character IDs. - if len(characterIds) > 0 { - query["characterIds"] = bson.M{"$all": characterIds} + if filter.Characters != nil { + query["characterIds"] = bson.M{"$all": *filter.Characters} } // Limit to only open logs - if open { - query["open"] = true + if filter.Open != nil { + query["open"] = *filter.Open + } + + limit := 0 + if filter.Limit != nil { + limit = int(*filter.Limit) } return listLog(query, limit)