feat: implement core backend architecture and project management services for the History API
Build and Release / release (push) Successful in 1m33s
Build and Release / release (push) Successful in 1m33s
This commit is contained in:
@@ -2,14 +2,13 @@ package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"history-api/internal/gen/sqlc"
|
||||
"history-api/internal/models"
|
||||
"history-api/pkg/cache"
|
||||
"history-api/pkg/constants"
|
||||
"history-api/pkg/convert"
|
||||
json "history-api/pkg/jsonx"
|
||||
"strconv"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
@@ -39,9 +38,7 @@ func NewChatRepository(db *pgxpool.Pool, c cache.Cache) ChatRepository {
|
||||
}
|
||||
|
||||
func (r *chatRepository) generateQueryKey(prefix string, params any) string {
|
||||
b, _ := json.Marshal(params)
|
||||
hash := fmt.Sprintf("%x", md5.Sum(b))
|
||||
return fmt.Sprintf("%s:query:%s", prefix, hash)
|
||||
return cache.QueryKey(prefix, params)
|
||||
}
|
||||
|
||||
func (r *chatRepository) getConversationsByIDsWithFallback(ctx context.Context, ids []string) ([]*models.ConversationEntity, error) {
|
||||
@@ -50,14 +47,14 @@ func (r *chatRepository) getConversationsByIDsWithFallback(ctx context.Context,
|
||||
}
|
||||
keys := make([]string, len(ids))
|
||||
for i, id := range ids {
|
||||
keys[i] = fmt.Sprintf("conversation:id:%s", id)
|
||||
keys[i] = cache.Key("conversation:id", id)
|
||||
}
|
||||
raws := r.c.MGet(ctx, keys...)
|
||||
|
||||
var results []*models.ConversationEntity
|
||||
missingToCache := make(map[string]any)
|
||||
results := make([]*models.ConversationEntity, 0, len(ids))
|
||||
missingToCache := make(map[string]any, len(ids))
|
||||
|
||||
var missingPgIds []pgtype.UUID
|
||||
missingPgIds := make([]pgtype.UUID, 0, len(ids))
|
||||
for i, b := range raws {
|
||||
if len(b) == 0 {
|
||||
pgId := pgtype.UUID{}
|
||||
@@ -68,7 +65,7 @@ func (r *chatRepository) getConversationsByIDsWithFallback(ctx context.Context,
|
||||
}
|
||||
}
|
||||
|
||||
dbMap := make(map[string]*models.ConversationEntity)
|
||||
dbMap := make(map[string]*models.ConversationEntity, len(missingPgIds))
|
||||
if len(missingPgIds) > 0 {
|
||||
dbRows, err := r.q.GetConversationsByIDs(ctx, missingPgIds)
|
||||
if err == nil {
|
||||
@@ -114,14 +111,14 @@ func (r *chatRepository) getMessagesByIDsWithFallback(ctx context.Context, ids [
|
||||
}
|
||||
keys := make([]string, len(ids))
|
||||
for i, id := range ids {
|
||||
keys[i] = fmt.Sprintf("message:id:%s", id)
|
||||
keys[i] = cache.Key("message:id", id)
|
||||
}
|
||||
raws := r.c.MGet(ctx, keys...)
|
||||
|
||||
var results []*models.MessageEntity
|
||||
missingToCache := make(map[string]any)
|
||||
results := make([]*models.MessageEntity, 0, len(ids))
|
||||
missingToCache := make(map[string]any, len(ids))
|
||||
|
||||
var missingPgIds []pgtype.UUID
|
||||
missingPgIds := make([]pgtype.UUID, 0, len(ids))
|
||||
for i, b := range raws {
|
||||
if len(b) == 0 {
|
||||
pgId := pgtype.UUID{}
|
||||
@@ -132,7 +129,7 @@ func (r *chatRepository) getMessagesByIDsWithFallback(ctx context.Context, ids [
|
||||
}
|
||||
}
|
||||
|
||||
dbMap := make(map[string]*models.MessageEntity)
|
||||
dbMap := make(map[string]*models.MessageEntity, len(missingPgIds))
|
||||
if len(missingPgIds) > 0 {
|
||||
dbRows, err := r.q.GetMessagesByIDs(ctx, missingPgIds)
|
||||
if err == nil {
|
||||
@@ -176,14 +173,14 @@ func (r *chatRepository) getChatbotHistoriesByIDsWithFallback(ctx context.Contex
|
||||
}
|
||||
keys := make([]string, len(ids))
|
||||
for i, id := range ids {
|
||||
keys[i] = fmt.Sprintf("chatbot_history:id:%s", id)
|
||||
keys[i] = cache.Key("chatbot_history:id", id)
|
||||
}
|
||||
raws := r.c.MGet(ctx, keys...)
|
||||
|
||||
var results []*models.ChatbotHistoryEntity
|
||||
missingToCache := make(map[string]any)
|
||||
results := make([]*models.ChatbotHistoryEntity, 0, len(ids))
|
||||
missingToCache := make(map[string]any, len(ids))
|
||||
|
||||
var missingPgIds []pgtype.UUID
|
||||
missingPgIds := make([]pgtype.UUID, 0, len(ids))
|
||||
for i, b := range raws {
|
||||
if len(b) == 0 {
|
||||
pgId := pgtype.UUID{}
|
||||
@@ -194,7 +191,7 @@ func (r *chatRepository) getChatbotHistoriesByIDsWithFallback(ctx context.Contex
|
||||
}
|
||||
}
|
||||
|
||||
dbMap := make(map[string]*models.ChatbotHistoryEntity)
|
||||
dbMap := make(map[string]*models.ChatbotHistoryEntity, len(missingPgIds))
|
||||
if len(missingPgIds) > 0 {
|
||||
dbRows, err := r.q.GetChatbotHistoriesByIDs(ctx, missingPgIds)
|
||||
if err == nil {
|
||||
@@ -263,7 +260,7 @@ func (r *chatRepository) UpdateConversationStatus(ctx context.Context, params sq
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
}
|
||||
_ = r.c.Del(ctx, fmt.Sprintf("conversation:id:%s", entity.ID))
|
||||
_ = r.c.Del(ctx, cache.Key("conversation:id", entity.ID))
|
||||
return entity, nil
|
||||
}
|
||||
|
||||
@@ -299,9 +296,9 @@ func (r *chatRepository) GetMessagesByConversation(ctx context.Context, params s
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var results []*models.MessageEntity
|
||||
var ids []string
|
||||
toCache := make(map[string]any)
|
||||
results := make([]*models.MessageEntity, 0, len(rows))
|
||||
ids := make([]string, 0, len(rows))
|
||||
toCache := make(map[string]any, len(rows))
|
||||
|
||||
for _, row := range rows {
|
||||
item := &models.MessageEntity{
|
||||
@@ -313,7 +310,7 @@ func (r *chatRepository) GetMessagesByConversation(ctx context.Context, params s
|
||||
}
|
||||
ids = append(ids, item.ID)
|
||||
results = append(results, item)
|
||||
toCache[fmt.Sprintf("message:id:%s", item.ID)] = item
|
||||
toCache[cache.Key("message:id", item.ID)] = item
|
||||
}
|
||||
|
||||
if len(toCache) > 0 {
|
||||
@@ -341,7 +338,7 @@ func (r *chatRepository) CreateChatbotHistory(ctx context.Context, params sqlc.C
|
||||
go func() {
|
||||
userId := convert.UUIDToString(params.UserID)
|
||||
if userId != "" {
|
||||
_ = r.c.DelByPattern(context.Background(), fmt.Sprintf("chatbot_history:userId:%s:*", userId))
|
||||
_ = r.c.DelByPattern(context.Background(), "chatbot_history:userId:"+userId+":*")
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -349,12 +346,9 @@ func (r *chatRepository) CreateChatbotHistory(ctx context.Context, params sqlc.C
|
||||
}
|
||||
|
||||
func (r *chatRepository) GetChatbotHistory(ctx context.Context, params sqlc.GetChatbotHistoryParams) ([]*models.ChatbotHistoryEntity, error) {
|
||||
queryKey := fmt.Sprintf(
|
||||
"chatbot_history:userId:%s:limit:%d:cursor:%s",
|
||||
convert.UUIDToString(params.UserID),
|
||||
params.Limit,
|
||||
convert.UUIDToString(params.CursorID),
|
||||
)
|
||||
queryKey := "chatbot_history:userId:" + convert.UUIDToString(params.UserID) +
|
||||
":limit:" + strconv.Itoa(int(params.Limit)) +
|
||||
":cursor:" + convert.UUIDToString(params.CursorID)
|
||||
var cachedIDs []string
|
||||
err := r.c.Get(ctx, queryKey, &cachedIDs)
|
||||
if err == nil {
|
||||
@@ -369,9 +363,9 @@ func (r *chatRepository) GetChatbotHistory(ctx context.Context, params sqlc.GetC
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var results []*models.ChatbotHistoryEntity
|
||||
var ids []string
|
||||
toCache := make(map[string]any)
|
||||
results := make([]*models.ChatbotHistoryEntity, 0, len(rows))
|
||||
ids := make([]string, 0, len(rows))
|
||||
toCache := make(map[string]any, len(rows))
|
||||
|
||||
for _, row := range rows {
|
||||
item := &models.ChatbotHistoryEntity{
|
||||
@@ -383,7 +377,7 @@ func (r *chatRepository) GetChatbotHistory(ctx context.Context, params sqlc.GetC
|
||||
}
|
||||
ids = append(ids, item.ID)
|
||||
results = append(results, item)
|
||||
toCache[fmt.Sprintf("chatbot_history:id:%s", item.ID)] = item
|
||||
toCache[cache.Key("chatbot_history:id", item.ID)] = item
|
||||
}
|
||||
|
||||
if len(toCache) > 0 {
|
||||
|
||||
Reference in New Issue
Block a user