user.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package module
  2. import (
  3. "errors"
  4. "strconv"
  5. "sync"
  6. "gitlab.alibaba-inc.com/pai_biz_arch/pairec/context"
  7. )
  8. type User struct {
  9. Id UID `json:"uid"`
  10. Vector string
  11. Properties map[string]interface{} `json:"properties"`
  12. mutex sync.RWMutex
  13. }
  14. func NewUser(id string) *User {
  15. user := User{
  16. Id: UID(id),
  17. }
  18. user.Properties = make(map[string]interface{})
  19. return &user
  20. }
  21. func NewUserWithContext(id UID, context *context.RecommendContext) *User {
  22. user := NewUser(string(id))
  23. user.AddProperty("uid", string(id))
  24. features := context.GetParameter("features")
  25. if features != nil {
  26. if _, ok := features.(map[string]interface{}); ok {
  27. for k, v := range features.(map[string]interface{}) {
  28. user.AddProperty(k, v)
  29. }
  30. }
  31. }
  32. return user
  33. }
  34. func (u *User) AddProperty(key string, value interface{}) {
  35. u.mutex.Lock()
  36. u.Properties[key] = value
  37. u.mutex.Unlock()
  38. }
  39. func (u *User) FloatProperty(key string) (float64, error) {
  40. //u.mutex.RLock()
  41. //defer u.mutex.RUnlock()
  42. val, ok := u.Properties[key]
  43. if !ok {
  44. return float64(0), errors.New("property key not exist")
  45. }
  46. switch value := val.(type) {
  47. case float64:
  48. return value, nil
  49. case int:
  50. return float64(value), nil
  51. case string:
  52. f, err := strconv.ParseFloat(value, 64)
  53. return f, err
  54. default:
  55. return float64(0), errors.New("unspport type")
  56. }
  57. }
  58. func (u *User) MakeUserFeatures() (features map[string]interface{}) {
  59. features = make(map[string]interface{})
  60. for k, v := range u.Properties {
  61. if k == "type" {
  62. continue
  63. }
  64. if s, ok := v.(float64); ok {
  65. features[k] = s
  66. continue
  67. }
  68. if str, ok := v.(string); ok {
  69. if s, err := strconv.ParseFloat(str, 64); err == nil {
  70. features[k] = s
  71. continue
  72. }
  73. }
  74. features[k] = v
  75. }
  76. return
  77. }
  78. // MakeUserFeatures2 for easyrec processor
  79. func (u *User) MakeUserFeatures2() (features map[string]interface{}) {
  80. features = make(map[string]interface{}, len(u.Properties))
  81. for k, v := range u.Properties {
  82. features[k] = v
  83. }
  84. return
  85. }
  86. func (u *User) StringProperty(key string) string {
  87. u.mutex.RLock()
  88. defer u.mutex.RUnlock()
  89. val, ok := u.Properties[key]
  90. if !ok {
  91. return ""
  92. }
  93. switch value := val.(type) {
  94. case string:
  95. return value
  96. case int:
  97. return strconv.Itoa(value)
  98. case float64:
  99. return strconv.Itoa(int(value))
  100. }
  101. return ""
  102. }
  103. func (u *User) DeleteProperty(key string) {
  104. u.mutex.Lock()
  105. delete(u.Properties, key)
  106. u.mutex.Unlock()
  107. }
  108. func (u *User) DeleteProperties(features []string) {
  109. u.mutex.Lock()
  110. for _, key := range features {
  111. delete(u.Properties, key)
  112. }
  113. u.mutex.Unlock()
  114. }
  115. func (u *User) IntProperty(key string) (int, error) {
  116. //u.mutex.RLock()
  117. //defer u.mutex.RUnlock()
  118. val, ok := u.Properties[key]
  119. if !ok {
  120. return int(0), errors.New("property key not exist")
  121. }
  122. switch value := val.(type) {
  123. case float64:
  124. return int(value), nil
  125. case int:
  126. return value, nil
  127. case uint:
  128. return int(value), nil
  129. case int32:
  130. return int(value), nil
  131. case int64:
  132. return int(value), nil
  133. case string:
  134. return strconv.Atoi(value)
  135. default:
  136. return int(0), errors.New("unspport type")
  137. }
  138. }
  139. func (u *User) GetProperty(key string) interface{} {
  140. //u.mutex.RLock()
  141. //defer u.mutex.RUnlock()
  142. val, ok := u.Properties[key]
  143. if !ok {
  144. return nil
  145. }
  146. return val
  147. }