mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-22 08:37:13 +00:00
req improvement
This commit is contained in:
parent
d1b3750c87
commit
33706b4200
@ -55,22 +55,37 @@ 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)
|
} else {
|
||||||
|
// Collect all kinds from filters and query those collections
|
||||||
|
kindsMap := make(map[int]bool)
|
||||||
|
for _, filter := range filters {
|
||||||
|
for _, kind := range filter.Kinds {
|
||||||
|
kindsMap[kind] = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Construct collection names based on kinds
|
||||||
|
for kind := range kindsMap {
|
||||||
|
collectionName := fmt.Sprintf("event-kind%d", kind)
|
||||||
|
collections = append(collections, collectionName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query each collection
|
||||||
for _, collectionName := range collections {
|
for _, collectionName := range collections {
|
||||||
collection := client.Database(databaseName).Collection(collectionName)
|
collection := client.Database(databaseName).Collection(collectionName)
|
||||||
cursor, err := collection.Find(context.TODO(), query, opts)
|
cursor, err := collection.Find(context.TODO(), query, opts)
|
||||||
@ -86,33 +101,12 @@ func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName stri
|
|||||||
}
|
}
|
||||||
results = append(results, event)
|
results = append(results, event)
|
||||||
}
|
}
|
||||||
if err := cursor.Err(); err != nil {
|
|
||||||
return nil, fmt.Errorf("cursor error in collection %s: %v", collectionName, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Query specific collections based on kinds
|
|
||||||
for _, kind := range filters[0].Kinds {
|
|
||||||
collectionName := fmt.Sprintf("event-kind%d", kind)
|
|
||||||
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()) {
|
// Handle cursor errors
|
||||||
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 {
|
if err := cursor.Err(); err != nil {
|
||||||
return nil, fmt.Errorf("cursor error in collection %s: %v", collectionName, err)
|
return nil, fmt.Errorf("cursor error in collection %s: %v", collectionName, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user