From 10d3929cdca5b1d4e8c81187d4f05c661ce980a3 Mon Sep 17 00:00:00 2001 From: agelostsal Date: Thu, 29 Sep 2022 18:17:38 +0300 Subject: [PATCH] AM-287 cascade delete of metrics for absent resources --- projects/project.go | 8 ++++++++ stores/mock.go | 19 +++++++++++++++++++ stores/mongo.go | 5 +++++ stores/store.go | 1 + stores/store_test.go | 3 +++ 5 files changed, 36 insertions(+) diff --git a/projects/project.go b/projects/project.go index 78fb278a..e90f0949 100644 --- a/projects/project.go +++ b/projects/project.go @@ -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 } diff --git a/stores/mock.go b/stores/mock.go index 844f4811..92336e22 100644 --- a/stores/mock.go +++ b/stores/mock.go @@ -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 { diff --git a/stores/mongo.go b/stores/mongo.go index 78298402..c373807b 100644 --- a/stores/mongo.go +++ b/stores/mongo.go @@ -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) { diff --git a/stores/store.go b/stores/store.go index 2c5b0832..682248db 100644 --- a/stores/store.go +++ b/stores/store.go @@ -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 diff --git a/stores/store_test.go b/stores/store_test.go index 48c25196..bcec0da7 100644 --- a/stores/store_test.go +++ b/stores/store_test.go @@ -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")