github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/model/alertrecord/alert_bookkeeping.go (about) 1 package alertrecord 2 3 import ( 4 "github.com/evergreen-ci/evergreen/db" 5 "github.com/evergreen-ci/evergreen/db/bsonutil" 6 "gopkg.in/mgo.v2" 7 "gopkg.in/mgo.v2/bson" 8 ) 9 10 const ( 11 Collection = "alertrecord" 12 ) 13 14 // Task triggers 15 var ( 16 TaskFailedId = "task_failed" 17 FirstVersionFailureId = "first_version_failure" 18 FirstVariantFailureId = "first_variant_failure" 19 FirstTaskTypeFailureId = "first_tasktype_failure" 20 TaskFailTransitionId = "task_transition_failure" 21 LastRevisionNotFound = "last_revision_not_found" 22 ) 23 24 // Host triggers 25 var ( 26 SpawnFailed = "spawn_failed" 27 SpawnHostTwoHourWarning = "spawn_twohour" 28 SpawnHostTwelveHourWarning = "spawn_twelvehour" 29 SlowProvisionWarning = "slow_provision" 30 ProvisionFailed = "provision_failed" 31 ) 32 33 type AlertRecord struct { 34 Id bson.ObjectId `bson:"_id"` 35 Type string `bson:"type"` 36 HostId string `bson:"host_id,omitempty"` 37 TaskId string `bson:"task_id,omitempty"` 38 ProjectId string `bson:"project_id,omitempty"` 39 VersionId string `bson:"version_id,omitempty"` 40 TaskName string `bson:"task_name,omitempty"` 41 Variant string `bson:"variant,omitempty"` 42 RevisionOrderNumber int `bson:"order,omitempty"` 43 } 44 45 var ( 46 IdKey = bsonutil.MustHaveTag(AlertRecord{}, "Id") 47 TypeKey = bsonutil.MustHaveTag(AlertRecord{}, "Type") 48 TaskIdKey = bsonutil.MustHaveTag(AlertRecord{}, "TaskId") 49 HostIdKey = bsonutil.MustHaveTag(AlertRecord{}, "HostId") 50 TaskNameKey = bsonutil.MustHaveTag(AlertRecord{}, "TaskName") 51 VariantKey = bsonutil.MustHaveTag(AlertRecord{}, "Variant") 52 ProjectIdKey = bsonutil.MustHaveTag(AlertRecord{}, "ProjectId") 53 VersionIdKey = bsonutil.MustHaveTag(AlertRecord{}, "VersionId") 54 RevisionOrderNumberKey = bsonutil.MustHaveTag(AlertRecord{}, "RevisionOrderNumber") 55 ) 56 57 // FindOne gets one AlertRecord for the given query. 58 func FindOne(query db.Q) (*AlertRecord, error) { 59 alertRec := &AlertRecord{} 60 err := db.FindOneQ(Collection, query, alertRec) 61 if err == mgo.ErrNotFound { 62 return nil, nil 63 } 64 return alertRec, err 65 } 66 67 // ByPreviousFailureTransition finds the last alert record that was stored for a task/variant 68 // within a given project. 69 // This can be used to determine whether or not a newly detected failure transition should trigger 70 // an alert. If an alert was already sent for a failed task, which transitioned from a succsesfulwhose commit order number is 71 func ByLastFailureTransition(taskName, variant, projectId string) db.Q { 72 return db.Query(bson.M{ 73 TypeKey: TaskFailTransitionId, 74 TaskNameKey: taskName, 75 VariantKey: variant, 76 ProjectIdKey: projectId, 77 }).Sort([]string{"-" + RevisionOrderNumberKey}).Limit(1) 78 } 79 80 func ByFirstFailureInVersion(projectId, versionId string) db.Q { 81 return db.Query(bson.M{ 82 TypeKey: FirstVersionFailureId, 83 VersionIdKey: versionId, 84 }).Limit(1) 85 } 86 87 func ByFirstFailureInVariant(versionId, variant string) db.Q { 88 return db.Query(bson.M{ 89 TypeKey: FirstVariantFailureId, 90 VersionIdKey: versionId, 91 VariantKey: variant, 92 }).Limit(1) 93 } 94 func ByFirstFailureInTaskType(versionId, taskName string) db.Q { 95 return db.Query(bson.M{ 96 TypeKey: FirstTaskTypeFailureId, 97 VersionIdKey: versionId, 98 TaskNameKey: taskName, 99 }).Limit(1) 100 } 101 102 func ByHostAlertRecordType(hostId, triggerId string) db.Q { 103 return db.Query(bson.M{ 104 TypeKey: triggerId, 105 HostIdKey: hostId, 106 }).Limit(1) 107 } 108 109 func ByLastRevNotFound(projectId, versionId string) db.Q { 110 return db.Query(bson.M{ 111 TypeKey: LastRevisionNotFound, 112 ProjectIdKey: projectId, 113 VersionIdKey: versionId, 114 }).Limit(1) 115 } 116 117 func (ar *AlertRecord) Insert() error { 118 return db.Insert(Collection, ar) 119 }