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

Commit

Permalink
Get distinct cluster names for filters
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Jan 18, 2022
1 parent 89f19ce commit 58139c8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 4 deletions.
7 changes: 7 additions & 0 deletions web/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func NewClusterListHandler(clustersService services.ClustersService) gin.Handler
return
}

filterClusterNames, err := clustersService.GetAllClusterNames()
if err != nil {
_ = c.Error(err)
return
}

filterClusterTypes, err := clustersService.GetAllClusterTypes()
if err != nil {
_ = c.Error(err)
Expand Down Expand Up @@ -88,6 +94,7 @@ func NewClusterListHandler(clustersService services.ClustersService) gin.Handler
c.HTML(http.StatusOK, "clusters.html.tmpl", gin.H{
"ClustersTable": clusterList,
"AppliedFilters": query,
"filterClusterNames": filterClusterNames,
"FilterClusterTypes": filterClusterTypes,
"FilterSIDs": filterSIDs,
"FilterTags": filterTags,
Expand Down
4 changes: 4 additions & 0 deletions web/clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ func TestClustersListHandler(t *testing.T) {
mockClusterService := new(services.MockClustersService)
mockClusterService.On("GetAll", mock.Anything, mock.Anything).Return(clustersList, nil)
mockClusterService.On("GetCount").Return(4, nil)
mockClusterService.On("GetAllClusterNames", mock.Anything).Return(
[]string{"hana_cluster", "other_cluster", "netweaver_cluster"},
nil,
)
mockClusterService.On("GetAllClusterTypes", mock.Anything).Return(
[]string{models.ClusterTypeHANAScaleUp, models.ClusterTypeUnknown},
nil,
Expand Down
16 changes: 16 additions & 0 deletions web/services/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ClustersService interface {
GetAll(*ClustersFilter, *Page) (models.ClusterList, error)
GetByID(string) (*models.Cluster, error)
GetCount() (int, error)
GetAllClusterNames() ([]string, error)
GetAllClusterTypes() ([]string, error)
GetAllSIDs() ([]string, error)
GetAllTags() ([]string, error)
Expand Down Expand Up @@ -134,6 +135,21 @@ func (s *clustersService) GetCount() (int, error) {
return int(count), err
}

func (s *clustersService) GetAllClusterNames() ([]string, error) {
var clusterNames []string

err := s.db.Model(&entities.Cluster{}).
Distinct().
Pluck("name", &clusterNames).
Error

if err != nil {
return nil, err
}

return clusterNames, nil
}

func (s *clustersService) GetAllClusterTypes() ([]string, error) {
var clusterTypes []string

Expand Down
23 changes: 23 additions & 0 deletions web/services/clusters_mock.go

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

5 changes: 5 additions & 0 deletions web/services/clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ func (suite *ClustersServiceTestSuite) TestClustersService_GetClustersCount() {
suite.Equal(3, count)
}

func (suite *ClustersServiceTestSuite) TestClustersService_GetAllClusterNames() {
clusterNames, _ := suite.clustersService.GetAllClusterNames()
suite.ElementsMatch([]string{"cluster1", "cluster2", "cluster3"}, clusterNames)
}

func (suite *ClustersServiceTestSuite) TestClustersService_GetAllClusterTypes() {
clusterTypes, _ := suite.clustersService.GetAllClusterTypes()
suite.ElementsMatch(
Expand Down
6 changes: 2 additions & 4 deletions web/templates/clusters.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
<select name="name" class="selectpicker" multiple
data-selected-text-format="count > 3" data-actions-box="true" data-live-search="true"
title="Cluster name">
{{- range .ClustersTable }}
{{- if .Name }}
<option value="{{ .Name }}">{{ .Name }}</option>
{{- end }}
{{- range .filterClusterNames }}
<option value="{{ . }}">{{ . }}</option>
{{- end}}
</select>
<select name="sids" class="selectpicker" multiple
Expand Down

0 comments on commit 58139c8

Please sign in to comment.