Skip to content

Commit

Permalink
feat: support upload logs (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
CNCSMonster authored Sep 25, 2023
1 parent c9b0578 commit fd0f614
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
3 changes: 2 additions & 1 deletion internal/group/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
13 changes: 7 additions & 6 deletions internal/login/init_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
71 changes: 71 additions & 0 deletions internal/third/log.go
Original file line number Diff line number Diff line change
@@ -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
}
39 changes: 39 additions & 0 deletions internal/third/log_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
}
}
22 changes: 13 additions & 9 deletions internal/third/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions open_im_sdk/third.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions pkg/ccontext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions pkg/ccontext/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
1 change: 1 addition & 0 deletions pkg/constant/server_api_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 8 additions & 1 deletion pkg/sdk_params_callback/third_sdk_struct.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit fd0f614

Please sign in to comment.