Add scaffolding for scope package

This commit is contained in:
David Stotijn
2020-10-01 21:46:35 +02:00
parent 46caa05d20
commit d48f1f058d
7 changed files with 177 additions and 12 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/cayleygraph/cayley"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/kv"
cpath "github.com/cayleygraph/cayley/graph/path"
"github.com/cayleygraph/cayley/schema"
"github.com/cayleygraph/quad"
"github.com/cayleygraph/quad/voc"
@ -21,6 +22,7 @@ import (
"github.com/google/uuid"
"github.com/dstotijn/hetty/pkg/reqlog"
"github.com/dstotijn/hetty/pkg/scope"
)
type HTTPRequest struct {
@ -107,17 +109,33 @@ func (db *Database) Close() error {
return db.store.Close()
}
func (db *Database) FindAllRequestLogs(ctx context.Context) ([]reqlog.Request, error) {
func (db *Database) FindRequestLogs(ctx context.Context, opts reqlog.FindRequestsOptions, scope *scope.Scope) ([]reqlog.Request, error) {
db.mu.Lock()
defer db.mu.Unlock()
var reqLogs []reqlog.Request
var reqs []HTTPRequest
path := cayley.StartPath(db.store, quad.IRI("hy:HTTPRequest")).In(quad.IRI(rdf.Type))
err := path.Iterate(ctx).EachValue(db.store, func(v quad.Value) {
reqPath := cayley.StartPath(db.store, quad.IRI("hy:HTTPRequest")).In(quad.IRI(rdf.Type))
if opts.OmitOutOfScope {
var filterPath *cpath.Path
for _, rule := range scope.Rules() {
if rule.URL != nil {
if filterPath == nil {
filterPath = reqPath.Out(quad.IRI("hy:url")).Regex(rule.URL).In(quad.IRI("hy:url"))
} else {
filterPath = filterPath.Or(reqPath.Out(quad.IRI("hy:url")).Regex(rule.URL).In(quad.IRI("hy:url")))
}
}
}
if filterPath != nil {
reqPath = filterPath
}
}
err := reqPath.Iterate(ctx).EachValue(db.store, func(v quad.Value) {
var req HTTPRequest
if err := db.schema.LoadToDepth(ctx, db.store, &req, -1, v); err != nil {
if err := db.schema.LoadToDepth(ctx, db.store, &req, 0, v); err != nil {
log.Printf("[ERROR] Could not load sub-graph for http requests: %v", err)
return
}