-
Notifications
You must be signed in to change notification settings - Fork 12
/
feature_service.go
84 lines (65 loc) · 2.09 KB
/
feature_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package pipeline
import (
"encoding/json"
"github.com/alibaba/pairec/v2/context"
"github.com/alibaba/pairec/v2/module"
"github.com/alibaba/pairec/v2/recconf"
"github.com/alibaba/pairec/v2/service/feature"
"github.com/alibaba/pairec/v2/utils"
)
type FeatureService struct {
pipelineName string
featureServices map[string]*feature.FeatureService
featureLoadConfs []recconf.FeatureLoadConfig
}
func NewFeatureService(config *recconf.PipelineConfig) *FeatureService {
service := FeatureService{
pipelineName: config.Name,
featureServices: make(map[string]*feature.FeatureService),
}
return &service
}
func (fs *FeatureService) LoadFeatures(user *module.User, items []*module.Item, context *context.RecommendContext) []*module.Item {
featureService := fs.GetFeatureServiceByContext(context)
if featureService == nil {
return items
}
featureService.LoadFeaturesForPipelineRank(user, items, context, fs.pipelineName)
return items
}
func (fs *FeatureService) GetFeatureServiceByContext(context *context.RecommendContext) *feature.FeatureService {
scene := context.GetParameter("scene").(string)
var featureLoadConfs []recconf.FeatureLoadConfig
found := false
if context.ExperimentResult != nil {
featureconf := context.ExperimentResult.GetExperimentParams().Get("pipelines."+fs.pipelineName+".FeatureLoadConfs", "")
if featureconf != "" {
d, _ := json.Marshal(featureconf)
if err := json.Unmarshal(d, &featureLoadConfs); err == nil {
found = true
}
}
}
if !found {
featureLoadConfs = fs.featureLoadConfs
found = true
}
if !found {
return nil
}
d, _ := json.Marshal(featureLoadConfs)
id := scene + "#" + utils.Md5(string(d))
if featureService, ok := fs.featureServices[id]; ok {
return featureService
}
featureService := feature.NewFeatureService()
var features []*feature.Feature
for _, conf := range featureLoadConfs {
f := feature.LoadWithConfig(conf)
features = append(features, f)
}
featureService.SetFeatureSceneAsync(scene, true)
featureService.SetFeatures(scene, features)
fs.featureServices[id] = featureService
return featureService
}