Skip to content

Commit 0fb9507

Browse files
authored
Merge pull request #258 from actiontech/issue-256-9
Issue 256 9
2 parents 91b0847 + 58a4684 commit 0fb9507

File tree

10 files changed

+177
-21
lines changed

10 files changed

+177
-21
lines changed

internal/apiserver/service/dms_controller.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2586,7 +2586,28 @@ func (d *DMSController) ListCBOperationLogs(c echo.Context) error {
25862586
// 200: ExportCBOperationLogsReply
25872587
// default: body:GenericResp
25882588
func (d *DMSController) ExportCBOperationLogs(c echo.Context) error {
2589-
return nil
2589+
req := &aV1.ExportCBOperationLogsReq{}
2590+
err := bindAndValidateReq(c, req)
2591+
if nil != err {
2592+
return NewErrResp(c, err, apiError.BadRequestErr)
2593+
}
2594+
2595+
currentUserUid, err := jwt.GetUserUidStrFromContext(c)
2596+
if err != nil {
2597+
return NewErrResp(c, err, apiError.DMSServiceErr)
2598+
}
2599+
2600+
content, err := d.DMS.ExportCBOperationLogs(c.Request().Context(), req, currentUserUid)
2601+
if err != nil {
2602+
return NewErrResp(c, err, apiError.APIServerErr)
2603+
}
2604+
2605+
fileName := fmt.Sprintf("CBoperation_%s.csv", time.Now().Format("20060102150405.000"))
2606+
c.Response().Header().Set(echo.HeaderContentDisposition,
2607+
mime.FormatMediaType("attachment", map[string]string{"filename": fileName}))
2608+
2609+
return c.Blob(http.StatusOK, "text/csv", content)
2610+
25902611
}
25912612

25922613
// swagger:route GET /v1/dms/projects/{project_uid}/cb_operation_logs/tips dms GetCBOperationLogTips

internal/dms/biz/cb_operation_log.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type CbOperationLogType string
1212

1313
const (
1414
CbOperationLogTypeSql CbOperationLogType = "SQL"
15+
16+
CbExecOpSuccess = "Success"
1517
)
1618

1719
// CbOperationLogRepo 定义操作日志的存储接口
@@ -21,6 +23,7 @@ type CbOperationLogRepo interface {
2123
UpdateCbOperationLog(ctx context.Context, log *CbOperationLog) error
2224
ListCbOperationLogs(ctx context.Context, opt *ListCbOperationLogOption) ([]*CbOperationLog, int64, error)
2325
CleanCbOperationLogOpTimeBefore(ctx context.Context, t time.Time) (int64, error)
26+
CountOperationLogs(ctx context.Context, opt *ListCbOperationLogOption) (int64, error)
2427
}
2528

2629
// CbOperationLog 代表操作日志记录
@@ -42,6 +45,7 @@ type CbOperationLog struct {
4245

4346
User *User
4447
DbService *DBService
48+
Project *Project
4549
}
4650

4751
func (c CbOperationLog) GetOpTime() time.Time {
@@ -58,6 +62,28 @@ func (c CbOperationLog) GetSessionID() string {
5862
return ""
5963
}
6064

65+
func (c CbOperationLog) GetUserName() string {
66+
if c.User != nil {
67+
return c.User.Name
68+
}
69+
return ""
70+
}
71+
72+
func (c CbOperationLog) GetProjectName() string {
73+
if c.Project != nil {
74+
return c.Project.Name
75+
}
76+
return ""
77+
}
78+
79+
func (c CbOperationLog) GetDbServiceName() string {
80+
if c.DbService != nil {
81+
return c.DbService.Name
82+
}
83+
return ""
84+
85+
}
86+
6187
// ListCbOperationLogOption 用于查询操作日志的选项
6288
type ListCbOperationLogOption struct {
6389
PageNumber uint32

internal/dms/biz/cloudbeaver.go

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@ import (
1414
"strings"
1515
"sync"
1616

17+
"github.com/99designs/gqlgen/graphql"
18+
"github.com/99designs/gqlgen/graphql/executor"
1719
"github.com/actiontech/dms/internal/dms/pkg/constant"
1820
"github.com/actiontech/dms/internal/pkg/cloudbeaver"
1921
"github.com/actiontech/dms/internal/pkg/cloudbeaver/model"
2022
"github.com/actiontech/dms/internal/pkg/cloudbeaver/resolver"
21-
2223
"github.com/actiontech/dms/pkg/dms-common/api/jwt"
2324
"github.com/actiontech/dms/pkg/dms-common/pkg/aes"
24-
25-
"github.com/99designs/gqlgen/graphql"
26-
"github.com/99designs/gqlgen/graphql/executor"
2725
utilLog "github.com/actiontech/dms/pkg/dms-common/pkg/log"
2826
"github.com/labstack/echo/v4"
2927
)
@@ -334,19 +332,13 @@ func (cu *CloudbeaverUsecase) GraphQLDistributor() echo.MiddlewareFunc {
334332
ctx := graphql.StartOperationTrace(context.Background())
335333

336334
var dbService *DBService
337-
338335
if params.OperationName == "asyncSqlExecuteQuery" {
339336
dbService, err = cu.getDbService(c.Request().Context(), params)
340337
if err != nil {
341338
cu.log.Error(err)
342339
return err
343340
}
344341

345-
err = cu.SaveCbOpLog(c, dbService, params, next)
346-
if err != nil {
347-
return err
348-
}
349-
350342
if !cu.isEnableSQLAudit(dbService) {
351343
cloudbeaverResBuf := new(bytes.Buffer)
352344
mw := io.MultiWriter(c.Response().Writer, cloudbeaverResBuf)
@@ -378,6 +370,70 @@ func (cu *CloudbeaverUsecase) GraphQLDistributor() echo.MiddlewareFunc {
378370
ctx = context.WithValue(ctx, cloudbeaver.SQLEDirectAudit, directAuditReq)
379371
}
380372

373+
if params.OperationName == "updateResultsDataBatch" {
374+
cloudbeaverResBuf := new(bytes.Buffer)
375+
mw := io.MultiWriter(c.Response().Writer, cloudbeaverResBuf)
376+
writer := &cloudbeaverResponseWriter{Writer: mw, ResponseWriter: c.Response().Writer}
377+
c.Response().Writer = writer
378+
379+
if err = next(c); err != nil {
380+
return err
381+
}
382+
383+
if err := cu.SaveUiOp(c, cloudbeaverResBuf, params); err != nil {
384+
cu.log.Errorf("save ui op err: %v", err)
385+
return nil
386+
}
387+
388+
return nil
389+
}
390+
391+
if params.OperationName == "getAsyncTaskInfo" {
392+
cloudbeaverResBuf := new(bytes.Buffer)
393+
mw := io.MultiWriter(c.Response().Writer, cloudbeaverResBuf)
394+
writer := &cloudbeaverResponseWriter{Writer: mw, ResponseWriter: c.Response().Writer}
395+
c.Response().Writer = writer
396+
397+
if err = next(c); err != nil {
398+
return err
399+
}
400+
401+
cbUid, exist := taskIDAssocUid.Load(params.Variables["taskId"])
402+
if !exist {
403+
return nil
404+
}
405+
cbUidStr, ok := cbUid.(string)
406+
if !ok {
407+
return nil
408+
}
409+
410+
operationLog, err := cu.cbOperationLogUsecase.GetCbOperationLogByID(ctx, cbUidStr)
411+
if err != nil {
412+
cu.log.Errorf("get cb operation log by id %s failed: %v", cbUidStr, err)
413+
return nil
414+
} else {
415+
var taskInfo TaskInfo
416+
if err := json.Unmarshal(cloudbeaverResBuf.Bytes(), &taskInfo); err != nil {
417+
cu.log.Errorf("extract task id err: %v", err)
418+
return nil
419+
}
420+
421+
task := taskInfo.Data.TaskInfo
422+
if task.Running == true || task.Error == nil {
423+
return nil
424+
}
425+
426+
operationLog.ExecResult = *task.Error.Message
427+
err := cu.cbOperationLogUsecase.UpdateCbOperationLog(ctx, operationLog)
428+
if err != nil {
429+
cu.log.Error(err)
430+
return nil
431+
}
432+
}
433+
434+
return nil
435+
}
436+
381437
if params.OperationName == "getSqlExecuteTaskResults" {
382438
cloudbeaverResBuf := new(bytes.Buffer)
383439
mw := io.MultiWriter(c.Response().Writer, cloudbeaverResBuf)
@@ -418,9 +474,14 @@ func (cu *CloudbeaverUsecase) GraphQLDistributor() echo.MiddlewareFunc {
418474
cloudbeaverNext = func(c echo.Context) ([]byte, error) {
419475
resp, ok = c.Get(cloudbeaver.AuditResultKey).(cloudbeaver.AuditResults)
420476
if ok {
421-
cu.UpdateCbOp(params, ctx, resp)
422477
if !resp.IsSuccess {
478+
cu.SaveCbOperationLogWithoutNext(c, dbService, params, resp)
423479
return nil, c.JSON(http.StatusOK, convertToResp(resp))
480+
} else {
481+
err = cu.SaveCbOpLog(c, dbService, params, resp, next)
482+
if err != nil {
483+
return nil, nil
484+
}
424485
}
425486
}
426487

@@ -446,9 +507,16 @@ func (cu *CloudbeaverUsecase) GraphQLDistributor() echo.MiddlewareFunc {
446507
} else {
447508
cloudbeaverNext = func(c echo.Context) ([]byte, error) {
448509
resp, ok = c.Get(cloudbeaver.AuditResultKey).(cloudbeaver.AuditResults)
449-
cu.UpdateCbOp(params, ctx, resp)
450-
if !resp.IsSuccess {
451-
return nil, c.JSON(http.StatusOK, convertToResp(resp))
510+
if ok {
511+
if !resp.IsSuccess {
512+
cu.SaveCbOperationLogWithoutNext(c, dbService, params, resp)
513+
return nil, c.JSON(http.StatusOK, convertToResp(resp))
514+
} else {
515+
err = cu.SaveCbOpLog(c, dbService, params, resp, next)
516+
if err != nil {
517+
return nil, nil
518+
}
519+
}
452520
}
453521

454522
resWrite = &responseProcessWriter{tmp: &bytes.Buffer{}, ResponseWriter: c.Response().Writer}

internal/dms/biz/cloudbeaver_ce.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ func (cu *CloudbeaverUsecase) ResetDbServiceByAuth(ctx context.Context, activeDB
1515
return activeDBServices, nil
1616
}
1717

18-
func (cu *CloudbeaverUsecase) UpdateCbOp(params *graphql.RawParams, ctx context.Context, resp cloudbeaver.AuditResults) {
19-
return
18+
func (cu *CloudbeaverUsecase) UpdateCbOpResult(c echo.Context, cloudbeaverResBuf *bytes.Buffer, params *graphql.RawParams, ctx context.Context) error {
19+
return nil
2020
}
2121

22-
func (cu *CloudbeaverUsecase) UpdateCbOpResult(c echo.Context, cloudbeaverResBuf *bytes.Buffer, params *graphql.RawParams, ctx context.Context) error {
22+
func (cu *CloudbeaverUsecase) SaveCbOpLog(c echo.Context, dbService *DBService, params *graphql.RawParams, resp cloudbeaver.AuditResults, next echo.HandlerFunc) error {
2323
return nil
2424
}
2525

26-
func (cu *CloudbeaverUsecase) SaveCbOpLog(c echo.Context, dbService *DBService, params *graphql.RawParams, next echo.HandlerFunc) error {
26+
func (cu *CloudbeaverUsecase) SaveUiOp(c echo.Context, buf *bytes.Buffer, params *graphql.RawParams) error {
2727
return nil
2828
}
29+
30+
func (cu *CloudbeaverUsecase) SaveCbOperationLogWithoutNext(c echo.Context, dbService *DBService, params *graphql.RawParams, resp cloudbeaver.AuditResults) {
31+
return
32+
}

internal/dms/service/cb_operation_log.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ func (d *DMSService) ListCBOperationLogs(ctx context.Context, req *dmsV1.ListCBO
1313
func (d *DMSService) GetCBOperationLogTips(ctx context.Context, req *dmsV1.GetCBOperationLogTipsReq, uid string) (reply *dmsV1.GetCBOperationLogTipsReply, err error) {
1414
return d.getCBOperationLogTips(ctx, req, uid)
1515
}
16+
17+
func (d *DMSService) ExportCBOperationLogs(ctx context.Context, req *dmsV1.ExportCBOperationLogsReq, uid string) ([]byte, error) {
18+
return d.exportCbOperationLogs(ctx, req, uid)
19+
}

internal/dms/service/cb_operation_log_ce.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ func (d *DMSService) listCBOperationLogs(ctx context.Context, req *dmsV1.ListCBO
1818
func (d *DMSService) getCBOperationLogTips(ctx context.Context, req *dmsV1.GetCBOperationLogTipsReq, currentUid string) (*dmsV1.GetCBOperationLogTipsReply, error) {
1919
return nil, errNotSupportCBOperationLog
2020
}
21+
22+
func (d *DMSService) exportCbOperationLogs(ctx context.Context, req *dmsV1.ExportCBOperationLogsReq, uid string) ([]byte, error) {
23+
return nil, errNotSupportCBOperationLog
24+
}

internal/dms/storage/cb_operation_log.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (d *CbOperationLogRepo) UpdateCbOperationLog(ctx context.Context, operation
5050

5151
func (d *CbOperationLogRepo) GetCbOperationLogByID(ctx context.Context, uid string) (*biz.CbOperationLog, error) {
5252
var model model.CbOperationLog
53-
if err := d.db.WithContext(ctx).Preload("User").Preload("DbService").Where("uid = ?", uid).First(&model).Error; err != nil {
53+
if err := d.db.WithContext(ctx).Preload("Project").Preload("User").Preload("DbService").Where("uid = ?", uid).First(&model).Error; err != nil {
5454
return nil, fmt.Errorf("failed to get cb operation log by uid: %v", err)
5555
}
5656

@@ -69,7 +69,7 @@ func (d *CbOperationLogRepo) ListCbOperationLogs(ctx context.Context, opt *biz.L
6969
if err := transaction(d.log, ctx, d.db, func(tx *gorm.DB) error {
7070
// find models
7171
{
72-
db := tx.WithContext(ctx).Preload("User").Preload("DbService")
72+
db := tx.WithContext(ctx).Preload("Project").Preload("User").Preload("DbService")
7373
if opt.OrderBy != "" {
7474
db = db.Order(fmt.Sprintf("%s DESC", opt.OrderBy))
7575
}
@@ -116,3 +116,19 @@ func (d *CbOperationLogRepo) CleanCbOperationLogOpTimeBefore(ctx context.Context
116116
})
117117
return
118118
}
119+
120+
func (d *CbOperationLogRepo) CountOperationLogs(ctx context.Context, opt *biz.ListCbOperationLogOption) (int64, error) {
121+
var total int64
122+
if err := transaction(d.log, ctx, d.db, func(tx *gorm.DB) error {
123+
db := tx.WithContext(ctx).Model(&model.CbOperationLog{})
124+
db = gormWheres(ctx, db, opt.FilterBy)
125+
if err := db.Count(&total).Error; err != nil {
126+
return fmt.Errorf("failed to count cb operation logs: %v", err)
127+
}
128+
return nil
129+
}); err != nil {
130+
return 0, err
131+
}
132+
133+
return total, nil
134+
}

internal/dms/storage/convert.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,11 @@ func convertModelCbOperationLog(model *model.CbOperationLog) (*biz.CbOperationLo
11881188
return nil, err
11891189
}
11901190

1191+
project, err := convertModelProject(model.Project)
1192+
if err != nil {
1193+
return nil, err
1194+
}
1195+
11911196
return &biz.CbOperationLog{
11921197
UID: model.UID,
11931198
ProjectID: model.ProjectID,
@@ -1205,5 +1210,6 @@ func convertModelCbOperationLog(model *model.CbOperationLog) (*biz.CbOperationLo
12051210
ResultSetRowCount: model.ResultSetRowCount,
12061211
User: user,
12071212
DbService: dbService,
1213+
Project: project,
12081214
}, nil
12091215
}

internal/dms/storage/model/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,4 +531,5 @@ type CbOperationLog struct {
531531

532532
User *User `json:"user" gorm:"foreignKey:OpPersonUID"`
533533
DbService *DBService `json:"db_service" gorm:"foreignKey:DBServiceUID"`
534+
Project *Project `json:"project" gorm:"foreignKey:ProjectID"`
534535
}

internal/pkg/cloudbeaver/graphql.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,16 @@ var GraphQLHandlerRouters = map[string] /* gql operation name */ gqlBehavior{
264264
return err
265265
},
266266
},
267+
"getAsyncTaskInfo": {
268+
UseLocalHandler: true,
269+
},
267270
"getSqlExecuteTaskResults": {
268271
UseLocalHandler: true,
269272
NeedModifyRemoteRes: true,
270273
},
274+
"updateResultsDataBatch": {
275+
UseLocalHandler: true,
276+
},
271277
"getActiveUser": {
272278
UseLocalHandler: true,
273279
NeedModifyRemoteRes: true,

0 commit comments

Comments
 (0)