From 2f55e56c07e1b28e6bf96b50a271fb40ed5cc860 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Thu, 23 Sep 2021 13:52:51 -0500 Subject: [PATCH] Stop writing webhook results --- core/handlers/webhook_called.go | 13 ------ core/handlers/webhook_called_test.go | 20 --------- core/hooks/insert_webhook_result.go | 34 --------------- core/models/webhook_results.go | 63 ---------------------------- core/models/webhook_results_test.go | 41 ------------------ 5 files changed, 171 deletions(-) delete mode 100644 core/hooks/insert_webhook_result.go delete mode 100644 core/models/webhook_results.go delete mode 100644 core/models/webhook_results_test.go diff --git a/core/handlers/webhook_called.go b/core/handlers/webhook_called.go index 6ba3d9c92..3979dae50 100644 --- a/core/handlers/webhook_called.go +++ b/core/handlers/webhook_called.go @@ -58,18 +58,5 @@ func handleWebhookCalled(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *m scene.AppendToEventPreCommitHook(hooks.InsertHTTPLogsHook, httpLog) } - // TODO drop this once RP UI is switched to using HTTP logs - response := event.Response - if event.Status == flows.CallStatusConnectionError { - response = "connection error" - } - result := models.NewWebhookResult( - oa.OrgID(), scene.ContactID(), - event.URL, event.Request, - event.StatusCode, response, - time.Millisecond*time.Duration(event.ElapsedMS), event.CreatedOn(), - ) - scene.AppendToEventPreCommitHook(hooks.InsertWebhookResultHook, result) - return nil } diff --git a/core/handlers/webhook_called_test.go b/core/handlers/webhook_called_test.go index 56cafcf02..b32e225d9 100644 --- a/core/handlers/webhook_called_test.go +++ b/core/handlers/webhook_called_test.go @@ -72,26 +72,6 @@ func TestWebhookCalled(t *testing.T) { SQL: "select count(*) from request_logs_httplog where log_type = 'webhook_called' AND flow_id IS NOT NULL AND status_code = 410", Count: 3, }, - { - SQL: "select count(*) from api_webhookresult where contact_id = $1 AND status_code = 200", - Args: []interface{}{testdata.Cathy.ID}, - Count: 1, - }, - { - SQL: "select count(*) from api_webhookresult where contact_id = $1 AND status_code = 410", - Args: []interface{}{testdata.Cathy.ID}, - Count: 1, - }, - { - SQL: "select count(*) from api_webhookresult where contact_id = $1", - Args: []interface{}{testdata.George.ID}, - Count: 3, - }, - { - SQL: "select count(*) from api_webhookresult where contact_id = $1", - Args: []interface{}{testdata.George.ID}, - Count: 3, - }, { SQL: "select count(*) from api_webhookevent where org_id = $1", Args: []interface{}{testdata.Org1.ID}, diff --git a/core/hooks/insert_webhook_result.go b/core/hooks/insert_webhook_result.go deleted file mode 100644 index bc7f1a583..000000000 --- a/core/hooks/insert_webhook_result.go +++ /dev/null @@ -1,34 +0,0 @@ -package hooks - -import ( - "context" - - "github.com/nyaruka/mailroom/core/models" - - "github.com/gomodule/redigo/redis" - "github.com/jmoiron/sqlx" - "github.com/pkg/errors" -) - -// InsertWebhookResultHook is our hook for inserting webhook results -var InsertWebhookResultHook models.EventCommitHook = &insertWebhookResultHook{} - -type insertWebhookResultHook struct{} - -// Apply inserts all the webook results that were created -func (h *insertWebhookResultHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, scenes map[*models.Scene][]interface{}) error { - // gather all our results - results := make([]*models.WebhookResult, 0, len(scenes)) - for _, rs := range scenes { - for _, r := range rs { - results = append(results, r.(*models.WebhookResult)) - } - } - - err := models.InsertWebhookResults(ctx, tx, results) - if err != nil { - return errors.Wrapf(err, "error inserting webhook results") - } - - return nil -} diff --git a/core/models/webhook_results.go b/core/models/webhook_results.go deleted file mode 100644 index 2b7be4e9a..000000000 --- a/core/models/webhook_results.go +++ /dev/null @@ -1,63 +0,0 @@ -package models - -import ( - "context" - "time" -) - -type ResultID int64 - -// WebhookResult represents a result of a webhook or resthook call -type WebhookResult struct { - r struct { - ID ResultID `db:"id"` - URL string `db:"url"` - Request string `db:"request"` - StatusCode int `db:"status_code"` - Response string `db:"response"` - RequestTime int `db:"request_time"` - ContactID ContactID `db:"contact_id"` - OrgID OrgID `db:"org_id"` - CreatedOn time.Time `db:"created_on"` - } -} - -func (r *WebhookResult) ID() ResultID { return r.r.ID } - -// NewWebhookResult creates a new webhook result with the passed in parameters -func NewWebhookResult( - orgID OrgID, contactID ContactID, - url string, request string, statusCode int, response string, - elapsed time.Duration, createdOn time.Time) *WebhookResult { - result := &WebhookResult{} - r := &result.r - - r.OrgID = orgID - r.ContactID = contactID - r.URL = url - r.Request = request - r.StatusCode = statusCode - r.Response = response - r.RequestTime = int(elapsed / time.Millisecond) - r.CreatedOn = createdOn - - return result -} - -// InsertWebhookResults will insert the passed in webhook results, setting the ID parameter on each -func InsertWebhookResults(ctx context.Context, db Queryer, results []*WebhookResult) error { - // convert to interface arrray - is := make([]interface{}, len(results)) - for i := range results { - is[i] = &results[i].r - } - - return BulkQuery(ctx, "inserting webhook results", db, insertWebhookResultsSQL, is) -} - -const insertWebhookResultsSQL = ` -INSERT INTO -api_webhookresult( org_id, contact_id, url, request, status_code, response, request_time, created_on) - VALUES(:org_id, :contact_id, :url, :request, :status_code, :response, :request_time, :created_on) -RETURNING id -` diff --git a/core/models/webhook_results_test.go b/core/models/webhook_results_test.go deleted file mode 100644 index 250cba0c9..000000000 --- a/core/models/webhook_results_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package models_test - -import ( - "testing" - "time" - - "github.com/nyaruka/mailroom/core/models" - "github.com/nyaruka/mailroom/testsuite" - "github.com/nyaruka/mailroom/testsuite/testdata" - - "github.com/stretchr/testify/assert" -) - -func TestWebhookResults(t *testing.T) { - ctx, _, db, _ := testsuite.Get() - - tcs := []struct { - OrgID models.OrgID - ContactID models.ContactID - URL string - Request string - StatusCode int - Response string - Duration time.Duration - RequestTime int - }{ - {testdata.Org1.ID, testdata.Cathy.ID, "http://foo.bar", "GET http://foo.bar", 200, "hello world", time.Millisecond * 1501, 1501}, - {testdata.Org1.ID, testdata.Bob.ID, "http://foo.bar", "GET http://foo.bar", 200, "hello world", time.Millisecond * 1502, 1502}, - } - - for _, tc := range tcs { - r := models.NewWebhookResult(tc.OrgID, tc.ContactID, tc.URL, tc.Request, tc.StatusCode, tc.Response, tc.Duration, time.Now()) - err := models.InsertWebhookResults(ctx, db, []*models.WebhookResult{r}) - assert.NoError(t, err) - assert.NotZero(t, r.ID()) - - testsuite.AssertQuery(t, db, - `SELECT count(*) FROM api_webhookresult WHERE org_id = $1 AND contact_id = $2 AND url = $3 AND request = $4 AND status_code = $5 AND response = $6 AND request_time = $7`, - tc.OrgID, tc.ContactID, tc.URL, tc.Request, tc.StatusCode, tc.Response, tc.RequestTime).Returns(1) - } -}