item.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package module
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "sync"
  7. )
  8. type ItemId string
  9. type Item struct {
  10. Id ItemId `json:"id"`
  11. Name string `json:"name,omitempty"`
  12. Score float64
  13. RetrieveId string
  14. ItemType string
  15. // ItemStatus int
  16. mutex sync.Mutex
  17. // TimeStamp time.Time
  18. Properties map[string]interface{} `json:"properties"`
  19. algoScores map[string]float64
  20. }
  21. func NewItem(id string) *Item {
  22. item := Item{
  23. Id: ItemId(id),
  24. Score: 0,
  25. Properties: make(map[string]interface{}, 32),
  26. }
  27. //item.algoScores = make(map[string]float64)
  28. return &item
  29. }
  30. func NewItemWithProperty(id string, properties map[string]interface{}) *Item {
  31. item := NewItem(id)
  32. for k, v := range properties {
  33. item.Properties[k] = v
  34. }
  35. return item
  36. }
  37. func (t *Item) GetRecallName() string {
  38. return t.RetrieveId
  39. }
  40. func (t *Item) GetAlgoScores() map[string]float64 {
  41. t.mutex.Lock()
  42. defer t.mutex.Unlock()
  43. return t.algoScores
  44. }
  45. func (t *Item) AddProperty(key string, value interface{}) {
  46. t.mutex.Lock()
  47. defer t.mutex.Unlock()
  48. t.Properties[key] = value
  49. }
  50. func (t *Item) GetProperty(key string) interface{} {
  51. t.mutex.Lock()
  52. defer t.mutex.Unlock()
  53. val, ok := t.Properties[key]
  54. if !ok {
  55. return nil
  56. }
  57. return val
  58. }
  59. func (t *Item) StringProperty(key string) string {
  60. t.mutex.Lock()
  61. defer t.mutex.Unlock()
  62. val, ok := t.Properties[key]
  63. if !ok {
  64. return ""
  65. }
  66. switch value := val.(type) {
  67. case string:
  68. return value
  69. case int:
  70. return strconv.Itoa(value)
  71. case float64:
  72. return strconv.Itoa(int(value))
  73. case int32:
  74. return strconv.Itoa(int(value))
  75. case int64:
  76. return strconv.Itoa(int(value))
  77. }
  78. return ""
  79. }
  80. func (t *Item) FloatProperty(key string) (float64, error) {
  81. t.mutex.Lock()
  82. defer t.mutex.Unlock()
  83. val, ok := t.Properties[key]
  84. if !ok {
  85. return float64(0), errors.New("property key not exist")
  86. }
  87. switch value := val.(type) {
  88. case float64:
  89. return value, nil
  90. case int:
  91. return float64(value), nil
  92. case string:
  93. f, err := strconv.ParseFloat(value, 64)
  94. return f, err
  95. default:
  96. return float64(0), errors.New("unspport type")
  97. }
  98. }
  99. func (t *Item) IntProperty(key string) (int, error) {
  100. t.mutex.Lock()
  101. defer t.mutex.Unlock()
  102. val, ok := t.Properties[key]
  103. if !ok {
  104. return int(0), errors.New("property key not exist")
  105. }
  106. switch value := val.(type) {
  107. case float64:
  108. return int(value), nil
  109. case int:
  110. return value, nil
  111. case uint:
  112. return int(value), nil
  113. case int32:
  114. return int(value), nil
  115. case int64:
  116. return int(value), nil
  117. case string:
  118. return strconv.Atoi(value)
  119. default:
  120. return int(0), errors.New("unspport type")
  121. }
  122. }
  123. func (t *Item) AddAlgoScore(name string, score float64) {
  124. t.mutex.Lock()
  125. defer t.mutex.Unlock()
  126. if t.algoScores == nil {
  127. t.algoScores = make(map[string]float64)
  128. }
  129. t.algoScores[name] = score
  130. }
  131. func (t *Item) FloatExprData(name string) (float64, error) {
  132. t.mutex.Lock()
  133. defer t.mutex.Unlock()
  134. if name == "current_score" {
  135. return t.Score, nil
  136. }
  137. val, ok := t.algoScores[name]
  138. if ok {
  139. return val, nil
  140. }
  141. return float64(0), errors.New(fmt.Sprintf("not found,name:%s", name))
  142. }
  143. func (t *Item) GetFeatures() map[string]interface{} {
  144. t.mutex.Lock()
  145. defer t.mutex.Unlock()
  146. if t.RetrieveId != "" {
  147. if _, ok := t.Properties[t.RetrieveId]; !ok {
  148. t.Properties[t.RetrieveId] = t.Score
  149. t.Properties["recall_name"] = t.RetrieveId
  150. t.Properties["recall_score"] = t.Score
  151. }
  152. }
  153. features := make(map[string]interface{}, len(t.Properties))
  154. for k, v := range t.Properties {
  155. features[k] = v
  156. }
  157. return features
  158. }
  159. func (t *Item) DeleteProperty(key string) {
  160. t.mutex.Lock()
  161. defer t.mutex.Unlock()
  162. delete(t.Properties, key)
  163. }
  164. func (t *Item) DeleteProperties(features []string) {
  165. t.mutex.Lock()
  166. defer t.mutex.Unlock()
  167. for _, key := range features {
  168. delete(t.Properties, key)
  169. }
  170. }
  171. func (t *Item) AddProperties(properties map[string]interface{}) {
  172. t.mutex.Lock()
  173. defer t.mutex.Unlock()
  174. for key, val := range properties {
  175. t.Properties[key] = val
  176. }
  177. }
  178. func (t *Item) DeepClone() *Item {
  179. item := NewItemWithProperty(string(t.Id), t.Properties)
  180. item.Score = t.Score
  181. item.RetrieveId = t.RetrieveId
  182. item.ItemType = t.ItemType
  183. return item
  184. }