-
Notifications
You must be signed in to change notification settings - Fork 18
Add log query endpoint #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,12 @@ import ( | |
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/linuxboot/contest/cmds/admin_server/storage" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
@@ -53,15 +56,59 @@ func NewMongoStorage(uri string) (storage.Storage, error) { | |
}, nil | ||
} | ||
|
||
func (s *MongoStorage) StoreLog(log storage.Log) error { | ||
logEntry := Log{ | ||
log, | ||
func toMongoQuery(query storage.Query) bson.D { | ||
q := bson.D{} | ||
|
||
if query.Text != nil { | ||
q = append(q, bson.E{ | ||
Key: "logdata", | ||
Value: bson.M{ | ||
"$regex": primitive.Regex{Pattern: *query.Text, Options: "ig"}, | ||
}, | ||
}) | ||
} | ||
|
||
if query.StartDate != nil && query.EndDate != nil { | ||
q = append(q, bson.E{ | ||
Key: "date", | ||
Value: bson.M{ | ||
"$gte": primitive.NewDateTimeFromTime(*query.StartDate), | ||
"$lte": primitive.NewDateTimeFromTime(*query.EndDate), | ||
}, | ||
}) | ||
} else if query.StartDate != nil { | ||
q = append(q, bson.E{ | ||
Key: "date", | ||
Value: bson.M{"$gte": primitive.NewDateTimeFromTime(*query.StartDate)}, | ||
}) | ||
} else if query.EndDate != nil { | ||
q = append(q, bson.E{ | ||
Key: "date", | ||
Value: bson.M{"$lte": primitive.NewDateTimeFromTime(*query.EndDate)}, | ||
}) | ||
} | ||
|
||
if query.LogLevel != nil && *query.LogLevel != "" { | ||
levels := strings.Split(*query.LogLevel, ",") | ||
q = append(q, | ||
bson.E{ | ||
Key: "loglevel", | ||
Value: bson.M{ | ||
"$in": levels, | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
return q | ||
} | ||
|
||
func (s *MongoStorage) StoreLog(log storage.Log) error { | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), DefaultConnectionTimeout) | ||
defer cancel() | ||
|
||
_, err := s.collection.InsertOne(ctx, logEntry) | ||
_, err := s.collection.InsertOne(ctx, log) | ||
if err != nil { | ||
// for better debugging | ||
fmt.Fprintf(os.Stderr, "Error while inserting into the db: %v", err) | ||
|
@@ -70,15 +117,48 @@ func (s *MongoStorage) StoreLog(log storage.Log) error { | |
return nil | ||
} | ||
|
||
func (s *MongoStorage) GetLogs(query storage.Query) ([]storage.Log, error) { | ||
// TODO | ||
return nil, nil | ||
func (s *MongoStorage) GetLogs(query storage.Query) (*storage.Result, error) { | ||
|
||
q := toMongoQuery(query) | ||
|
||
//get the count of the logs | ||
ctx, cancel := context.WithTimeout(context.Background(), DefaultConnectionTimeout) | ||
defer cancel() | ||
count, err := s.collection.CountDocuments(ctx, q) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error while performing count query: %v", err) | ||
mimir-d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil, storage.ErrQuery | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to add original err There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's not the concern of the frontend to know the details of the err. And I am logging it already in the server for debugging. |
||
} | ||
|
||
opts := options.Find() | ||
opts.SetSkip(int64(int(query.PageSize) * int(query.Page))) | ||
opts.SetLimit(int64(query.PageSize)) | ||
|
||
ctx, cancel = context.WithTimeout(context.Background(), DefaultConnectionTimeout) | ||
defer cancel() | ||
cur, err := s.collection.Find(ctx, q, opts) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error while querying logs from db: %v", err) | ||
return nil, storage.ErrQuery | ||
} | ||
|
||
var logs []storage.Log | ||
ctx, cancel = context.WithTimeout(context.Background(), DefaultConnectionTimeout) | ||
mimir-d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
defer cancel() | ||
err = cur.All(ctx, &logs) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error while reading query result from db: %v", err) | ||
return nil, storage.ErrQuery | ||
} | ||
|
||
return &storage.Result{ | ||
Logs: logs, | ||
Count: uint64(count), | ||
Page: query.Page, | ||
PageSize: query.PageSize, | ||
}, nil | ||
} | ||
|
||
func (s *MongoStorage) Close() error { | ||
return s.dbClient.Disconnect(context.Background()) | ||
} | ||
|
||
type Log struct { | ||
storage.Log `json:"log"` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package test | ||
|
||
import ( | ||
"flag" | ||
"time" | ||
) | ||
|
||
var ( | ||
flagAdminEndpoint = flag.String("adminServer", "http://adminserver:8000/log", "admin server log push endpoint") | ||
flagMongoEndpoint = flag.String("mongoDBURI", "mongodb://mongostorage:27017", "mongodb URI") | ||
flagOperationTimeout = flag.Duration("operationTimeout", time.Duration(10*time.Second), "operation timeout duration") | ||
) |
Uh oh!
There was an error while loading. Please reload this page.