updating matcher nodes works!

This commit is contained in:
Christopher Talib 2020-06-02 10:58:31 +02:00
parent 3961e79062
commit e533c2c335
2 changed files with 74 additions and 68 deletions

View file

@ -2,7 +2,6 @@ package graph
import ( import (
"context" "context"
"fmt"
"github.com/dgraph-io/dgo/v2" "github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api" "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{ err = c.Alter(context.Background(), &api.Operation{
Schema: ` Schema: `
id: string @index(term) . id: string @index(exact, term) .
type: string @index(term) . type: string @index(term) .
ndata: string . ndata: string .
nodeOne: uid . nodeOne: uid .
@ -196,7 +195,6 @@ fullPaste: FullPaste
} }
`}) `})
fmt.Println("hello")
if err != nil { if err != nil {
return err return err
} }

View file

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -104,6 +105,7 @@ func loadTargets(graphClient *dgo.Dgraph) error {
logrus.Error(err) logrus.Error(err)
return err return err
} }
logrus.Info("adding", scanner.Text())
} }
if err := scanner.Err(); err != nil { 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. // Run runs the routine trying to find matches in the ingested data.
func (m *Matcher) Run(wg *sync.WaitGroup, graphClient *dgo.Dgraph) { func (m *Matcher) Run(wg *sync.WaitGroup, graphClient *dgo.Dgraph) {
logrus.Info("loading matcher targets")
if err := loadTargets(graphClient); err != nil { if err := loadTargets(graphClient); err != nil {
logrus.Error(err) logrus.Error(err)
} }
logrus.Info("finished loading matcher targets")
// Created nodes based on the IOCs // Created nodes based on the IOCs
// Upsert those nodes if the values are found // Upsert those nodes if the values are found
if !m.Running { if !m.Running {
// m.StoppedChan = make(chan bool) m.StoppedChan = make(chan bool)
// wg.Add(1) wg.Add(1)
// for { for {
// q := `query allofterms($a: string) { q := `query allofterms($a: string) {
// Node(func: allofterms(full, $a)) { Node(func: allofterms(full, $a)) {
// uid uid
// }}` }}`
// ctx := context.Background() ctx := context.Background()
// txn := graphClient.NewTxn() txn := graphClient.NewTxn()
// defer txn.Discard(ctx) defer txn.Discard(ctx)
// res, err := txn.QueryWithVars(ctx, q, map[string]string{"$a": "code"}) res, err := txn.QueryWithVars(ctx, q, map[string]string{"$a": "code"})
// if err != nil { if err != nil {
// logrus.Warn(err) logrus.Warn(err)
// } }
// n := Result{} n := Result{}
// json.Unmarshal([]byte(res.Json), &n) json.Unmarshal([]byte(res.Json), &n)
// uuid := uuid.New().String() uuid := uuid.New().String()
// t := time.Now() t := time.Now()
// rfc3339time := t.Format(time.RFC3339) rfc3339time := t.Format(time.RFC3339)
// matcher := models.Match{ matcher := models.Match{
// ID: uuid, ID: uuid,
// Timestamp: rfc3339time, Timestamp: rfc3339time,
// Target: "code", Target: "java",
// Nodes: []models.Node{}, Nodes: []models.Node{},
// Type: "matcher", Type: "matcher",
// } }
// if len(n.Result) != 0 { if len(n.Result) != 0 {
// // TODO: review time and id to be updated on new resulsts // TODO: review time and id to be updated on new resulsts
// for _, res := range n.Result { for _, res := range n.Result {
// if len(matcher.Nodes) == 0 { if len(matcher.Nodes) == 0 {
// fmt.Println("First node appending") matcher.Nodes = append(matcher.Nodes, res)
// matcher.Nodes = append(matcher.Nodes, res) continue
// continue }
// }
// for _, node := range matcher.Nodes { for _, node := range matcher.Nodes {
// if res.UID != node.UID { if res.UID != node.UID {
// fmt.Println("not there, appending...") matcher.Nodes = append(matcher.Nodes, res)
// 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 := `query { match as var(func: eq(target, "java")) } `
// query {
// node as var(func: eq(id, %s))
// }
// `, matcher.ID)
// 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{ mu := &api.Mutation{
// Cond: fmt.Sprintf(`@if(eq(id, %s))`, matcher.ID), SetJson: pb,
// SetNquads: []byte(nquads), }
// }
// req := &api.Request{ req := &api.Request{
// Query: query, Query: query,
// Mutations: []*api.Mutation{mu}, Mutations: []*api.Mutation{mu},
// CommitNow: true, CommitNow: true,
// } }
// ret, err := graphClient.NewTxn().Do(ctx, req) fmt.Println("requests", req)
// fmt.Println("#####", ret) _, err = graphClient.NewTxn().Do(ctx, req)
// if err != nil { if err != nil {
// logrus.Fatal(err) logrus.Fatal(err)
// } }
// } }
m.Running = true m.Running = true
// } }
} }
} }