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

graphql: update gqlgen to v0.10.1 #200

Merged
merged 19 commits into from
Nov 26, 2019
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
174 changes: 159 additions & 15 deletions alert/log/entry.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,171 @@
package alertlog

import (
"context"
"database/sql"
"encoding/json"
"fmt"
"time"

"github.com/target/goalert/util/log"
)

// An Entry is an alert log entry.
type Entry interface {
// AlertID returns the ID of the alert the Entry belongs to.
AlertID() int
type Entry struct {
id int
alertID int
timestamp time.Time
_type Type
message string
subject struct {
_type SubjectType
userID sql.NullString
userName sql.NullString
integrationKeyID sql.NullString
integrationKeyName sql.NullString
heartbeatMonitorID sql.NullString
heartbeatMonitorName sql.NullString
channelID sql.NullString
channelName sql.NullString
classifier string
}
meta rawJSON
}

func (e Entry) Meta() interface{} {
switch e.Type() {
case TypeEscalated:
var esc EscalationMetaData
err := json.Unmarshal(e.meta, &esc)
if err != nil {
log.Debug(context.Background(), err)
return nil
}
return &esc
}

return nil
}
func (e Entry) AlertID() int {
return e.alertID
}

func (e Entry) ID() int {
return e.id
}

func (e Entry) Timestamp() time.Time {
return e.timestamp
}
func (e Entry) Type() Type {
switch e._type {
case _TypeResponseReceived:
return respRecvType(e.message)
case _TypeStatusChanged:
return statChgType(e.message)
}

return e._type
}

func (e Entry) Subject() *Subject {
if e.subject._type == SubjectTypeNone {
if e.message != "" {
return e.subjectFromMessage()
}
return nil
}

s := &Subject{
Type: e.subject._type,
Classifier: e.subject.classifier,
}

switch s.Type {
case SubjectTypeUser:
s.ID = e.subject.userID.String
s.Name = e.subject.userName.String
case SubjectTypeIntegrationKey:
s.ID = e.subject.integrationKeyID.String
s.Name = e.subject.integrationKeyName.String
case SubjectTypeHeartbeatMonitor:
s.ID = e.subject.heartbeatMonitorID.String
s.Name = e.subject.heartbeatMonitorName.String
case SubjectTypeChannel:
s.ID = e.subject.channelID.String
s.Name = e.subject.channelName.String
}

// ID returns the ID of the log entry.
ID() int
// Timestamp returns the time the Entry was created.
Timestamp() time.Time
return s
}

func escalationMsg(m *EscalationMetaData) string {
msg := fmt.Sprintf(" to step #%d", m.NewStepIndex+1)
if m.Repeat {
msg += " (policy repeat)"
}
if m.Forced {
msg += " due to manual escalation"
} else if m.Deleted {
msg += " due to current step being deleted"
} else if m.OldDelayMinutes > 0 {
msg += fmt.Sprintf(" automatically after %d minutes", m.OldDelayMinutes)
}

// Type returns type type of log entry.
Type() Type
return msg
}

// Subject will return the subject, if available of the Entry.
Subject() *Subject
func (e Entry) String() string {
var msg string
var infinitive bool
switch e.Type() {
case TypeCreated:
msg = "Created"
case TypeAcknowledged:
msg = "Acknowledged"
case TypeClosed:
msg = "Closed"
case TypeEscalated:
msg = "Escalated"
meta, ok := e.Meta().(*EscalationMetaData)
if ok {
msg += escalationMsg(meta)
}
case TypeNotificationSent:
msg = "Notification sent"
infinitive = true
case TypePolicyUpdated:
msg = "Policy updated"
case TypeDuplicateSupressed:
msg = "Suppressed duplicate: created"
case TypeEscalationRequest:
msg = "Escalation requested"
default:
return "Error"
}

// String returns the string representation of a log Event.
String() string
// include subject, if available
msg += subjectString(infinitive, e.Subject())

return msg
}

Meta() interface{}
func (e *Entry) scanWith(scan func(...interface{}) error) error {
return scan(
&e.id,
&e.alertID,
&e.timestamp,
&e._type,
&e.message,
&e.subject._type,
&e.subject.userID,
&e.subject.userName,
&e.subject.integrationKeyID,
&e.subject.integrationKeyName,
&e.subject.heartbeatMonitorID,
&e.subject.heartbeatMonitorName,
&e.subject.channelID,
&e.subject.channelName,
&e.subject.classifier,
&e.meta,
)
}
2 changes: 1 addition & 1 deletion alert/log/legacylogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
)

func (e *rawEntry) subjectFromMessage() *Subject {
func (e *Entry) subjectFromMessage() *Subject {
switch e._type {
case TypeCreated:
return createdSubject(e.message)
Expand Down
15 changes: 5 additions & 10 deletions alert/log/legacysearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
"context"
"database/sql"
"fmt"
"github.com/target/goalert/util/sqlutil"
"time"

"github.com/pkg/errors"
"github.com/target/goalert/permission"
"github.com/target/goalert/search"
"github.com/target/goalert/util/sqlutil"
"github.com/target/goalert/validation"
"github.com/target/goalert/validation/validate"

"github.com/pkg/errors"
)

// LegacySearchOptions contains criteria for filtering alert logs. At a minimum, at least one of AlertID or ServiceID must be specified.
Expand Down Expand Up @@ -213,21 +212,17 @@ func (db *DB) LegacySearch(ctx context.Context, opts *LegacySearchOptions) ([]En
}
defer rows.Close()

var result []rawEntry
var result []Entry

for rows.Next() {
var r rawEntry
var r Entry
err = r.scanWith(rows.Scan)
if err != nil {
return nil, 0, err
}
result = append(result, r)
}
var logs []Entry
for _, e := range result {
logs = append(logs, e)
}

return logs, total, nil
return result, total, nil

}
Loading