updating matcher nodes works!
This commit is contained in:
parent
3961e79062
commit
e533c2c335
|
@ -2,7 +2,6 @@ package graph
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/dgraph-io/dgo/v2"
|
||||
"github.com/dgraph-io/dgo/v2/protos/api"
|
||||
|
@ -35,7 +34,7 @@ func setupDgraphSchema(c *dgo.Dgraph) error {
|
|||
|
||||
err = c.Alter(context.Background(), &api.Operation{
|
||||
Schema: `
|
||||
id: string @index(term) .
|
||||
id: string @index(exact, term) .
|
||||
type: string @index(term) .
|
||||
ndata: string .
|
||||
nodeOne: uid .
|
||||
|
@ -196,7 +195,6 @@ fullPaste: FullPaste
|
|||
}
|
||||
|
||||
`})
|
||||
fmt.Println("hello")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
138
matcher/main.go
138
matcher/main.go
|
@ -4,6 +4,7 @@ import (
|
|||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -104,6 +105,7 @@ func loadTargets(graphClient *dgo.Dgraph) error {
|
|||
logrus.Error(err)
|
||||
return err
|
||||
}
|
||||
logrus.Info("adding", scanner.Text())
|
||||
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
|
@ -118,89 +120,95 @@ func loadTargets(graphClient *dgo.Dgraph) error {
|
|||
|
||||
// Run runs the routine trying to find matches in the ingested data.
|
||||
func (m *Matcher) Run(wg *sync.WaitGroup, graphClient *dgo.Dgraph) {
|
||||
logrus.Info("loading matcher targets")
|
||||
if err := loadTargets(graphClient); err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
logrus.Info("finished loading matcher targets")
|
||||
// Created nodes based on the IOCs
|
||||
// Upsert those nodes if the values are found
|
||||
if !m.Running {
|
||||
// m.StoppedChan = make(chan bool)
|
||||
// wg.Add(1)
|
||||
// for {
|
||||
// q := `query allofterms($a: string) {
|
||||
// Node(func: allofterms(full, $a)) {
|
||||
// uid
|
||||
// }}`
|
||||
m.StoppedChan = make(chan bool)
|
||||
wg.Add(1)
|
||||
for {
|
||||
q := `query allofterms($a: string) {
|
||||
Node(func: allofterms(full, $a)) {
|
||||
uid
|
||||
}}`
|
||||
|
||||
// ctx := context.Background()
|
||||
// txn := graphClient.NewTxn()
|
||||
// defer txn.Discard(ctx)
|
||||
// res, err := txn.QueryWithVars(ctx, q, map[string]string{"$a": "code"})
|
||||
// if err != nil {
|
||||
// logrus.Warn(err)
|
||||
// }
|
||||
ctx := context.Background()
|
||||
txn := graphClient.NewTxn()
|
||||
defer txn.Discard(ctx)
|
||||
res, err := txn.QueryWithVars(ctx, q, map[string]string{"$a": "code"})
|
||||
if err != nil {
|
||||
logrus.Warn(err)
|
||||
}
|
||||
|
||||
// n := Result{}
|
||||
// json.Unmarshal([]byte(res.Json), &n)
|
||||
// uuid := uuid.New().String()
|
||||
// t := time.Now()
|
||||
// rfc3339time := t.Format(time.RFC3339)
|
||||
// matcher := models.Match{
|
||||
// ID: uuid,
|
||||
// Timestamp: rfc3339time,
|
||||
// Target: "code",
|
||||
// Nodes: []models.Node{},
|
||||
// Type: "matcher",
|
||||
// }
|
||||
// if len(n.Result) != 0 {
|
||||
// // TODO: review time and id to be updated on new resulsts
|
||||
n := Result{}
|
||||
json.Unmarshal([]byte(res.Json), &n)
|
||||
uuid := uuid.New().String()
|
||||
t := time.Now()
|
||||
rfc3339time := t.Format(time.RFC3339)
|
||||
matcher := models.Match{
|
||||
ID: uuid,
|
||||
Timestamp: rfc3339time,
|
||||
Target: "java",
|
||||
Nodes: []models.Node{},
|
||||
Type: "matcher",
|
||||
}
|
||||
if len(n.Result) != 0 {
|
||||
// TODO: review time and id to be updated on new resulsts
|
||||
|
||||
// for _, res := range n.Result {
|
||||
// if len(matcher.Nodes) == 0 {
|
||||
// fmt.Println("First node appending")
|
||||
// matcher.Nodes = append(matcher.Nodes, res)
|
||||
// continue
|
||||
// }
|
||||
for _, res := range n.Result {
|
||||
if len(matcher.Nodes) == 0 {
|
||||
matcher.Nodes = append(matcher.Nodes, res)
|
||||
continue
|
||||
}
|
||||
|
||||
// for _, node := range matcher.Nodes {
|
||||
// if res.UID != node.UID {
|
||||
// fmt.Println("not there, appending...")
|
||||
// matcher.Nodes = append(matcher.Nodes, res)
|
||||
for _, node := range matcher.Nodes {
|
||||
if res.UID != node.UID {
|
||||
matcher.Nodes = append(matcher.Nodes, res)
|
||||
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fmt.Println("matcher:", matcher)
|
||||
// query := fmt.Sprintf(`
|
||||
// query {
|
||||
// (func: eq(id, "%s")) {
|
||||
// uid
|
||||
// }
|
||||
// }
|
||||
// `, matcher.ID)
|
||||
|
||||
// query := fmt.Sprintf(`
|
||||
// query {
|
||||
// node as var(func: eq(id, %s))
|
||||
// }
|
||||
// `, matcher.ID)
|
||||
query := `query { match as var(func: eq(target, "java")) } `
|
||||
|
||||
// nquads := fmt.Sprintf(`uid(node) <nodes> \"%v\"`, matcher.Nodes)
|
||||
// nquads := fmt.Sprintf(`uid(match) <nodes> "%v" .`, matcher.Nodes)
|
||||
fmt.Println("nodes", matcher.Nodes)
|
||||
pb, err := json.Marshal(models.Match{UID: "uid(match)", Target: "java", Nodes: matcher.Nodes})
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
// mu := &api.Mutation{
|
||||
// Cond: fmt.Sprintf(`@if(eq(id, %s))`, matcher.ID),
|
||||
// SetNquads: []byte(nquads),
|
||||
// }
|
||||
mu := &api.Mutation{
|
||||
SetJson: pb,
|
||||
}
|
||||
|
||||
// req := &api.Request{
|
||||
// Query: query,
|
||||
// Mutations: []*api.Mutation{mu},
|
||||
// CommitNow: true,
|
||||
// }
|
||||
req := &api.Request{
|
||||
Query: query,
|
||||
Mutations: []*api.Mutation{mu},
|
||||
CommitNow: true,
|
||||
}
|
||||
|
||||
// ret, err := graphClient.NewTxn().Do(ctx, req)
|
||||
// fmt.Println("#####", ret)
|
||||
// if err != nil {
|
||||
// logrus.Fatal(err)
|
||||
// }
|
||||
// }
|
||||
fmt.Println("requests", req)
|
||||
_, err = graphClient.NewTxn().Do(ctx, req)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
m.Running = true
|
||||
// }
|
||||
m.Running = true
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue