-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathscore_calculator.go
812 lines (705 loc) · 21.2 KB
/
score_calculator.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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package policy
import (
"math"
"strconv"
"github.com/cockroachdb/errors"
"go.mondoo.com/cnquery/v11/explorer"
"google.golang.org/protobuf/proto"
)
// ScoreCalculator interface for calculating scores
type ScoreCalculator interface {
Add(score *Score, impact *explorer.Impact)
Calculate() *Score
Init()
String() string
}
type averageScoreCalculator struct {
value uint32
weight uint32
scoreTotal uint32
scoreCompletion uint32
scoreCnt uint32
dataTotal uint32
dataCompletion uint32
hasResults bool
hasErrors bool
featureFlagFailErrors bool
}
func (c *averageScoreCalculator) String() string {
return "Average"
}
func (c *averageScoreCalculator) Init() {
c.value = 0
c.weight = 0
c.scoreTotal = 0
c.scoreCompletion = 0
c.scoreCnt = 0
c.dataTotal = 0
c.dataCompletion = 0
c.hasResults = false
c.hasErrors = false
}
func AddSpecdScore(calculator ScoreCalculator, s *Score, found bool, impact *explorer.Impact) {
if !found {
calculator.Add(&Score{
ScoreCompletion: 0,
DataCompletion: 0,
}, nil)
return
}
score := proto.Clone(s).(*Score)
if impact != nil && impact.Value != nil {
floor := 100 - uint32(impact.Value.Value)
if floor > score.Value {
score.Value = floor
}
}
// we ignore the UNSPECIFIED specs
if impact == nil {
calculator.Add(score, nil)
return
}
// everything else is modify or activate
if impact.Scoring == explorer.ScoringSystem_IGNORE_SCORE {
calculator.Add(&Score{
// We override the type because:
// 1. If it is set to Result, its value will be added to the total
// calculation in most calculators despite its weight.
// 2. We don't want to set it to unscored, because technically we
// just ignore the score.
// Thus we set the score to unknown for the sake of the calculator,
// thus it knows it is handling a scored result, but also knows not
// to count it.
Type: ScoreType_Unknown,
Value: score.Value,
Weight: 0,
ScoreCompletion: score.ScoreCompletion,
DataCompletion: score.DataCompletion,
DataTotal: score.DataTotal,
}, nil)
return
}
if impact.Weight > 0 {
score.Weight = uint32(impact.Weight)
} else if score.Weight == 0 {
score.Weight = 1
}
calculator.Add(score, impact)
}
func AddDataScore(calculator ScoreCalculator, totalDeps int, finishedDeps int) {
if totalDeps == 0 {
return
}
dataCompletion := uint32((100 * finishedDeps) / totalDeps)
calculator.Add(&Score{
Type: ScoreType_Unscored,
DataTotal: uint32(totalDeps),
DataCompletion: dataCompletion,
}, nil)
}
func (c *averageScoreCalculator) Add(score *Score, impact *explorer.Impact) {
switch score.Type {
case ScoreType_Skip, ScoreType_Disabled, ScoreType_OutOfScope:
return
case ScoreType_Unscored:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
case ScoreType_Result:
if impact != nil && (impact.Action == explorer.Action_IGNORE || impact.Action == explorer.Action_DEACTIVATE) {
return
}
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
c.scoreCompletion += score.ScoreCompletion
c.scoreTotal++
if score.ScoreCompletion != 0 {
c.scoreCnt++
c.value += score.Value
}
c.hasResults = true
case ScoreType_Error:
c.hasErrors = true
if c.featureFlagFailErrors {
// This case is the same as ScoreType_Result. Once the feature flag
// is removed, this case can be merged with the ScoreType_Result
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
c.scoreCompletion += score.ScoreCompletion
c.scoreTotal++
if score.ScoreCompletion != 0 {
c.scoreCnt++
c.value += score.Value
}
c.hasResults = true
} else {
// This case is the same as ScoreType_Unscored. Once the feature flag
// is removed, this case can be removed
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.scoreCompletion += score.ScoreCompletion
c.scoreTotal++
}
default:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.scoreCompletion += score.ScoreCompletion
c.scoreTotal++
}
}
func (c *averageScoreCalculator) Calculate() *Score {
res := &Score{
Type: ScoreType_Unscored,
DataTotal: c.dataTotal,
// unless we know otherwise, we are setting the data completion to 100
// until we determine how many datapoints we are looking for
DataCompletion: 100,
// if the item is indeed unscored, then the score completion is 100
// since we are done with the scoring piece
ScoreCompletion: 100,
}
if c.dataTotal != 0 {
res.DataCompletion = c.dataCompletion / c.dataTotal
}
if c.hasResults {
// if this is scored indicator, we need to calculate the value
res.Type = ScoreType_Result
res.ScoreCompletion = c.scoreCompletion / c.scoreTotal
res.Weight = c.weight
if c.scoreCnt != 0 {
res.Value = c.value / c.scoreCnt
}
} else if c.hasErrors {
res.Type = ScoreType_Error
}
return res
}
type weightedScoreCalculator struct {
value uint32
weight uint32
scoreTotal uint32
scoreCompletion uint32
scoreCnt uint32
dataTotal uint32
dataCompletion uint32
hasResults bool
hasErrors bool
featureFlagFailErrors bool
}
func (c *weightedScoreCalculator) String() string {
return "Weighted Average"
}
func (c *weightedScoreCalculator) Init() {
c.value = 0
c.weight = 0
c.scoreTotal = 0
c.scoreCompletion = 0
c.scoreCnt = 0
c.dataTotal = 0
c.dataCompletion = 0
c.hasResults = false
c.hasErrors = false
}
func (c *weightedScoreCalculator) Add(score *Score, impact *explorer.Impact) {
switch score.Type {
case ScoreType_Skip, ScoreType_Disabled, ScoreType_OutOfScope:
return
case ScoreType_Unscored:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
case ScoreType_Result:
if impact != nil && (impact.Action == explorer.Action_IGNORE || impact.Action == explorer.Action_DEACTIVATE) {
return
}
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
c.scoreCompletion += score.ScoreCompletion
c.scoreTotal++
if score.ScoreCompletion != 0 {
c.scoreCnt += score.Weight
c.value += score.Value * score.Weight
}
c.hasResults = true
case ScoreType_Error:
c.hasErrors = true
if c.featureFlagFailErrors {
// This case is the same as ScoreType_Result. Once the feature flag
// is removed, this case can be merged with the ScoreType_Result
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
c.scoreCompletion += score.ScoreCompletion
c.scoreTotal++
if score.ScoreCompletion != 0 {
c.scoreCnt += score.Weight
c.value += score.Value * score.Weight
}
c.hasResults = true
} else {
// This case is the same as ScoreType_Unscored. Once the feature flag
// is removed, this case can be removed
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.scoreCompletion += score.ScoreCompletion
c.scoreTotal++
}
default:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.scoreCompletion += score.ScoreCompletion
c.scoreTotal++
}
}
func (c *weightedScoreCalculator) Calculate() *Score {
res := &Score{
Type: ScoreType_Unscored,
DataTotal: c.dataTotal,
// unless we know otherwise, we are setting the data completion to 100
// until we determine how many datapoints we are looking for
DataCompletion: 100,
// if the item is indeed unscored, then the score completion is 100
// since we are done with the scoring piece
ScoreCompletion: 100,
}
if c.dataTotal != 0 {
res.DataCompletion = c.dataCompletion / c.dataTotal
}
if c.hasResults {
res.Type = ScoreType_Result
res.ScoreCompletion = c.scoreCompletion / c.scoreTotal
res.Weight = c.weight
res.Value = c.value / c.scoreCnt
} else if c.hasErrors {
res.Type = ScoreType_Error
}
return res
}
type worstScoreCalculator struct {
value uint32
weight uint32
scoreTotal uint32
scoreCompletion uint32
dataTotal uint32
dataCompletion uint32
hasResults bool
hasErrors bool
featureFlagFailErrors bool
}
func (c *worstScoreCalculator) String() string {
return "Highest Impact"
}
func (c *worstScoreCalculator) Init() {
c.value = 100
c.weight = 0
c.scoreTotal = 0
c.scoreCompletion = 0
c.dataTotal = 0
c.dataCompletion = 0
c.hasResults = false
c.hasErrors = false
}
func (c *worstScoreCalculator) Add(score *Score, impact *explorer.Impact) {
switch score.Type {
case ScoreType_Skip, ScoreType_Disabled, ScoreType_OutOfScope:
return
case ScoreType_Unscored:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
case ScoreType_Result:
if impact != nil && (impact.Action == explorer.Action_IGNORE || impact.Action == explorer.Action_DEACTIVATE) {
return
}
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
c.scoreTotal++
c.scoreCompletion += score.ScoreCompletion
if score.ScoreCompletion != 0 && score.Weight != 0 && score.Value < c.value {
c.value = score.Value
}
c.hasResults = true
case ScoreType_Error:
c.hasErrors = true
if c.featureFlagFailErrors {
// This case is the same as ScoreType_Result. Once the feature flag
// is removed, this case can be merged with the ScoreType_Result
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
c.scoreTotal++
c.scoreCompletion += score.ScoreCompletion
if score.ScoreCompletion != 0 && score.Weight != 0 && score.Value < c.value {
c.value = score.Value
}
c.hasResults = true
} else {
// This case is the same as ScoreType_Unscored. Once the feature flag
// is removed, this case can be removed
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.scoreCompletion += score.ScoreCompletion
c.scoreTotal++
}
default:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.scoreCompletion += score.ScoreCompletion
c.scoreTotal++
}
}
func (c *worstScoreCalculator) Calculate() *Score {
res := &Score{
Type: ScoreType_Unscored,
DataTotal: c.dataTotal,
// unless we know otherwise, we are setting the data completion to 100
// until we determine how many datapoints we are looking for
DataCompletion: 100,
// if the item is indeed unscored, then the score completion is 100
// since we are done with the scoring piece
ScoreCompletion: 100,
}
if c.dataTotal != 0 {
res.DataCompletion = c.dataCompletion / c.dataTotal
}
if c.scoreTotal == 0 {
return res
}
if c.hasResults {
res.Type = ScoreType_Result
res.ScoreCompletion = c.scoreCompletion / c.scoreTotal
res.Weight = c.weight
res.Value = c.value
} else if c.hasErrors {
res.Type = ScoreType_Error
}
return res
}
type bandedScoreCalculator struct {
crit uint32
high uint32
mid uint32
low uint32
critMax uint32
highMax uint32
midMax uint32
lowMax uint32
minscore uint32
value uint32
weight uint32
scoreTotal uint32
scoreCompletion uint32
dataTotal uint32
dataCompletion uint32
hasResults bool
hasErrors bool
featureFlagFailErrors bool
}
func (c *bandedScoreCalculator) String() string {
return "Banded"
}
func (c *bandedScoreCalculator) Init() {
c.minscore = 100
c.value = 100
c.weight = 0
c.scoreTotal = 0
c.scoreCompletion = 0
c.dataTotal = 0
c.dataCompletion = 0
c.hasResults = false
c.hasErrors = false
}
func (c *bandedScoreCalculator) Add(score *Score, impact *explorer.Impact) {
switch score.Type {
case ScoreType_Skip, ScoreType_OutOfScope, ScoreType_Disabled:
return
case ScoreType_Unscored:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
case ScoreType_Result:
if impact != nil && (impact.Action == explorer.Action_IGNORE || impact.Action == explorer.Action_DEACTIVATE) {
return
}
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
c.scoreTotal++
c.scoreCompletion += score.ScoreCompletion
if score.ScoreCompletion != 0 && score.Weight != 0 {
category := uint32(0)
if impact != nil && impact.Value != nil {
category = 100 - uint32(impact.Value.Value)
}
if category <= 10 {
c.critMax += score.Weight
if score.Value < 100 {
c.crit += score.Weight
}
} else if category <= 30 {
c.highMax += score.Weight
if score.Value < 100 {
c.high += score.Weight
}
} else if category <= 60 {
c.midMax += score.Weight
if score.Value < 100 {
c.mid += score.Weight
}
} else {
c.lowMax += score.Weight
if score.Value < 100 {
c.low += score.Weight
}
}
}
c.hasResults = true
case ScoreType_Error:
c.hasErrors = true
if c.featureFlagFailErrors {
// This case is the same as ScoreType_Result. Once the feature flag
// is removed, this case can be merged with the ScoreType_Result
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
c.scoreTotal++
c.scoreCompletion += score.ScoreCompletion
if score.ScoreCompletion != 0 && score.Weight != 0 && score.Value < c.value {
c.value = score.Value
}
c.hasResults = true
} else {
// This case is the same as ScoreType_Unscored. Once the feature flag
// is removed, this case can be removed
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.scoreCompletion += score.ScoreCompletion
c.scoreTotal++
}
default:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.scoreCompletion += score.ScoreCompletion
c.scoreTotal++
}
}
func (c *bandedScoreCalculator) Calculate() *Score {
res := &Score{
Type: ScoreType_Unscored,
DataTotal: c.dataTotal,
// unless we know otherwise, we are setting the data completion to 100
// until we determine how many datapoints we are looking for
DataCompletion: 100,
// if the item is indeed unscored, then the score completion is 100
// since we are done with the scoring piece
ScoreCompletion: 100,
}
if c.dataTotal != 0 {
res.DataCompletion = c.dataCompletion / c.dataTotal
}
if c.scoreTotal == 0 {
return res
}
if c.hasResults {
res.Type = ScoreType_Result
pcrLow := float64(1)
if c.lowMax != 0 {
pcrLow = float64(c.lowMax-c.low) / float64(c.lowMax)
}
fMid := float64(1)
if c.midMax != 0 {
fMid = float64(c.midMax-c.mid) / float64(c.midMax)
}
pcrMid := (3 + pcrLow) / 4 * fMid
fHigh := float64(1)
if c.highMax != 0 {
fHigh = float64(c.highMax-c.high) / float64(c.highMax)
}
pcrHigh := (1 + pcrMid) / 2 * fHigh
fCrit := float64(1)
if c.critMax != 0 {
fCrit = float64(c.critMax-c.crit) / float64(c.critMax)
}
pcrCrit := (1 + 4*pcrHigh) / 5 * fCrit
if c.crit != 0 {
res.Value = uint32(math.Floor(float64(50) * pcrCrit))
} else if c.high != 0 {
res.Value = uint32(math.Floor(float64(50)*pcrHigh)) + 10
} else if c.mid != 0 {
res.Value = uint32(math.Floor(float64(50)*pcrMid)) + 30
} else if c.low != 0 {
res.Value = uint32(math.Floor(float64(40)*pcrLow)) + 60
} else {
res.Value = 100
}
res.ScoreCompletion = c.scoreCompletion / c.scoreTotal
res.Weight = c.weight
} else if c.hasErrors {
res.Type = ScoreType_Error
}
return res
}
type decayedScoreCalculator struct {
x float64
xmax float64
value uint32
weight uint32
scoreTotal uint32
scoreCompletion uint32
dataTotal uint32
dataCompletion uint32
hasResults bool
hasErrors bool
featureFlagFailErrors bool
}
func (c *decayedScoreCalculator) String() string {
return "Decayed"
}
var gravity float64 = 10
func (c *decayedScoreCalculator) Init() {
c.x = 0
c.xmax = 0
c.value = 100
c.weight = 0
c.scoreTotal = 0
c.scoreCompletion = 0
c.dataTotal = 0
c.dataCompletion = 0
c.hasResults = false
c.hasErrors = false
}
func (c *decayedScoreCalculator) Add(score *Score, impact *explorer.Impact) {
switch score.Type {
case ScoreType_Skip, ScoreType_OutOfScope, ScoreType_Disabled:
return
case ScoreType_Unscored:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
case ScoreType_Result:
if impact != nil && (impact.Action == explorer.Action_IGNORE || impact.Action == explorer.Action_DEACTIVATE) {
return
}
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
c.scoreTotal++
c.scoreCompletion += score.ScoreCompletion
if score.ScoreCompletion != 0 && score.Weight != 0 {
// TODO: we can add an optional accelerator here later on.
// The function changes to v := math.Pow( ... , accelerator)
// with accelerator > 0, default = 1
v := float64(100-score.Value) / 100
c.x += v * float64(score.Weight)
if impact.Value == nil {
c.xmax += 1 * float64(score.Weight)
} else {
c.xmax += float64(impact.Value.Value) / 100 * float64(score.Weight)
}
}
c.hasResults = true
case ScoreType_Error:
c.hasErrors = true
if c.featureFlagFailErrors {
// This case is the same as ScoreType_Result. Once the feature flag
// is removed, this case can be merged with the ScoreType_Result
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
c.scoreTotal++
c.scoreCompletion += score.ScoreCompletion
if score.ScoreCompletion != 0 && score.Weight != 0 && score.Value < c.value {
c.value = score.Value
}
c.hasResults = true
} else {
// This case is the same as ScoreType_Unscored. Once the feature flag
// is removed, this case can be removed
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.scoreCompletion += score.ScoreCompletion
c.scoreTotal++
}
default:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.scoreCompletion += score.ScoreCompletion
c.scoreTotal++
}
}
func (c *decayedScoreCalculator) Calculate() *Score {
res := &Score{
Type: ScoreType_Unscored,
DataTotal: c.dataTotal,
// unless we know otherwise, we are setting the data completion to 100
// until we determine how many datapoints we are looking for
DataCompletion: 100,
// if the item is indeed unscored, then the score completion is 100
// since we are done with the scoring piece
ScoreCompletion: 100,
}
if c.dataTotal != 0 {
res.DataCompletion = c.dataCompletion / c.dataTotal
}
if c.scoreTotal == 0 {
return res
}
if c.hasResults {
res.Type = ScoreType_Result
relGravity := float64(c.weight) / gravity
xscaled := c.x / c.xmax * (relGravity)
floor := math.Exp(-relGravity)
res.Value = uint32(math.Floor(100 * (math.Exp(-xscaled) - floor) / (1 - floor)))
res.ScoreCompletion = c.scoreCompletion / c.scoreTotal
res.Weight = c.weight
} else if c.hasErrors {
res.Type = ScoreType_Error
}
return res
}
type scoreCalculatorOptions struct {
featureFlagFailErrors bool
}
// ScoreCalculatorOption is a function that sets some option on a score calculator
type ScoreCalculatorOption func(*scoreCalculatorOptions)
// WithScoreCalculatorFeatureFlagFailErrors sets the feature flag fail errors option
func WithScoreCalculatorFeatureFlagFailErrors() ScoreCalculatorOption {
return func(o *scoreCalculatorOptions) {
o.featureFlagFailErrors = true
}
}
// NewScoreCalculator returns a score calculator based on a scoring system
func NewScoreCalculator(scoringSystem explorer.ScoringSystem, opts ...ScoreCalculatorOption) (ScoreCalculator, error) {
var res ScoreCalculator
options := scoreCalculatorOptions{}
for _, opt := range opts {
opt(&options)
}
switch scoringSystem {
case explorer.ScoringSystem_AVERAGE, explorer.ScoringSystem_SCORING_UNSPECIFIED, explorer.ScoringSystem_DATA_ONLY:
res = &averageScoreCalculator{
featureFlagFailErrors: options.featureFlagFailErrors,
}
case explorer.ScoringSystem_WEIGHTED:
res = &weightedScoreCalculator{
featureFlagFailErrors: options.featureFlagFailErrors,
}
case explorer.ScoringSystem_WORST:
res = &worstScoreCalculator{
featureFlagFailErrors: options.featureFlagFailErrors,
}
case explorer.ScoringSystem_BANDED:
res = &bandedScoreCalculator{
featureFlagFailErrors: options.featureFlagFailErrors,
}
case explorer.ScoringSystem_DECAYED:
res = &decayedScoreCalculator{
featureFlagFailErrors: options.featureFlagFailErrors,
}
default:
return nil, errors.New("don't know how to create scoring calculator for system " + strconv.Itoa(int(scoringSystem)))
}
res.Init()
return res, nil
}