github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/notify_times.go (about) 1 package model 2 3 import ( 4 "time" 5 6 "github.com/evergreen-ci/evergreen/db" 7 "github.com/evergreen-ci/evergreen/db/bsonutil" 8 "github.com/pkg/errors" 9 "gopkg.in/mgo.v2" 10 "gopkg.in/mgo.v2/bson" 11 ) 12 13 var EarliestDateToConsider time.Time 14 15 const ( 16 NotifyTimesCollection = "notify_times" 17 ) 18 19 type ProjectNotificationTime struct { 20 ProjectName string `bson:"_id"` 21 LastNotificationEventTime time.Time `bson:"last_notification_event_time"` 22 } 23 24 var ( 25 PntProjectNameKey = bsonutil.MustHaveTag(ProjectNotificationTime{}, 26 "ProjectName") 27 PntLastEventTime = bsonutil.MustHaveTag(ProjectNotificationTime{}, 28 "LastNotificationEventTime") 29 ) 30 31 // Record the last-notification time for a given project. 32 func SetLastNotificationsEventTime(projectName string, 33 timeOfEvent time.Time) error { 34 _, err := db.Upsert( 35 NotifyTimesCollection, 36 bson.M{ 37 PntProjectNameKey: projectName, 38 }, 39 bson.M{ 40 "$set": bson.M{ 41 PntLastEventTime: timeOfEvent, 42 }, 43 }, 44 ) 45 return err 46 } 47 48 func LastNotificationsEventTime(projectName string) (time.Time, 49 error) { 50 51 nAnswers, err := db.Count( 52 NotifyTimesCollection, 53 bson.M{ 54 PntProjectNameKey: projectName, 55 }, 56 ) 57 58 if err != nil { 59 return EarliestDateToConsider, err 60 } 61 if nAnswers == 0 { 62 return EarliestDateToConsider, nil 63 } 64 65 if nAnswers > 1 { 66 return EarliestDateToConsider, errors.Errorf("There are %v notification"+ 67 " times listed for having seen the NOTIFICATION_REPOSITORY ā%vā;"+ 68 " there should be at most one.", nAnswers, projectName) 69 } 70 71 event := &ProjectNotificationTime{} 72 err = db.FindOne( 73 NotifyTimesCollection, 74 bson.M{ 75 PntProjectNameKey: projectName, 76 }, 77 db.NoProjection, 78 db.NoSort, 79 event, 80 ) 81 if err != nil { 82 return EarliestDateToConsider, err 83 } 84 if err == mgo.ErrNotFound { 85 return EarliestDateToConsider, nil 86 } 87 88 return event.LastNotificationEventTime, nil 89 }