mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-10-29 17:16:31 +00:00
req improvement
This commit is contained in:
parent
d1b3750c87
commit
33706b4200
@ -55,62 +55,56 @@ func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName stri
|
|||||||
query["$or"] = combinedFilters
|
query["$or"] = combinedFilters
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply sorting by creation date (descending)
|
||||||
opts := options.Find().SetSort(bson.D{{Key: "created_at", Value: -1}})
|
opts := options.Find().SetSort(bson.D{{Key: "created_at", Value: -1}})
|
||||||
|
|
||||||
// Apply limit if set for initial query
|
// Apply limit if set in any filter
|
||||||
for _, filter := range filters {
|
for _, filter := range filters {
|
||||||
if filter.Limit != nil {
|
if filter.Limit != nil {
|
||||||
opts.SetLimit(int64(*filter.Limit))
|
opts.SetLimit(int64(*filter.Limit))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no specific kinds are specified, query all collections
|
// If no kinds are specified in any filter, query all collections
|
||||||
if len(filters[0].Kinds) == 0 {
|
var collections []string
|
||||||
collections, err := client.Database(databaseName).ListCollectionNames(context.TODO(), bson.D{})
|
if len(filters) > 0 && len(filters[0].Kinds) == 0 {
|
||||||
if err != nil {
|
collections, _ = client.Database(databaseName).ListCollectionNames(context.TODO(), bson.D{})
|
||||||
return nil, fmt.Errorf("error listing collections: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, collectionName := range collections {
|
|
||||||
collection := client.Database(databaseName).Collection(collectionName)
|
|
||||||
cursor, err := collection.Find(context.TODO(), query, opts)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error querying collection %s: %v", collectionName, err)
|
|
||||||
}
|
|
||||||
defer cursor.Close(context.TODO())
|
|
||||||
|
|
||||||
for cursor.Next(context.TODO()) {
|
|
||||||
var event relay.Event
|
|
||||||
if err := cursor.Decode(&event); err != nil {
|
|
||||||
return nil, fmt.Errorf("error decoding event from collection %s: %v", collectionName, err)
|
|
||||||
}
|
|
||||||
results = append(results, event)
|
|
||||||
}
|
|
||||||
if err := cursor.Err(); err != nil {
|
|
||||||
return nil, fmt.Errorf("cursor error in collection %s: %v", collectionName, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Query specific collections based on kinds
|
// Collect all kinds from filters and query those collections
|
||||||
for _, kind := range filters[0].Kinds {
|
kindsMap := make(map[int]bool)
|
||||||
collectionName := fmt.Sprintf("event-kind%d", kind)
|
for _, filter := range filters {
|
||||||
collection := client.Database(databaseName).Collection(collectionName)
|
for _, kind := range filter.Kinds {
|
||||||
cursor, err := collection.Find(context.TODO(), query, opts)
|
kindsMap[kind] = true
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error querying collection %s: %v", collectionName, err)
|
|
||||||
}
|
}
|
||||||
defer cursor.Close(context.TODO())
|
}
|
||||||
|
|
||||||
for cursor.Next(context.TODO()) {
|
// Construct collection names based on kinds
|
||||||
var event relay.Event
|
for kind := range kindsMap {
|
||||||
if err := cursor.Decode(&event); err != nil {
|
collectionName := fmt.Sprintf("event-kind%d", kind)
|
||||||
return nil, fmt.Errorf("error decoding event from collection %s: %v", collectionName, err)
|
collections = append(collections, collectionName)
|
||||||
}
|
}
|
||||||
results = append(results, event)
|
}
|
||||||
}
|
|
||||||
if err := cursor.Err(); err != nil {
|
// Query each collection
|
||||||
return nil, fmt.Errorf("cursor error in collection %s: %v", collectionName, err)
|
for _, collectionName := range collections {
|
||||||
|
collection := client.Database(databaseName).Collection(collectionName)
|
||||||
|
cursor, err := collection.Find(context.TODO(), query, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error querying collection %s: %v", collectionName, err)
|
||||||
|
}
|
||||||
|
defer cursor.Close(context.TODO())
|
||||||
|
|
||||||
|
for cursor.Next(context.TODO()) {
|
||||||
|
var event relay.Event
|
||||||
|
if err := cursor.Decode(&event); err != nil {
|
||||||
|
return nil, fmt.Errorf("error decoding event from collection %s: %v", collectionName, err)
|
||||||
}
|
}
|
||||||
|
results = append(results, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle cursor errors
|
||||||
|
if err := cursor.Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("cursor error in collection %s: %v", collectionName, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user