Skip to content

Commit 5fa7ef0

Browse files
authored
Merge pull request #2110 from afumagalli98/fix/existhostdatabatch
fix: add ExistHostdataBatch method for batch existence checks of host
2 parents 8e81009 + e4a7efc commit 5fa7ef0

File tree

7 files changed

+79
-37
lines changed

7 files changed

+79
-37
lines changed

api-service/database/database.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ type MongoDatabaseInterface interface {
192192
FindHostData(hostname string) (model.HostDataBE, error)
193193
// ExistHostdata return true if the host specified by hostname exist, otherwise false
194194
ExistHostdata(hostname string) (bool, error)
195+
ExistHostdataBatch(hostnames []string) ([]string, error)
195196
GetCpuCore(hostname string) (int, error)
196197
// GetHostsCountUsingTechnologies return a map that contains the number of usages for every features
197198
GetHostsCountUsingTechnologies(location string, environment string, olderThan time.Time) (map[string]float64, error)

api-service/database/hosts.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,52 @@ func (md *MongoDatabase) ExistHostdata(hostname string) (bool, error) {
903903
return val > 0, nil
904904
}
905905

906+
func (md *MongoDatabase) ExistHostdataBatch(hostnames []string) ([]string, error) {
907+
if len(hostnames) == 0 {
908+
return []string{}, nil
909+
}
910+
911+
filter := bson.M{
912+
"archived": false,
913+
"hostname": bson.M{
914+
"$in": hostnames,
915+
},
916+
}
917+
918+
cursor, err := md.Client.Database(md.Config.Mongodb.DBName).
919+
Collection("hosts").
920+
Find(context.TODO(), filter, options.Find().SetProjection(bson.M{"hostname": 1}))
921+
if err != nil {
922+
return nil, utils.NewError(err, "DB ERROR")
923+
}
924+
925+
defer cursor.Close(context.TODO())
926+
927+
existing := make(map[string]struct{})
928+
929+
for cursor.Next(context.TODO()) {
930+
var result struct {
931+
Hostname string `bson:"hostname"`
932+
}
933+
934+
if err := cursor.Decode(&result); err != nil {
935+
return nil, utils.NewError(err, "DECODE ERROR")
936+
}
937+
938+
existing[result.Hostname] = struct{}{}
939+
}
940+
941+
existingList := make([]string, 0, len(existing))
942+
943+
for _, h := range hostnames {
944+
if _, ok := existing[h]; ok {
945+
existingList = append(existingList, h)
946+
}
947+
}
948+
949+
return existingList, nil
950+
}
951+
906952
func (md *MongoDatabase) GetCpuCore(hostname string) (int, error) {
907953
pipeline := bson.A{
908954
bson.D{

api-service/service/databases.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -661,18 +661,11 @@ func (as *APIService) manageStandardDBVersionLicenses(usedLicenses []dto.Databas
661661
func (as *APIService) CalcVeritasClusterLicenses(usedLicenses []dto.DatabaseUsedLicense) {
662662
for i := 0; i < len(usedLicenses); i++ {
663663
ul := &usedLicenses[i]
664+
hosts := strings.Split(ul.ClusterName, ",")
664665

665-
realClusterHosts := make([]string, 0)
666-
667-
for _, host := range strings.Split(ul.ClusterName, ",") {
668-
exists, err := as.Database.ExistHostdata(host)
669-
if err != nil {
670-
continue
671-
}
672-
673-
if exists {
674-
realClusterHosts = append(realClusterHosts, host)
675-
}
666+
realClusterHosts, err := as.Database.ExistHostdataBatch(hosts)
667+
if err != nil {
668+
return
676669
}
677670

678671
if ul.ClusterType == "VeritasCluster" && strings.Contains(ul.Hostname, "_DR") {

api-service/service/databases_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ func TestGetUsedLicensesPerDatabases_Success(t *testing.T) {
779779

780780
sqlServerContracts := []model.SqlServerDatabaseContract{}
781781

782-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
782+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
783783
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
784784
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
785785
gomock.InOrder(
@@ -909,7 +909,7 @@ func TestGetUsedLicensesPerDatabases_VMWareCluster_Success(t *testing.T) {
909909

910910
sqlServerContracts := []model.SqlServerDatabaseContract{}
911911

912-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
912+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
913913
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
914914
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
915915
gomock.InOrder(
@@ -1012,7 +1012,7 @@ func TestGetUsedLicensesPerDatabases_VeritasCluster_Success(t *testing.T) {
10121012

10131013
sqlServerContracts := []model.SqlServerDatabaseContract{}
10141014

1015-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
1015+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
10161016
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
10171017
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
10181018
gomock.InOrder(
@@ -1187,7 +1187,7 @@ func TestGetUsedLicensesPerDatabasesAsXLSX_Success(t *testing.T) {
11871187

11881188
sqlServerContracts := []model.SqlServerDatabaseContract{}
11891189

1190-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
1190+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
11911191
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
11921192
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
11931193
gomock.InOrder(
@@ -1349,7 +1349,7 @@ func TestGetOracleDatabasesUsedLicenses_Host_WithActiveDataguardAndGoldenGate_Su
13491349
History: []model.History{},
13501350
}
13511351

1352-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
1352+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
13531353
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
13541354
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
13551355
gomock.InOrder(
@@ -1525,7 +1525,7 @@ func TestGetOracleDatabasesUsedLicenses_VeritasCluster_WithActiveDataguardAndGol
15251525
},
15261526
}
15271527

1528-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
1528+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
15291529
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
15301530
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
15311531
gomock.InOrder(
@@ -1672,7 +1672,7 @@ func TestGetOracleDatabasesUsedLicenses_Host_WithRacAndRacOneNode_Success(t *tes
16721672
History: []model.History{},
16731673
}
16741674

1675-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
1675+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
16761676
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
16771677
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
16781678
gomock.InOrder(
@@ -1813,7 +1813,7 @@ func TestGetOracleDatabasesUsedLicenses_VeritasCluster_WithRacAndRacOneNode_Succ
18131813
History: []model.History{},
18141814
}
18151815

1816-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
1816+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
18171817
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
18181818
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
18191819
gomock.InOrder(
@@ -2006,7 +2006,7 @@ func TestGetOracleDatabasesUsedLicenses_VmwareCluster_WithRacAndRacOneNode_Succe
20062006
History: []model.History{},
20072007
}
20082008

2009-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
2009+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
20102010
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
20112011
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
20122012
gomock.InOrder(
@@ -2204,7 +2204,7 @@ func TestGetDatabaseLicensesComplianceAsXLSX_Success(t *testing.T) {
22042204
}}, nil).AnyTimes()
22052205
db.EXPECT().GetClusters(globalFilterAny).
22062206
Return(clusters, nil).AnyTimes()
2207-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
2207+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
22082208
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
22092209
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
22102210
gomock.InOrder(
@@ -2431,7 +2431,7 @@ func TestGetDatabaseLicensesCompliance_Success(t *testing.T) {
24312431
}}, nil).AnyTimes()
24322432
db.EXPECT().GetClusters(globalFilterAny).
24332433
Return(clusters, nil).AnyTimes()
2434-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
2434+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
24352435
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
24362436
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
24372437
gomock.InOrder(
@@ -2673,7 +2673,7 @@ func TestGetUsedLicensesPerHost_Success(t *testing.T) {
26732673
Return(hostdatas, nil).AnyTimes()
26742674
db.EXPECT().GetClusters(globalFilterAny).
26752675
Return(clusters, nil).AnyTimes()
2676-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
2676+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
26772677
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
26782678
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
26792679
gomock.InOrder(
@@ -2864,7 +2864,7 @@ func TestGetUsedLicensesPerHostAsXLSX_Success(t *testing.T) {
28642864
Return(hostdatas, nil).AnyTimes()
28652865
db.EXPECT().GetClusters(globalFilterAny).
28662866
Return(clusters, nil).AnyTimes()
2867-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
2867+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
28682868
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
28692869
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
28702870
gomock.InOrder(
@@ -2950,7 +2950,7 @@ func TestGetUsedLicensesPerCluster_OneVm_Success(t *testing.T) {
29502950
Return(hostdatasVm1, nil).AnyTimes()
29512951
db.EXPECT().GetClusters(globalFilterAny).
29522952
Return(clusters, nil).AnyTimes()
2953-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
2953+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
29542954
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
29552955
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
29562956
gomock.InOrder(
@@ -3081,7 +3081,7 @@ func TestGetUsedLicensesPerCluster_MultipleVms_Success(t *testing.T) {
30813081
Return(hostdatasVm1, nil).AnyTimes()
30823082
db.EXPECT().GetClusters(globalFilterAny).
30833083
Return(clusters, nil).AnyTimes()
3084-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
3084+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
30853085
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
30863086
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
30873087
gomock.InOrder(
@@ -3167,7 +3167,7 @@ func TestGetUsedLicensesPerClusterAsXLSX_Success(t *testing.T) {
31673167
Return(hostdatasVm1, nil).AnyTimes()
31683168
db.EXPECT().GetClusters(globalFilterAny).
31693169
Return(clusters, nil).AnyTimes()
3170-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
3170+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
31713171
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
31723172
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
31733173
gomock.InOrder(
@@ -3245,7 +3245,7 @@ func TestGetDatabaseLicensesComplianceSqlServerHostWithContractContract_Success(
32453245
}}, nil).AnyTimes()
32463246
db.EXPECT().GetClusters(globalFilterAny).
32473247
Return(clusters, nil).AnyTimes()
3248-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
3248+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
32493249
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
32503250
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
32513251
gomock.InOrder(
@@ -3516,7 +3516,7 @@ func TestGetDatabaseLicensesComplianceSqlServerHostInClusterWithContract_Success
35163516
}}, nil).AnyTimes()
35173517
db.EXPECT().GetClusters(globalFilterAny).
35183518
Return(clusters, nil).AnyTimes()
3519-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
3519+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{"plutocluster"}, nil).AnyTimes()
35203520
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
35213521
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
35223522
gomock.InOrder(
@@ -3559,7 +3559,7 @@ func TestGetDatabaseLicensesComplianceSqlServerHostInClusterWithContract_Success
35593559
Return(&cluster, nil),
35603560
)
35613561

3562-
db.EXPECT().ExistHostdata("plutocluster").Return(true, nil).AnyTimes()
3562+
db.EXPECT().ExistHostdata("plutohost").Return(true, nil).AnyTimes()
35633563

35643564
actual, err := as.GetDatabaseLicensesCompliance([]string{})
35653565
require.NoError(t, err)

api-service/service/frontend_api_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ func TestGetComplianceStatsAsAdmin(t *testing.T) {
681681
Return(hostdatas, nil).
682682
AnyTimes()
683683

684-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
684+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
685685
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
686686
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
687687

@@ -992,7 +992,7 @@ func TestGetComplianceStatsAsUser(t *testing.T) {
992992
Return(hostdatas, nil).
993993
AnyTimes()
994994

995-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
995+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
996996
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
997997
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
998998

api-service/service/oracle_database_contracts_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ func TestGetOracleDatabaseContracts_Success(t *testing.T) {
405405
CoveredLicenses: 3,
406406
},
407407
}
408-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
408+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
409409
db.EXPECT().GetHostDatas(dto.GlobalFilter{OlderThan: utils.MAX_TIME}).
410410
Return(hostdatas, nil).AnyTimes()
411411
db.EXPECT().GetClusters(globalFilterAny).
@@ -576,6 +576,7 @@ func TestGetOracleDatabaseContractsCluster_Success(t *testing.T) {
576576
}
577577

578578
db.EXPECT().ExistHostdata(gomock.Any()).Return(false, nil).AnyTimes()
579+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{"test-db"}, nil).AnyTimes()
579580
db.EXPECT().GetHostDatas(dto.GlobalFilter{OlderThan: utils.MAX_TIME}).
580581
Return(hostdatas, nil).AnyTimes()
581582
db.EXPECT().GetClusters(globalFilterAny).
@@ -747,6 +748,7 @@ func TestGetOracleDatabaseContractsClusterCappedCPU_Success(t *testing.T) {
747748
}
748749

749750
db.EXPECT().ExistHostdata(gomock.Any()).Return(false, nil).AnyTimes()
751+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{"test-db"}, nil).AnyTimes()
750752
db.EXPECT().GetHostDatas(dto.GlobalFilter{OlderThan: utils.MAX_TIME}).
751753
Return(hostdatas, nil).AnyTimes()
752754
db.EXPECT().GetClusters(globalFilterAny).
@@ -957,6 +959,7 @@ func TestGetOracleDatabaseContractsClusterCappedCPU2_Success(t *testing.T) {
957959
}
958960

959961
db.EXPECT().ExistHostdata(gomock.Any()).Return(false, nil).AnyTimes()
962+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{"bart"}, nil).AnyTimes()
960963
db.EXPECT().ListOracleDatabaseContracts(gomock.Any()).
961964
Return(returnedContracts, nil)
962965

@@ -1057,8 +1060,7 @@ func TestGetOracleDatabaseContracts_SuccessFilter1(t *testing.T) {
10571060
},
10581061
}
10591062

1060-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
1061-
1063+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
10621064
db.EXPECT().GetHostDatas(dto.GlobalFilter{OlderThan: utils.MAX_TIME}).
10631065
Return(hostdatas, nil).AnyTimes()
10641066
db.EXPECT().GetClusters(globalFilterAny).

api-service/service/oracle_database_license_types_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func TestGetLicensesCompliance(t *testing.T) {
237237
Return(hostdatas, nil).AnyTimes()
238238
db.EXPECT().GetClusters(filter).
239239
Return(clusters, nil).AnyTimes()
240-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
240+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
241241
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
242242
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
243243

@@ -480,7 +480,7 @@ func TestGetLicensesCompliance_Veritas(t *testing.T) {
480480
db.EXPECT().GetClusters(filter).
481481
Return(clusters, nil).AnyTimes()
482482

483-
db.EXPECT().ExistHostdata(gomock.Any()).Return(true, nil).AnyTimes()
483+
db.EXPECT().ExistHostdataBatch(gomock.Any()).Return([]string{}, nil).AnyTimes()
484484
db.EXPECT().FindClusterVeritasLicenses(gomock.Any()).
485485
Return([]dto.ClusterVeritasLicense{}, nil).AnyTimes()
486486

0 commit comments

Comments
 (0)