From 68bb3442015d709bbc178cb8fdf728d3f80c31c3 Mon Sep 17 00:00:00 2001 From: rainzm Date: Tue, 12 Apr 2022 13:52:55 +0800 Subject: [PATCH] feat(notify): filter notification by topic_type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 之前的模型,消息和Event是一一对应的,一个Event可以和多个Topic对应, 为了支持通过Topic Type过滤消息,现在Event和Topic是一一对应的。 --- cmd/climc/shell/notifyv2/notification.go | 1 + pkg/apis/notify/notification.go | 1 + pkg/notify/models/event.go | 4 ++- pkg/notify/models/notification.go | 39 ++++++++++++------------ pkg/notify/models/topic.go | 13 ++++++++ 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/cmd/climc/shell/notifyv2/notification.go b/cmd/climc/shell/notifyv2/notification.go index 1638d25b22..1870204122 100644 --- a/cmd/climc/shell/notifyv2/notification.go +++ b/cmd/climc/shell/notifyv2/notification.go @@ -89,6 +89,7 @@ func init() { ContactType string `help:"contact_type"` ReceiverId string `help:"receiver_id"` + TopicType string `help:"topic type"` } R(&NotificationListInput{}, "notify-list", "List notify message", func(s *mcclient.ClientSession, args *NotificationListInput) error { params, err := options.ListStructToParams(args) diff --git a/pkg/apis/notify/notification.go b/pkg/apis/notify/notification.go index 8d1c4c7a0d..c7b596f919 100644 --- a/pkg/apis/notify/notification.go +++ b/pkg/apis/notify/notification.go @@ -85,6 +85,7 @@ type NotificationListInput struct { ContactType string ReceiverId string Tag string + TopicType string } type SContact struct { diff --git a/pkg/notify/models/event.go b/pkg/notify/models/event.go index 855f5456dd..f0b2ed2923 100644 --- a/pkg/notify/models/event.go +++ b/pkg/notify/models/event.go @@ -44,13 +44,15 @@ type SEvent struct { Message string Event string `width:"64" nullable:"true"` AdvanceDays int + TopicId string `width:"128" nullable:"true" index:"true"` } -func (e *SEventManager) CreateEvent(ctx context.Context, event, message string, advanceDays int) (*SEvent, error) { +func (e *SEventManager) CreateEvent(ctx context.Context, event, topicId, message string, advanceDays int) (*SEvent, error) { eve := &SEvent{ Message: message, Event: event, AdvanceDays: advanceDays, + TopicId: topicId, } err := e.TableSpec().Insert(ctx, eve) if err != nil { diff --git a/pkg/notify/models/notification.go b/pkg/notify/models/notification.go index 47471a474f..3c8e00fa1d 100644 --- a/pkg/notify/models/notification.go +++ b/pkg/notify/models/notification.go @@ -231,34 +231,31 @@ func (nm *SNotificationManager) PerformEventNotify(ctx context.Context, userCred } // receiver - topics, err := TopicManager.TopicsByEvent(input.Event, input.AdvanceDays) + topic, err := TopicManager.TopicByEvent(input.Event, input.AdvanceDays) if err != nil { return output, errors.Wrapf(err, "unable fetch subscriptions by event %q", input.Event) } - if len(topics) == 0 { + if topic == nil { return output, nil } var receiverIds []string - for i := range topics { - receiverIds1, err := SubscriberManager.getReceiversSent(ctx, topics[i].Id, input.ProjectDomainId, input.ProjectId) - if err != nil { - return output, errors.Wrap(err, "unable to get receive") - } - receiverIds = append(receiverIds, receiverIds1...) + receiverIds1, err := SubscriberManager.getReceiversSent(ctx, topic.Id, input.ProjectDomainId, input.ProjectId) + if err != nil { + return output, errors.Wrap(err, "unable to get receive") } + receiverIds = append(receiverIds, receiverIds1...) // robot var robots []string - for i := range topics { - _robots, err := SubscriberManager.robot(topics[i].Id, input.ProjectDomainId, input.ProjectId) - if err != nil { - if errors.Cause(err) != errors.ErrNotFound { - return output, errors.Wrapf(err, "unable fetch robot of subscription %q", topics[i].Id) - } - } else { - robots = append(robots, _robots...) + _robots, err := SubscriberManager.robot(topic.Id, input.ProjectDomainId, input.ProjectId) + if err != nil { + if errors.Cause(err) != errors.ErrNotFound { + return output, errors.Wrapf(err, "unable fetch robot of subscription %q", topic.Id) } + } else { + robots = append(robots, _robots...) } + var webhookRobots []string if len(robots) > 0 { robots = sets.NewString(robots...).UnsortedList() @@ -299,13 +296,12 @@ func (nm *SNotificationManager) PerformEventNotify(ctx context.Context, userCred receiverIds = idSet.UnsortedList() // create event - event, err := EventManager.CreateEvent(ctx, input.Event, message, input.AdvanceDays) + event, err := EventManager.CreateEvent(ctx, input.Event, topic.Id, message, input.AdvanceDays) if err != nil { return output, errors.Wrap(err, "unable to create Event") } - if nm.needWebconsole(topics) { - + if nm.needWebconsole([]STopic{*topic}) { // webconsole err = nm.create(ctx, userCred, api.WEBCONSOLE, receiverIds, webconsoleContacts.UnsortedList(), input.Priority, event.Id) if err != nil { @@ -777,6 +773,11 @@ func (nm *SNotificationManager) ListItemFilter(ctx context.Context, q *sqlchemy. if len(input.Tag) > 0 { q = q.Equals("tag", input.Tag) } + if len(input.TopicType) > 0 { + topicq := TopicManager.Query("id").Equals("type", input.TopicType).SubQuery() + eventq := EventManager.Query("id").In("topic_id", topicq).SubQuery() + q = q.In("event_id", eventq) + } return q, nil } diff --git a/pkg/notify/models/topic.go b/pkg/notify/models/topic.go index d925525b5e..f0867e5d3a 100644 --- a/pkg/notify/models/topic.go +++ b/pkg/notify/models/topic.go @@ -436,6 +436,19 @@ func (s *STopic) getActions() []notify.SAction { return actions } +func (sm *STopicManager) TopicByEvent(eventStr string, advanceDays int) (*STopic, error) { + topics, err := sm.TopicsByEvent(eventStr, advanceDays) + if err != nil { + return nil, err + } + if len(topics) == 0 { + return nil, nil + } + // free memory in time + topic := topics[0] + return &topic, nil +} + func (sm *STopicManager) TopicsByEvent(eventStr string, advanceDays int) ([]STopic, error) { event, err := parseEvent(eventStr) if err != nil {