Skip to content

Commit

Permalink
Revert "fix: empty dora data when run the tasks (#7070) (#7072)"
Browse files Browse the repository at this point in the history
This reverts commit f82045d.
  • Loading branch information
abeizn committed Mar 7, 2024
1 parent f82045d commit f83a09f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 53 deletions.
19 changes: 14 additions & 5 deletions backend/plugins/dora/tasks/change_lead_time_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package tasks

import (
"fmt"
"math"
"reflect"
"time"
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down
34 changes: 13 additions & 21 deletions backend/plugins/dora/tasks/deployment_commits_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
36 changes: 14 additions & 22 deletions backend/plugins/dora/tasks/deployment_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package tasks

import (
"fmt"
"reflect"
"time"

Expand Down Expand Up @@ -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{
Expand Down
19 changes: 14 additions & 5 deletions backend/plugins/dora/tasks/incident_deploy_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package tasks

import (
"fmt"
"reflect"
"time"

Expand Down Expand Up @@ -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`),
Expand All @@ -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{
Expand Down

0 comments on commit f83a09f

Please sign in to comment.