Skip to content

Commit

Permalink
#1443 Fix for empty region displayed from kube hosts (#1638)
Browse files Browse the repository at this point in the history
* #1443 Fix for empty region displayed from kube hosts

* renaming cr_shown to res_shown

* Detach Cloud Region from parent and delete
  • Loading branch information
saurabh2253 authored Oct 10, 2023
1 parent ec2b7d9 commit d3ac0ee
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions deepfence_worker/cronjobs/neo4j.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,14 @@ func CleanUpDB(ctx context.Context, task *asynq.Task) error {

// Delete old with no data
if _, err = session.Run(`
MATCH (n:Node)
MATCH (n:Node) <-[:HOSTS]- (cr:CloudRegion)
WHERE n.active = false
AND (NOT exists((n) <-[:SCANNED]-())
AND n.updated_at < TIMESTAMP() - $delete_threshold_ms)
OR n.updated_at < TIMESTAMP()-$old_time_ms
WITH n LIMIT 10000
WITH n, cr LIMIT 10000
SET cr.res_shown = CASE WHEN n.kubernetes_cluster_id= "" or n.kubernetes_cluster_id is null THEN cr.res_shown - 1 ELSE cr.res_shown END,
cr.active = cr.res_shown <> 0
DETACH DELETE n`,
map[string]interface{}{
"time_ms": dbReportCleanUpTimeout.Milliseconds(),
Expand Down Expand Up @@ -339,8 +341,8 @@ func CleanUpDB(ctx context.Context, task *asynq.Task) error {
AND NOT exists((n) <-[:SCANNED]-())
OR n.updated_at < TIMESTAMP()-$old_time_ms
WITH n, cr LIMIT 10000
SET cr.cr_shown = CASE WHEN n.is_shown THEN cr.cr_shown - 1 ELSE cr.cr_shown END,
cr.active = cr.cr_shown <> 0
SET cr.res_shown = CASE WHEN n.is_shown THEN cr.res_shown - 1 ELSE cr.res_shown END,
cr.active = cr.res_shown <> 0
WITH n
DETACH DELETE n`,
map[string]interface{}{
Expand All @@ -365,7 +367,7 @@ func CleanUpDB(ctx context.Context, task *asynq.Task) error {
MATCH (n:CloudRegion)
WHERE not (n) -[:HOSTS]-> ()
WITH n LIMIT 10000
DELETE n`,
DETACH DELETE n`,
map[string]interface{}{}, txConfig); err != nil {
log.Error().Msgf("Error in Clean up DB task: %v", err)
return err
Expand Down Expand Up @@ -424,15 +426,24 @@ func LinkCloudResources(ctx context.Context, task *asynq.Task) error {
WITH cr, n
WHERE n.is_shown = true
WITH cr, count(n) as cnt
SET cr.cr_shown = COALESCE(cr.cr_shown, 0) + cnt`,
SET cr.res_shown = COALESCE(cr.res_shown, 0) + cnt`,
map[string]interface{}{}, txConfig); err != nil {
return err
}

if _, err = session.Run(`
MATCH (n:Node) <-[:HOSTS]- (cr:CloudRegion)
WHERE n.active = true and (n.kubernetes_cluster_id= "" or n.kubernetes_cluster_id is null)
WITH cr, count(n) as cnt
SET cr.res_shown = COALESCE(cr.res_shown, 0) + cnt`,
map[string]interface{}{}, txConfig); err != nil {
return err
}

if _, err = session.Run(`
MATCH (cr:CloudRegion)
WHERE NOT cr.cr_shown IS NULL
SET cr.active = cr.cr_shown <> 0`,
WHERE NOT cr.res_shown IS NULL
SET cr.active = cr.res_shown <> 0`,
map[string]interface{}{}, txConfig); err != nil {
return err
}
Expand Down

0 comments on commit d3ac0ee

Please sign in to comment.