From fd0f614f5e03ce9157816147b7fd166fc9d5ed64 Mon Sep 17 00:00:00 2001 From: CNCSMonster <99234657+CNCSMonster@users.noreply.github.com> Date: Mon, 25 Sep 2023 15:48:55 +0800 Subject: [PATCH] feat: support upload logs (#340) --- go.mod | 2 +- go.sum | 4 +- internal/group/sdk.go | 3 +- internal/login/init_login.go | 13 ++-- internal/third/log.go | 71 +++++++++++++++++++++ internal/third/log_test.go | 39 +++++++++++ internal/third/push.go | 22 ++++--- open_im_sdk/third.go | 8 ++- pkg/ccontext/context.go | 1 + pkg/ccontext/context_test.go | 1 + pkg/constant/server_api_router.go | 1 + pkg/sdk_params_callback/third_sdk_struct.go | 9 ++- 12 files changed, 152 insertions(+), 22 deletions(-) create mode 100644 internal/third/log.go create mode 100644 internal/third/log_test.go mode change 100644 => 100755 pkg/sdk_params_callback/third_sdk_struct.go diff --git a/go.mod b/go.mod index 7d0d22749..c9e8cadb6 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( require golang.org/x/net v0.11.0 require ( - github.com/OpenIMSDK/protocol v0.0.14 + github.com/OpenIMSDK/protocol v0.0.23 github.com/OpenIMSDK/tools v0.0.5 github.com/google/go-cmp v0.5.9 golang.org/x/image v0.9.0 diff --git a/go.sum b/go.sum index e766f67b0..04b403369 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/OpenIMSDK/protocol v0.0.14 h1:cvQ3f8MTcyYygAnZ7Exq6zIbvHGCEV0fWdpzjQEDDBQ= -github.com/OpenIMSDK/protocol v0.0.14/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y= +github.com/OpenIMSDK/protocol v0.0.23 h1:L545aRQez6Ro+AaJB1Z6Mz7ojnDtp41WqASxYveCkcE= +github.com/OpenIMSDK/protocol v0.0.23/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y= github.com/OpenIMSDK/tools v0.0.5 h1:yBVHJ3EpIDcp8VFKPjuGr6MQvFa3t4JByZ+vmeC06/Q= github.com/OpenIMSDK/tools v0.0.5/go.mod h1:eg+q4A34Qmu73xkY0mt37FHGMCMfC6CtmOnm0kFEGFI= github.com/antonfisher/nested-logrus-formatter v1.3.1 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ= diff --git a/internal/group/sdk.go b/internal/group/sdk.go index 97c856e18..175d7ebee 100644 --- a/internal/group/sdk.go +++ b/internal/group/sdk.go @@ -16,12 +16,13 @@ package group import ( "context" + "time" + "github.com/openimsdk/openim-sdk-core/v3/internal/util" "github.com/openimsdk/openim-sdk-core/v3/pkg/constant" "github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct" "github.com/openimsdk/openim-sdk-core/v3/pkg/sdk_params_callback" "github.com/openimsdk/openim-sdk-core/v3/pkg/sdkerrs" - "time" "github.com/OpenIMSDK/tools/log" diff --git a/internal/login/init_login.go b/internal/login/init_login.go index cf76ff466..734c8cbff 100644 --- a/internal/login/init_login.go +++ b/internal/login/init_login.go @@ -17,6 +17,9 @@ package login import ( "context" "fmt" + "sync" + "time" + "github.com/openimsdk/openim-sdk-core/v3/internal/business" "github.com/openimsdk/openim-sdk-core/v3/internal/cache" conv "github.com/openimsdk/openim-sdk-core/v3/internal/conversation_msg" @@ -36,8 +39,6 @@ import ( "github.com/openimsdk/openim-sdk-core/v3/pkg/sdkerrs" "github.com/openimsdk/openim-sdk-core/v3/pkg/utils" "github.com/openimsdk/openim-sdk-core/v3/sdk_struct" - "sync" - "time" "github.com/OpenIMSDK/protocol/sdkws" @@ -66,7 +67,7 @@ type LoginMgr struct { db db_interface.DataBase longConnMgr *interaction.LongConnMgr msgSyncer *interaction.MsgSyncer - push *third.Push + third *third.Third cache *cache.Cache token string loginUserID string @@ -113,8 +114,8 @@ func (u *LoginMgr) GetToken() string { return u.token } -func (u *LoginMgr) Push() *third.Push { - return u.push +func (u *LoginMgr) Third() *third.Third { + return u.third } func (u *LoginMgr) ImConfig() sdk_struct.IMConfig { @@ -287,7 +288,7 @@ func (u *LoginMgr) login(ctx context.Context, userID, token string) error { if u.businessListener != nil { u.business.SetListener(u.businessListener) } - u.push = third.NewPush(u.info.PlatformID, u.loginUserID) + u.third = third.NewThird(u.info.PlatformID, u.loginUserID, constant.SdkVersion, u.info.LogFilePath, u.file) log.ZDebug(ctx, "forcedSynchronization success...", "login cost time: ", time.Since(t1)) u.longConnMgr.Run(ctx) diff --git a/internal/third/log.go b/internal/third/log.go new file mode 100644 index 000000000..4f861f466 --- /dev/null +++ b/internal/third/log.go @@ -0,0 +1,71 @@ +package third + +import ( + "context" + "errors" + "os" + "path/filepath" + "strings" + "time" + + "github.com/OpenIMSDK/protocol/third" + uploadfile "github.com/openimsdk/openim-sdk-core/v3/internal/file" + "github.com/openimsdk/openim-sdk-core/v3/internal/util" + "github.com/openimsdk/openim-sdk-core/v3/pkg/constant" + "github.com/openimsdk/openim-sdk-core/v3/pkg/sdk_params_callback" +) + +func (c *Third) UploadLogs(ctx context.Context, params []sdk_params_callback.UploadLogParams) error { + + return c.uploadLogs(ctx, params) +} + +func (t *Third) uploadLogs(ctx context.Context, params []sdk_params_callback.UploadLogParams) error { + + logFilePath := t.LogFilePath + files, err := os.ReadDir(logFilePath) + if err != nil { + return err + } + req := third.UploadLogsReq{} + errsb := strings.Builder{} + for _, file := range files { + if !checkLogPath(file.Name()) { + continue + } + var filename = filepath.Join(logFilePath, file.Name()) + resp, err := t.fileUploader.UploadFile(ctx, &uploadfile.UploadFileReq{Filepath: filename, Name: file.Name(), Cause: "upload_logs"}, nil) + if err != nil { + errsb.WriteString(err.Error()) + } + var fileURL third.FileURL + fileURL.Filename = filename + fileURL.URL = resp.URL + req.FileURLs = append(req.FileURLs, &fileURL) + } + errs := errsb.String() + if errs != "" { + return errors.New(errs) + } + _, err = util.CallApi[third.UploadLogsResp](ctx, constant.UploadLogsRouter, &req) + if err != nil { + return err + } + + return nil +} + +func checkLogPath(logpath string) bool { + if len(logpath) < len("open-im-sdk-core.yyyy-mm-dd") { + return false + } + logTime := logpath[len(logpath)-len(".yyyy-mm-dd"):] + if _, err := time.Parse(".2006-01-02", logTime); err != nil { + return false + } + if !strings.HasPrefix(logpath, "open-im-sdk-core.") { + return false + } + + return true +} diff --git a/internal/third/log_test.go b/internal/third/log_test.go new file mode 100644 index 000000000..289ccfb86 --- /dev/null +++ b/internal/third/log_test.go @@ -0,0 +1,39 @@ +package third + +import ( + "testing" +) + +func TestLogMatch(t *testing.T) { + + filenames := []string{ + "log1.txt", + "log2.log", + "log3.log.txt", + "log4.log.2022-01-01", + "log5.log.2022-01-01.txt", + "log20230918.log", + "OpenIM.CronTask.log.all.2023-09-18", "OpenIM.log.all.2023-09-18", + } + + expected := []string{ + "OpenIM.CronTask.log.all.2023-09-18", "OpenIM.log.all.2023-09-18", + } + + var actual []string + for _, filename := range filenames { + if checkLogPath(filename) { + actual = append(actual, filename) + } + } + + if len(actual) != len(expected) { + t.Errorf("Expected %d matches, but got %d", len(expected), len(actual)) + } + + for i := range expected { + if actual[i] != expected[i] { + t.Errorf("Expected match %d to be %q, but got %q", i, expected[i], actual[i]) + } + } +} diff --git a/internal/third/push.go b/internal/third/push.go index 5317e4cf9..c2396b51c 100644 --- a/internal/third/push.go +++ b/internal/third/push.go @@ -17,21 +17,25 @@ package third import ( "github.com/openimsdk/openim-sdk-core/v3/open_im_sdk_callback" + "github.com/openimsdk/openim-sdk-core/v3/internal/file" "github.com/openimsdk/openim-sdk-core/v3/pkg/log" "github.com/openimsdk/openim-sdk-core/v3/pkg/sdk_params_callback" "github.com/openimsdk/openim-sdk-core/v3/pkg/server_api_params" ) -type Push struct { - platformID int32 - loginUserID string +type Third struct { + platformID int32 + loginUserID string + version string + LogFilePath string + fileUploader *file.File } -func NewPush(platformID int32, loginUserID string) *Push { - return &Push{platformID: platformID, loginUserID: loginUserID} +func NewThird(platformID int32, loginUserID, version, LogFilePath string, fileUploader *file.File) *Third { + return &Third{platformID: platformID, loginUserID: loginUserID, version: version, LogFilePath: LogFilePath, fileUploader: fileUploader} } -func (c *Push) UpdateFcmToken(callback open_im_sdk_callback.Base, fcmToken, operationID string) { +func (c *Third) UpdateFcmToken(callback open_im_sdk_callback.Base, fcmToken, operationID string) { if callback == nil { return } @@ -44,14 +48,14 @@ func (c *Push) UpdateFcmToken(callback open_im_sdk_callback.Base, fcmToken, oper } -func (c *Push) fmcUpdateToken(callback open_im_sdk_callback.Base, fcmToken, operationID string) { +func (c *Third) fmcUpdateToken(callback open_im_sdk_callback.Base, fcmToken, operationID string) { apiReq := server_api_params.FcmUpdateTokenReq{} apiReq.OperationID = operationID apiReq.Platform = int(c.platformID) apiReq.FcmToken = fcmToken //c.p.PostFatalCallback(callback, constant.FcmUpdateTokenRouter, apiReq, nil, apiReq.OperationID) } -func (c *Push) SetAppBadge(callback open_im_sdk_callback.Base, appUnreadCount int32, operationID string) { +func (c *Third) SetAppBadge(callback open_im_sdk_callback.Base, appUnreadCount int32, operationID string) { if callback == nil { return } @@ -62,7 +66,7 @@ func (c *Push) SetAppBadge(callback open_im_sdk_callback.Base, appUnreadCount in log.NewInfo(operationID, "SetAppBadge callback: ", sdk_params_callback.SetAppBadgeCallback) }() } -func (c *Push) setAppBadge(callback open_im_sdk_callback.Base, appUnreadCount int32, operationID string) { +func (c *Third) setAppBadge(callback open_im_sdk_callback.Base, appUnreadCount int32, operationID string) { apiReq := server_api_params.SetAppBadgeReq{} apiReq.OperationID = operationID apiReq.FromUserID = c.loginUserID diff --git a/open_im_sdk/third.go b/open_im_sdk/third.go index 42d26c6fe..3693ab315 100644 --- a/open_im_sdk/third.go +++ b/open_im_sdk/third.go @@ -19,9 +19,13 @@ import ( ) func UpdateFcmToken(callback open_im_sdk_callback.Base, operationID, userIDList string) { - call(callback, operationID, UserForSDK.Push().UpdateFcmToken, userIDList) + call(callback, operationID, UserForSDK.Third().UpdateFcmToken, userIDList) } func SetAppBadge(callback open_im_sdk_callback.Base, operationID string, appUnreadCount int32) { - call(callback, operationID, UserForSDK.Push().SetAppBadge, appUnreadCount) + call(callback, operationID, UserForSDK.Third().SetAppBadge, appUnreadCount) +} + +func UploadLogs(callback open_im_sdk_callback.Base, operationID string, uploadlogParams string) { + call(callback, operationID, UserForSDK.Third().UploadLogs, uploadlogParams) } diff --git a/pkg/ccontext/context.go b/pkg/ccontext/context.go index 26bd672ea..f7729e024 100644 --- a/pkg/ccontext/context.go +++ b/pkg/ccontext/context.go @@ -16,6 +16,7 @@ package ccontext import ( "context" + "github.com/openimsdk/openim-sdk-core/v3/open_im_sdk_callback" "github.com/openimsdk/openim-sdk-core/v3/sdk_struct" diff --git a/pkg/ccontext/context_test.go b/pkg/ccontext/context_test.go index 85aa874b0..704d24eaf 100644 --- a/pkg/ccontext/context_test.go +++ b/pkg/ccontext/context_test.go @@ -34,6 +34,7 @@ func TestName(t *testing.T) { if Info(ctx).UserID() != conf.UserID { t.Fatal("UserID not match") } + if Info(ctx).OperationID() != operationID { t.Fatal("OperationID not match") } diff --git a/pkg/constant/server_api_router.go b/pkg/constant/server_api_router.go index e4f8e6d02..3f1e41a12 100644 --- a/pkg/constant/server_api_router.go +++ b/pkg/constant/server_api_router.go @@ -120,6 +120,7 @@ const ( // third FcmUpdateTokenRouter = RouterThird + "/fcm_update_token" SetAppBadgeRouter = RouterThird + "/set_app_badge" + UploadLogsRouter = RouterThird + "/logs/upload" // auth GetUsersToken = RouterAuth + "/user_token" diff --git a/pkg/sdk_params_callback/third_sdk_struct.go b/pkg/sdk_params_callback/third_sdk_struct.go old mode 100644 new mode 100755 index 7abf9a64c..327d76c84 --- a/pkg/sdk_params_callback/third_sdk_struct.go +++ b/pkg/sdk_params_callback/third_sdk_struct.go @@ -14,7 +14,14 @@ package sdk_params_callback -import "github.com/openimsdk/openim-sdk-core/v3/pkg/constant" +import ( + "github.com/openimsdk/openim-sdk-core/v3/pkg/constant" +) const UpdateFcmTokenCallback = constant.SuccessCallbackDefault const SetAppBadgeCallback = constant.SuccessCallbackDefault + +type UploadLogParams struct { + SystemType string `json:"system_type"` + Ex string `json:"ex"` +}