偏好推荐修正
This commit is contained in:
		
							
								
								
									
										109
									
								
								api/gorse.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								api/gorse.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,109 @@
 | 
			
		||||
package api
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/zhenghaoz/gorse/client"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var gorse *client.GorseClient
 | 
			
		||||
 | 
			
		||||
func gorseInit(host string, port int) {
 | 
			
		||||
	gorse = client.NewGorseClient(fmt.Sprintf("http://%s:%d", host, port), "")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 同步图片数据
 | 
			
		||||
func PutImages(page int) error {
 | 
			
		||||
	var ctx context.Context = context.Background()
 | 
			
		||||
 | 
			
		||||
	for i := 0; i < page; i++ {
 | 
			
		||||
		var items []client.Item
 | 
			
		||||
		var data []Image
 | 
			
		||||
 | 
			
		||||
		if err := db.Table("web_images").Select("id", "tags", "create_time").Limit(100).Offset(i * 100).Scan(&data).Error; err != nil {
 | 
			
		||||
			fmt.Println("获取图像记录失败", err)
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		for _, item := range data {
 | 
			
		||||
			fmt.Println(item.ID, item.Tags, item.CreateTime)
 | 
			
		||||
			items = append(items, client.Item{
 | 
			
		||||
				ItemId:    fmt.Sprintf("%d", item.ID),
 | 
			
		||||
				Timestamp: item.CreateTime.Format("2006-01-02 15:04:05"),
 | 
			
		||||
				Labels:    strings.Split(item.Tags, ", "),
 | 
			
		||||
				Comment:   fmt.Sprintf("%d", item.Content),
 | 
			
		||||
			})
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if _, err := gorse.InsertItems(ctx, items); err != nil {
 | 
			
		||||
			fmt.Println("写入图像记录失败", err)
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("写入图像记录结束..", page*100)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 同步点赞数据
 | 
			
		||||
func PutPraises(page int) error {
 | 
			
		||||
	var ctx context.Context = context.Background()
 | 
			
		||||
 | 
			
		||||
	for i := 0; i < page; i++ {
 | 
			
		||||
		var feedbacks []client.Feedback
 | 
			
		||||
		var data []struct {
 | 
			
		||||
			ID         int
 | 
			
		||||
			PraiseID   int
 | 
			
		||||
			UserID     int
 | 
			
		||||
			CreateTime time.Time
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if err := db.Table("web_praises").Select("id", "praise_id", "user_id", "create_time").Where("type = ?", 4).Limit(100).Offset(i * 100).Scan(&data).Error; err != nil {
 | 
			
		||||
			fmt.Println("获取点赞记录失败", err)
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		for _, item := range data {
 | 
			
		||||
			fmt.Println(item.ID, item.UserID, item.PraiseID, item.CreateTime)
 | 
			
		||||
			feedbacks = append(feedbacks, client.Feedback{
 | 
			
		||||
				FeedbackType: "like",
 | 
			
		||||
				UserId:       fmt.Sprintf("%d", item.UserID),
 | 
			
		||||
				ItemId:       fmt.Sprintf("%d", item.PraiseID),
 | 
			
		||||
				Timestamp:    item.CreateTime.Format("2006-01-02 15:04:05"),
 | 
			
		||||
			})
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if _, err := gorse.InsertFeedback(ctx, feedbacks); err != nil {
 | 
			
		||||
			fmt.Println("写入点赞记录失败", err)
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("写入点赞记录结束..", page*100)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 同步收藏数据
 | 
			
		||||
 | 
			
		||||
// 获取推荐ID
 | 
			
		||||
func GetRecommend(user_id int, category string) ([]int, error) {
 | 
			
		||||
	var ctx context.Context = context.Background()
 | 
			
		||||
 | 
			
		||||
	data, err := gorse.GetItemLatestWithCategory(ctx, fmt.Sprintf("%d", user_id), category, 100, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("获取推荐失败", err)
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var ids []int
 | 
			
		||||
	for _, item := range data {
 | 
			
		||||
		id, _ := strconv.Atoi(item.Id)
 | 
			
		||||
		ids = append(ids, id)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return ids, nil
 | 
			
		||||
}
 | 
			
		||||
@@ -197,6 +197,12 @@ func NewSchema(config Config) (graphql.Schema, error) {
 | 
			
		||||
		go checkColorNullRows()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 定时检查点赞收藏记录
 | 
			
		||||
	if config.Gorse.Open {
 | 
			
		||||
		fmt.Println("开启用户偏好收集")
 | 
			
		||||
		gorseInit(config.Gorse.Host, config.Gorse.Port)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 用户的可选字段
 | 
			
		||||
	user := graphql.NewObject(graphql.ObjectConfig{
 | 
			
		||||
		Name:        "User",
 | 
			
		||||
 
 | 
			
		||||
@@ -147,7 +147,7 @@ func (Category) TableName() string {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 输入配置
 | 
			
		||||
type ConfigMysql struct {
 | 
			
		||||
type MysqlConfig struct {
 | 
			
		||||
	Host     string
 | 
			
		||||
	Port     int
 | 
			
		||||
	Database string
 | 
			
		||||
@@ -159,7 +159,14 @@ type Oss struct {
 | 
			
		||||
	Local bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type GorseConfig struct {
 | 
			
		||||
	Host string
 | 
			
		||||
	Port int
 | 
			
		||||
	Open bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Config struct {
 | 
			
		||||
	Mysql ConfigMysql
 | 
			
		||||
	Mysql MysqlConfig
 | 
			
		||||
	Gorse GorseConfig
 | 
			
		||||
	Oss
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user