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

This commit is contained in:
2018-09-02 13:46:12 +02:00
parent 3184b60310
commit e0af2685ee
4 changed files with 59 additions and 75 deletions
+7 -51
View File
@@ -11,65 +11,21 @@ import (
// LogsArgs is args for the logs query // LogsArgs is args for the logs query
type LogsArgs struct { type LogsArgs struct {
Filter *struct { Filter *log.Filter
Search *string
Characters *[]string
Channels *[]string
Events *[]string
Open *bool
Limit *int32
}
} }
// 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 {
filter = log.NewFilter().WithLimit(100)
}
if filter != nil { logs, err := log.List(filter)
limit := 100 if err != nil {
search := "" return nil, err
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
}
} }
if len(logs) >= 100 { if len(logs) >= 100 {
+2 -2
View File
@@ -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
View File
@@ -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
}
+18 -22
View File
@@ -82,49 +82,45 @@ func FindID(id string) (Log, error) {
} }
// List lists all logs // List lists all logs
func List(limit int) ([]Log, error) { func List(filter *Filter) ([]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()
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 { if filter.Channels != nil {
query["channel"] = bson.M{"$in": channels} query["channel"] = bson.M{"$in": *filter.Channels}
} }
if len(events) > 0 { if filter.Events != nil {
query["events"] = bson.M{"$in": channels} 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 { if filter.Characters != nil {
query["characterIds"] = bson.M{"$all": characterIds} query["characterIds"] = bson.M{"$all": *filter.Characters}
} }
// Limit to only open logs // Limit to only open logs
if open { if filter.Open != nil {
query["open"] = true query["open"] = *filter.Open
}
limit := 0
if filter.Limit != nil {
limit = int(*filter.Limit)
} }
return listLog(query, limit) return listLog(query, limit)