Skip to content

Commit

Permalink
add: ListNotificationForInternal
Browse files Browse the repository at this point in the history
  • Loading branch information
gassara-kys committed Aug 1, 2023
1 parent 074391c commit bfbb824
Show file tree
Hide file tree
Showing 9 changed files with 866 additions and 542 deletions.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,16 @@ delete-alert_cond_rule:
list-notification:
$(GRPCURL) \
-plaintext \
-d '{"project_id":1001, "type":"slack", "from_at":1560000000, "to_at":1660000000}' \
-d '{"project_id":1001, "type":"slack"}' \
$(CORE_API_ADDR) core.alert.AlertService.ListNotification

.PHONY: list-notification-for-internal
list-notification-for-internal:
$(GRPCURL) \
-plaintext \
-d '{"project_id":1001, "type":"slack"}' \
$(CORE_API_ADDR) core.alert.AlertService.ListNotificationForInternal

.PHONY: get-notification
get-notification:
$(GRPCURL) \
Expand Down
42 changes: 33 additions & 9 deletions pkg/server/alert/alert_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ func (a *AlertService) ListNotification(ctx context.Context, req *alert.ListNoti
if err := req.Validate(); err != nil {
return nil, err
}
return a.listNotification(ctx, req, true)
}

func (a *AlertService) ListNotificationForInternal(ctx context.Context, req *alert.ListNotificationForInternalRequest) (*alert.ListNotificationForInternalResponse, error) {
if err := req.Validate(); err != nil {
return nil, err
}
resp, err := a.listNotification(ctx, &alert.ListNotificationRequest{
ProjectId: req.ProjectId,
Type: req.Type,
}, false)
if err != nil {
return nil, err
}
return &alert.ListNotificationForInternalResponse{
Notification: resp.Notification,
}, nil
}

func (a *AlertService) listNotification(ctx context.Context, req *alert.ListNotificationRequest, mask bool) (*alert.ListNotificationResponse, error) {
converted := convertListNotificationRequest(req)
list, err := a.repository.ListNotification(ctx, converted.ProjectId, converted.Type, converted.FromAt, converted.ToAt)
if err != nil {
Expand All @@ -33,7 +53,7 @@ func (a *AlertService) ListNotification(ctx context.Context, req *alert.ListNoti
}
data := alert.ListNotificationResponse{}
for _, d := range *list {
convertedNotification, err := a.convertNotification(ctx, &d)
convertedNotification, err := a.convertNotification(ctx, &d, mask)
if err != nil {
a.logger.Errorf(ctx, "Failed to convert Notification. error: %v", err)
return nil, err
Expand Down Expand Up @@ -67,7 +87,7 @@ func (a *AlertService) GetNotification(ctx context.Context, req *alert.GetNotifi
}
return nil, err
}
convertedNotification, err := a.convertNotification(ctx, data)
convertedNotification, err := a.convertNotification(ctx, data, true)
if err != nil {
a.logger.Errorf(ctx, "Failed to convert Notification. error: %v", err)
return nil, err
Expand Down Expand Up @@ -122,7 +142,7 @@ func (a *AlertService) PutNotification(ctx context.Context, req *alert.PutNotifi
if err != nil {
return nil, err
}
convertedNotification, err := a.convertNotification(ctx, registerdData)
convertedNotification, err := a.convertNotification(ctx, registerdData, true)
if err != nil {
a.logger.Errorf(ctx, "Failed to convert Notification. error: %v", err)
return nil, err
Expand Down Expand Up @@ -199,21 +219,25 @@ func (a *AlertService) TestNotification(ctx context.Context, req *alert.TestNoti
return &empty.Empty{}, nil
}

func (a *AlertService) convertNotification(ctx context.Context, n *model.Notification) (*alert.Notification, error) {
func (a *AlertService) convertNotification(ctx context.Context, n *model.Notification, mask bool) (*alert.Notification, error) {
if n == nil {
return &alert.Notification{}, nil
}
maskingSetting, err := maskingNotifySetting(n.Type, n.NotifySetting)
if err != nil {
a.logger.Errorf(ctx, "Failed to masking notify setting. %v", err)
return &alert.Notification{}, err
var err error
setting := n.NotifySetting
if mask {
setting, err = maskingNotifySetting(n.Type, setting)
if err != nil {
a.logger.Errorf(ctx, "Failed to masking notify setting. %v", err)
return &alert.Notification{}, err
}
}
return &alert.Notification{
NotificationId: n.NotificationID,
Name: n.Name,
ProjectId: n.ProjectID,
Type: n.Type,
NotifySetting: maskingSetting,
NotifySetting: setting,
CreatedAt: n.CreatedAt.Unix(),
UpdatedAt: n.UpdatedAt.Unix(),
}, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/alert/alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,7 @@ func TestConvertNotification(t *testing.T) {
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, err := a.convertNotification(context.Background(), c.input)
got, err := a.convertNotification(context.Background(), c.input, true)
if !reflect.DeepEqual(got, c.want) {
t.Fatalf("Unexpected mapping: want=%+v, got=%+v", c.want, got)
}
Expand Down
33 changes: 33 additions & 0 deletions proto/alert/mocks/AlertServiceClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions proto/alert/mocks/AlertServiceServer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bfbb824

Please sign in to comment.