Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin/message-log: reflect sent time in sort order #2742

Merged
merged 3 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions graphql2/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions graphql2/graphqlapp/messagelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,14 @@ func (q *Query) MessageLogs(ctx context.Context, opts *graphql2.MessageLogSearch
}

dm := graphql2.DebugMessage{
ID: log.ID,
CreatedAt: log.CreatedAt,
UpdatedAt: log.LastStatusAt,
Type: strings.TrimPrefix(log.MessageType.String(), "MessageType"),
Status: msgStatus(notification.Status{State: log.LastStatus, Details: log.StatusDetails}),
AlertID: &log.AlertID,
ID: log.ID,
CreatedAt: log.CreatedAt,
UpdatedAt: log.LastStatusAt,
Type: strings.TrimPrefix(log.MessageType.String(), "MessageType"),
Status: msgStatus(notification.Status{State: log.LastStatus, Details: log.StatusDetails}),
AlertID: &log.AlertID,
RetryCount: log.RetryCount,
SentAt: log.SentAt,
}
if dest.ID != "" {
dm.Destination, err = q.formatDest(ctx, dest)
Expand Down
28 changes: 15 additions & 13 deletions graphql2/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion graphql2/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ type DebugMessage {
serviceName: String
alertID: Int
providerID: ID
sentAt: ISOTimestamp
retryCount: Int!
}

input MessageLogSearchOptions {
Expand Down Expand Up @@ -291,7 +293,6 @@ input UserOverrideSearchOptions {
end: ISOTimestamp # end of the window to search for.
}


type UserOverrideConnection {
nodes: [UserOverride!]!
pageInfo: PageInfo!
Expand Down
17 changes: 14 additions & 3 deletions notification/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type MessageLog struct {

ServiceID string
ServiceName string

SentAt *time.Time
RetryCount int
}

// SearchOptions allow filtering and paginating the list of messages.
Expand All @@ -62,7 +65,8 @@ var searchTemplate = template.Must(template.New("search").Funcs(search.Helpers()
SELECT
om.id, om.created_at, om.last_status_at, om.message_type, om.last_status, om.status_details,
om.src_value, om.alert_id, om.provider_msg_id,
om.user_id, u.name, om.contact_method_id, om.channel_id, om.service_id, s.name
om.user_id, u.name, om.contact_method_id, om.channel_id, om.service_id, s.name,
om.sent_at, om.retry_count
FROM outgoing_messages om
LEFT JOIN users u ON om.user_id = u.id
LEFT JOIN services s ON om.service_id = s.id
Expand Down Expand Up @@ -98,7 +102,7 @@ var searchTemplate = template.Must(template.New("search").Funcs(search.Helpers()
OR (om.created_at = :cursorCreatedAt AND om.id > :afterID)
{{end}}
AND om.last_status != 'bundled'
ORDER BY om.created_at desc, om.id asc
ORDER BY om.last_status = 'pending' desc, coalesce(om.sent_at, om.last_status_at) desc, om.created_at desc, om.id asc
LIMIT {{.Limit}}
`))

Expand Down Expand Up @@ -166,13 +170,14 @@ func (s *Store) Search(ctx context.Context, opts *SearchOptions) ([]MessageLog,
for rows.Next() {
var l MessageLog
var alertID sql.NullInt64
var retryCount sql.NullInt32
var chanID sqlutil.NullUUID
var serviceID, svcName sql.NullString
var srcValue sql.NullString
var userID, userName sql.NullString
var cmID sql.NullString
var providerID sql.NullString
var lastStatusAt sql.NullTime
var lastStatusAt, sentAt sql.NullTime
err = rows.Scan(
&l.ID,
&l.CreatedAt,
Expand All @@ -189,6 +194,8 @@ func (s *Store) Search(ctx context.Context, opts *SearchOptions) ([]MessageLog,
&chanID,
&serviceID,
&svcName,
&sentAt,
&retryCount,
)
if err != nil {
return nil, err
Expand All @@ -211,6 +218,10 @@ func (s *Store) Search(ctx context.Context, opts *SearchOptions) ([]MessageLog,
l.UserName = userName.String
l.ContactMethodID = cmID.String
l.LastStatusAt = lastStatusAt.Time
if sentAt.Valid {
l.SentAt = &sentAt.Time
}
l.RetryCount = int(retryCount.Int32)

result = append(result, l)
}
Expand Down
79 changes: 79 additions & 0 deletions test/smoke/graphqladminmessagelogs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package smoke

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/target/goalert/test/smoke/harness"
)

// TestGraphQLAdminMessageLogs tests that logs are properly generated for messages.
func TestGraphQLAdminMessageLogs(t *testing.T) {
t.Parallel()

const sql = `
insert into users (id, name, email)
values
({{uuid "user"}}, 'bob', 'joe');
insert into user_contact_methods (id, user_id, name, type, value, disabled)
values
({{uuid "cm1"}}, {{uuid "user"}}, 'personal', 'SMS', {{phone "1"}}, true);
insert into outgoing_messages (id, message_type, created_at, sent_at, contact_method_id, last_status, user_id)
values
({{uuid "om1"}}, 'test_notification', '2022-01-01 00:01:00', '2022-01-01 00:01:01', {{uuid "cm1"}}, 'delivered', {{uuid "user"}}),
({{uuid "om2"}}, 'test_notification', '2022-01-01 00:02:00', '2022-01-01 00:01:02', {{uuid "cm1"}}, 'delivered', {{uuid "user"}}),
({{uuid "om3"}}, 'test_notification', '2022-01-01 00:03:00', null, {{uuid "cm1"}}, 'failed', {{uuid "user"}}),
({{uuid "om4"}}, 'test_notification', '2022-01-01 00:05:00', null, {{uuid "cm1"}}, 'pending', {{uuid "user"}}),
({{uuid "om5"}}, 'test_notification', '2022-01-01 00:04:00', '2022-01-01 00:01:04', {{uuid "cm1"}}, 'delivered', {{uuid "user"}});
`

h := harness.NewHarness(t, sql, "switchover-mk2")
defer h.Close()

doQL := func(query string, res interface{}) {
g := h.GraphQLQuery2(query)
for _, err := range g.Errors {
t.Error("GraphQL Error:", err.Message)
}
if len(g.Errors) > 0 {
t.Fatal("errors returned from GraphQL")
}
t.Log("Response:", string(g.Data))
if res == nil {
return
}
err := json.Unmarshal(g.Data, &res)
if err != nil {
t.Fatal("failed to parse response:", err)
}
}

type messageLogs struct {
MessageLogs struct {
Nodes []struct {
ID string `json:"id"`
} `json:"nodes"`
} `json:"messageLogs"`
}

var logs messageLogs

doQL(`query {
messageLogs(input: {}) {
nodes {
id
}
}
}`, &logs)

// tests that the message logs are returned in the correct order
// of not sent then most recent to least recent
assert.Len(t, logs.MessageLogs.Nodes, 5, "messageLogs query")
assert.Equal(t, h.UUID("om4"), logs.MessageLogs.Nodes[0].ID)
assert.Equal(t, h.UUID("om3"), logs.MessageLogs.Nodes[1].ID)
assert.Equal(t, h.UUID("om5"), logs.MessageLogs.Nodes[2].ID)
assert.Equal(t, h.UUID("om2"), logs.MessageLogs.Nodes[3].ID)
assert.Equal(t, h.UUID("om1"), logs.MessageLogs.Nodes[4].ID)

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const query = gql`
serviceName
alertID
providerID
retryCount
sentAt
}
pageInfo {
hasNextPage
Expand Down
Loading