From f83a09f620e4323218a1f058adc29319a6e99b06 Mon Sep 17 00:00:00 2001 From: abeizn Date: Thu, 7 Mar 2024 11:48:54 +0800 Subject: [PATCH] Revert "fix: empty dora data when run the tasks (#7070) (#7072)" This reverts commit f82045dfc2119110719954987368cbd7372feb60. --- .../dora/tasks/change_lead_time_calculator.go | 19 +++++++--- .../tasks/deployment_commits_generator.go | 34 +++++++----------- .../dora/tasks/deployment_generator.go | 36 ++++++++----------- .../dora/tasks/incident_deploy_connector.go | 19 +++++++--- 4 files changed, 55 insertions(+), 53 deletions(-) diff --git a/backend/plugins/dora/tasks/change_lead_time_calculator.go b/backend/plugins/dora/tasks/change_lead_time_calculator.go index 8e29f1678ee..dc5134cbe20 100644 --- a/backend/plugins/dora/tasks/change_lead_time_calculator.go +++ b/backend/plugins/dora/tasks/change_lead_time_calculator.go @@ -18,6 +18,7 @@ limitations under the License. package tasks import ( + "fmt" "math" "reflect" "time" @@ -46,11 +47,6 @@ func CalculateChangeLeadTime(taskCtx plugin.SubTaskContext) errors.Error { db := taskCtx.GetDal() logger := taskCtx.GetLogger() data := taskCtx.GetData().(*DoraTaskData) - // Clear previous results from the project - err := db.Exec("DELETE FROM project_pr_metrics WHERE project_name = ? ", data.Options.ProjectName) - if err != nil { - return errors.Default.Wrap(err, "error deleting previous project_pr_metrics") - } // Get pull requests by repo project_name var clauses = []dal.Clause{ @@ -64,6 +60,19 @@ func CalculateChangeLeadTime(taskCtx plugin.SubTaskContext) errors.Error { return err } defer cursor.Close() + count, err := db.Count(clauses...) + if err != nil { + return errors.Default.Wrap(err, "error getting count of clauses") + } + if count == 0 { + // Clear previous results from the project + deleteSql := fmt.Sprintf("DELETE FROM project_pr_metrics WHERE project_name = '%s'", data.Options.ProjectName) + err := db.Exec(deleteSql) + if err != nil { + return errors.Default.Wrap(err, "error deleting previous project_pr_metrics") + } + return nil + } converter, err := api.NewDataConverter(api.DataConverterArgs{ RawDataSubTaskArgs: api.RawDataSubTaskArgs{ diff --git a/backend/plugins/dora/tasks/deployment_commits_generator.go b/backend/plugins/dora/tasks/deployment_commits_generator.go index 64133ce06e0..d960f578505 100644 --- a/backend/plugins/dora/tasks/deployment_commits_generator.go +++ b/backend/plugins/dora/tasks/deployment_commits_generator.go @@ -101,38 +101,30 @@ func GenerateDeploymentCommits(taskCtx plugin.SubTaskContext) errors.Error { } if data.Options.ScopeId != nil { clauses = append(clauses, dal.Where(`p.cicd_scope_id = ?`, data.Options.ScopeId)) - // Clear previous results from the project - deleteSql := `DELETE FROM cicd_deployment_commits WHERE cicd_scope_id = ? ;` - err := db.Exec(deleteSql, data.Options.ScopeId) - if err != nil { - return errors.Default.Wrap(err, "error deleting previous cicd_deployment_commits") - } } else { clauses = append(clauses, dal.Join("LEFT JOIN project_mapping pm ON (pm.table = 'cicd_scopes' AND pm.row_id = p.cicd_scope_id)"), dal.Where(`pm.project_name = ?`, data.Options.ProjectName), ) - // Clear previous results from the project - deleteSql := `DELETE FROM cicd_deployment_commits - WHERE cicd_scope_id IN ( - SELECT cicd_scope_id - FROM ( - SELECT cdc.cicd_scope_id - FROM cicd_deployment_commits cdc - LEFT JOIN project_mapping pm ON (pm.table = 'cicd_scopes' AND pm.row_id = cdc.cicd_scope_id) - WHERE pm.project_name = ? - ) AS subquery - );` - err := db.Exec(deleteSql, data.Options.ProjectName) - if err != nil { - return errors.Default.Wrap(err, "error deleting previous cicd_deployment_commits") - } } cursor, err := db.Cursor(clauses...) if err != nil { return err } defer cursor.Close() + count, err := db.Count(clauses...) + if err != nil { + return errors.Default.Wrap(err, "error getting count of clauses") + } + if count == 0 { + // Clear previous results from the project + deleteSql := fmt.Sprintf("DELETE cdc FROM cicd_deployment_commits cdc LEFT JOIN project_mapping pm ON (pm.table = 'cicd_scopes' AND pm.row_id = cdc.cicd_scope_id) WHERE pm.project_name = '%s'", data.Options.ProjectName) + err := db.Exec(deleteSql) + if err != nil { + return errors.Default.Wrap(err, "error deleting previous cicd_deployment_commits") + } + return nil + } enricher, err := api.NewDataConverter(api.DataConverterArgs{ RawDataSubTaskArgs: api.RawDataSubTaskArgs{ diff --git a/backend/plugins/dora/tasks/deployment_generator.go b/backend/plugins/dora/tasks/deployment_generator.go index 237b7216ccf..f7caada5ee3 100644 --- a/backend/plugins/dora/tasks/deployment_generator.go +++ b/backend/plugins/dora/tasks/deployment_generator.go @@ -18,6 +18,7 @@ limitations under the License. package tasks import ( + "fmt" "reflect" "time" @@ -79,39 +80,30 @@ func GenerateDeployment(taskCtx plugin.SubTaskContext) errors.Error { clauses = append(clauses, dal.Where("p.cicd_scope_id = ?", data.Options.ScopeId), ) - // Clear previous results from the cicd_scope_id - deleteSql := `DELETE FROM cicd_deployments WHERE cicd_scope_id = ?;` - err := db.Exec(deleteSql, data.Options.ScopeId) - if err != nil { - return errors.Default.Wrap(err, "error deleting previous deployments") - } } else { clauses = append(clauses, dal.Join("LEFT JOIN project_mapping pm ON (pm.table = 'cicd_scopes' AND pm.row_id = p.cicd_scope_id)"), dal.Where("pm.project_name = ?", data.Options.ProjectName), ) - // Clear previous results from the project - deleteSql := `DELETE FROM cicd_deployments - WHERE cicd_scope_id IN ( - SELECT cicd_scope_id - FROM ( - SELECT cd.cicd_scope_id - FROM cicd_deployments cd - LEFT JOIN project_mapping pm ON (pm.table = 'cicd_scopes' AND pm.row_id = cd.cicd_scope_id) - WHERE pm.project_name = ? - ) AS subquery - );` - err := db.Exec(deleteSql, data.Options.ProjectName) - if err != nil { - return errors.Default.Wrap(err, "error deleting previous deployments") - } } - cursor, err := db.Cursor(clauses...) if err != nil { return err } defer cursor.Close() + count, err := db.Count(clauses...) + if err != nil { + return errors.Default.Wrap(err, "error getting count of clauses") + } + if count == 0 { + // Clear previous results from the project + deleteSql := fmt.Sprintf("DELETE cd FROM cicd_deployments cd LEFT JOIN project_mapping pm ON (pm.table = 'cicd_scopes' AND pm.row_id = cd.cicd_scope_id) WHERE pm.project_name = '%s'", data.Options.ProjectName) + err := db.Exec(deleteSql) + if err != nil { + return errors.Default.Wrap(err, "error deleting previous deployments") + } + return nil + } enricher, err := api.NewDataConverter(api.DataConverterArgs{ RawDataSubTaskArgs: api.RawDataSubTaskArgs{ diff --git a/backend/plugins/dora/tasks/incident_deploy_connector.go b/backend/plugins/dora/tasks/incident_deploy_connector.go index 6d9a1365d63..f2903d03c1a 100644 --- a/backend/plugins/dora/tasks/incident_deploy_connector.go +++ b/backend/plugins/dora/tasks/incident_deploy_connector.go @@ -18,6 +18,7 @@ limitations under the License. package tasks import ( + "fmt" "reflect" "time" @@ -47,11 +48,6 @@ type simpleCicdDeploymentCommit struct { func ConnectIncidentToDeployment(taskCtx plugin.SubTaskContext) errors.Error { db := taskCtx.GetDal() data := taskCtx.GetData().(*DoraTaskData) - // Clear previous results from the project - err := db.Exec("DELETE FROM project_issue_metrics WHERE project_name = ?", data.Options.ProjectName) - if err != nil { - return errors.Default.Wrap(err, "error deleting previous project_issue_metrics") - } // select all issues belongs to the board clauses := []dal.Clause{ dal.From(`issues i`), @@ -67,6 +63,19 @@ func ConnectIncidentToDeployment(taskCtx plugin.SubTaskContext) errors.Error { return err } defer cursor.Close() + count, err := db.Count(clauses...) + if err != nil { + return errors.Default.Wrap(err, "error getting count of clauses") + } + if count == 0 { + // Clear previous results from the project + deleteSql := fmt.Sprintf("DELETE FROM project_issue_metrics WHERE project_name = '%s'", data.Options.ProjectName) + err := db.Exec(deleteSql) + if err != nil { + return errors.Default.Wrap(err, "error deleting previous project_issue_metrics") + } + return nil + } enricher, err := api.NewDataConverter(api.DataConverterArgs{ RawDataSubTaskArgs: api.RawDataSubTaskArgs{