@@ -2,7 +2,6 @@ package models
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
5
"fmt"
7
6
"log/slog"
8
7
"slices"
@@ -13,14 +12,10 @@ import (
13
12
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
14
13
"github.com/nyaruka/gocommon/aws/dynamo"
15
14
"github.com/nyaruka/gocommon/httpx"
16
- "github.com/nyaruka/gocommon/jsonx"
17
15
"github.com/nyaruka/mailroom/runtime"
18
16
"github.com/nyaruka/mailroom/utils/clogs"
19
17
)
20
18
21
- // ChannelLogID is our type for a channel log id
22
- type ChannelLogID int64
23
-
24
19
const (
25
20
ChannelLogTypeIVRStart clogs.Type = "ivr_start"
26
21
ChannelLogTypeIVRIncoming clogs.Type = "ivr_incoming"
@@ -35,8 +30,7 @@ const (
35
30
type ChannelLog struct {
36
31
* clogs.Log
37
32
38
- channel * Channel
39
- attached bool
33
+ channel * Channel
40
34
}
41
35
42
36
// NewChannelLog creates a new channel log with the given type and channel
@@ -56,21 +50,6 @@ func newChannelLog(t clogs.Type, ch *Channel, r *httpx.Recorder, redactVals []st
56
50
}
57
51
}
58
52
59
- // if we have an error or a non 2XX/3XX http response then log is considered an error
60
- func (l * ChannelLog ) isError () bool {
61
- if len (l .Errors ) > 0 {
62
- return true
63
- }
64
-
65
- for _ , l := range l .HttpLogs {
66
- if l .StatusCode < 200 || l .StatusCode >= 400 {
67
- return true
68
- }
69
- }
70
-
71
- return false
72
- }
73
-
74
53
func (l * ChannelLog ) DynamoKey () runtime.DynamoKey {
75
54
pk := fmt .Sprintf ("cha#%s#%s" , l .channel .UUID (), l .UUID [35 :36 ]) // 16 buckets for each channel
76
55
sk := fmt .Sprintf ("log#%s" , l .UUID )
@@ -96,29 +75,12 @@ func (l *ChannelLog) DynamoItem() (*runtime.DynamoItem, error) {
96
75
"type" : l .Type ,
97
76
"elapsed_ms" : int (l .Elapsed / time .Millisecond ),
98
77
"created_on" : l .CreatedOn ,
78
+ "is_error" : l .IsError (),
99
79
},
100
80
DataGZ : dataGZ ,
101
81
}, nil
102
82
}
103
83
104
- const sqlInsertChannelLog = `
105
- INSERT INTO channels_channellog( uuid, channel_id, log_type, http_logs, errors, is_error, elapsed_ms, created_on)
106
- VALUES(:uuid, :channel_id, :log_type, :http_logs, :errors, :is_error, :elapsed_ms, :created_on)
107
- RETURNING id`
108
-
109
- // channel log to be inserted into the database
110
- type dbChannelLog struct {
111
- ID ChannelLogID `db:"id"`
112
- UUID clogs.UUID `db:"uuid"`
113
- ChannelID ChannelID `db:"channel_id"`
114
- Type clogs.Type `db:"log_type"`
115
- HTTPLogs json.RawMessage `db:"http_logs"`
116
- Errors json.RawMessage `db:"errors"`
117
- IsError bool `db:"is_error"`
118
- ElapsedMS int `db:"elapsed_ms"`
119
- CreatedOn time.Time `db:"created_on"`
120
- }
121
-
122
84
// InsertChannelLogs writes the given channel logs to the db
123
85
func InsertChannelLogs (ctx context.Context , rt * runtime.Runtime , logs []* ChannelLog ) error {
124
86
// write all logs to DynamoDB
@@ -150,30 +112,5 @@ func InsertChannelLogs(ctx context.Context, rt *runtime.Runtime, logs []*Channel
150
112
}
151
113
}
152
114
153
- unattached := make ([]* dbChannelLog , 0 , len (logs ))
154
-
155
- for _ , l := range logs {
156
- if ! l .attached {
157
- // if log isn't attached to a message or call we need to write it to the db so that it's retrievable
158
- unattached = append (unattached , & dbChannelLog {
159
- UUID : l .UUID ,
160
- ChannelID : l .channel .ID (),
161
- Type : l .Type ,
162
- HTTPLogs : jsonx .MustMarshal (l .HttpLogs ),
163
- Errors : jsonx .MustMarshal (l .Errors ),
164
- IsError : l .isError (),
165
- CreatedOn : l .CreatedOn ,
166
- ElapsedMS : int (l .Elapsed / time .Millisecond ),
167
- })
168
- }
169
- }
170
-
171
- if len (unattached ) > 0 {
172
- err := BulkQuery (ctx , "insert channel log" , rt .DB , sqlInsertChannelLog , unattached )
173
- if err != nil {
174
- return fmt .Errorf ("error inserting unattached channel logs: %w" , err )
175
- }
176
- }
177
-
178
115
return nil
179
116
}
0 commit comments