提交 ea1c2441 authored 作者: mooncake9527's avatar mooncake9527

update

上级 e06545fc
package convertUtils package convert_utils
import "strconv" import "strconv"
......
package xeg package eg_utils
import ( import (
"context" "context"
...@@ -16,14 +16,13 @@ func Do(ctx context.Context, fns ...FnCtx) error { ...@@ -16,14 +16,13 @@ func Do(ctx context.Context, fns ...FnCtx) error {
} }
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
g, ctx := errgroup.WithContext(ctx) eg, ctx := errgroup.WithContext(ctx)
for _, fn := range fns { for _, fn := range fns {
g.Go(func() error { eg.Go(func() error {
return fn(ctx) return fn(ctx)
}) })
} }
err := g.Wait() if err := eg.Wait(); err != nil {
if err != nil {
return xerror.New(err.Error()) return xerror.New(err.Error())
} }
return nil return nil
...@@ -35,14 +34,13 @@ func DoWithTimeout(timeout time.Duration, fns ...FnCtx) error { ...@@ -35,14 +34,13 @@ func DoWithTimeout(timeout time.Duration, fns ...FnCtx) error {
} }
ctx, cancel := context.WithTimeout(context.Background(), timeout) ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel() defer cancel()
g, ctx := errgroup.WithContext(ctx) eg, ctx := errgroup.WithContext(ctx)
for _, fn := range fns { for _, fn := range fns {
g.Go(func() error { eg.Go(func() error {
return fn(ctx) return fn(ctx)
}) })
} }
err := g.Wait() if err := eg.Wait(); err != nil {
if err != nil {
return xerror.New(err.Error()) return xerror.New(err.Error())
} }
return nil return nil
......
package httpUtils
import (
"net/http"
"time"
)
type Request struct {
method string
url string
params map[string]interface{}
headers map[string]string
body any
bodyJSON any
timeout time.Duration
retry uint
request *http.Request
response *http.Response
err error
}
func New() *Request {
return &Request{
request: &http.Request{},
headers: map[string]string{},
params: map[string]interface{}{},
}
}
func (req *Request) SetURL(path string) *Request {
req.url = path
return req
}
func (req *Request) SetParams(params map[string]any) *Request {
if req.params == nil {
req.params = params
} else {
for k, v := range params {
req.params[k] = v
}
}
return req
}
func (req *Request) SetParam(k string, v interface{}) *Request {
if req.params == nil {
req.params = make(map[string]interface{})
}
req.params[k] = v
return req
}
func (req *Request) SetBody(body interface{}) *Request {
switch v := body.(type) {
case string:
req.body = v
case []byte:
req.body = string(v)
default:
req.bodyJSON = body
}
return req
}
func (req *Request) SetHeader(k, v string) *Request {
if req.headers == nil {
req.headers = make(map[string]string)
}
req.headers[k] = v
return req
}
func (req *Request) SetTimeout(t time.Duration) *Request {
req.timeout = t
return req
}
func (req *Request) SetRetry(retry uint) *Request {
req.retry = retry
return req
}
func (req *Request) SetContentType(a string) *Request {
req.SetHeader("Content-Type", a)
return req
}
package jsonUtils package json_utils
import ( import (
"bytes" "bytes"
......
package mapUtils package map_utils
import ( import (
"encoding/json" "encoding/json"
......
...@@ -16,7 +16,7 @@ import ( ...@@ -16,7 +16,7 @@ import (
"gitlab.wanzhuangkj.com/tush/xpkg/logger" "gitlab.wanzhuangkj.com/tush/xpkg/logger"
merge "gitlab.wanzhuangkj.com/tush/xpkg/merger" merge "gitlab.wanzhuangkj.com/tush/xpkg/merger"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils" "gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/jsonUtils" "gitlab.wanzhuangkj.com/tush/xpkg/utils/json_utils"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/retryUtils" "gitlab.wanzhuangkj.com/tush/xpkg/utils/retryUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf" "gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xtime" "gitlab.wanzhuangkj.com/tush/xpkg/utils/xtime"
...@@ -101,7 +101,7 @@ func Init(schema string, isProd bool) { ...@@ -101,7 +101,7 @@ func Init(schema string, isProd bool) {
} }
insertFn := func() error { insertFn := func() error {
if err := WebLogDao.CreateSliceSilent(ctx, ors); err != nil { if err := WebLogDao.CreateSliceSilent(ctx, ors); err != nil {
logger.Error("[webLog] insert fail detail", ctxUtils.CtxTraceIDField(ctx)) logger.Error("[webLog] insert fail detail", logger.Err(err), ctxUtils.CtxTraceIDField(ctx))
return err return err
} }
return nil return nil
...@@ -123,21 +123,21 @@ func Init(schema string, isProd bool) { ...@@ -123,21 +123,21 @@ func Init(schema string, isProd bool) {
} }
func AddAsync(ctx context.Context, traceNo, operate string, req *entity.CopyHttpReq, rsp *entity.CopyHttpRsp) { func AddAsync(ctx context.Context, traceNo, operate string, req *entity.CopyHttpReq, rsp *entity.CopyHttpRsp) {
op := &WebLog{} wl := &WebLog{}
op.ID = xsf.GenerateID() wl.ID = xsf.GenerateID()
op.CompanyID = ctxUtils.GetCtxCompanyID(ctx) wl.CompanyID = ctxUtils.GetCtxCompanyID(ctx)
op.UserID = ctxUtils.GetCtxUID(ctx) wl.UserID = ctxUtils.GetCtxUID(ctx)
op.TraceID = ctxUtils.GetCtxTid(ctx) wl.TraceID = ctxUtils.GetCtxTid(ctx)
op.Operate = operate wl.Operate = operate
op.CreatedAt = xtime.Now() wl.CreatedAt = xtime.Now()
if req != nil { if req != nil {
op.Req = jsonUtils.ToJSONStringPtr(req) wl.Req = json_utils.ToJSONStringPtr(req)
} }
if rsp != nil { if rsp != nil {
op.Rsp = jsonUtils.ToJSONStringPtr(rsp) wl.Rsp = json_utils.ToJSONStringPtr(rsp)
} }
go func() { go func() {
merger.Add(op) merger.Add(wl)
}() }()
} }
...@@ -179,10 +179,6 @@ const TBWebLog = "web_log" ...@@ -179,10 +179,6 @@ const TBWebLog = "web_log"
func (WebLog) TableName() string { func (WebLog) TableName() string {
return TBWebLog return TBWebLog
} }
func (x WebLog) GetOrder() string {
return "id asc"
}
func (x *WebLog) BeforeCreate(tx *gorm.DB) (err error) { func (x *WebLog) BeforeCreate(tx *gorm.DB) (err error) {
if x.CreatedAt.IsZero() { if x.CreatedAt.IsZero() {
x.CreatedAt = xtime.Now() x.CreatedAt = xtime.Now()
......
...@@ -2,8 +2,8 @@ DROP TABLE if EXISTS `web_log`; ...@@ -2,8 +2,8 @@ DROP TABLE if EXISTS `web_log`;
CREATE TABLE `web_log` ( CREATE TABLE `web_log` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID eg[10001]', `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID eg[10001]',
`company_id` bigint(20) unsigned DEFAULT NULL COMMENT '公司ID eg[20001]', `company_id` bigint(20) DEFAULT NULL COMMENT '公司ID eg[20001]',
`user_id` bigint(20) unsigned DEFAULT NULL COMMENT '用户ID eg[30001]', `user_id` bigint(20) DEFAULT NULL COMMENT '用户ID eg[30001]',
`operate` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL DEFAULT '' COMMENT '操作 eg[用户登出]', `operate` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL DEFAULT '' COMMENT '操作 eg[用户登出]',
`trace_id` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL DEFAULT '' COMMENT '溯源id eg[TRACE_ID12345]', `trace_id` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL DEFAULT '' COMMENT '溯源id eg[TRACE_ID12345]',
`req` json DEFAULT NULL COMMENT '请求 eg[]', `req` json DEFAULT NULL COMMENT '请求 eg[]',
......
...@@ -77,8 +77,6 @@ func NowPtr() *DateTime { ...@@ -77,8 +77,6 @@ func NowPtr() *DateTime {
return &j return &j
} }
const TimeFormat = "2006-01-02 15:04:05"
// Value insert timestamp into mysql need this function. // Value insert timestamp into mysql need this function.
func (t DateTime) Value() (driver.Value, error) { func (t DateTime) Value() (driver.Value, error) {
var zeroTime time.Time var zeroTime time.Time
......
差异被折叠。
package xtime
import (
"testing"
"time"
)
func TestParseExtendedDuration(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want time.Duration
wantErr bool
}{
{name: "1", args: args{s: "1d"}, want: 24 * time.Hour, wantErr: false},
{name: "2", args: args{s: "2w"}, want: 2 * 7 * 24 * time.Hour, wantErr: false},
{name: "3", args: args{s: "3M"}, want: 3 * 30 * 24 * time.Hour, wantErr: false},
{name: "4", args: args{s: "4y"}, want: 4 * 365 * 24 * time.Hour, wantErr: false},
{name: "5", args: args{s: "1d2h"}, want: 26 * time.Hour, wantErr: false},
{name: "6", args: args{s: "1W3D"}, want: (7 + 3) * 24 * time.Hour, wantErr: false},
{name: "7", args: args{s: "30m"}, want: 30 * time.Minute, wantErr: false},
{name: "7", args: args{s: "20s"}, want: 20 * time.Second, wantErr: false},
{name: "7", args: args{s: "20ms"}, want: 20 * time.Millisecond, wantErr: false},
{name: "8", args: args{s: "1x"}, want: 0, wantErr: true},
{name: "9", args: args{s: "invalid"}, want: 0, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseExtendedDuration(tt.args.s)
if (err != nil) != tt.wantErr {
t.Errorf("ParseExtendedDuration() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ParseExtendedDuration() = %v, want %v", got, tt.want)
}
})
}
}
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/response" "gitlab.wanzhuangkj.com/tush/xpkg/gin/response"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli" "gitlab.wanzhuangkj.com/tush/xpkg/httpcli"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/mapUtils" "gitlab.wanzhuangkj.com/tush/xpkg/utils/map_utils"
"gitlab.wanzhuangkj.com/tush/xpkg/xcommon/validator" "gitlab.wanzhuangkj.com/tush/xpkg/xcommon/validator"
) )
...@@ -102,7 +102,7 @@ func (x *ControllerTester) After(t *testing.T) { ...@@ -102,7 +102,7 @@ func (x *ControllerTester) After(t *testing.T) {
func (x *ControllerTester) CommonHttp(method, URL string, in any, reply *response.Result) error { func (x *ControllerTester) CommonHttp(method, URL string, in any, reply *response.Result) error {
if method == http.MethodDelete || method == http.MethodGet { if method == http.MethodDelete || method == http.MethodGet {
in = mapUtils.StructToMapWithJsonTag(in) in = map_utils.StructToMapWithJsonTag(in)
} }
if err := x.client.NewRequest(). if err := x.client.NewRequest().
SetHeaders(x.Headers). SetHeaders(x.Headers).
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论