Skip to content
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

ISSUE-1794-2: Adding timestamps to req and response #1644

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions deepfence_server/model/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ type GraphResult struct {
}

type TopologyDeltaReq struct {
Timestamp int64 `json:"timestamp" required:"true" format:"int64"`
Addition bool `json:"addition" required:"true"`
Deletion bool `json:"deletion" required:"true"`
EntityTypes []string `json:"entity_types" required:"true"`
AdditionTimestamp int64 `json:"addition_timestamp" required:"true" format:"int64"`
DeletionTimestamp int64 `json:"deletion_timestamp" required:"true" format:"int64"`
Addition bool `json:"addition" required:"true"`
Deletion bool `json:"deletion" required:"true"`
EntityTypes []string `json:"entity_types" required:"true"`
}

type TopologyDeltaResponse struct {
Additions []NodeIdentifier `json:"additons"`
Deletions []NodeIdentifier `json:"deletions"`
Additions []NodeIdentifier `json:"additons"`
Deletions []NodeIdentifier `json:"deletions"`
AdditionTimestamp int64 `json:"addition_timestamp" format:"int64"`
DeletionTimestamp int64 `json:"deletion_timestamp" format:"int64"`
}
30 changes: 23 additions & 7 deletions deepfence_server/reporters/graph/topology_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ func GetTopologyDelta(ctx context.Context,
}
defer tx.Close()

processRecords := func(isAdd bool, query string) error {
processRecords := func(isAdd bool, query string, timestamp int64) error {
r, err := tx.Run(query, map[string]interface{}{})
if err != nil {
return err
Expand All @@ -1218,6 +1218,7 @@ func GetTopologyDelta(ctx context.Context,
return err
}

maxTime := int64(0)
for _, record := range records {
nodeid := record.Values[0].(string)
nodeType := record.Values[1].(string)
Expand All @@ -1228,6 +1229,21 @@ func GetTopologyDelta(ctx context.Context,
deltaResp.Deletions = append(deltaResp.Deletions,
model.NodeIdentifier{nodeid, nodeType})
}
ts := record.Values[2].(int64)
if ts > maxTime {
maxTime = ts
}
}

if maxTime == 0 {
//Set it to previous timestamp
maxTime = timestamp
}

if isAdd {
deltaResp.AdditionTimestamp = maxTime
} else {
deltaResp.DeletionTimestamp = maxTime
}

return nil
Expand All @@ -1245,23 +1261,23 @@ func GetTopologyDelta(ctx context.Context,

if deltaReq.Addition == true {
additionQuery := `MATCH (n) WHERE ` + nodeTypeQueryStr + `
AND n.active=true AND n.created_at >=%d
RETURN n.node_id, n.node_type`
AND n.active=true AND n.created_at > %d
RETURN n.node_id, n.node_type, n.created_at`

err = processRecords(true, fmt.Sprintf(additionQuery,
deltaReq.Timestamp))
deltaReq.AdditionTimestamp), deltaReq.AdditionTimestamp)
if err != nil {
return deltaResp, err
}
}

if deltaReq.Deletion == true {
deletionQuery := `MATCH (n) WHERE ` + nodeTypeQueryStr + `
AND n.active=false AND n.updated_at >=%d
RETURN n.node_id, n.node_type`
AND n.active=false AND n.updated_at > %d
RETURN n.node_id, n.node_type, n.updated_at`

err = processRecords(false, fmt.Sprintf(deletionQuery,
deltaReq.Timestamp))
deltaReq.DeletionTimestamp), deltaReq.DeletionTimestamp)
if err != nil {
return deltaResp, err
}
Expand Down