Skip to content

Commit

Permalink
scorecard
Browse files Browse the repository at this point in the history
  • Loading branch information
skyhackvip committed Sep 18, 2022
1 parent 0fe49dd commit 6b2a8fb
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 220 deletions.
3 changes: 3 additions & 0 deletions api/engine_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/skyhackvip/risk_engine/core"
"github.com/skyhackvip/risk_engine/internal/dto"
"github.com/skyhackvip/risk_engine/service"
"log"
"net/http"
)

Expand All @@ -29,12 +30,14 @@ func (handler *EngineHandler) Run(c *gin.Context) {
})
return
}
log.Printf("======[trace]request start req_id (%s)======\n", request.ReqId)
svr := service.NewEngineService(handler.kernel)
result, err := svr.Run(c, &request)
if err != nil {
code = 501
errs = err.Error()
}
log.Printf("======[trace]request end req_id (%s)======\n", request.ReqId)
c.JSON(http.StatusOK, gin.H{
"code": code,
"result": result,
Expand Down
45 changes: 14 additions & 31 deletions configs/const.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package configs

//logic
var LogicMap = map[string]string{
"OR": "||",
"AND": "&&",
}

//operator
//all operators
const (
GT = "GT"
LT = "LT"
Expand Down Expand Up @@ -87,30 +81,6 @@ var DefaultSupportOperator = map[string]struct{}{
NEQ: struct{}{},
}

//ruleset decision
var DecisionMap = map[string]int{
"reject": 100, //first priority
"pass": 0,
"record": 1,
}

const (
ScoreReplace = "((score))"
)

const (
Sum = "SUM"
Min = "MIN"
Max = "MAX"
Avg = "AVG"
)

//decision
const (
NilDecision = 0 //not hit rules strategy
BreakDecision = "reject" //if hit,break at once
)

//all support node
const (
START = "start"
Expand All @@ -129,6 +99,19 @@ const (
MATRIXY = "matrixY"
)

//all type
const (
INT = "int"
FLOAT = "float"
STRING = "string"
BOOL = "bool"
DATE = "date"
ARRAY = "array"
MAP = "map"
DEFAULT = "default"
)

//date type
const (
DATE_FORMAT = "2006-01-02"
DATE_FORMAT_DETAIL = "2006-01-02 15:04:05"
Expand Down
10 changes: 0 additions & 10 deletions configs/mock.go

This file was deleted.

2 changes: 1 addition & 1 deletion core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,5 @@ func (block Block) parse(depends map[string]IFeature) (interface{}, bool, error)
continue
}
}
return nil, false, errcode.ParseErrorTreeNotMatch
return nil, false, errcode.ParseErrorBlockNotMatch
}
32 changes: 16 additions & 16 deletions core/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ const (
)

var FeatureTypeMap = map[string]FeatureType{
"int": TypeInt,
"float": TypeFloat,
"string": TypeString,
"bool": TypeBool,
"date": TypeDate,
"array": TypeArray,
"map": TypeMap,
"default": TypeDefault,
configs.INT: TypeInt,
configs.FLOAT: TypeFloat,
configs.STRING: TypeString,
configs.BOOL: TypeBool,
configs.DATE: TypeDate,
configs.ARRAY: TypeArray,
configs.MAP: TypeMap,
configs.DEFAULT: TypeDefault,
}

var FeatureStrMap = map[FeatureType]string{
TypeInt: "int",
TypeFloat: "float",
TypeString: "string",
TypeBool: "bool",
TypeDate: "date",
TypeArray: "array",
TypeMap: "map",
TypeDefault: "default",
TypeInt: configs.INT,
TypeFloat: configs.FLOAT,
TypeString: configs.STRING,
TypeBool: configs.BOOL,
TypeDate: configs.DATE,
TypeArray: configs.ARRAY,
TypeMap: configs.MAP,
TypeDefault: configs.DEFAULT,
}

func GetFeatureType(name string) FeatureType {
Expand Down
2 changes: 1 addition & 1 deletion demo/flow_scorecard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ features:
name: num
tag: aa
label: 数字
kind: int
kind: float
- feature:
id: 2
name: sex
Expand Down
22 changes: 22 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,28 @@ curl -XPOST http://localhost:8889/engine/run -d '{"key":"flow_tree", "version":"
开始->测试决策树->结束
决策树,从 block_1 开始,feature_bool = false 命中,goto block_3。feature_num < 1 命中,输出结果为 f,结束并赋值。

### 评分卡决策流

- yaml 源文件: [demo/flow_scorecard](../demo/flow_scorecard.yaml)
- key: flow_scorecard
- version: 1.0


*CURL*
```shell
curl -XPOST http://localhost:8889/engine/run -d '{"key":"flow_scorecard", "version":"1.0", "req_id":"123456789", "uid":1,"features":{"num":55.5,"sex":"M", "age": 11}}'
```

*执行结果*
```json
```

*执行过程分析*
开始->测试决策树->结束
决策树,从 block_1 开始,feature_bool = false 命中,goto block_3。feature_num < 1 命中,输出结果为 f,结束并赋值。



### 冠军挑战者决策流

- yaml 源文件: [demo/flow_abtest](../demo/flow_abtest.yaml)
Expand Down
25 changes: 0 additions & 25 deletions internal/datasource/datasource.go

This file was deleted.

10 changes: 10 additions & 0 deletions internal/dto/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ()

/**
* dsl run request
* url: /engine/run
* example: {"key":"flow_abtest", "req_id":"123456", "uid":1,"features":{"feature_1":5,"feature_2":3,"feature_3":true}}
*/
type EngineRunRequest struct {
Expand All @@ -13,6 +14,11 @@ type EngineRunRequest struct {
Uid int64 `json:"uid"`
Features map[string]interface{} `json:"features"`
}

/**
* dsl run response
* url: /engine/run
*/
type EngineRunResponse struct {
Key string `json:"key"`
ReqId string `json:"req_id"`
Expand All @@ -26,6 +32,10 @@ type EngineRunResponse struct {
RunTime int64 `json:"run_time"`
}

/**
* dsl list response
* url: /engine/list
*/
type DslListResponse struct {
Code int `json:"code"`
Err string `json:"err"`
Expand Down
49 changes: 0 additions & 49 deletions internal/dto/feature.go

This file was deleted.

62 changes: 0 additions & 62 deletions internal/dto/result.go

This file was deleted.

14 changes: 8 additions & 6 deletions internal/errcode/parse_error.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package errcode

var (
ParseErrorNoBranchMatch = NewError(1000005, "flow error, no branch match")
ParseErrorRulesetOutputEmpty = NewError(1000011, "ruleset output is empty")
ParseErrorTreeNotMatch = NewError(1000021, "tree not match error")
ParseErrorTreeOutputEmpty = NewError(1000022, "tree output is empty")
ParseErrorMatrixNotMatch = NewError(1000031, "matrix not match error")
ParseErrorMatrixOutputEmpty = NewError(1000032, "matrix output is empty")
ParseErrorNoBranchMatch = NewError(1000005, "flow error, no branch match")
ParseErrorRulesetOutputEmpty = NewError(1000011, "ruleset output is empty")
ParseErrorTreeNotMatch = NewError(1000021, "tree not match error")
ParseErrorTreeOutputEmpty = NewError(1000022, "tree output is empty")
ParseErrorMatrixNotMatch = NewError(1000031, "matrix not match error")
ParseErrorMatrixOutputEmpty = NewError(1000032, "matrix output is empty")
ParseErrorBlockNotMatch = NewError(1000041, "block not match error")

ParseErrorNotSupportOperator = NewError(1000101, "not support operator")
ParseErrorTargetMustBeArray = NewError(1000102, "target must be array, check yaml first")
ParseErrorTargetNotSupport = NewError(1000103, "target not support error")
Expand Down
Loading

0 comments on commit 6b2a8fb

Please sign in to comment.