Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
Move last checks result query to checks service
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Jan 20, 2022
1 parent 730fd83 commit 1c80b6e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 38 deletions.
20 changes: 20 additions & 0 deletions web/services/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ChecksService interface {
CreateChecksCatalog(checkList models.ChecksCatalog) error
// Check result services
CreateChecksResult(checksResult *models.ChecksResult) error
GetLastExecutionByGroup() ([]*models.ChecksResult, error)
GetChecksResultByCluster(clusterId string) (*models.ChecksResult, error)
GetChecksResultAndMetadataByCluster(clusterId string) (*models.ChecksResultAsList, error)
GetAggregatedChecksResultByHost(clusterId string) (map[string]*models.AggregatedCheckData, error)
Expand Down Expand Up @@ -149,6 +150,25 @@ func (c *checksService) CreateChecksResult(checksResult *models.ChecksResult) er
return result.Error
}

func (c *checksService) GetLastExecutionByGroup() ([]*models.ChecksResult, error) {
var checksResults []entities.ChecksResult

err := c.db.Where("(group_id, created_at) IN (?)", c.db.Model(&entities.ChecksResult{}).
Select("group_id, max(created_at)").
Group("group_id")).Order("id").Find(&checksResults).Error
if err != nil {
return nil, err
}

var checksResultModels []*models.ChecksResult
for _, checksResult := range checksResults {
cModel, _ := checksResult.ToModel()
checksResultModels = append(checksResultModels, cModel)
}

return checksResultModels, nil
}

func (c *checksService) GetChecksResultByCluster(clusterId string) (*models.ChecksResult, error) {
var checksResult entities.ChecksResult
result := c.db.Where("group_id", clusterId).Last(&checksResult)
Expand Down
23 changes: 23 additions & 0 deletions web/services/checks_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion web/services/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func loadChecksResultFixtures(db *gorm.DB) {
GroupID: "group1",
Payload: datatypes.JSON([]byte(group1payloadLast)),
})
group2payload := `{"hosts":{"host3":{"reachable":true "msg":""},"host4":{"reachable":true,"msg":""}},
group2payload := `{"hosts":{"host3":{"reachable":true, "msg":""},"host4":{"reachable":true,"msg":""}},
"checks":{"check1":{"hosts":{"host3":{"result":"critical"},"host4":{"result":"critical"}}},
"check2":{"hosts":{"host3":{"result":"passing"},"host4":{"result":"warning"}}}}}`
db.Create(&entities.ChecksResult{
Expand Down Expand Up @@ -319,6 +319,15 @@ func (suite *ChecksServiceTestSuite) TestChecksService_CreateChecksCatalog() {
suite.Equal(int64(2), count)
}

func (suite *ChecksServiceTestSuite) TestChecksService_GetLastExecutionByGroup() {
results, err := suite.checksService.GetLastExecutionByGroup()

suite.NoError(err)
suite.Equal(len(results), 2)
suite.Equal(results[0].ID, "group1")
suite.Equal(results[1].ID, "group2")
}

func (suite *ChecksServiceTestSuite) TestChecksService_GetChecksResultByCluster() {
results, err := suite.checksService.GetChecksResultByCluster("group1")

Expand Down
11 changes: 3 additions & 8 deletions web/services/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,15 @@ func (s *clustersService) GetAll(filter *ClustersFilter, page *Page) (models.Clu

// Filter the clusters by Health
if filter != nil && len(filter.Health) > 0 {
var checksResults []entities.ChecksResult

err := s.db.Where("(group_id, created_at) IN (?)", s.db.Model(&entities.ChecksResult{}).
Select("group_id, max(created_at)").
Group("group_id")).Find(&checksResults).Error
checksResults, err := s.checksService.GetLastExecutionByGroup()
if err != nil {
return nil, err
}

for _, checksResult := range checksResults {
checksResultModel, _ := checksResult.ToModel()
clusterHealth := checksResultModel.GetAggregatedChecksResultByCluster().String()
clusterHealth := checksResult.GetAggregatedChecksResultByCluster().String()
if internal.Contains(filter.Health, clusterHealth) {
healthFilteredClusters = append(healthFilteredClusters, checksResultModel.ID)
healthFilteredClusters = append(healthFilteredClusters, checksResult.ID)
}
}
}
Expand Down
69 changes: 40 additions & 29 deletions web/services/clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/trento-project/trento/test/helpers"
"github.com/trento-project/trento/web/entities"
"github.com/trento-project/trento/web/models"
"gorm.io/datatypes"
"gorm.io/gorm"
)

Expand All @@ -30,7 +29,6 @@ func (suite *ClustersServiceTestSuite) SetupSuite() {

suite.db.AutoMigrate(entities.Cluster{}, entities.Host{}, models.Tag{}, models.SelectedChecks{}, models.ConnectionSettings{}, entities.ChecksResult{})
loadClustersFixtures(suite.db)
loadChecksFixtures(suite.db)
}

func (suite *ClustersServiceTestSuite) TearDownSuite() {
Expand Down Expand Up @@ -141,32 +139,6 @@ func loadClustersFixtures(db *gorm.DB) {
})
}

func loadChecksFixtures(db *gorm.DB) {
group1payloadCritical := `{"hosts":{"host1":{"reachable": true, "msg":""},"host2":{"reachable":false,"msg":"error connecting"}},
"checks":{"check1":{"hosts":{"host1":{"result":"passing"},"host2":{"result":"passing"}}},
"check2":{"hosts":{"host1":{"result":"warning"}, "host2":{"result":"critical"}}}}}`
db.Create(&entities.ChecksResult{
GroupID: "1",
Payload: datatypes.JSON([]byte(group1payloadCritical)),
})

group1payloadPassing := `{"hosts":{"host1":{"reachable": true, "msg":""},"host2":{"reachable":false,"msg":"error connecting"}},
"checks":{"check1":{"hosts":{"host1":{"result":"passing"},"host2":{"result":"passing"}}},
"check2":{"hosts":{"host1":{"result":"passing"}, "host2":{"result":"passing"}}}}}`
db.Create(&entities.ChecksResult{
GroupID: "1",
Payload: datatypes.JSON([]byte(group1payloadPassing)),
})

group2payloadPassing := `{"hosts":{"host1":{"reachable": true, "msg":""},"host2":{"reachable":false,"msg":"error connecting"}},
"checks":{"check1":{"hosts":{"host1":{"result":"passing"},"host2":{"result":"passing"}}},
"check2":{"hosts":{"host1":{"result":"passing"}, "host2":{"result":"passing"}}}}}`
db.Create(&entities.ChecksResult{
GroupID: "2",
Payload: datatypes.JSON([]byte(group2payloadPassing)),
})
}

func (suite *ClustersServiceTestSuite) TestClustersService_GetAll() {
suite.checksService.On("GetAggregatedChecksResultByCluster", "1").Return(&models.AggregatedCheckData{PassingCount: 1}, nil)
suite.checksService.On("GetAggregatedChecksResultByCluster", "2").Return(&models.AggregatedCheckData{WarningCount: 1}, nil)
Expand Down Expand Up @@ -218,7 +190,46 @@ func (suite *ClustersServiceTestSuite) TestClustersService_GetAll() {
}

func (suite *ClustersServiceTestSuite) TestClustersService_GetAll_Filter() {
suite.checksService.On("GetAggregatedChecksResultByCluster", "1").Return(&models.AggregatedCheckData{PassingCount: 1}, nil)
suite.checksService.On("GetLastExecutionByGroup").Return(
[]*models.ChecksResult{
&models.ChecksResult{
ID: "1",
Checks: map[string]*models.ChecksByHost{
"check1": &models.ChecksByHost{
Hosts: map[string]*models.Check{
"host1": &models.Check{Result: models.CheckPassing},
"host2": &models.Check{Result: models.CheckPassing},
},
},
"check2": &models.ChecksByHost{
Hosts: map[string]*models.Check{
"host1": &models.Check{Result: models.CheckPassing},
"host2": &models.Check{Result: models.CheckPassing},
},
},
},
},
&models.ChecksResult{
ID: "2",
Checks: map[string]*models.ChecksByHost{
"check1": &models.ChecksByHost{
Hosts: map[string]*models.Check{
"host1": &models.Check{Result: models.CheckCritical},
"host2": &models.Check{Result: models.CheckPassing},
},
},
"check2": &models.ChecksByHost{
Hosts: map[string]*models.Check{
"host1": &models.Check{Result: models.CheckWarning},
"host2": &models.Check{Result: models.CheckPassing},
},
},
},
},
}, nil,
)
suite.checksService.On("GetAggregatedChecksResultByCluster", "1").Return(
&models.AggregatedCheckData{PassingCount: 1}, nil)

clusters, _ := suite.clustersService.GetAll(&ClustersFilter{
Name: []string{"cluster1"},
Expand Down

0 comments on commit 1c80b6e

Please sign in to comment.