github.com/Financial-Times/publish-availability-monitor@v1.12.0/feeds/baseNotificationsFeed.go (about) 1 package feeds 2 3 import ( 4 "strings" 5 "sync" 6 "time" 7 8 "github.com/Financial-Times/publish-availability-monitor/httpcaller" 9 ) 10 11 type baseNotificationsFeed struct { 12 feedName string 13 httpCaller httpcaller.Caller 14 baseURL string 15 username string 16 password string 17 expiry int 18 notifications map[string][]*Notification 19 notificationsLock *sync.RWMutex 20 } 21 22 func parseUUIDFromURL(url string) string { 23 i := strings.LastIndex(url, "/") 24 return url[i+1:] 25 } 26 27 func (f *baseNotificationsFeed) FeedName() string { 28 return f.feedName 29 } 30 31 func (f *baseNotificationsFeed) SetCredentials(username string, password string) { 32 f.username = username 33 f.password = password 34 } 35 36 func (f *baseNotificationsFeed) SetHTTPCaller(httpCaller httpcaller.Caller) { 37 f.httpCaller = httpCaller 38 } 39 40 func (f *baseNotificationsFeed) purgeObsoleteNotifications() { 41 earliest := time.Now().Add(time.Duration(-f.expiry) * time.Second).Format(time.RFC3339) 42 empty := make([]string, 0) 43 44 f.notificationsLock.Lock() 45 defer f.notificationsLock.Unlock() 46 47 for u, n := range f.notifications { 48 earliestIndex := 0 49 for _, e := range n { 50 if strings.Compare(e.LastModified, earliest) >= 0 { 51 break 52 } else { 53 earliestIndex++ 54 } 55 } 56 f.notifications[u] = n[earliestIndex:] 57 58 if len(f.notifications[u]) == 0 { 59 empty = append(empty, u) 60 } 61 } 62 63 for _, u := range empty { 64 delete(f.notifications, u) 65 } 66 } 67 68 func (f *baseNotificationsFeed) NotificationsFor(uuid string) []*Notification { 69 var history []*Notification 70 var found bool 71 72 f.notificationsLock.RLock() 73 defer f.notificationsLock.RUnlock() 74 75 if history, found = f.notifications[uuid]; !found { 76 history = make([]*Notification, 0) 77 } 78 79 return history 80 } 81 82 func (f *baseNotificationsFeed) FeedURL() string { 83 return f.baseURL 84 }