Skip to content

Commit

Permalink
feat(alert): add create alert with namespace API (#164)
Browse files Browse the repository at this point in the history
* feat(alert): add create alert with namespace api

* feat(subscription): filter by namespace_id

* feat: update proto

* fix(test): fix e2e test

* fix(alert): write now() to created_at and updated_at in DB
  • Loading branch information
mabdh authored Jan 10, 2023
1 parent ac9f5fc commit fb8ebbd
Show file tree
Hide file tree
Showing 63 changed files with 2,090 additions and 1,267 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NAME="github.com/odpf/siren"
LAST_COMMIT := $(shell git rev-parse --short HEAD)
LAST_TAG := "$(shell git rev-list --tags --max-count=1)"
APP_VERSION := "$(shell git describe --tags ${LAST_TAG})-next"
PROTON_COMMIT := "f21015688d165bec2d859c6fca284754dd81755f"
PROTON_COMMIT := "daa6a69e9351cbe3cc0413fb31263e7be60593c1"

.PHONY: all build test clean dist vet proto install

Expand All @@ -17,7 +17,7 @@ test: ## Run the tests
go test -race $(shell go list ./... | grep -v /test/) -covermode=atomic -coverprofile=coverage.out

e2e-test: ## Run all e2e tests
go test -v -race ./test/e2e_test/... -coverprofile=coverage.out --timeout 180s
go test -v -race ./test/e2e_test/... -coverprofile=coverage.out --timeout 300s

coverage: ## Print code coverage
go test -race -coverprofile coverage.out -covermode=atomic ./... && go tool cover -html=coverage.out
Expand Down
1 change: 1 addition & 0 deletions core/alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Repository interface {
type Alert struct {
ID uint64 `json:"id"`
ProviderID uint64 `json:"provider_id"`
NamespaceID uint64 `json:"namespace_id"`
ResourceName string `json:"resource_name"`
MetricName string `json:"metric_name"`
MetricValue string `json:"metric_value"`
Expand Down
1 change: 1 addition & 0 deletions core/alert/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package alert
type Filter struct {
ResourceName string
ProviderID uint64
NamespaceID uint64
StartTime int64
EndTime int64
}
10 changes: 5 additions & 5 deletions core/alert/mocks/alert_repository.go

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

35 changes: 18 additions & 17 deletions core/alert/mocks/alert_transformer.go

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

2 changes: 1 addition & 1 deletion core/alert/provider_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (

//go:generate mockery --name=AlertTransformer -r --case underscore --with-expecter --structname AlertTransformer --filename alert_transformer.go --output=./mocks
type AlertTransformer interface {
TransformToAlerts(ctx context.Context, providerID uint64, body map[string]interface{}) ([]*Alert, int, error)
TransformToAlerts(ctx context.Context, providerID uint64, namespaceID uint64, body map[string]interface{}) ([]*Alert, int, error)
}
4 changes: 2 additions & 2 deletions core/alert/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ func NewService(repository Repository, registry map[string]AlertTransformer) *Se
return &Service{repository, registry}
}

func (s *Service) CreateAlerts(ctx context.Context, providerType string, providerID uint64, body map[string]interface{}) ([]*Alert, int, error) {
func (s *Service) CreateAlerts(ctx context.Context, providerType string, providerID uint64, namespaceID uint64, body map[string]interface{}) ([]*Alert, int, error) {
pluginService, err := s.getProviderPluginService(providerType)
if err != nil {
return nil, 0, err
}

alerts, firingLen, err := pluginService.TransformToAlerts(ctx, providerID, body)
alerts, firingLen, err := pluginService.TransformToAlerts(ctx, providerID, namespaceID, body)
if err != nil {
return nil, 0, err
}
Expand Down
10 changes: 5 additions & 5 deletions core/alert/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ func TestService_Create(t *testing.T) {
{
name: "should return error if TransformToAlerts return error",
setup: func(ar *mocks.AlertRepository, at *mocks.AlertTransformer) {
at.EXPECT().TransformToAlerts(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return(nil, 0, errors.New("some error"))
at.EXPECT().TransformToAlerts(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return(nil, 0, errors.New("some error"))
},
alertsToBeCreated: alertsToBeCreated,
wantErr: true,
},
{
name: "should call repository Create method with proper arguments",
setup: func(ar *mocks.AlertRepository, at *mocks.AlertTransformer) {
at.EXPECT().TransformToAlerts(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return([]*alert.Alert{
at.EXPECT().TransformToAlerts(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return([]*alert.Alert{
{ID: 1, ProviderID: 1, ResourceName: "foo", Severity: "CRITICAL", MetricName: "lag", MetricValue: "20",
Rule: "lagHigh", TriggeredAt: timenow},
}, 1, nil)
Expand All @@ -140,7 +140,7 @@ func TestService_Create(t *testing.T) {
{
name: "should return error not found if repository return err relation",
setup: func(ar *mocks.AlertRepository, at *mocks.AlertTransformer) {
at.EXPECT().TransformToAlerts(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return([]*alert.Alert{
at.EXPECT().TransformToAlerts(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return([]*alert.Alert{
{ID: 1, ProviderID: 1, ResourceName: "foo", Severity: "CRITICAL", MetricName: "lag", MetricValue: "20",
Rule: "lagHigh", TriggeredAt: timenow},
}, 1, nil)
Expand All @@ -152,7 +152,7 @@ func TestService_Create(t *testing.T) {
{
name: "should handle errors from repository",
setup: func(ar *mocks.AlertRepository, at *mocks.AlertTransformer) {
at.EXPECT().TransformToAlerts(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return([]*alert.Alert{
at.EXPECT().TransformToAlerts(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64"), mock.AnythingOfType("map[string]interface {}")).Return([]*alert.Alert{
{ID: 1, ProviderID: 1, ResourceName: "foo", Severity: "CRITICAL", MetricName: "lag", MetricValue: "20",
Rule: "lagHigh", TriggeredAt: timenow},
}, 1, nil)
Expand All @@ -177,7 +177,7 @@ func TestService_Create(t *testing.T) {
svc := alert.NewService(repositoryMock, map[string]alert.AlertTransformer{
testType: alertTransformerMock,
})
actualAlerts, firingLen, err := svc.CreateAlerts(ctx, testType, 1, tc.alertsToBeCreated)
actualAlerts, firingLen, err := svc.CreateAlerts(ctx, testType, 1, 1, tc.alertsToBeCreated)
if tc.wantErr {
if err == nil {
t.Error("error should not be nil")
Expand Down
27 changes: 14 additions & 13 deletions core/namespace/mocks/config_syncer.go

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

6 changes: 3 additions & 3 deletions core/namespace/mocks/encryptor.go

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

28 changes: 14 additions & 14 deletions core/namespace/mocks/namespace_repository.go

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

6 changes: 3 additions & 3 deletions core/namespace/mocks/provider_service.go

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

Loading

0 comments on commit fb8ebbd

Please sign in to comment.