/
user_global_hot_recall_tablestore_dao.go
114 lines (102 loc) · 3.1 KB
/
user_global_hot_recall_tablestore_dao.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package module
import (
"fmt"
"math/rand"
"sort"
"strings"
"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
"github.com/alibaba/pairec/context"
"github.com/alibaba/pairec/log"
"github.com/alibaba/pairec/persist/tablestoredb"
"github.com/alibaba/pairec/recconf"
"github.com/alibaba/pairec/utils"
)
type UserGlobalHotRecallTableStoreDao struct {
tablestore *tablestoredb.TableStore
itemType string
recallName string
table string
recallCount int
}
func NewUserGlobalHotRecallTableStoreDao(config recconf.RecallConfig) *UserGlobalHotRecallTableStoreDao {
tablestore, err := tablestoredb.GetTableStore(config.DaoConf.TableStoreName)
if err != nil {
log.Error(fmt.Sprintf("error=%v", err))
return nil
}
dao := &UserGlobalHotRecallTableStoreDao{
recallCount: config.RecallCount,
tablestore: tablestore,
table: config.DaoConf.TableStoreTableName,
itemType: config.ItemType,
recallName: config.Name,
}
return dao
}
func (d *UserGlobalHotRecallTableStoreDao) ListItemsByUser(user *User, context *context.RecommendContext) (ret []*Item) {
getRowRequest := new(tablestore.GetRowRequest)
criteria := new(tablestore.SingleRowQueryCriteria)
putPk := new(tablestore.PrimaryKey)
putPk.AddPrimaryKeyColumn("trigger_id", "-1")
criteria.PrimaryKey = putPk
criteria.ColumnsToGet = []string{"item_ids"}
getRowRequest.SingleRowQueryCriteria = criteria
getRowRequest.SingleRowQueryCriteria.TableName = d.table
getRowRequest.SingleRowQueryCriteria.MaxVersion = 1
getResp, err := d.tablestore.Client.GetRow(getRowRequest)
if err != nil {
log.Error(fmt.Sprintf("requestId=%s\tmodule=UserGlobalHotRecallTableStoreDao\terror=%v", context.RecommendId, err))
return
}
if len(getResp.Columns) == 0 {
log.Info(fmt.Sprintf("requestId=%s\tmodule=UserGlobalHotRecallTableStoreDao\tuid=%s\terr=item ids empty", context.RecommendId, user.Id))
return
}
var ids string
if str, ok := getResp.Columns[0].Value.(string); ok {
ids = str
}
itemIds := strings.Split(ids, ",")
if len(itemIds) == 0 {
log.Info(fmt.Sprintf("requestId=%s\tmodule=UserGlobalHotRecallTableStoreDao\tuid=%s\terr=item ids empty", context.RecommendId, user.Id))
return
}
for _, id := range itemIds {
strs := strings.Split(id, ":")
if len(strs) == 1 {
// itemid
item := NewItem(strs[0])
item.ItemType = d.itemType
item.RetrieveId = d.recallName
item.Score = rand.Float64()
ret = append(ret, item)
} else if len(strs) == 2 {
// itemid:RetrieveId
item := NewItem(strs[0])
item.ItemType = d.itemType
if strs[1] != "" {
item.RetrieveId = strs[1]
} else {
item.RetrieveId = d.recallName
}
item.Score = rand.Float64()
ret = append(ret, item)
} else if len(strs) == 3 {
item := NewItem(strs[0])
item.ItemType = d.itemType
if strs[1] != "" {
item.RetrieveId = strs[1]
} else {
item.RetrieveId = d.recallName
}
item.Score = utils.ToFloat(strs[2], float64(0))
item.AddAlgoScore("hot_score", item.Score)
ret = append(ret, item)
}
}
if len(ret) > d.recallCount {
sort.Sort(sort.Reverse(ItemScoreSlice(ret)))
return ret[:d.recallCount]
}
return
}