You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.6 KiB

  1. package trust
  2. import "time"
  3. // TrustMetricConfig - Configures the weight functions and time intervals for the metric
  4. type TrustMetricConfig struct {
  5. // Determines the percentage given to current behavior
  6. ProportionalWeight float64
  7. // Determines the percentage given to prior behavior
  8. IntegralWeight float64
  9. // The window of time that the trust metric will track events across.
  10. // This can be set to cover many days without issue
  11. TrackingWindow time.Duration
  12. // Each interval should be short for adapability.
  13. // Less than 30 seconds is too sensitive,
  14. // and greater than 5 minutes will make the metric numb
  15. IntervalLength time.Duration
  16. }
  17. // DefaultConfig returns a config with values that have been tested and produce desirable results
  18. func DefaultConfig() TrustMetricConfig {
  19. return TrustMetricConfig{
  20. ProportionalWeight: 0.4,
  21. IntegralWeight: 0.6,
  22. TrackingWindow: (time.Minute * 60 * 24) * 14, // 14 days.
  23. IntervalLength: 1 * time.Minute,
  24. }
  25. }
  26. // Ensures that all configuration elements have valid values
  27. func customConfig(tmc TrustMetricConfig) TrustMetricConfig {
  28. config := DefaultConfig()
  29. // Check the config for set values, and setup appropriately
  30. if tmc.ProportionalWeight > 0 {
  31. config.ProportionalWeight = tmc.ProportionalWeight
  32. }
  33. if tmc.IntegralWeight > 0 {
  34. config.IntegralWeight = tmc.IntegralWeight
  35. }
  36. if tmc.IntervalLength > time.Duration(0) {
  37. config.IntervalLength = tmc.IntervalLength
  38. }
  39. if tmc.TrackingWindow > time.Duration(0) &&
  40. tmc.TrackingWindow >= config.IntervalLength {
  41. config.TrackingWindow = tmc.TrackingWindow
  42. }
  43. return config
  44. }