private RuleApiModel GetSampleRuleWithCalculation(string calculation, string timePeriod)
        {
            var condition = new ConditionApiModel()
            {
                Field    = "pressure",
                Operator = "GreaterThan",
                Value    = "150"
            };

            var conditions = new List <ConditionApiModel> {
                condition
            };

            return(new RuleApiModel()
            {
                Name = calculation + " Test Rule",
                Description = "Test Description",
                GroupId = DEFAULT_CHILLERS_GROUP_ID,
                Severity = "Info",
                Enabled = true,
                Calculation = calculation,
                TimePeriod = timePeriod,
                Conditions = conditions
            });
        }
        private RuleApiModel GetSampleRuleWithCalculation(string calculation, string timePeriod, bool includeActions = false)
        {
            var condition = new ConditionApiModel()
            {
                Field    = "pressure",
                Operator = "GreaterThan",
                Value    = "150"
            };

            var conditions = new List <ConditionApiModel> {
                condition
            };

            RuleApiModel result = new RuleApiModel()
            {
                Name        = calculation + " Test Rule",
                Description = "Test Description",
                GroupId     = DEFAULT_CHILLERS_GROUP_ID,
                Severity    = "Info",
                Enabled     = true,
                Calculation = calculation,
                TimePeriod  = timePeriod,
                Conditions  = conditions
            };

            if (includeActions)
            {
                var parameters = new Dictionary <string, object>
                {
                    { "Notes", "Fake Note" },
                    { "Subject", "Fake Subject" }
                };
                var emails = new JArray {
                    "*****@*****.**"
                };
                parameters.Add("Recipients", emails);
                ActionApiModel action = new ActionApiModel
                {
                    Type       = "Email",
                    Parameters = parameters
                };
                result.Actions = new List <ActionApiModel> {
                    action
                };
            }

            return(result);
        }
        private ConditionApiModel GenerateCondition(string field, string value, bool match)
        {
            var condition = new ConditionApiModel
            {
                Field    = field,
                Operator = rand.NextString(RulesEvaluation.OperatorLogicLookup.Keys)
            };

            switch (condition.Operator)
            {
            case ">":
            case "greaterthan":
            case ">=":
            case "greaterthanorequal":
                condition.Value = match ? $"{value.First()}" : $"{value}a";
                break;

            case "<":
            case "lessthan":
            case "<=":
            case "lessthanorequal":
                condition.Value = match ? $"{value}a" : $"{value.First()}";
                break;

            case "=":
            case "==":
            case "equal":
            case "equals":
                condition.Value = match ? value : $"{value}a";
                break;

            case "!=":
            case "<>":
            case "notequal":
            case "notequals":
                condition.Value = match ? $"{value}a" : value;
                break;
            }

            return(condition);
        }
Пример #4
0
        private RulesEvaluationResult EvaluateCondition(
            ConditionApiModel condition,
            RawMessage message)
        {
            var r = new RulesEvaluationResult();

            var    field = condition.Field;
            object value;

            if (!message.PayloadData.TryGetValue(field, out value))
            {
                logger.Debug($"Message payload doesn't contain field {field}", () => { });
                return(r);
            }

            OperatorLogic logic;

            if (!OperatorLogicLookup.TryGetValue(condition.Operator.ToLowerInvariant(), out logic))
            {
                logger.Error("Unknown operator", () => new { condition });
                return(r);
            }

            switch (Type.GetTypeCode(value.GetType()))
            {
            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.UInt32:
            case TypeCode.Int64:
            case TypeCode.UInt64:
            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
            {
                var actualValue = Convert.ToDouble(value);
                var threshold   = Convert.ToDouble(condition.Value);

                logger.Debug($"Field {field}, Value {actualValue}, Operator {condition.Operator}, Threshold {threshold}", () => { });
                if (logic.DoubleTestFunc(actualValue, threshold))
                {
                    r.Match   = true;
                    r.Message = string.Format(logic.Message, field, actualValue, threshold);
                }
            }
            break;

            case TypeCode.String:
            {
                var actualValue = value.ToString();
                var threshold   = condition.Value;

                logger.Debug($"Field {field}, Value '{actualValue}', Operator {condition.Operator}, Threshold '{threshold}'", () => { });
                if (logic.StringTestFunc(actualValue, threshold))
                {
                    r.Match   = true;
                    r.Message = string.Format(logic.Message, field, actualValue, threshold);
                }
            }
            break;

            default:
                logger.Error($"Unknown type for `{field}` value sent by device {message.DeviceId}", () => { });
                break;
            }

            return(r);
        }
        private void CreateRuleWithCalculation(string calculation, string timePeriod, ref string id, bool includeActions)
        {
            var condition = new ConditionApiModel()
            {
                Field    = "temperature",
                Operator = "GreaterThan",
                Value    = "1"
            };

            var conditions = new List <ConditionApiModel> {
                condition
            };

            var newRule = new RuleApiModel()
            {
                Id          = id,
                Name        = calculation + " Faulty Test Rule " + DateTime.UtcNow.ToString("yyyyMMddHHmmss") + "-" + Guid.NewGuid(),
                Description = "Test Description",
                GroupId     = DEFAULT_CHILLERS_GROUP_ID,
                Severity    = "Info",
                Enabled     = true,
                Calculation = calculation,
                TimePeriod  = timePeriod,
                Conditions  = conditions
            };

            if (includeActions)
            {
                var parameters = new Dictionary <string, object>
                {
                    { "Notes", "Fake Note" },
                    { "Subject", "Fake Subject" }
                };
                var emails = new JArray {
                    "*****@*****.**"
                };
                parameters.Add("Recipients", emails);
                ActionApiModel action = new ActionApiModel
                {
                    Type       = "Email",
                    Parameters = parameters
                };
                newRule.Actions = new List <ActionApiModel> {
                    action
                };
            }

            var request = new HttpRequest(Constants.TELEMETRY_ADDRESS + RULES_ENDPOINT_SUFFIX);

            request.AddHeader("Content-Type", "application/json");
            request.SetContent(JsonConvert.SerializeObject(newRule));

            var response     = this.httpClient.PostAsync(request).Result;
            var ruleResponse = JsonConvert.DeserializeObject <RuleApiModel>(response.Content);

            // Update the saved rule ID
            id = ruleResponse.Id;

            // Dispose after tests run
            this.disposeRulesList.Add(ruleResponse.Id);
        }