File size: 1,896 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package alertlog

import (
	"database/sql/driver"
	"fmt"
)

// SubjectType represents the type of subject or causer of an alert event.
type SubjectType string

// Possible subject types
const (
	SubjectTypeUser             SubjectType = "user"
	SubjectTypeIntegrationKey   SubjectType = "integration_key"
	SubjectTypeHeartbeatMonitor SubjectType = "heartbeat_monitor"
	SubjectTypeChannel          SubjectType = "channel"
	SubjectTypeNone             SubjectType = ""
)

// Scan handles reading a Type from the DB enum
func (s *SubjectType) Scan(value interface{}) error {
	switch t := value.(type) {
	case []byte:
		*s = SubjectType(t)
	case string:
		*s = SubjectType(t)
	case nil:
		*s = SubjectTypeNone
	default:
		return fmt.Errorf("could not process unknown type %T", t)
	}

	return nil
}
func (s SubjectType) Value() (driver.Value, error) {
	switch s {
	case SubjectTypeUser, SubjectTypeIntegrationKey, SubjectTypeHeartbeatMonitor, SubjectTypeChannel:
		return string(s), nil
	default:
		return nil, nil
	}
}

// A Subject is generally the causer of an event. If a user closes an alert,
// the entry would have a Subject set to the user.
type Subject struct {
	ID         string      `json:"id"`
	Name       string      `json:"name"`
	Type       SubjectType `json:"type"`
	Classifier string      `json:"classifier"`
}

func subjectString(infinitive bool, s *Subject) string {
	if s == nil {
		return ""
	}
	var str string
	if infinitive {
		str += " to"
	} else {
		switch s.Type {
		case SubjectTypeUser:
			str += " by"
		case SubjectTypeNone:
			return ""
		default:
			str += " via"
		}
	}
	if s.Name == "" {
		str += " [unknown]"
	} else {
		str += " " + s.Name
	}
	switch s.Type {
	case SubjectTypeIntegrationKey:
		str += " integration"
	case SubjectTypeHeartbeatMonitor:
		str += " heartbeat monitor"
	}

	if s.Classifier != "" {
		str += " (" + s.Classifier + ")"
	}
	return str
}