Feature: add log support for developer (#1101)
* fix: to start im or chat, ZooKeeper must be started first. * fix: msg gateway start output err info Signed-off-by: Gordon <1432970085@qq.com> * fix: msg gateway start output err info Signed-off-by: Gordon <1432970085@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * fix: go mod update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: get all userID Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: msggateway add online status call Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: log change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: log change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * chore: network mode change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * feat: add api of get server time Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * feat: remove go work sum Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: pull message add isRead field Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: check msg-transfer script Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: start don't kill old process Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: check component Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: pull message set isRead only message come from single. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: add ex field to update group info. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * cicd: robot automated Change * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: to start im or chat, ZooKeeper must be started first. --------- Signed-off-by: Gordon <1432970085@qq.com> Signed-off-by: withchao <993506633@qq.com> Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: withchao <993506633@qq.com> Co-authored-by: Xinwei Xiong <3293172751NSS@gmail.com> Co-authored-by: FGadvancer <FGadvancer@users.noreply.github.com>pull/1102/head
parent
7a3c3d7939
commit
bba662f404
@ -0,0 +1,145 @@
|
|||||||
|
package third
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/OpenIMSDK/protocol/constant"
|
||||||
|
"github.com/OpenIMSDK/protocol/third"
|
||||||
|
"github.com/OpenIMSDK/tools/errs"
|
||||||
|
"github.com/OpenIMSDK/tools/utils"
|
||||||
|
utils2 "github.com/OpenIMSDK/tools/utils"
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/authverify"
|
||||||
|
relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
||||||
|
)
|
||||||
|
|
||||||
|
func genLogID() string {
|
||||||
|
const dataLen = 10
|
||||||
|
data := make([]byte, dataLen)
|
||||||
|
rand.Read(data)
|
||||||
|
chars := []byte("0123456789")
|
||||||
|
for i := 0; i < len(data); i++ {
|
||||||
|
if i == 0 {
|
||||||
|
data[i] = chars[1:][data[i]%9]
|
||||||
|
} else {
|
||||||
|
data[i] = chars[data[i]%10]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *thirdServer) UploadLogs(ctx context.Context, req *third.UploadLogsReq) (*third.UploadLogsResp, error) {
|
||||||
|
var DBlogs []*relationtb.Log
|
||||||
|
userID := ctx.Value(constant.OpUserID).(string)
|
||||||
|
platform := constant.PlatformID2Name[int(req.Platform)]
|
||||||
|
for _, fileURL := range req.FileURLs {
|
||||||
|
log := relationtb.Log{
|
||||||
|
Version: req.Version,
|
||||||
|
SystemType: req.SystemType,
|
||||||
|
Platform: platform,
|
||||||
|
UserID: userID,
|
||||||
|
CreateTime: time.Now(),
|
||||||
|
Url: fileURL.URL,
|
||||||
|
FileName: fileURL.Filename,
|
||||||
|
}
|
||||||
|
for i := 0; i < 20; i++ {
|
||||||
|
id := genLogID()
|
||||||
|
logs, err := t.thirdDatabase.GetLogs(ctx, []string{id}, "")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(logs) == 0 {
|
||||||
|
log.LogID = id
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if log.LogID == "" {
|
||||||
|
return nil, errs.ErrData.Wrap("Log id gen error")
|
||||||
|
}
|
||||||
|
DBlogs = append(DBlogs, &log)
|
||||||
|
}
|
||||||
|
err := t.thirdDatabase.UploadLogs(ctx, DBlogs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &third.UploadLogsResp{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *thirdServer) DeleteLogs(ctx context.Context, req *third.DeleteLogsReq) (*third.DeleteLogsResp, error) {
|
||||||
|
|
||||||
|
if err := authverify.CheckAdmin(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
userID := ""
|
||||||
|
logs, err := t.thirdDatabase.GetLogs(ctx, req.LogIDs, userID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var logIDs []string
|
||||||
|
for _, log := range logs {
|
||||||
|
logIDs = append(logIDs, log.LogID)
|
||||||
|
}
|
||||||
|
if ids := utils2.Single(req.LogIDs, logIDs); len(ids) > 0 {
|
||||||
|
return nil, errs.ErrRecordNotFound.Wrap(fmt.Sprintf("logIDs not found%#v", ids))
|
||||||
|
}
|
||||||
|
err = t.thirdDatabase.DeleteLogs(ctx, req.LogIDs, userID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &third.DeleteLogsResp{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func dbToPbLogInfos(logs []*relationtb.Log) []*third.LogInfo {
|
||||||
|
db2pbForLogInfo := func(log *relationtb.Log) *third.LogInfo {
|
||||||
|
return &third.LogInfo{
|
||||||
|
Filename: log.FileName,
|
||||||
|
UserID: log.UserID,
|
||||||
|
Platform: utils.StringToInt32(log.Platform),
|
||||||
|
Url: log.Url,
|
||||||
|
CreateTime: log.CreateTime.UnixMilli(),
|
||||||
|
LogID: log.LogID,
|
||||||
|
SystemType: log.SystemType,
|
||||||
|
Version: log.Version,
|
||||||
|
Ex: log.Ex,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return utils.Slice(logs, db2pbForLogInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *thirdServer) SearchLogs(ctx context.Context, req *third.SearchLogsReq) (*third.SearchLogsResp, error) {
|
||||||
|
if err := authverify.CheckAdmin(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var (
|
||||||
|
resp third.SearchLogsResp
|
||||||
|
userIDs []string
|
||||||
|
)
|
||||||
|
if req.StartTime > req.EndTime {
|
||||||
|
return nil, errs.ErrArgs.Wrap("startTime>endTime")
|
||||||
|
}
|
||||||
|
total, logs, err := t.thirdDatabase.SearchLogs(ctx, req.Keyword, time.UnixMilli(req.StartTime), time.UnixMilli(req.EndTime), req.Pagination.PageNumber, req.Pagination.ShowNumber)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
pbLogs := dbToPbLogInfos(logs)
|
||||||
|
for _, log := range logs {
|
||||||
|
userIDs = append(userIDs, log.UserID)
|
||||||
|
}
|
||||||
|
users, err := t.thirdDatabase.FindUsers(ctx, userIDs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
IDtoName := make(map[string]string)
|
||||||
|
for _, user := range users {
|
||||||
|
IDtoName[user.UserID] = user.Nickname
|
||||||
|
}
|
||||||
|
for _, pbLog := range pbLogs {
|
||||||
|
pbLog.Nickname = IDtoName[pbLog.UserID]
|
||||||
|
}
|
||||||
|
resp.LogsInfos = pbLogs
|
||||||
|
resp.Total = total
|
||||||
|
return &resp, nil
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package relation
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/OpenIMSDK/tools/errs"
|
||||||
|
"github.com/OpenIMSDK/tools/ormutil"
|
||||||
|
relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LogGorm struct {
|
||||||
|
db *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LogGorm) Create(ctx context.Context, log []*relationtb.Log) error {
|
||||||
|
return errs.Wrap(l.db.WithContext(ctx).Create(log).Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LogGorm) Search(ctx context.Context, keyword string, start time.Time, end time.Time, pageNumber int32, showNumber int32) (uint32, []*relationtb.Log, error) {
|
||||||
|
db := l.db.WithContext(ctx).Where("create_time >= ?", start)
|
||||||
|
if end.UnixMilli() != 0 {
|
||||||
|
db = l.db.WithContext(ctx).Where("create_time <= ?", end)
|
||||||
|
}
|
||||||
|
return ormutil.GormSearch[relationtb.Log](db, []string{"user_id"}, keyword, pageNumber, showNumber)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LogGorm) Delete(ctx context.Context, logIDs []string, userID string) error {
|
||||||
|
if userID == "" {
|
||||||
|
return errs.Wrap(l.db.WithContext(ctx).Where("log_id in ?", logIDs).Delete(&relationtb.Log{}).Error)
|
||||||
|
}
|
||||||
|
return errs.Wrap(l.db.WithContext(ctx).Where("log_id in ? and user_id=?", logIDs, userID).Delete(&relationtb.Log{}).Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LogGorm) Get(ctx context.Context, logIDs []string, userID string) ([]*relationtb.Log, error) {
|
||||||
|
var logs []*relationtb.Log
|
||||||
|
if userID == "" {
|
||||||
|
return logs, errs.Wrap(l.db.WithContext(ctx).Where("log_id in ?", logIDs).Find(&logs).Error)
|
||||||
|
}
|
||||||
|
return logs, errs.Wrap(l.db.WithContext(ctx).Where("log_id in ? and user_id=?", logIDs, userID).Find(&logs).Error)
|
||||||
|
}
|
||||||
|
func NewLogGorm(db *gorm.DB) relationtb.LogInterface {
|
||||||
|
db.AutoMigrate(&relationtb.Log{})
|
||||||
|
return &LogGorm{db: db}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package relation
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Log struct {
|
||||||
|
LogID string `gorm:"column:log_id;primary_key;type:char(64)"`
|
||||||
|
Platform string `gorm:"column:platform;type:varchar(32)"`
|
||||||
|
UserID string `gorm:"column:user_id;type:char(64)"`
|
||||||
|
CreateTime time.Time `gorm:"index:,sort:desc"`
|
||||||
|
Url string `gorm:"column:url;type varchar(255)"`
|
||||||
|
FileName string `gorm:"column:filename;type varchar(255)"`
|
||||||
|
SystemType string `gorm:"column:system_type;type varchar(255)"`
|
||||||
|
Version string `gorm:"column:version;type varchar(255)"`
|
||||||
|
Ex string `gorm:"column:ex;type varchar(255)"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type LogInterface interface {
|
||||||
|
Create(ctx context.Context, log []*Log) error
|
||||||
|
Search(ctx context.Context, keyword string, start time.Time, end time.Time, pageNumber int32, showNumber int32) (uint32, []*Log, error)
|
||||||
|
Delete(ctx context.Context, logID []string, userID string) error
|
||||||
|
Get(ctx context.Context, logIDs []string, userID string) ([]*Log, error)
|
||||||
|
}
|
Loading…
Reference in new issue