Browse Source

Logs: Moved filtering logic out of GraphQL query resolver to the Log model.

1.0
Gisle Aune 6 years ago
parent
commit
e0af2685ee
  1. 52
      graphql/resolver/queries/logs.go
  2. 4
      graphql/schema/types/log.graphql
  3. 32
      model/log/filter.go
  4. 40
      model/log/log.go

52
graphql/resolver/queries/logs.go

@ -11,66 +11,22 @@ import (
// LogsArgs is args for the logs query // LogsArgs is args for the logs query
type LogsArgs struct { 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 // Logs resolves the logs query
func (r *QueryResolver) Logs(ctx context.Context, args *LogsArgs) ([]*types.LogResolver, error) { func (r *QueryResolver) Logs(ctx context.Context, args *LogsArgs) ([]*types.LogResolver, error) {
var logs []log.Log var logs []log.Log
var err error
filter := args.Filter filter := args.Filter
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)
if filter == nil {
filter = log.NewFilter().WithLimit(100)
} }
open := filter.Open != nil && *filter.Open == true
logs, err = log.ListSearch(search, channels, characters, events, open, limit)
logs, err := log.List(filter)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else {
logs, err = log.List(100)
if err != nil {
return nil, err
}
}
if len(logs) >= 100 { if len(logs) >= 100 {
loader := loader.FromContext(ctx) loader := loader.FromContext(ctx)

4
graphql/schema/types/log.graphql

@ -50,10 +50,10 @@ input LogsFilter {
# Search post content # Search post content
search: String search: String
# Limit to only open logs
# Limit by whether it's open or not
open: Boolean open: Boolean
# Limit the amount of posts
# Limit the amount of results you get back
limit: Int limit: Int
} }

32
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
}

40
model/log/log.go

@ -82,49 +82,45 @@ func FindID(id string) (Log, error) {
} }
// List lists all logs // 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{} query := bson.M{}
// Run a text search // Run a text search
if textSearch != "" {
if filter.Search != nil {
searchResults := make([]string, 0, 32) 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 { if err != nil {
return nil, err return nil, err
} }
postMutex.RUnlock()
// Posts always use shortId to refer to the log // Posts always use shortId to refer to the log
query["shortId"] = bson.M{"$in": searchResults} query["shortId"] = bson.M{"$in": searchResults}
} }
// Find logs including any of the specified events and channels // 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. // 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 // 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) return listLog(query, limit)

Loading…
Cancel
Save