Skip to content

Commit 5e29151

Browse files
committed
feat:support export api impl
1 parent 1c4bc31 commit 5e29151

7 files changed

Lines changed: 80 additions & 2 deletions

File tree

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/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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
}

0 commit comments

Comments
 (0)