Skip to content

Commit

Permalink
Set status_code when saving new HTTP log objects
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Sep 21, 2021
1 parent 2f5f495 commit 95b647b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 22 deletions.
1 change: 1 addition & 0 deletions core/handlers/airtime_transferred.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func handleAirtimeTransferred(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool,
transfer.AddLog(models.NewAirtimeTransferredLog(
oa.OrgID(),
httpLog.URL,
httpLog.StatusCode,
httpLog.Request,
httpLog.Response,
httpLog.Status != flows.CallStatusSuccess,
Expand Down
2 changes: 2 additions & 0 deletions core/handlers/service_called.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func handleServiceCalled(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *m
oa.OrgID(),
classifier.ID(),
httpLog.URL,
httpLog.StatusCode,
httpLog.Request,
httpLog.Response,
httpLog.Status != flows.CallStatusSuccess,
Expand All @@ -65,6 +66,7 @@ func handleServiceCalled(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *m
oa.OrgID(),
ticketer.ID(),
httpLog.URL,
httpLog.StatusCode,
httpLog.Request,
httpLog.Response,
httpLog.Status != flows.CallStatusSuccess,
Expand Down
2 changes: 1 addition & 1 deletion core/handlers/webhook_called.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func handleWebhookCalled(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *m
httpLog := models.NewWebhookCalledLog(
oa.OrgID(),
flow.(*models.Flow).ID(),
event.URL, event.Request, event.Response,
event.URL, event.StatusCode, event.Request, event.Response,
event.Status != flows.CallStatusSuccess,
time.Millisecond*time.Duration(event.ElapsedMS), event.CreatedOn(),
)
Expand Down
10 changes: 6 additions & 4 deletions core/handlers/webhook_called_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func TestWebhookCalled(t *testing.T) {
SQLAssertions: []handlers.SQLAssertion{
{
SQL: "select count(*) from api_resthooksubscriber where is_active = FALSE",
Args: nil,
Count: 1,
},
{
Expand All @@ -63,12 +62,15 @@ func TestWebhookCalled(t *testing.T) {
},
{
SQL: "select count(*) from api_resthooksubscriber where is_active = TRUE",
Args: nil,
Count: 2,
},
{
SQL: "select count(*) from request_logs_httplog where log_type = 'webhook_called' AND flow_id IS NOT NULL",
Count: 5,
SQL: "select count(*) from request_logs_httplog where log_type = 'webhook_called' AND flow_id IS NOT NULL AND status_code = 200",
Count: 2,
},
{
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",
Expand Down
24 changes: 13 additions & 11 deletions core/models/http_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ type HTTPLog struct {
AirtimeTransferID AirtimeTransferID `db:"airtime_transfer_id"`
}

func newHTTPLog(orgID OrgID, logType HTTPLogType, url string, request string, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
func newHTTPLog(orgID OrgID, logType HTTPLogType, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
return &HTTPLog{
OrgID: orgID,
LogType: logType,
URL: url,
StatusCode: statusCode,
Request: request,
Response: null.String(response),
IsError: isError,
Expand All @@ -64,29 +65,29 @@ func newHTTPLog(orgID OrgID, logType HTTPLogType, url string, request string, re
}

// NewWebhookCalledLog creates a new HTTP log for an in-flow webhook call
func NewWebhookCalledLog(orgID OrgID, fid FlowID, url string, request string, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
h := newHTTPLog(orgID, LogTypeWebhookCalled, url, request, response, isError, elapsed, createdOn)
func NewWebhookCalledLog(orgID OrgID, fid FlowID, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
h := newHTTPLog(orgID, LogTypeWebhookCalled, url, statusCode, request, response, isError, elapsed, createdOn)
h.FlowID = fid
return h
}

// NewClassifierCalledLog creates a new HTTP log for a classifier call
func NewClassifierCalledLog(orgID OrgID, cid ClassifierID, url string, request string, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
h := newHTTPLog(orgID, LogTypeClassifierCalled, url, request, response, isError, elapsed, createdOn)
func NewClassifierCalledLog(orgID OrgID, cid ClassifierID, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
h := newHTTPLog(orgID, LogTypeClassifierCalled, url, statusCode, request, response, isError, elapsed, createdOn)
h.ClassifierID = cid
return h
}

// NewTicketerCalledLog creates a new HTTP log for a ticketer call
func NewTicketerCalledLog(orgID OrgID, tid TicketerID, url string, request string, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
h := newHTTPLog(orgID, LogTypeTicketerCalled, url, request, response, isError, elapsed, createdOn)
func NewTicketerCalledLog(orgID OrgID, tid TicketerID, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
h := newHTTPLog(orgID, LogTypeTicketerCalled, url, statusCode, request, response, isError, elapsed, createdOn)
h.TicketerID = tid
return h
}

// NewAirtimeTransferredLog creates a new HTTP log for an airtime transfer
func NewAirtimeTransferredLog(orgID OrgID, url string, request string, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
return newHTTPLog(orgID, LogTypeAirtimeTransferred, url, request, response, isError, elapsed, createdOn)
func NewAirtimeTransferredLog(orgID OrgID, url string, statusCode int, request, response string, isError bool, elapsed time.Duration, createdOn time.Time) *HTTPLog {
return newHTTPLog(orgID, LogTypeAirtimeTransferred, url, statusCode, request, response, isError, elapsed, createdOn)
}

// SetAirtimeTransferID called to set the transfer ID on a log after the transfer has been created
Expand All @@ -95,8 +96,8 @@ func (h *HTTPLog) SetAirtimeTransferID(tid AirtimeTransferID) {
}

const insertHTTPLogsSQL = `
INSERT INTO request_logs_httplog( log_type, org_id, flow_id, classifier_id, ticketer_id, airtime_transfer_id, url, request, response, is_error, request_time, created_on)
VALUES(:log_type, :org_id, :flow_id, :classifier_id, :ticketer_id, :airtime_transfer_id, :url, :request, :response, :is_error, :request_time, :created_on)
INSERT INTO request_logs_httplog( log_type, org_id, url, status_code, flow_id, classifier_id, ticketer_id, airtime_transfer_id, request, response, is_error, request_time, created_on)
VALUES(:log_type, :org_id, :url, :status_code, :flow_id, :classifier_id, :ticketer_id, :airtime_transfer_id, :request, :response, :is_error, :request_time, :created_on)
RETURNING id
`

Expand Down Expand Up @@ -146,6 +147,7 @@ func (h *HTTPLogger) Ticketer(t *Ticketer) flows.HTTPLogCallback {
t.OrgID(),
t.ID(),
l.URL,
l.StatusCode,
l.Request,
l.Response,
l.Status != flows.CallStatusSuccess,
Expand Down
12 changes: 6 additions & 6 deletions core/models/http_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ func TestHTTPLogs(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

// insert a classifier log
log := models.NewClassifierCalledLog(testdata.Org1.ID, testdata.Wit.ID, "http://foo.bar", "GET /", "STATUS 200", false, time.Second, time.Now())
log := models.NewClassifierCalledLog(testdata.Org1.ID, testdata.Wit.ID, "http://foo.bar", 200, "GET /", "STATUS 200", false, time.Second, time.Now())
err := models.InsertHTTPLogs(ctx, db, []*models.HTTPLog{log})
assert.Nil(t, err)

testsuite.AssertQuery(t, db, `SELECT count(*) from request_logs_httplog WHERE org_id = $1 AND classifier_id = $2 AND is_error = FALSE`, testdata.Org1.ID, testdata.Wit.ID).Returns(1)
testsuite.AssertQuery(t, db, `SELECT count(*) from request_logs_httplog WHERE org_id = $1 AND status_code = 200 AND classifier_id = $2 AND is_error = FALSE`, testdata.Org1.ID, testdata.Wit.ID).Returns(1)

// insert a log with nil response
log = models.NewClassifierCalledLog(testdata.Org1.ID, testdata.Wit.ID, "http://foo.bar", "GET /", "", true, time.Second, time.Now())
log = models.NewClassifierCalledLog(testdata.Org1.ID, testdata.Wit.ID, "http://foo.bar", 0, "GET /", "", true, time.Second, time.Now())
err = models.InsertHTTPLogs(ctx, db, []*models.HTTPLog{log})
assert.Nil(t, err)

testsuite.AssertQuery(t, db, `SELECT count(*) from request_logs_httplog WHERE org_id = $1 AND classifier_id = $2 AND is_error = TRUE AND response IS NULL`, testdata.Org1.ID, testdata.Wit.ID).Returns(1)
testsuite.AssertQuery(t, db, `SELECT count(*) from request_logs_httplog WHERE org_id = $1 AND status_code = 0 AND classifier_id = $2 AND is_error = TRUE AND response IS NULL`, testdata.Org1.ID, testdata.Wit.ID).Returns(1)

// insert a webhook log
log = models.NewWebhookCalledLog(testdata.Org1.ID, testdata.Favorites.ID, "http://foo.bar", "GET /", "HTTP 200", false, time.Second, time.Now())
log = models.NewWebhookCalledLog(testdata.Org1.ID, testdata.Favorites.ID, "http://foo.bar", 400, "GET /", "HTTP 200", false, time.Second, time.Now())
err = models.InsertHTTPLogs(ctx, db, []*models.HTTPLog{log})
assert.Nil(t, err)

testsuite.AssertQuery(t, db, `SELECT count(*) from request_logs_httplog WHERE org_id = $1 AND flow_id = $2`, testdata.Org1.ID, testdata.Favorites.ID).Returns(1)
testsuite.AssertQuery(t, db, `SELECT count(*) from request_logs_httplog WHERE org_id = $1 AND status_code = 400 AND flow_id = $2`, testdata.Org1.ID, testdata.Favorites.ID).Returns(1)
}

func TestHTTPLogger(t *testing.T) {
Expand Down

0 comments on commit 95b647b

Please sign in to comment.