mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-12 11:23:42 +08:00
fix: models api return models in deactivate channels
- Enhance logging functionality by adding context support and improving debugging options. - Standardize function naming conventions across middleware to ensure consistency. - Optimize data retrieval and handling in the model controller, including caching and error management. - Simplify the bug report template to streamline the issue reporting process.
This commit is contained in:
@@ -5,10 +5,10 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Ability struct {
|
||||
@@ -110,3 +110,34 @@ func GetGroupModels(ctx context.Context, group string) ([]string, error) {
|
||||
sort.Strings(models)
|
||||
return models, err
|
||||
}
|
||||
|
||||
type EnabledAbility struct {
|
||||
Model string `json:"model" gorm:"model"`
|
||||
ChannelType int `json:"channel_type" gorm:"channel_type"`
|
||||
}
|
||||
|
||||
// GetGroupModelsV2 returns all enabled models for this group with their channel names.
|
||||
func GetGroupModelsV2(ctx context.Context, group string) ([]EnabledAbility, error) {
|
||||
// prepare query based on database type
|
||||
groupCol := "`group`"
|
||||
trueVal := "1"
|
||||
if common.UsingPostgreSQL {
|
||||
groupCol = `"group"`
|
||||
trueVal = "true"
|
||||
}
|
||||
|
||||
// query with JOIN to get model and channel name in a single query
|
||||
var models []EnabledAbility
|
||||
query := DB.Model(&Ability{}).
|
||||
Select("abilities.model AS model, channels.type AS channel_type").
|
||||
Joins("JOIN channels ON abilities.channel_id = channels.id").
|
||||
Where("abilities."+groupCol+" = ? AND abilities.enabled = "+trueVal, group).
|
||||
Order("abilities.priority DESC")
|
||||
|
||||
err := query.Find(&models).Error
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get group models")
|
||||
}
|
||||
|
||||
return models, nil
|
||||
}
|
||||
|
||||
@@ -3,18 +3,19 @@ package model
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/random"
|
||||
"math/rand"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/random"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -148,7 +149,10 @@ func CacheIsUserEnabled(userId int) (bool, error) {
|
||||
return userEnabled, err
|
||||
}
|
||||
|
||||
func CacheGetGroupModels(ctx context.Context, group string) ([]string, error) {
|
||||
// CacheGetGroupModels returns models of a group
|
||||
//
|
||||
// Deprecated: use CacheGetGroupModelsV2 instead
|
||||
func CacheGetGroupModels(ctx context.Context, group string) (models []string, err error) {
|
||||
if !common.RedisEnabled {
|
||||
return GetGroupModels(ctx, group)
|
||||
}
|
||||
@@ -156,7 +160,7 @@ func CacheGetGroupModels(ctx context.Context, group string) ([]string, error) {
|
||||
if err == nil {
|
||||
return strings.Split(modelsStr, ","), nil
|
||||
}
|
||||
models, err := GetGroupModels(ctx, group)
|
||||
models, err = GetGroupModels(ctx, group)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -167,6 +171,42 @@ func CacheGetGroupModels(ctx context.Context, group string) ([]string, error) {
|
||||
return models, nil
|
||||
}
|
||||
|
||||
// CacheGetGroupModelsV2 is a version of CacheGetGroupModels that returns EnabledAbility instead of string
|
||||
func CacheGetGroupModelsV2(ctx context.Context, group string) (models []EnabledAbility, err error) {
|
||||
if !common.RedisEnabled {
|
||||
return GetGroupModelsV2(ctx, group)
|
||||
}
|
||||
modelsStr, err := common.RedisGet(fmt.Sprintf("group_models_v2:%s", group))
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "Redis get group models error: %+v", err)
|
||||
} else {
|
||||
if err = json.Unmarshal([]byte(modelsStr), &models); err != nil {
|
||||
logger.Warnf(ctx, "Redis get group models error: %+v", err)
|
||||
} else {
|
||||
return models, nil
|
||||
}
|
||||
}
|
||||
|
||||
models, err = GetGroupModelsV2(ctx, group)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get group models")
|
||||
}
|
||||
|
||||
cachePayload, err := json.Marshal(models)
|
||||
if err != nil {
|
||||
logger.SysError("Redis set group models error: " + err.Error())
|
||||
return models, nil
|
||||
}
|
||||
|
||||
err = common.RedisSet(fmt.Sprintf("group_models:%s", group), string(cachePayload),
|
||||
time.Duration(GroupModelsCacheSeconds)*time.Second)
|
||||
if err != nil {
|
||||
logger.SysError("Redis set group models error: " + err.Error())
|
||||
}
|
||||
|
||||
return models, nil
|
||||
}
|
||||
|
||||
var group2model2channels map[string]map[string][]*Channel
|
||||
var channelSyncLock sync.RWMutex
|
||||
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/env"
|
||||
@@ -13,9 +18,6 @@ import (
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
@@ -116,6 +118,11 @@ func InitDB() {
|
||||
return
|
||||
}
|
||||
|
||||
if config.DebugSQLEnabled {
|
||||
logger.Debug(context.TODO(), "debug sql enabled")
|
||||
DB = DB.Debug()
|
||||
}
|
||||
|
||||
sqlDB := setDBConns(DB)
|
||||
|
||||
if !config.IsMasterNode {
|
||||
@@ -201,10 +208,6 @@ func migrateLOGDB() error {
|
||||
}
|
||||
|
||||
func setDBConns(db *gorm.DB) *sql.DB {
|
||||
if config.DebugSQLEnabled {
|
||||
db = db.Debug()
|
||||
}
|
||||
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
logger.FatalLog("failed to connect database: " + err.Error())
|
||||
|
||||
Reference in New Issue
Block a user