Skip to content

Commit af8a321

Browse files
authored
feat: aiproxy db index and import from oneapi (labring#5398)
* feat: import from oneapi * feat: model error rate * fix: lint * fix: ali embedding use openai schema * fix: disable content slow search * fix: optimize controller code and limit log search time span * refactor: sql index * feat: replace distinct to group by * feat: fast date_trunc * fix: time span format * feat: time trunc index * fix: miss dashboard index * fix: distinct unused no lint
1 parent f667853 commit af8a321

File tree

17 files changed

+958
-664
lines changed

17 files changed

+958
-664
lines changed

service/aiproxy/common/fastJSONSerializer/fastJSONSerializer.go

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ func (*JSONSerializer) Scan(ctx context.Context, field *schema.Field, dst reflec
2626
return fmt.Errorf("failed to unmarshal JSONB value: %#v", dbValue)
2727
}
2828

29+
if len(bytes) == 0 {
30+
field.ReflectValueOf(ctx, dst).Set(reflect.Zero(field.FieldType))
31+
return nil
32+
}
33+
2934
err = json.Unmarshal(bytes, fieldValue.Interface())
3035
}
3136

service/aiproxy/controller/channel.go

+4-24
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,14 @@ func ChannelTypeMetas(c *gin.Context) {
2626
}
2727

2828
func GetChannels(c *gin.Context) {
29-
p, _ := strconv.Atoi(c.Query("p"))
30-
p--
31-
if p < 0 {
32-
p = 0
33-
}
34-
perPage, _ := strconv.Atoi(c.Query("per_page"))
35-
if perPage <= 0 {
36-
perPage = 10
37-
} else if perPage > 100 {
38-
perPage = 100
39-
}
29+
page, perPage := parsePageParams(c)
4030
id, _ := strconv.Atoi(c.Query("id"))
4131
name := c.Query("name")
4232
key := c.Query("key")
4333
channelType, _ := strconv.Atoi(c.Query("channel_type"))
4434
baseURL := c.Query("base_url")
4535
order := c.Query("order")
46-
channels, total, err := model.GetChannels(p*perPage, perPage, id, name, key, channelType, baseURL, order)
36+
channels, total, err := model.GetChannels(page*perPage, perPage, id, name, key, channelType, baseURL, order)
4737
if err != nil {
4838
middleware.ErrorResponse(c, http.StatusOK, err.Error())
4939
return
@@ -89,24 +79,14 @@ func AddChannels(c *gin.Context) {
8979

9080
func SearchChannels(c *gin.Context) {
9181
keyword := c.Query("keyword")
92-
p, _ := strconv.Atoi(c.Query("p"))
93-
p--
94-
if p < 0 {
95-
p = 0
96-
}
97-
perPage, _ := strconv.Atoi(c.Query("per_page"))
98-
if perPage <= 0 {
99-
perPage = 10
100-
} else if perPage > 100 {
101-
perPage = 100
102-
}
82+
page, perPage := parsePageParams(c)
10383
id, _ := strconv.Atoi(c.Query("id"))
10484
name := c.Query("name")
10585
key := c.Query("key")
10686
channelType, _ := strconv.Atoi(c.Query("channel_type"))
10787
baseURL := c.Query("base_url")
10888
order := c.Query("order")
109-
channels, total, err := model.SearchChannels(keyword, p*perPage, perPage, id, name, key, channelType, baseURL, order)
89+
channels, total, err := model.SearchChannels(keyword, page*perPage, perPage, id, name, key, channelType, baseURL, order)
11090
if err != nil {
11191
middleware.ErrorResponse(c, http.StatusOK, err.Error())
11292
return

service/aiproxy/controller/dashboard.go

+15-25
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"net/http"
7-
"strconv"
87
"time"
98

109
"github.com/gin-gonic/gin"
@@ -14,34 +13,42 @@ import (
1413
"github.com/labring/sealos/service/aiproxy/model"
1514
)
1615

17-
func getDashboardTime(t string) (time.Time, time.Time, time.Duration) {
16+
func getDashboardTime(t string) (time.Time, time.Time, model.TimeSpanType) {
1817
end := time.Now()
1918
var start time.Time
20-
var timeSpan time.Duration
19+
var timeSpan model.TimeSpanType
2120
switch t {
2221
case "month":
2322
start = end.AddDate(0, 0, -30)
24-
timeSpan = time.Hour * 24
23+
timeSpan = model.TimeSpanDay
2524
case "two_week":
2625
start = end.AddDate(0, 0, -15)
27-
timeSpan = time.Hour * 24
26+
timeSpan = model.TimeSpanDay
2827
case "week":
2928
start = end.AddDate(0, 0, -7)
30-
timeSpan = time.Hour * 24
29+
timeSpan = model.TimeSpanDay
3130
case "day":
3231
fallthrough
3332
default:
3433
start = end.AddDate(0, 0, -1)
35-
timeSpan = time.Hour * 1
34+
timeSpan = model.TimeSpanHour
3635
}
3736
return start, end, timeSpan
3837
}
3938

40-
func fillGaps(data []*model.ChartData, start, end time.Time, timeSpan time.Duration) []*model.ChartData {
39+
func fillGaps(data []*model.ChartData, start, end time.Time, t model.TimeSpanType) []*model.ChartData {
4140
if len(data) == 0 {
4241
return data
4342
}
4443

44+
var timeSpan time.Duration
45+
switch t {
46+
case model.TimeSpanDay:
47+
timeSpan = time.Hour * 24
48+
default:
49+
timeSpan = time.Hour
50+
}
51+
4552
// Handle first point
4653
firstPoint := time.Unix(data[0].Timestamp, 0)
4754
firstAlignedTime := firstPoint
@@ -116,27 +123,11 @@ func fillGaps(data []*model.ChartData, start, end time.Time, timeSpan time.Durat
116123
return result
117124
}
118125

119-
func getTimeSpanWithDefault(c *gin.Context, defaultTimeSpan time.Duration) time.Duration {
120-
spanStr := c.Query("span")
121-
if spanStr == "" {
122-
return defaultTimeSpan
123-
}
124-
span, err := strconv.Atoi(spanStr)
125-
if err != nil {
126-
return defaultTimeSpan
127-
}
128-
if span < 1 || span > 48 {
129-
return defaultTimeSpan
130-
}
131-
return time.Duration(span) * time.Hour
132-
}
133-
134126
func GetDashboard(c *gin.Context) {
135127
log := middleware.GetLogger(c)
136128

137129
start, end, timeSpan := getDashboardTime(c.Query("type"))
138130
modelName := c.Query("model")
139-
timeSpan = getTimeSpanWithDefault(c, timeSpan)
140131

141132
dashboards, err := model.GetDashboardData(start, end, modelName, timeSpan)
142133
if err != nil {
@@ -170,7 +161,6 @@ func GetGroupDashboard(c *gin.Context) {
170161
start, end, timeSpan := getDashboardTime(c.Query("type"))
171162
tokenName := c.Query("token_name")
172163
modelName := c.Query("model")
173-
timeSpan = getTimeSpanWithDefault(c, timeSpan)
174164

175165
dashboards, err := model.GetGroupDashboardData(group, start, end, tokenName, modelName, timeSpan)
176166
if err != nil {

service/aiproxy/controller/group.go

+4-25
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,9 @@ func (g *GroupResponse) MarshalJSON() ([]byte, error) {
3030
}
3131

3232
func GetGroups(c *gin.Context) {
33-
p, _ := strconv.Atoi(c.Query("p"))
34-
p--
35-
if p < 0 {
36-
p = 0
37-
}
38-
perPage, _ := strconv.Atoi(c.Query("per_page"))
39-
if perPage <= 0 {
40-
perPage = 10
41-
} else if perPage > 100 {
42-
perPage = 100
43-
}
44-
33+
page, perPage := parsePageParams(c)
4534
order := c.DefaultQuery("order", "")
46-
groups, total, err := model.GetGroups(p*perPage, perPage, order, false)
35+
groups, total, err := model.GetGroups(page*perPage, perPage, order, false)
4736
if err != nil {
4837
middleware.ErrorResponse(c, http.StatusOK, err.Error())
4938
return
@@ -64,20 +53,10 @@ func GetGroups(c *gin.Context) {
6453

6554
func SearchGroups(c *gin.Context) {
6655
keyword := c.Query("keyword")
67-
p, _ := strconv.Atoi(c.Query("p"))
68-
p--
69-
if p < 0 {
70-
p = 0
71-
}
72-
perPage, _ := strconv.Atoi(c.Query("per_page"))
73-
if perPage <= 0 {
74-
perPage = 10
75-
} else if perPage > 100 {
76-
perPage = 100
77-
}
56+
page, perPage := parsePageParams(c)
7857
order := c.DefaultQuery("order", "")
7958
status, _ := strconv.Atoi(c.Query("status"))
80-
groups, total, err := model.SearchGroup(keyword, p*perPage, perPage, order, status)
59+
groups, total, err := model.SearchGroup(keyword, page*perPage, perPage, order, status)
8160
if err != nil {
8261
middleware.ErrorResponse(c, http.StatusOK, err.Error())
8362
return

0 commit comments

Comments
 (0)