Ejemplo n.º 1
0
func measurementObjectiveEvaluate(context *ctp.ApiContext, item *Measurement) *ctp.HttpError {
	item.Objective.Status = ctp.Terror

	ctp.Log(context, ctp.DEBUG, "Evaluating objective: %s\n", item.Objective.Condition)

	machine, err := jsmm.Compile(item.Objective.Condition)
	if err != nil {
		return ctp.NewBadRequestErrorf("Error in objective compilation - %s", err.Error())
	}

	if item.Result == nil {
		item.Objective.Status = ctp.Ttrue
		return nil
	}

	if context.DebugVM {
		machine.DebugMode(true)
	}

	if err := importMeasurementResultInJSMM(machine, item.Result); err != nil {
		return ctp.NewBadRequestErrorf("Error in objective evaluation while importing result - %s", err.Error)
	}

	v, exception := machine.Execute()
	if exception != nil {
		return ctp.NewBadRequestErrorf("Error in objective evaluation - %s", exception.Error())
	}
	if v != nil {
		item.Objective.Status = ctp.ToBoolErr(v.ToBoolean())
	}
	return nil
}
Ejemplo n.º 2
0
func triggerCheckCondition(context *ctp.ApiContext, trigger *Trigger, measurement *Measurement) (bool, error) {

	if measurement == nil {
		measurementParams, ok := ctp.ParseLink(context.CtpBase, "@/measurements/$", trigger.Measurement)
		if !ok {
			return false, fmt.Errorf("Measurement URL is incorrect")
		}

		measurement = new(Measurement)
		if !ctp.LoadResource(context, "measurements", ctp.Base64Id(measurementParams[0]), measurement) {
			return false, fmt.Errorf("Measurement %s does not exist", ctp.ExpandLink(context.CtpBase, trigger.Measurement))
		}
	}

	machine, err := jsmm.Compile(trigger.Condition)
	if err != nil {
		return false, ctp.NewBadRequestErrorf("Error in condition specification - %s", err.Error())
	}

	if context.DebugVM {
		machine.DebugMode(true)
	}

	if measurement.State != "activated" {
		return false, nil
	}
	if measurement.Result == nil {
		ctp.Log(context, ctp.ERROR, "In /measurements/%s, the state is activated but the value is null.", measurement.Id)
		return false, nil
	}

	if err := importMeasurementResultInJSMM(machine, measurement.Result); err != nil {
		return false, err
	}

	v, exception := machine.Execute()
	if exception != nil {
		return false, fmt.Errorf("Failed to evaluate condition: %s", exception.Error())
	}
	return v.ToBoolean(), nil
}