-
Notifications
You must be signed in to change notification settings - Fork 11
/
oracle_database_agreements.go
743 lines (590 loc) · 22.9 KB
/
oracle_database_agreements.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
// Copyright (c) 2020 Sorint.lab S.p.A.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package service
import (
"math"
"sort"
"strings"
"github.com/ercole-io/ercole/api-service/database"
"github.com/ercole-io/ercole/api-service/dto"
"github.com/ercole-io/ercole/model"
"github.com/ercole-io/ercole/utils"
"go.mongodb.org/mongo-driver/bson/primitive"
)
//TODO Instead of use 25 everywhere for NamedUserPlus licenses, use const
// TODO When insert or update unlimited agr, set count == 0
// AddAssociatedPartToOracleDbAgreement add associated part to OracleDatabaseAgreement or create a new one
func (as *APIService) AddAssociatedPartToOracleDbAgreement(request dto.AssociatedPartInOracleDbAgreementRequest,
) (string, utils.AdvancedErrorInterface) {
if err := checkHosts(as, request.Hosts); err != nil {
return "", err
}
agreement, err := as.Database.GetOracleDatabaseAgreement(request.AgreementID)
if err == utils.AerrOracleDatabaseAgreementNotFound {
agreement = &model.OracleDatabaseAgreement{
AgreementID: request.AgreementID,
CSI: request.CSI,
Parts: make([]model.AssociatedPart, 0),
}
} else if err != nil {
return "", err
}
if err := addAssociatedPart(as, agreement, request); err != nil {
return "", err
}
if agreement.ID == primitive.NilObjectID {
agreement.ID = as.NewObjectID()
res, err := as.Database.InsertOracleDatabaseAgreement(*agreement)
if err != nil {
return "", err
}
agreement.ID = res.InsertedID.(primitive.ObjectID)
} else {
err := as.Database.UpdateOracleDatabaseAgreement(*agreement)
if err != nil {
return "", err
}
}
return agreement.ID.Hex(), nil
}
func addAssociatedPart(as *APIService, agreement *model.OracleDatabaseAgreement,
req dto.AssociatedPartInOracleDbAgreementRequest) utils.AdvancedErrorInterface {
part, err := as.GetOraclePart(req.PartID)
if err != nil {
return err
}
associatedPart := model.AssociatedPart{
ID: as.NewObjectID(),
OracleDatabasePart: *part,
ReferenceNumber: req.ReferenceNumber,
Unlimited: req.Unlimited,
Count: req.Count,
CatchAll: req.CatchAll,
Hosts: req.Hosts,
}
agreement.Parts = append(agreement.Parts, associatedPart)
return nil
}
func checkHosts(as *APIService, hosts []string) utils.AdvancedErrorInterface {
notInClusterHosts, aerr := as.SearchHosts("hostnames", "", database.SearchHostsFilters{
GTECPUCores: -1,
LTECPUCores: -1,
LTECPUThreads: -1,
LTEMemoryTotal: -1,
GTECPUThreads: -1,
GTESwapTotal: -1,
GTEMemoryTotal: -1,
LTESwapTotal: -1,
}, "", false, -1, -1, "", "", utils.MAX_TIME)
if aerr != nil {
return aerr
}
notInClusterHostnames := make([]string, len(notInClusterHosts))
for i, h := range notInClusterHosts {
notInClusterHostnames[i] = h["hostname"].(string)
}
hosts_loop:
for _, host := range hosts {
for _, notInClusterHostname := range notInClusterHostnames {
if host == notInClusterHostname {
continue hosts_loop
}
}
return utils.AerrHostNotFound
}
return nil
}
// UpdateAssociatedPartOfOracleDbAgreement update associated part in OracleDatabaseAgreement
func (as *APIService) UpdateAssociatedPartOfOracleDbAgreement(request dto.AssociatedPartInOracleDbAgreementRequest,
) utils.AdvancedErrorInterface {
if err := checkHosts(as, request.Hosts); err != nil {
return err
}
associatedPartID := utils.Str2oid(request.ID)
agreement, err := as.Database.GetOracleDatabaseAgreementByAssociatedPart(associatedPartID)
if err != nil {
return err
}
err = updateAssociatedPart(as, agreement, request)
if err != nil {
return err
}
return as.Database.UpdateOracleDatabaseAgreement(*agreement)
}
func updateAssociatedPart(as *APIService, agreement *model.OracleDatabaseAgreement,
req dto.AssociatedPartInOracleDbAgreementRequest) utils.AdvancedErrorInterface {
var associatedPart *model.AssociatedPart
reqID := utils.Str2oid(req.ID)
for i := range agreement.Parts {
if agreement.Parts[i].ID == reqID {
associatedPart = &agreement.Parts[i]
break
}
}
if associatedPart == nil {
return utils.AerrOracleDatabaseAssociatedPartNotFound
}
part, err := as.GetOraclePart(req.PartID)
if err != nil {
return err
}
associatedPart.OracleDatabasePart = *part
associatedPart.ReferenceNumber = req.ReferenceNumber
associatedPart.Unlimited = req.Unlimited
associatedPart.Count = req.Count
associatedPart.CatchAll = req.CatchAll
associatedPart.Hosts = req.Hosts
return nil
}
// SearchAssociatedPartsInOracleDatabaseAgreements search OracleDatabase associated parts agreements
func (as *APIService) SearchAssociatedPartsInOracleDatabaseAgreements(filters dto.SearchOracleDatabaseAgreementsFilter,
) ([]dto.OracleDatabaseAgreementFE, utils.AdvancedErrorInterface) {
agreements, err := as.Database.ListOracleDatabaseAgreements()
if err != nil {
return nil, err
}
hosts, err := as.Database.ListHostUsingOracleDatabaseLicenses()
if err != nil {
return nil, err
}
as.assignOracleDatabaseAgreementsToHosts(agreements, hosts)
filteredAgrs := make([]dto.OracleDatabaseAgreementFE, 0)
for _, agr := range agreements {
if checkOracleDatabaseAgreementMatchFilter(agr, filters) {
filteredAgrs = append(filteredAgrs, agr)
}
}
return filteredAgrs, nil
}
// assignOracleDatabaseAgreementsToHosts assign available licenses in each agreements to hosts using licenses
func (as *APIService) assignOracleDatabaseAgreementsToHosts(
agrs []dto.OracleDatabaseAgreementFE,
hosts []dto.HostUsingOracleDatabaseLicenses) {
hosts:
for i := range hosts {
host := &hosts[i]
for _, part := range as.OracleDatabaseAgreementParts {
for _, alias := range part.Aliases {
if host.LicenseName == alias {
host.PartID = part.PartID
continue hosts
}
}
}
as.Log.Errorf("Can't find OracleDatabaseAgreementPart for host: %#v\n", host)
}
sortOracleDatabaseAgreements(agrs)
sortHostsUsingLicenses(hosts)
if as.Config.APIService.DebugOracleDatabaseAgreementsAssignmentAlgorithm {
as.Log.Debugf("Agreements = %s\nHosts= %s\n", utils.ToJSON(agrs), utils.ToJSON(hosts))
}
hostsMap := buildHostUsingLicensesMap(hosts)
partsMap := buildAgreementPartMap(as.OracleDatabaseAgreementParts)
fillAgreementsInfo(as, agrs, partsMap)
assignAgreementsLicensesToItsAssociatedHosts(as, agrs, hostsMap, partsMap)
// sort again and rebuild map because the references are updated during the sort
sortHostsUsingLicenses(hosts)
hostsMap = buildHostUsingLicensesMap(hosts)
if as.Config.APIService.DebugOracleDatabaseAgreementsAssignmentAlgorithm {
as.Log.Debugf("Resorted LicensingObjects: %#v\n", hosts)
}
assignLicensesFromCatchAllAgreements(as, agrs, hosts, partsMap)
calculateTotalCoveredLicensesAndAvailableCount(as, agrs, hosts, hostsMap, partsMap)
}
// sortOracleDatabaseAgreements sort the list of dto.OracleDatabaseAgreementsFE
// by CatchAll (falses first), Unlimited (falses first), decreasing UsersCount, decreasing LicensesCount
func sortOracleDatabaseAgreements(obj []dto.OracleDatabaseAgreementFE) {
sort.Slice(obj, func(i, j int) bool {
if obj[i].CatchAll != obj[j].CatchAll {
return obj[j].CatchAll
} else if obj[i].Unlimited != obj[j].Unlimited {
return obj[j].Unlimited
} else if obj[i].UsersCount != obj[j].UsersCount {
return obj[i].UsersCount > obj[j].UsersCount
} else {
return obj[i].LicensesCount > obj[j].LicensesCount
}
})
}
// sortHostsUsingLicenses sort the list of hosts by decreasing license count,
// alphabetical name, alphabetical license name
func sortHostsUsingLicenses(obj []dto.HostUsingOracleDatabaseLicenses) {
sort.Slice(obj, func(i, j int) bool {
if obj[i].LicenseCount != obj[j].LicenseCount {
return obj[i].LicenseCount > obj[j].LicenseCount
} else if obj[i].Name != obj[j].Name {
return obj[i].Name > obj[j].Name
} else {
return obj[i].LicenseName > obj[j].LicenseName
}
})
}
// buildHostUsingLicensesMap return a map of license name to map of object name to pointer to
// dto.HostUsingOracleDatabaseLicenses for fast object lookup
// Assume that doesn't exist a cluster and a host with the same name
func buildHostUsingLicensesMap(hosts []dto.HostUsingOracleDatabaseLicenses,
) map[string]map[string]*dto.HostUsingOracleDatabaseLicenses {
res := make(map[string]map[string]*dto.HostUsingOracleDatabaseLicenses)
for i, host := range hosts {
if _, ok := res[host.LicenseName]; !ok {
res[host.LicenseName] = make(map[string]*dto.HostUsingOracleDatabaseLicenses)
}
res[host.LicenseName][host.Name] = &hosts[i]
}
return res
}
// buildAgreementPartMap return a map of partID to OracleDatabaseAgreementPart
func buildAgreementPartMap(parts []model.OracleDatabasePart) map[string]*model.OracleDatabasePart {
partsMap := make(map[string]*model.OracleDatabasePart)
for i, part := range parts {
partsMap[part.PartID] = &parts[i]
}
return partsMap
}
func fillAgreementsInfo(as *APIService, agrs []dto.OracleDatabaseAgreementFE, partsMap map[string]*model.OracleDatabasePart) {
for i := range agrs {
agr := &agrs[i]
if part, ok := partsMap[agr.PartID]; ok {
agr.ItemDescription = part.ItemDescription
agr.Metric = part.Metric
switch agr.Metric {
case model.AgreementPartMetricProcessorPerpetual:
agr.LicensesCount = agr.Count
case model.AgreementPartMetricNamedUserPlusPerpetual:
agr.UsersCount = agr.Count
}
} else {
as.Log.Errorf("Unknown PartID: [%s] in agreement: [%#v]", agr.PartID, agr)
}
}
}
// Assign available licenses in each agreement to each host associated in each agreement
// if this host is using that kind of license.
func assignAgreementsLicensesToItsAssociatedHosts(
as *APIService,
agreements []dto.OracleDatabaseAgreementFE,
hostsMap map[string]map[string]*dto.HostUsingOracleDatabaseLicenses,
partsMap map[string]*model.OracleDatabasePart) {
for i := range agreements {
agreement := &agreements[i]
sortHostsInAgreementByLicenseCount(agreement, hostsMap, partsMap)
if as.Config.APIService.DebugOracleDatabaseAgreementsAssignmentAlgorithm {
as.Log.Debugf("Distributing licenses of agreement #%d to host. Agreement = %s\n", i, utils.ToJSON(agreement))
}
for j := range agreement.Hosts {
associatedHost := &agreement.Hosts[j]
for _, alias := range partsMap[agreement.PartID].Aliases {
if agreement.AvailableCount <= 0 && !agreement.Unlimited {
break
}
if _, ok := hostsMap[alias]; !ok {
// no host use this license
continue
}
var hostUsingLicenses *dto.HostUsingOracleDatabaseLicenses
var ok bool
if hostUsingLicenses, ok = hostsMap[alias][associatedHost.Hostname]; !ok {
// host doesn't use this license
continue
}
if hostUsingLicenses.LicenseCount <= 0 {
continue
}
doAssignAgreementLicensesToAssociatedHost(as, agreement, hostUsingLicenses, associatedHost)
if as.Config.APIService.DebugOracleDatabaseAgreementsAssignmentAlgorithm {
as.Log.Debugf(`Distributing %f licenses to host %s. agr.Metrics=%s agr.AvailableCount=%f \
hostInAgr.CoveredLicensesCount=%f hostUsingLicenses.LicenseCount=%f licenseName=%s\n`,
hostUsingLicenses.LicenseCount,
associatedHost.Hostname,
agreement.Metric,
agreement.AvailableCount,
associatedHost.CoveredLicensesCount,
hostUsingLicenses.LicenseCount,
alias)
}
}
if agreement.AvailableCount <= 0 && !agreement.Unlimited {
break
}
}
}
}
// sortHostsInAgreementByLicenseCount sort the associated hosts by license count
// considering that parts may have multiple aliases
func sortHostsInAgreementByLicenseCount(agr *dto.OracleDatabaseAgreementFE,
hostsMap map[string]map[string]*dto.HostUsingOracleDatabaseLicenses,
partsMap map[string]*model.OracleDatabasePart) {
sort.Slice(agr.Hosts, func(i, j int) bool {
var iMaxLicenseCount float64 = 0
var jMaxLicenseCount float64 = 0
for _, alias := range partsMap[agr.PartID].Aliases {
if mapHostnamesLicenses, ok := hostsMap[alias]; ok {
if hostUsingLicenses, ok := mapHostnamesLicenses[agr.Hosts[i].Hostname]; ok {
iMaxLicenseCount = math.Max(iMaxLicenseCount, hostUsingLicenses.LicenseCount)
}
if hostUsingLicenses, ok := mapHostnamesLicenses[agr.Hosts[j].Hostname]; ok {
jMaxLicenseCount = math.Max(jMaxLicenseCount, hostUsingLicenses.LicenseCount)
}
}
}
return iMaxLicenseCount > jMaxLicenseCount
})
}
// Use all the licenses available in agreement to cover host and associatedHost if provided
func doAssignAgreementLicensesToAssociatedHost(
as *APIService,
agreement *dto.OracleDatabaseAgreementFE,
host *dto.HostUsingOracleDatabaseLicenses,
associatedHost *dto.OracleDatabaseAgreementAssociatedHostFE) {
switch {
case agreement.Metric == model.AgreementPartMetricProcessorPerpetual ||
agreement.Metric == model.AgreementPartMetricComputerPerpetual:
var coverableLicenses float64
if agreement.Unlimited {
coverableLicenses = host.LicenseCount
agreement.AvailableCount = 0
} else {
coverableLicenses = math.Min(agreement.AvailableCount, host.LicenseCount)
agreement.AvailableCount -= coverableLicenses
}
associatedHost.CoveredLicensesCount += coverableLicenses
host.LicenseCount -= coverableLicenses
case agreement.Metric == model.AgreementPartMetricNamedUserPlusPerpetual:
var coverableLicenses float64
if agreement.Unlimited {
coverableLicenses = host.LicenseCount
agreement.AvailableCount = 0
} else {
coverableLicenses = math.Floor(math.Min(agreement.AvailableCount, host.LicenseCount*25) / 25)
agreement.AvailableCount -= coverableLicenses * 25
}
associatedHost.CoveredLicensesCount += coverableLicenses * 25
host.LicenseCount -= coverableLicenses
default:
as.Log.Errorf("Distributing licenses. Unknown metric type: [%s]", agreement.Metric)
}
}
// If an agreement is catchAll (or basket..) distributes its licenses to every hosts that use that kind of license
func assignLicensesFromCatchAllAgreements(
as *APIService,
agrs []dto.OracleDatabaseAgreementFE,
hosts []dto.HostUsingOracleDatabaseLicenses,
partsMap map[string]*model.OracleDatabasePart) {
for i := range hosts {
host := &hosts[i]
if host.LicenseCount <= 0 {
continue
}
for j := range agrs {
agr := &agrs[j]
if !agr.CatchAll {
continue
}
if agr.AvailableCount <= 0 && !agr.Unlimited {
continue
}
for _, alias := range partsMap[agr.PartID].Aliases {
if agr.AvailableCount <= 0 && !agr.Unlimited {
break
}
if host.LicenseName != alias {
continue
}
doAssignLicenseFromCatchAllAgreement(as, agr, host)
if as.Config.APIService.DebugOracleDatabaseAgreementsAssignmentAlgorithm {
as.Log.Debugf("Distributing with metric [%s] [ULA? %t] %f licenses to obj %s. aggCount=%f objCount=0 licenseName=%s\n",
agr.Metric,
agr.Unlimited,
host.LicenseCount,
host.Name,
agr.Count,
alias)
}
}
}
}
if as.Config.APIService.DebugOracleDatabaseAgreementsAssignmentAlgorithm {
as.Log.Debugf("Associations finished. LicensingObjects: %#v\n", hosts)
}
}
// Use all the licenses available in agreement to cover host and associatedHost if provided
func doAssignLicenseFromCatchAllAgreement(
as *APIService,
agreement *dto.OracleDatabaseAgreementFE,
hostUsingLicenses *dto.HostUsingOracleDatabaseLicenses) {
switch {
case agreement.Metric == model.AgreementPartMetricProcessorPerpetual ||
agreement.Metric == model.AgreementPartMetricComputerPerpetual:
var coverableLicenses float64
if agreement.Unlimited {
coverableLicenses = hostUsingLicenses.LicenseCount
agreement.AvailableCount = 0
} else {
coverableLicenses = math.Min(agreement.AvailableCount, hostUsingLicenses.LicenseCount)
agreement.AvailableCount -= coverableLicenses
}
hostUsingLicenses.LicenseCount -= coverableLicenses
case agreement.Metric == model.AgreementPartMetricNamedUserPlusPerpetual:
var coverableLicenses float64
if agreement.Unlimited {
coverableLicenses = hostUsingLicenses.LicenseCount
agreement.AvailableCount = 0
} else {
coverableLicenses = math.Floor(math.Min(agreement.AvailableCount, hostUsingLicenses.LicenseCount*25) / 25)
agreement.AvailableCount -= coverableLicenses * 25
}
hostUsingLicenses.LicenseCount -= coverableLicenses
default:
as.Log.Errorf("Distributing licenses. Unknown metric type: [%s]", agreement.Metric)
}
}
func calculateTotalCoveredLicensesAndAvailableCount(
as *APIService,
agrs []dto.OracleDatabaseAgreementFE,
hosts []dto.HostUsingOracleDatabaseLicenses,
hostsMap map[string]map[string]*dto.HostUsingOracleDatabaseLicenses,
partsMap map[string]*model.OracleDatabasePart) {
licensesCoverStatusByName := calculateCoverStatusByLicenseName(hosts)
for i := range agrs {
agreement := &agrs[i]
uncoveredLicensesByAssociatedHosts := 0.0
uncoveredLicensesByAllHosts := 0.0
for _, alias := range partsMap[agreement.PartID].Aliases {
for j := range agreement.Hosts {
associatedHost := &agreement.Hosts[j]
if _, ok := hostsMap[alias]; !ok {
continue
}
host, ok := hostsMap[alias][associatedHost.Hostname]
if !ok {
continue
}
switch {
case agreement.Metric == model.AgreementPartMetricProcessorPerpetual ||
agreement.Metric == model.AgreementPartMetricComputerPerpetual:
associatedHost.TotalCoveredLicensesCount = host.OriginalCount - host.LicenseCount
associatedHost.ConsumedLicensesCount = host.OriginalCount
uncoveredLicensesByAssociatedHosts += host.LicenseCount
case agreement.Metric == model.AgreementPartMetricNamedUserPlusPerpetual:
associatedHost.TotalCoveredLicensesCount = (host.OriginalCount - host.LicenseCount) * 25
associatedHost.ConsumedLicensesCount = host.OriginalCount * 25
uncoveredLicensesByAssociatedHosts += host.LicenseCount * 25
default:
as.Log.Errorf("Unknown metric type: [%s]", agreement.Metric)
}
}
uncoveredLicensesByAllHosts += licensesCoverStatusByName[alias].Consumed - licensesCoverStatusByName[alias].Covered
}
var uncoveredLicenses float64
if agreement.CatchAll {
uncoveredLicenses = uncoveredLicensesByAllHosts
} else {
uncoveredLicenses = uncoveredLicensesByAssociatedHosts
}
if uncoveredLicenses > 0 {
if (agreement.AvailableCount > 0 && agreement.Metric != model.AgreementPartMetricNamedUserPlusPerpetual) ||
(agreement.AvailableCount > 25 && agreement.Metric == model.AgreementPartMetricNamedUserPlusPerpetual) {
as.Log.Errorf("Agreement has still some available licenses but hosts are uncovered. Agreement: [%v]",
agreement)
}
agreement.AvailableCount -= uncoveredLicenses
}
}
}
type coverStatus struct {
Covered float64 //==purchased
Consumed float64 //==consumed
}
// Calculate total number of covered/uncovered for each host
func calculateCoverStatusByLicenseName(hosts []dto.HostUsingOracleDatabaseLicenses) map[string]coverStatus {
licensesStatus := make(map[string]coverStatus)
for _, host := range hosts {
licensesStatus[host.LicenseName] = coverStatus{
Consumed: licensesStatus[host.LicenseName].Consumed + host.OriginalCount,
Covered: licensesStatus[host.LicenseName].Covered + (host.OriginalCount - host.LicenseCount),
}
}
return licensesStatus
}
// checkOracleDatabaseAgreementMatchFilter check that agr match the filters
func checkOracleDatabaseAgreementMatchFilter(agr dto.OracleDatabaseAgreementFE, filters dto.SearchOracleDatabaseAgreementsFilter) bool {
return strings.Contains(strings.ToLower(agr.AgreementID), strings.ToLower(filters.AgreementID)) &&
strings.Contains(strings.ToLower(agr.PartID), strings.ToLower(filters.PartID)) &&
strings.Contains(strings.ToLower(agr.ItemDescription), strings.ToLower(filters.ItemDescription)) &&
strings.Contains(strings.ToLower(agr.CSI), strings.ToLower(filters.CSI)) &&
(filters.Metric == "" || strings.ToLower(agr.Metric) == strings.ToLower(filters.Metric)) &&
strings.Contains(strings.ToLower(agr.ReferenceNumber), strings.ToLower(filters.ReferenceNumber)) &&
(filters.Unlimited == "NULL" || agr.Unlimited == (filters.Unlimited == "true")) &&
(filters.CatchAll == "NULL" || agr.CatchAll == (filters.CatchAll == "true")) &&
(filters.LicensesCountLTE == -1 || agr.LicensesCount <= float64(filters.LicensesCountLTE)) &&
(filters.LicensesCountGTE == -1 || agr.LicensesCount >= float64(filters.LicensesCountGTE)) &&
(filters.UsersCountLTE == -1 || agr.UsersCount <= float64(filters.UsersCountLTE)) &&
(filters.UsersCountGTE == -1 || agr.UsersCount >= float64(filters.UsersCountGTE)) &&
(filters.AvailableCountLTE == -1 || agr.AvailableCount <= float64(filters.AvailableCountLTE)) &&
(filters.AvailableCountGTE == -1 || agr.AvailableCount >= float64(filters.AvailableCountGTE))
}
// DeleteAssociatedPartFromOracleDatabaseAgreement delete associated part from OracleDatabaseAgreement
func (as *APIService) DeleteAssociatedPartFromOracleDatabaseAgreement(associatedPartID primitive.ObjectID,
) utils.AdvancedErrorInterface {
agreement, err := as.Database.GetOracleDatabaseAgreementByAssociatedPart(associatedPartID)
if err != nil {
return err
}
if len(agreement.Parts) <= 1 {
return as.Database.RemoveOracleDatabaseAgreement(agreement.ID)
}
for i := range agreement.Parts {
if agreement.Parts[i].ID == associatedPartID {
agreement.Parts = append(agreement.Parts[:i], agreement.Parts[i+1])
break
}
}
return as.Database.UpdateOracleDatabaseAgreement(*agreement)
}
// AddHostToAssociatedPart add an host to AssociatedPart
func (as *APIService) AddHostToAssociatedPart(associatedPartID primitive.ObjectID, hostname string,
) utils.AdvancedErrorInterface {
agreement, err := as.Database.GetOracleDatabaseAgreementByAssociatedPart(associatedPartID)
if err != nil {
return err
}
associatedPart := agreement.AssociatedPartByID(associatedPartID)
for _, host := range associatedPart.Hosts {
if host == hostname {
return nil
}
}
if err := checkHosts(as, []string{hostname}); err != nil {
return err
}
associatedPart.Hosts = append(associatedPart.Hosts, hostname)
return as.Database.UpdateOracleDatabaseAgreement(*agreement)
}
// RemoveHostFromAssociatedPart remove host from AssociatedPart
func (as *APIService) RemoveHostFromAssociatedPart(associatedPartID primitive.ObjectID, hostname string,
) utils.AdvancedErrorInterface {
agreement, err := as.Database.GetOracleDatabaseAgreementByAssociatedPart(associatedPartID)
if err != nil {
return err
}
associatedPart := agreement.AssociatedPartByID(associatedPartID)
for i, host := range associatedPart.Hosts {
if host == hostname {
associatedPart.Hosts = append(associatedPart.Hosts[:i], associatedPart.Hosts[i+1:]...)
return as.Database.UpdateOracleDatabaseAgreement(*agreement)
}
}
return nil
}