Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AM-287 cascade delete of metrics for absent resources #432

Merged
merged 1 commit into from
Oct 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions projects/project.go
Original file line number Diff line number Diff line change
@@ -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
@@ -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 {
5 changes: 5 additions & 0 deletions stores/mongo.go
Original file line number Diff line number Diff line change
@@ -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) {

1 change: 1 addition & 0 deletions stores/store.go
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions stores/store_test.go
Original file line number Diff line number Diff line change
@@ -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")