Prechádzať zdrojové kódy

make recommender cleanup project and cluster scoped

Alexander Belanger 3 rokov pred
rodič
commit
5c9c649da0

+ 4 - 4
internal/repository/gorm/monitor.go

@@ -43,16 +43,16 @@ func (m *MonitorTestResultRepository) UpdateMonitorTestResult(monitor *models.Mo
 	return monitor, nil
 }
 
-func (m *MonitorTestResultRepository) ArchiveMonitorTestResults(recommenderID string) error {
-	query := m.db.Debug().Unscoped().Model(&models.MonitorTestResult{}).Where("last_recommender_run_id != ?", recommenderID)
+func (m *MonitorTestResultRepository) ArchiveMonitorTestResults(projectID, clusterID uint, recommenderID string) error {
+	query := m.db.Debug().Unscoped().Model(&models.MonitorTestResult{}).Where("project_id = ? AND cluster_id = ? AND last_recommender_run_id != ?", projectID, clusterID, recommenderID)
 
 	return query.Update("archived", true).Error
 }
 
-func (m *MonitorTestResultRepository) DeleteOldMonitorTestResults(recommenderID string) error {
+func (m *MonitorTestResultRepository) DeleteOldMonitorTestResults(projectID, clusterID uint, recommenderID string) error {
 	monitors := make([]*models.MonitorTestResult, 0)
 
-	query := m.db.Debug().Unscoped().Where("last_recommender_run_id != ?", recommenderID)
+	query := m.db.Debug().Unscoped().Where("project_id = ? AND cluster_id = ? AND last_recommender_run_id != ?", projectID, clusterID, recommenderID)
 
 	// we need to switch on the database type to delete records older than 24 hours
 	switch m.db.Dialector.Name() {

+ 2 - 2
internal/repository/monitor.go

@@ -7,6 +7,6 @@ type MonitorTestResultRepository interface {
 	ReadMonitorTestResult(projectID, clusterID uint, operationID string) (*models.MonitorTestResult, error)
 	UpdateMonitorTestResult(monitor *models.MonitorTestResult) (*models.MonitorTestResult, error)
 
-	ArchiveMonitorTestResults(recommenderID string) error
-	DeleteOldMonitorTestResults(recommenderID string) error
+	ArchiveMonitorTestResults(projectID, clusterID uint, recommenderID string) error
+	DeleteOldMonitorTestResults(projectID, clusterID uint, recommenderID string) error
 }

+ 2 - 2
internal/repository/test/monitor.go

@@ -23,10 +23,10 @@ func (n *MonitorTestResultRepository) UpdateMonitorTestResult(monitor *models.Mo
 	panic("not implemented") // TODO: Implement
 }
 
-func (n *MonitorTestResultRepository) ArchiveMonitorTestResults(recommenderID string) error {
+func (n *MonitorTestResultRepository) ArchiveMonitorTestResults(projectID, clusterID uint, recommenderID string) error {
 	panic("not implemented") // TODO: Implement
 }
 
-func (n *MonitorTestResultRepository) DeleteOldMonitorTestResults(recommenderID string) error {
+func (n *MonitorTestResultRepository) DeleteOldMonitorTestResults(projectID, clusterID uint, recommenderID string) error {
 	panic("not implemented") // TODO: Implement
 }

+ 13 - 6
workers/jobs/recommender.go

@@ -251,16 +251,23 @@ func (n *recommender) Run() error {
 				continue
 			}
 		}
-	}
 
-	// archive any test results which don't match
-	err := n.repo.MonitorTestResult().ArchiveMonitorTestResults(n.runRecommenderID)
+		err = n.repo.MonitorTestResult().ArchiveMonitorTestResults(ids.projectID, ids.clusterID, n.runRecommenderID)
 
-	if err != nil {
-		return err
+		if err != nil {
+			log.Printf("error archiving test results for cluster ID %d: %v", ids.clusterID, err)
+			continue
+		}
+
+		err = n.repo.MonitorTestResult().DeleteOldMonitorTestResults(ids.projectID, ids.clusterID, n.runRecommenderID)
+
+		if err != nil {
+			log.Printf("error deleting old test results for cluster ID %d: %v", ids.clusterID, err)
+			continue
+		}
 	}
 
-	return n.repo.MonitorTestResult().DeleteOldMonitorTestResults(n.runRecommenderID)
+	return nil
 }
 
 func (n *recommender) getMonitorTestResultFromQueryResult(cluster *models.Cluster, queryRes *opa.OPARecommenderQueryResult, recommenderID string) *models.MonitorTestResult {

+ 2 - 0
workers/main.go

@@ -66,6 +66,8 @@ func main() {
 	log.Printf("setting max worker count to: %d\n", envDecoder.MaxWorkers)
 	log.Printf("setting max job queue count to: %d\n", envDecoder.MaxQueue)
 
+	log.Printf("legacy project ids are: %v", envDecoder.LegacyProjectIDs)
+
 	db, err := adapter.New(&envDecoder.DBConf)
 
 	if err != nil {