github.com/Rookout/GoSDK@v0.1.48/pkg/augs/aug.go (about) 1 package augs 2 3 import ( 4 "github.com/Rookout/GoSDK/pkg/augs/actions" 5 "github.com/Rookout/GoSDK/pkg/augs/conditions" 6 "github.com/Rookout/GoSDK/pkg/com_ws" 7 "github.com/Rookout/GoSDK/pkg/logger" 8 "github.com/Rookout/GoSDK/pkg/services/collection" 9 "github.com/Rookout/GoSDK/pkg/types" 10 "github.com/google/uuid" 11 ) 12 13 type Aug interface { 14 Execute(collectionService *collection.CollectionService) 15 GetAugID() types.AugID 16 SetCondition(condition conditions.Condition) 17 SetLimitsManager(manager LimitsManager) 18 } 19 20 type aug struct { 21 augID types.AugID 22 action actions.Action 23 output com_ws.Output 24 condition conditions.Condition 25 limitsManager LimitsManager 26 executed bool 27 } 28 29 func NewAug(augID types.AugID, action actions.Action, output com_ws.Output) Aug { 30 return &aug{ 31 augID: augID, 32 action: action, 33 output: output, 34 executed: false, 35 } 36 } 37 38 func (a *aug) GetAugID() types.AugID { 39 return a.augID 40 } 41 42 func (a *aug) SetCondition(condition conditions.Condition) { 43 a.condition = condition 44 } 45 46 func (a *aug) SetLimitsManager(manager LimitsManager) { 47 a.limitsManager = manager 48 } 49 50 func (a *aug) execute(collectionService *collection.CollectionService, reportID string) { 51 namespace, err := newAugNamespace(collectionService) 52 if err != nil { 53 logger.Logger().WithError(err).Warningf("Error while executing aug: %s\n", a.augID) 54 _ = a.output.SendWarning(a.augID, err) 55 return 56 } 57 58 if a.condition != nil { 59 res, err := a.condition.Evaluate(namespace.GetAugNamespace()) 60 if err != nil { 61 logger.Logger().WithError(err).Warningf("Error while executing condition on aug: %s", a.augID) 62 _ = a.output.SendWarning(a.augID, err) 63 } 64 65 if !res { 66 return 67 } 68 } 69 70 a.executed = true 71 err = a.action.Execute(a.augID, reportID, namespace.GetAugNamespace(), a.output) 72 if err != nil { 73 logger.Logger().WithError(err).Warningf("Error while executing aug: %s", a.augID) 74 _ = a.output.SendWarning(a.augID, err) 75 return 76 } 77 } 78 79 func (a *aug) Execute(collectionService *collection.CollectionService) { 80 executionId := uuid.NewString() 81 if a.limitsManager == nil { 82 a.execute(collectionService, executionId) 83 return 84 } 85 86 if len(a.limitsManager.GetAllLimiters()) == 0 { 87 logger.Logger().Warningf("Aug (%s) has no limiters", a.augID) 88 } 89 90 shouldSkipLimiters := (!a.executed) && (a.condition == nil) 91 if ok := a.limitsManager.BeforeRun(executionId, shouldSkipLimiters); ok { 92 a.execute(collectionService, executionId) 93 a.limitsManager.AfterRun(executionId) 94 } 95 }