item.go 4.1 KB

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