Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: aiproxy db index and import from oneapi #5398

Merged
merged 13 commits into from
Feb 21, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func (*JSONSerializer) Scan(ctx context.Context, field *schema.Field, dst reflec
return fmt.Errorf("failed to unmarshal JSONB value: %#v", dbValue)
}

if len(bytes) == 0 {
field.ReflectValueOf(ctx, dst).Set(reflect.Zero(field.FieldType))
return nil
}

err = json.Unmarshal(bytes, fieldValue.Interface())
}

Expand Down
28 changes: 4 additions & 24 deletions service/aiproxy/controller/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,14 @@ func ChannelTypeMetas(c *gin.Context) {
}

func GetChannels(c *gin.Context) {
p, _ := strconv.Atoi(c.Query("p"))
p--
if p < 0 {
p = 0
}
perPage, _ := strconv.Atoi(c.Query("per_page"))
if perPage <= 0 {
perPage = 10
} else if perPage > 100 {
perPage = 100
}
page, perPage := parsePageParams(c)
id, _ := strconv.Atoi(c.Query("id"))
name := c.Query("name")
key := c.Query("key")
channelType, _ := strconv.Atoi(c.Query("channel_type"))
baseURL := c.Query("base_url")
order := c.Query("order")
channels, total, err := model.GetChannels(p*perPage, perPage, id, name, key, channelType, baseURL, order)
channels, total, err := model.GetChannels(page*perPage, perPage, id, name, key, channelType, baseURL, order)
if err != nil {
middleware.ErrorResponse(c, http.StatusOK, err.Error())
return
Expand Down Expand Up @@ -89,24 +79,14 @@ func AddChannels(c *gin.Context) {

func SearchChannels(c *gin.Context) {
keyword := c.Query("keyword")
p, _ := strconv.Atoi(c.Query("p"))
p--
if p < 0 {
p = 0
}
perPage, _ := strconv.Atoi(c.Query("per_page"))
if perPage <= 0 {
perPage = 10
} else if perPage > 100 {
perPage = 100
}
page, perPage := parsePageParams(c)
id, _ := strconv.Atoi(c.Query("id"))
name := c.Query("name")
key := c.Query("key")
channelType, _ := strconv.Atoi(c.Query("channel_type"))
baseURL := c.Query("base_url")
order := c.Query("order")
channels, total, err := model.SearchChannels(keyword, p*perPage, perPage, id, name, key, channelType, baseURL, order)
channels, total, err := model.SearchChannels(keyword, page*perPage, perPage, id, name, key, channelType, baseURL, order)
if err != nil {
middleware.ErrorResponse(c, http.StatusOK, err.Error())
return
Expand Down
40 changes: 15 additions & 25 deletions service/aiproxy/controller/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"net/http"
"strconv"
"time"

"github.com/gin-gonic/gin"
Expand All @@ -14,34 +13,42 @@ import (
"github.com/labring/sealos/service/aiproxy/model"
)

func getDashboardTime(t string) (time.Time, time.Time, time.Duration) {
func getDashboardTime(t string) (time.Time, time.Time, model.TimeSpanType) {
end := time.Now()
var start time.Time
var timeSpan time.Duration
var timeSpan model.TimeSpanType
switch t {
case "month":
start = end.AddDate(0, 0, -30)
timeSpan = time.Hour * 24
timeSpan = model.TimeSpanDay
case "two_week":
start = end.AddDate(0, 0, -15)
timeSpan = time.Hour * 24
timeSpan = model.TimeSpanDay
case "week":
start = end.AddDate(0, 0, -7)
timeSpan = time.Hour * 24
timeSpan = model.TimeSpanDay
case "day":
fallthrough
default:
start = end.AddDate(0, 0, -1)
timeSpan = time.Hour * 1
timeSpan = model.TimeSpanHour
}
return start, end, timeSpan
}

func fillGaps(data []*model.ChartData, start, end time.Time, timeSpan time.Duration) []*model.ChartData {
func fillGaps(data []*model.ChartData, start, end time.Time, t model.TimeSpanType) []*model.ChartData {
if len(data) == 0 {
return data
}

var timeSpan time.Duration
switch t {
case model.TimeSpanDay:
timeSpan = time.Hour * 24
default:
timeSpan = time.Hour
}

// Handle first point
firstPoint := time.Unix(data[0].Timestamp, 0)
firstAlignedTime := firstPoint
Expand Down Expand Up @@ -116,27 +123,11 @@ func fillGaps(data []*model.ChartData, start, end time.Time, timeSpan time.Durat
return result
}

func getTimeSpanWithDefault(c *gin.Context, defaultTimeSpan time.Duration) time.Duration {
spanStr := c.Query("span")
if spanStr == "" {
return defaultTimeSpan
}
span, err := strconv.Atoi(spanStr)
if err != nil {
return defaultTimeSpan
}
if span < 1 || span > 48 {
return defaultTimeSpan
}
return time.Duration(span) * time.Hour
}

func GetDashboard(c *gin.Context) {
log := middleware.GetLogger(c)

start, end, timeSpan := getDashboardTime(c.Query("type"))
modelName := c.Query("model")
timeSpan = getTimeSpanWithDefault(c, timeSpan)

dashboards, err := model.GetDashboardData(start, end, modelName, timeSpan)
if err != nil {
Expand Down Expand Up @@ -170,7 +161,6 @@ func GetGroupDashboard(c *gin.Context) {
start, end, timeSpan := getDashboardTime(c.Query("type"))
tokenName := c.Query("token_name")
modelName := c.Query("model")
timeSpan = getTimeSpanWithDefault(c, timeSpan)

dashboards, err := model.GetGroupDashboardData(group, start, end, tokenName, modelName, timeSpan)
if err != nil {
Expand Down
29 changes: 4 additions & 25 deletions service/aiproxy/controller/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,9 @@ func (g *GroupResponse) MarshalJSON() ([]byte, error) {
}

func GetGroups(c *gin.Context) {
p, _ := strconv.Atoi(c.Query("p"))
p--
if p < 0 {
p = 0
}
perPage, _ := strconv.Atoi(c.Query("per_page"))
if perPage <= 0 {
perPage = 10
} else if perPage > 100 {
perPage = 100
}

page, perPage := parsePageParams(c)
order := c.DefaultQuery("order", "")
groups, total, err := model.GetGroups(p*perPage, perPage, order, false)
groups, total, err := model.GetGroups(page*perPage, perPage, order, false)
if err != nil {
middleware.ErrorResponse(c, http.StatusOK, err.Error())
return
Expand All @@ -64,20 +53,10 @@ func GetGroups(c *gin.Context) {

func SearchGroups(c *gin.Context) {
keyword := c.Query("keyword")
p, _ := strconv.Atoi(c.Query("p"))
p--
if p < 0 {
p = 0
}
perPage, _ := strconv.Atoi(c.Query("per_page"))
if perPage <= 0 {
perPage = 10
} else if perPage > 100 {
perPage = 100
}
page, perPage := parsePageParams(c)
order := c.DefaultQuery("order", "")
status, _ := strconv.Atoi(c.Query("status"))
groups, total, err := model.SearchGroup(keyword, p*perPage, perPage, order, status)
groups, total, err := model.SearchGroup(keyword, page*perPage, perPage, order, status)
if err != nil {
middleware.ErrorResponse(c, http.StatusOK, err.Error())
return
Expand Down
Loading