fusenapi/server/collection/internal/logic/testailogic.go

97 lines
21 KiB
Go
Raw Normal View History

2023-10-09 09:56:28 +00:00
package logic
import (
"encoding/json"
"fmt"
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/curl"
2023-10-09 10:28:43 +00:00
"github.com/google/uuid"
"strings"
2023-10-09 09:56:28 +00:00
"sync"
"time"
"context"
"fusenapi/server/collection/internal/svc"
"fusenapi/server/collection/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type TestAiLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewTestAiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TestAiLogic {
return &TestAiLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *TestAiLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
2023-10-09 10:28:43 +00:00
var data = `{"module_data":{"groupOptions":{"fill_1":{"list":[{"id":"442c4e24-53ae-2a76-8e6b-7550e2747369","tag":""},{"id":"646004b2-ad82-761f-c939-b85f96f97f9a","tag":""},{"id":"b6e4ab68-b36a-9534-d53f-722c93d54657","tag":""},{"id":"280ced37-6725-562f-4272-79bacfcadb12","tag":""},{"id":"a6aac5c1-8e54-c0f1-5749-9847f0fe497e","tag":""},{"id":"2b2eac02-37ae-0b1e-898e-d9e3166dfc9a","tag":""},{"id":"88181235-0317-fe9e-b202-cc7938acd449","tag":""},{"id":"93aba661-9260-8420-4adc-feef1657f4e3","tag":""},{"id":"50dec384-7501-dfdd-9c65-ddf3142540af","tag":""},{"id":"d8337893-bfab-0e43-2ef2-2606d1612fec","tag":""},{"id":"508859ae-cc8f-0b19-3554-c7632838d3fc","tag":""},{"id":"d5d3a1cb-2b13-2345-088d-b9163edf8aa6","tag":"","type":"color"}],"tag":"","title":"主色","value":"#E71F18"},"fill_2":{"list":[],"tag":"","title":"副色","value":"#000"},"fill_3":{"list":[],"tag":"","title":"备选色","value":"#000"},"fill_4":{"list":[{"id":"dbc50336-de0c-4b4a-f0aa-1a6444a3f615","tag":"","type":"color"},{"id":"cd0bf3b0-761f-e5b4-d7b6-f63fc54baa77","tag":"","type":"color"}],"tag":"","title":"固定色A","value":"#FFFFFF"},"material_1":{"id":"material_1","list":[{"id":"9baa3b65-c4f0-ebd8-866f-7e8d8e26e462","tag":"Logo"},{"id":"d9634ebf-78e2-3ead-30c5-cd545c3bfdaa","tag":"Logo"}],"tag":"Logo","title":"LOGO分组","value":""},"text_1":{"id":"text_1","list":[],"tag":"Slogan","title":"Slogan分组","value":"The ultimate experience"},"text_2":{"id":"text_2","list":[],"tag":"QRcode","title":"二维码分组","value":"www.fusen.cn"}},"id":1108,"material":"https://s3.us-west-1.amazonaws.com/storage.fusenpack.com/a0914c5b48319ebeed923ae780a5f1a00736056e0b5406a60095cfd4010b31d8","materialList":[{"QRcodeType":1,"algorithm":{"isLogoSplit":false,"list":[]},"align":"center","cameraStand":{"x":0,"y":0,"z":0},"fill":"#000000","follow":{"fill":"","material":"","text":"","visible":""},"fontFamily":"Aqum2SmallCaps3","fontSize":20,"fromCombineId":-1,"group":[],"height":1024,"id":"cf7caf81-1470-613a-7691-9c5eac710a0e","ifGroupNoBr":false,"lineHeight":1,"material":"https://s3.us-west-1.amazonaws.com/storage.fusenpack.com/c83b49a900c3bffff4fbf0d5b8e7d494033b3b793b660ccd1bf98b2558573adb","maxNum":50,"opacity":1,"paddingNum":0,"proportion":60,"rotation":0,"svgPath":"","systemShow":true,"tag":"","text":"","title":"底图","type":"background","verticalAlign":"middle","visible":true,"width":1024,"x":0,"y":0,"zIndex":0},{"QRcodeType":1,"algorithm":{"isLogoSplit":false,"list":[]},"align":"center","cameraStand":{"x":0,"y":0,"z":0},"fill":"#000000","follow":{"fill":"","material":"","text":"","visible":""},"fontFamily":"Aqum2SmallCaps3","fontSize":20,"fromCombineId":-1,"group":[],"height":100,"id":"0991b333-4baf-8fcc-a8bb-aa5546811815","ifGroupNoBr":false,"lineHeight":1,"material":"","maxNum":50,"opacity":1,"paddingNum":0,"proportion":60,"rotation":0,"svgPath":"","systemShow":true,"tag":"","text":"","title":"SVG元素","type":"color","verticalAlign":"middle","visible":true,"width":100,"x":0,"y":0,"zIndex":1},{"QRcodeType":1,"algorithm":{"isLogoSplit":false,"list":[]},"align":"center","cameraStand":{"x":0,"y":0,"z":0},"fill":"#000000","follow":{"fill":"","material":"","text":"","visible":""},"fontFamily":"Aqum2SmallCaps3","fontSize":20,"fromCombineId":-1,"group":["b3ce7508-232a-89aa-72fd-dbbacc32f938","37ffcea7-0134-e6ee-014a-f0dddc466cf0","869c16d3-4b82-b052-6def-9162e126c57a"],"height":100,"id":"f9b30c55-6aa1-0d82-9a85-945fd6b061d7","ifGroupNoBr":false,"lineHeight":1,"material":"","maxNum":50,"opacity":1,"paddingNum":0,"proportion":60,"rotation":0,"svgPath":"","systemShow":true,"tag":"","text":"","title":"合图组","type":"combine","verticalAlign":"middle","visible":false,"width":100,"x":0,"y":0,"zIndex":2},{"QRcodeType":1,"algorithm":{"isLogoSplit":false,"list":[]},"align":"center","cameraStand":{"x":0,"y":0,"z":0},"fill":"#000000","follow":{"fill":"","material":"","text":"","visible":""},"fontFamily":"Aqum2SmallCaps3","fontSize":20,"fromCombineId":"f9b30c55-6aa1-0d82-9a85-945fd6b061d7","group":[],"height":100,"id":"b3ce7508-232a-89aa-72fd-dbbacc32f938","ifGroupNoBr":fals
2023-10-09 09:56:28 +00:00
func (l *TestAiLogic) TestAi(req *types.TestAiReq, userinfo *auth.UserInfo) (resp *basic.Response) {
2023-10-09 10:21:45 +00:00
if req.Num > 100 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "num can`t not large than 100")
}
2023-10-09 09:56:28 +00:00
defer func() {
if err := recover(); err != nil {
logx.Error("测试异常:", err)
}
}()
begin := time.Now().UTC().UnixMilli()
errChan := make(chan string)
w := sync.WaitGroup{}
for i := 0; i < req.Num; i++ {
w.Add(1)
go func() {
defer func() {
if err := recover(); err != nil {
logx.Error("测试异常2", err)
}
}()
defer w.Done()
2023-10-09 10:28:43 +00:00
data = strings.ReplaceAll(data, "{{rand}}", uuid.New().String())
var postMap map[string]interface{}
if err := json.Unmarshal([]byte(data), &postMap); err != nil {
errChan <- err.Error()
return
}
2023-10-09 09:56:28 +00:00
var resultBLM constants.BLMServiceUrlResult
err := curl.NewClient(l.ctx, &curl.Config{
BaseUrl: "http://ai.fusen.3718.cn:8999",
Url: constants.BLMServiceUrlLogoCombine,
RequireTimeout: time.Second * 15,
}).PostJson(postMap, &resultBLM)
if err != nil {
errChan <- err.Error()
return
}
}()
}
go func() {
w.Wait()
close(errChan)
}()
for v := range errChan {
return resp.SetStatusWithMessage(basic.CodeServiceErr, v)
}
end := time.Now().UTC().UnixMilli() - begin
2023-10-09 09:57:54 +00:00
avg := end / int64(req.Num)
return resp.SetStatus(basic.CodeOK, fmt.Sprintf("总耗时:%d ms平均耗时%d ms", end, avg))
2023-10-09 09:56:28 +00:00
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *TestAiLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }