Skip to content

Commit

Permalink
Attach channel logs to channel connections
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Sep 15, 2022
1 parent 9963686 commit c0c6d3b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
14 changes: 10 additions & 4 deletions core/ivr/ivr.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,13 @@ func HangupCall(ctx context.Context, rt *runtime.Runtime, conn *models.ChannelCo
}
if err != nil {
clog.Error(err)
return clog, errors.Wrapf(err, "error hanging call up")
}
return clog, nil

if err := conn.AttachLog(ctx, rt.DB, clog); err != nil {
logrus.WithError(err).Error("error attaching ivr channel log")
}

return clog, err
}

// RequestCallStart creates a new ChannelSession for the passed in flow start and contact, returning the created session
Expand Down Expand Up @@ -270,10 +274,12 @@ func RequestCallStartForConnection(ctx context.Context, rt *runtime.Runtime, cha
}

// update our channel session
err = conn.UpdateExternalID(ctx, rt.DB, string(callID))
if err != nil {
if err := conn.UpdateExternalID(ctx, rt.DB, string(callID)); err != nil {
return clog, errors.Wrapf(err, "error updating session external id")
}
if err := conn.AttachLog(ctx, rt.DB, clog); err != nil {
logrus.WithError(err).Error("error attaching ivr channel log")
}

return clog, nil
}
Expand Down
5 changes: 5 additions & 0 deletions core/models/channel_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ func (c *ChannelConnection) MarkErrored(ctx context.Context, db Queryer, now tim
return nil
}

func (c *ChannelConnection) AttachLog(ctx context.Context, db Queryer, clog *ChannelLog) error {
_, err := db.ExecContext(ctx, `UPDATE channels_channelconnection SET log_uuids = array_append(log_uuids, $2) WHERE id = $1`, c.c.ID, clog.UUID())
return errors.Wrap(err, "error attaching log to channel connection")
}

// MarkFailed updates the status for this connection
func (c *ChannelConnection) MarkFailed(ctx context.Context, db Queryer, now time.Time) error {
c.c.Status = ConnectionStatusFailed
Expand Down
2 changes: 2 additions & 0 deletions core/models/channel_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func newChannelLog(t ChannelLogType, ch *Channel, r *httpx.Recorder, redactVals
}
}

func (l *ChannelLog) UUID() ChannelLogUUID { return l.uuid }

func (l *ChannelLog) SetConnection(c *ChannelConnection) {
l.connection = c
}
Expand Down
10 changes: 7 additions & 3 deletions web/ivr/ivr.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,20 @@ func newIVRHandler(handler ivrHandlerFn, logType models.ChannelLogType) web.Hand
clog := models.NewChannelLogForIncoming(logType, ch, recorder, svc.RedactValues(ch))

connection, rerr := handler(ctx, rt, oa, ch, svc, r, recorder.ResponseWriter)
clog.SetConnection(connection)
if connection != nil {
clog.SetConnection(connection)
if err := connection.AttachLog(ctx, rt.DB, clog); err != nil {
logrus.WithError(err).WithField("http_request", r).Error("error attaching ivr channel log")
}
}

if err := recorder.End(); err != nil {
logrus.WithError(err).WithField("http_request", r).Error("error recording IVR request")
}

clog.End()

err = models.InsertChannelLogs(ctx, rt.DB, []*models.ChannelLog{clog})
if err != nil {
if err := models.InsertChannelLogs(ctx, rt.DB, []*models.ChannelLog{clog}); err != nil {
logrus.WithError(err).WithField("http_request", r).Error("error writing ivr channel log")
}

Expand Down

0 comments on commit c0c6d3b

Please sign in to comment.