Skip to content

Commit

Permalink
Merge pull request #432 from agelostsal/bug/proj-del-cas-metrics
Browse files Browse the repository at this point in the history
AM-287 cascade delete of metrics for absent resources
  • Loading branch information
kaggis authored Oct 6, 2022
2 parents a7601d7 + 10d3929 commit 4339343
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions projects/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ func RemoveProject(uuid string, store stores.Store) error {
return errors.New("backend error")
}

if err := store.RemoveProjectDailyMessageCounters(uuid); err != nil {
if err.Error() == "not found" {
return err
}

return errors.New("backend error")
}

return nil

}
19 changes: 19 additions & 0 deletions stores/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,25 @@ func (mk *MockStore) RemoveProjectSubs(projectUUID string) error {
return errors.New("not found")
}

// RemoveProjectDailyMessageCounters removes all existing message counters belonging to a specific project uuid
func (mk *MockStore) RemoveProjectDailyMessageCounters(projectUUID string) error {
found := false
newList := []QDailyTopicMsgCount{}
for _, qc := range mk.DailyTopicMsgCount {
if qc.ProjectUUID != projectUUID {
// found item at i, remove it using index
newList = append(newList, qc)
} else {
found = true
}
}
mk.DailyTopicMsgCount = newList
if found {
return nil
}
return errors.New("not found")
}

// RemoveSub removes an existing sub from the store
func (mk *MockStore) RemoveSub(projectUUID string, name string) error {
for i, sub := range mk.SubList {
Expand Down
5 changes: 5 additions & 0 deletions stores/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,11 @@ func (mong *MongoStore) RemoveProjectSubs(projectUUID string) error {
return mong.RemoveAll("subscriptions", subMatch)
}

// RemoveProjectDailyMessageCount removes all message counts related to a project UUID
func (mong *MongoStore) RemoveProjectDailyMessageCounters(projectUUID string) error {
return mong.RemoveAll("daily_topic_msg_count", bson.M{"project_uuid": projectUUID})
}

// QueryTotalMessagesPerProject returns the total amount of messages per project for the given time window
func (mong *MongoStore) QueryTotalMessagesPerProject(projectUUIDs []string, startDate time.Time, endDate time.Time) ([]QProjectMessageCount, error) {

Expand Down
1 change: 1 addition & 0 deletions stores/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Store interface {
RemoveProject(uuid string) error
RemoveProjectTopics(projectUUID string) error
RemoveProjectSubs(projectUUID string) error
RemoveProjectDailyMessageCounters(projectUUID string) error
QueryDailyProjectMsgCount(projectUUID string) ([]QDailyProjectMsgCount, error)
QueryTotalMessagesPerProject(projectUUIDs []string, startDate time.Time, endDate time.Time) ([]QProjectMessageCount, error)
RegisterUser(uuid, name, firstName, lastName, email, org, desc, registeredAt, atkn, status string) error
Expand Down
3 changes: 3 additions & 0 deletions stores/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,9 @@ func (suite *StoreTestSuite) TestMockStore() {
store.RemoveProjectSubs("argo_uuid")
resSub, _, _, _ := store.QuerySubs("argo_uuid", "", "", "", 0)
suite.Equal(0, len(resSub))
store.RemoveProjectDailyMessageCounters("argo_uuid")
resMc, _ := store.QueryDailyProjectMsgCount("argo_uuid")
suite.Equal(0, len(resMc))

// Test RemoveProject
store.RemoveProject("argo_uuid")
Expand Down

0 comments on commit 4339343

Please sign in to comment.