Skip to content

Commit

Permalink
feat(dataset): wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mclcavalcante committed Feb 24, 2022
1 parent c2288cc commit 64fe6a9
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 233 deletions.
28 changes: 14 additions & 14 deletions policies/api/http/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ type loggingMiddleware struct {
svc policies.Service
}

func (l loggingMiddleware) InactivateDatasetByID(ctx context.Context, datasetID string, token string) (err error) {
defer func(begin time.Time) {
if err != nil {
l.logger.Warn("method call: inactivate_dataset_by_id",
zap.Error(err),
zap.Duration("duration", time.Since(begin)))
} else {
l.logger.Info("method call: inactivate_dataset_by_id",
zap.Duration("duration", time.Since(begin)))
}
}(time.Now())
return l.svc.InactivateDatasetByID(ctx, datasetID, token)
}

func (l loggingMiddleware) ViewDatasetByIDInternal(ctx context.Context, ownerID string, datasetID string) (_ policies.Dataset, err error) {
defer func(begin time.Time) {
if err != nil {
Expand Down Expand Up @@ -256,20 +270,6 @@ func (l loggingMiddleware) ListDatasets(ctx context.Context, token string, pm po
return l.svc.ListDatasets(ctx, token, pm)
}

func (l loggingMiddleware) InactivateDatasetBySinkID(ctx context.Context, sinkID string, token string) (err error) {
defer func(begin time.Time) {
if err != nil {
l.logger.Warn("method call: inactivate_dataset_by_sink_id",
zap.Error(err),
zap.Duration("duration", time.Since(begin)))
} else {
l.logger.Info("method call: inactivate_dataset_by_sink_id",
zap.Duration("duration", time.Since(begin)))
}
}(time.Now())
return l.svc.InactivateDatasetBySinkID(ctx, sinkID, token)
}

func (l loggingMiddleware) DeleteSinkFromDataset(ctx context.Context, sinkID string, token string) (err error) {
defer func(begin time.Time) {
if err != nil {
Expand Down
44 changes: 22 additions & 22 deletions policies/api/http/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ type metricsMiddleware struct {
svc policies.Service
}

func (m metricsMiddleware) InactivateDatasetByID(ctx context.Context, datasetID string, token string) error {
ownerID, err := m.identify(token)
if err != nil {
return err
}

defer func(begin time.Time) {
labels := []string{
"method", "inactivateDatasetByID",
"owner_id", ownerID,
"policy_id", "",
"dataset_id", datasetID,
}

m.counter.With(labels...).Add(1)
m.latency.With(labels...).Observe(float64(time.Since(begin).Microseconds()))

}(time.Now())

return m.svc.InactivateDatasetByID(ctx, token, datasetID)
}

func (m metricsMiddleware) ViewDatasetByIDInternal(ctx context.Context, ownerID string, datasetID string) (policies.Dataset, error) {
defer func(begin time.Time) {
labels := []string{
Expand Down Expand Up @@ -376,28 +398,6 @@ func (m metricsMiddleware) ListDatasets(ctx context.Context, token string, pm po
return m.svc.ListDatasets(ctx, token, pm)
}

func (m metricsMiddleware) InactivateDatasetBySinkID(ctx context.Context, sinkID string, token string) error {
ownerID, err := m.identify(token)
if err != nil {
return err
}

defer func(begin time.Time) {
labels := []string{
"method", "inactivateDatasetByGroupID",
"owner_id", ownerID,
"policy_id", "",
"dataset_id", "",
}

m.counter.With(labels...).Add(1)
m.latency.With(labels...).Observe(float64(time.Since(begin).Microseconds()))

}(time.Now())

return m.svc.InactivateDatasetBySinkID(ctx, sinkID, token)
}

func (m metricsMiddleware) DeleteSinkFromDataset(ctx context.Context, sinkID string, token string) error {
ownerID, err := m.identify(token)
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions policies/mocks/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,23 @@ func (m *mockPoliciesRepository) InactivateDatasetByID(ctx context.Context, sink
return nil
}

func (m *mockPoliciesRepository) DeleteSinkFromDataset(ctx context.Context, sinkID string, ownerID string) error {
func (m *mockPoliciesRepository) DeleteSinkFromDataset(ctx context.Context, sinkID string, ownerID string) ([]policies.Dataset, error) {
var datasets []policies.Dataset

for _, ds := range m.ddb{
if ds.MFOwnerID == ownerID{
for i, sID := range ds.SinkIDs {
if sID == sinkID{
ds.SinkIDs[i] = ds.SinkIDs[len(ds.SinkIDs)-1]
ds.SinkIDs[len(ds.SinkIDs)-1] = ""
ds.SinkIDs = ds.SinkIDs[:len(ds.SinkIDs)-1]

datasets = append(datasets, ds)
}
}
}
}
return nil
return datasets, nil
}

func (m *mockPoliciesRepository) DeleteDataset(ctx context.Context, ownerID string, dsID string) error {
Expand Down
9 changes: 3 additions & 6 deletions policies/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ type Service interface {
// ListDatasets retrieve a list of Dataset by owner
ListDatasets(ctx context.Context, token string, pm PageMetadata) (PageDataset, error)

// InactivateDatasetBySinkID inactivate a dataset
InactivateDatasetBySinkID(ctx context.Context, sinkID string, token string) error
// InactivateDatasetByDatasetID inactivate a dataset
InactivateDatasetByID(ctx context.Context, datasetID string, token string) error

// DeleteSinkFromDataset removes a sink from a dataset
DeleteSinkFromDataset(ctx context.Context, sinkID string, token string) error
Expand Down Expand Up @@ -159,8 +159,5 @@ type Repository interface {
InactivateDatasetByID(ctx context.Context, sinkID string, ownerID string) error

// DeleteSinkFromDataset removes a sink from a dataset
DeleteSinkFromDataset(ctx context.Context, sinkID string, ownerID string) error

// RetrieveAllDatasetsInternal retrieves all datasets by owner
RetrieveAllDatasetsInternal(ctx context.Context, owner string) ([]Dataset, error)
DeleteSinkFromDataset(ctx context.Context, sinkID string, ownerID string) ([]Dataset, error)
}
23 changes: 10 additions & 13 deletions policies/policy_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (s policiesService) ListDatasets(ctx context.Context, token string, pm Page
return s.repo.RetrieveAllDatasetsByOwner(ctx, ownerID, pm)
}

func (s policiesService) InactivateDatasetBySinkID(ctx context.Context, sinkID string, token string) error {
func (s policiesService) DeleteSinkFromDataset(ctx context.Context, sinkID string, token string) error {
ownerID, err := s.identify(token)
if err != nil {
return err
Expand All @@ -339,32 +339,29 @@ func (s policiesService) InactivateDatasetBySinkID(ctx context.Context, sinkID s
return ErrMalformedEntity
}

datasets, err := s.repo.RetrieveAllDatasetsInternal(ctx, ownerID)
if err != nil{
return err
}
datasets, err := s.repo.DeleteSinkFromDataset(ctx, sinkID, ownerID)

for _, ds := range datasets{
if len(ds.SinkIDs) == 1 && ds.SinkIDs[0] == sinkID{
err = s.repo.InactivateDatasetByID(ctx, ds.ID, ownerID)
}
if err != nil {
return err
if len(ds.SinkIDs) == 0{
s.InactivateDatasetByID(ctx, sinkID, ownerID)

}
}

return nil
}

func (s policiesService) DeleteSinkFromDataset(ctx context.Context, sinkID string, token string) error {
func (s policiesService) InactivateDatasetByID(ctx context.Context, datasetID string, token string) error {
ownerID, err := s.identify(token)
if err != nil {
return err
}

if sinkID == "" {
if datasetID == "" {
return ErrMalformedEntity
}

return s.repo.DeleteSinkFromDataset(ctx, sinkID, ownerID)
err = s.repo.InactivateDatasetByID(ctx, datasetID, ownerID)

return nil
}
82 changes: 0 additions & 82 deletions policies/policy_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1103,88 +1103,6 @@ func createDataset(t *testing.T, svc policies.Service, name string) policies.Dat
return res
}

func TestInactivateDatasetsBySinkID(t *testing.T) {
users := flmocks.NewAuthService(map[string]string{token: email})
svc := newService(users)

agentGroupID, err := uuid.NewV4()
require.Nil(t, err, fmt.Sprintf("Unexpected error: %s", err))

policy := createPolicy(t, svc, "policy")

var total = 10

datasetsTest := make([]policies.Dataset, total)

sinkIDs := make([]string, 0)
for i := 0; i < total; i++ {
ID, err := uuid.NewV4()
require.Nil(t, err, fmt.Sprintf("Unexpected error: %s", err))

for i := 0; i < 2; i++ {
sinkID, err := uuid.NewV4()
require.Nil(t, err, fmt.Sprintf("Unexpected error: %s", err))
sinkIDs = append(sinkIDs, sinkID.String())
}

validName, err := types.NewIdentifier(fmt.Sprintf("dataset-%d", i))
require.Nil(t, err, fmt.Sprintf("Unexpected error: %s", err))

dataset := policies.Dataset{
ID: ID.String(),
Name: validName,
PolicyID: policy.ID,
AgentGroupID: agentGroupID.String(),
SinkIDs: sinkIDs,
}

ds, err := svc.AddDataset(context.Background(), token, dataset)
if err != nil {
require.Nil(t, err, fmt.Sprintf("Unexpected error: %s", err))
}

datasetsTest[i] = ds
}

cases := map[string]struct {
token string
sinkID string
valid bool
err error
}{
"inactivate a set of datasets by sinkID": {
sinkID: sinkIDs[0],
token: token,
valid: false,
err: nil,
},
"inactivate datasets with empty sinkID": {
sinkID: "",
token: token,
err: policies.ErrMalformedEntity,
},
"inactivate a set of datasets with invalid token": {
sinkID: sinkIDs[0],
token: invalidToken,
valid: false,
err: errors.ErrUnauthorizedAccess,
},
}

for desc, tc := range cases {
t.Run(desc, func(t *testing.T) {
err := svc.InactivateDatasetBySinkID(context.Background(), tc.sinkID, tc.token)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s", desc, tc.err, err))

assertate, _ := svc.ListDatasetsByPolicyIDInternal(context.Background(), policy.ID, tc.token)
for _, ds := range assertate {
assert.Equal(t, tc.valid, ds.Valid, fmt.Sprintf("%s: expected '%t' got '%t'", desc, tc.valid, ds.Valid))
}
})

}
}

func TestDeleteSinkFromDataset(t *testing.T) {
users := flmocks.NewAuthService(map[string]string{token: email})
svc := newService(users)
Expand Down
65 changes: 9 additions & 56 deletions policies/postgres/datasets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,74 +805,27 @@ func TestDeleteSinkFromDataset(t *testing.T) {
owner: "",
contains: true,
dataset: dataset2,
err: policies.ErrMalformedEntity,
err: errors.ErrMalformedEntity,
},
}

for desc, tc := range cases {
t.Run(desc, func(t *testing.T) {
err := repo.DeleteSinkFromDataset(context.Background(), tc.sinkID, tc.owner)
dataset, err := repo.DeleteSinkFromDataset(context.Background(), tc.sinkID, tc.owner)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected '%s' got '%s'", desc, tc.err, err))

assertate, _ := repo.RetrieveDatasetByID(context.Background(), tc.dataset.ID, tc.dataset.MFOwnerID)
switch tc.contains {
case false:
assert.NotContains(t, assertate.SinkIDs, tc.sinkID, fmt.Sprintf("%s: expected '%s' to not contains '%s'", desc, assertate.SinkIDs, tc.sinkID))
case true:
assert.Contains(t, assertate.SinkIDs, tc.sinkID, fmt.Sprintf("%s: expected '%s' to contains '%s'", desc, assertate.SinkIDs, tc.sinkID))
for _, d := range dataset {
switch tc.contains {
case false:
assert.NotContains(t, d.SinkIDs, tc.sinkID, fmt.Sprintf("%s: expected '%s' to not contains '%s'", desc, d.SinkIDs, tc.sinkID))
case true:
assert.Contains(t, d.SinkIDs, tc.sinkID, fmt.Sprintf("%s: expected '%s' to contains '%s'", desc, d.SinkIDs, tc.sinkID))
}
}
})
}
}

func TestMultiDatasetRetrievalInternal(t *testing.T) {
dbMiddleware := postgres.NewDatabase(db)
repo := postgres.NewPoliciesRepository(dbMiddleware, logger)

oID, err := uuid.NewV4()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

wrongID, err := uuid.NewV4()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

n := uint64(10)
for i := uint64(0); i < n; i++ {
nameID, err := types.NewIdentifier(fmt.Sprintf("mydataset-%d", i))
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

dataset := policies.Dataset{
Name: nameID,
MFOwnerID: oID.String(),
}

_, err = repo.SaveDataset(context.Background(), dataset)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err))
}

cases := map[string]struct {
owner string
size uint64
}{
"retrieve all datasets with existing owner": {
owner: oID.String(),
size: n,
},
"retrieve datasets with no-existing owner": {
owner: wrongID.String(),
size: 0,
},
}

for desc, tc := range cases {
t.Run(desc, func(t *testing.T) {
datasets, err := repo.RetrieveAllDatasetsInternal(context.Background(), tc.owner)
require.Nil(t, err, fmt.Sprintf("%s: unexpected error: %s\n", desc, err))
size := uint64(len(datasets))
assert.Equal(t, tc.size, size, fmt.Sprintf("%s: expected size %d got %d", desc, tc.size, size))
})
}
}

func testSortDataset(t *testing.T, pm policies.PageMetadata, ags []policies.Dataset) {
t.Helper()
switch pm.Order {
Expand Down
Loading

0 comments on commit 64fe6a9

Please sign in to comment.