scene.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package service
  2. import (
  3. "fmt"
  4. "gitlab.alibaba-inc.com/pai_biz_arch/pairec/recconf"
  5. )
  6. var scenes = make(map[string]*Scene)
  7. func GetSence(sceneId string) (*Scene, error) {
  8. scene, ok := scenes[sceneId]
  9. if !ok {
  10. return nil, fmt.Errorf("Scene:not found, SenceId:%s", sceneId)
  11. }
  12. return scene, nil
  13. }
  14. func Load(conf *recconf.RecommendConfig) {
  15. for sceneId, categoryConfs := range conf.SceneConfs {
  16. scene := NewScene(sceneId)
  17. // scene.Init(sceneConf)
  18. for categoryName, categoryConf := range categoryConfs {
  19. // if _, ok := conf.CategoryConfs[categoryName]; !ok {
  20. // panic(fmt.Sprintf("category name not found, name:%s", categoryName))
  21. // }
  22. category := NewCategory(categoryName)
  23. category.Init(categoryConf)
  24. scene.AddCategory(categoryName, category)
  25. }
  26. scenes[sceneId] = scene
  27. }
  28. }
  29. type Scene struct {
  30. SceneId string
  31. Categories map[string]*Category
  32. }
  33. func NewScene(senceId string) *Scene {
  34. scene := Scene{SceneId: senceId}
  35. scene.Categories = make(map[string]*Category)
  36. return &scene
  37. }
  38. func (s *Scene) Init(config recconf.SceneConfig) {
  39. }
  40. func (s *Scene) AddCategory(name string, category *Category) {
  41. s.Categories[name] = category
  42. }
  43. func (s *Scene) GetCategory(name string) (*Category, error) {
  44. category, ok := s.Categories[name]
  45. if !ok {
  46. return s.Categories["default_category"], nil
  47. }
  48. return category, nil
  49. }