refactor: 引入日志切割库、indexApi拆分等

This commit is contained in:
meilin.huang
2024-01-23 19:30:28 +08:00
parent d530365ef9
commit e4d13f3377
43 changed files with 324 additions and 241 deletions

View File

@@ -0,0 +1,23 @@
package api
import (
"mayfly-go/internal/common/consts"
"mayfly-go/internal/db/application"
tagapp "mayfly-go/internal/tag/application"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
)
type Dashbord struct {
TagTreeApp tagapp.TagTree `inject:""`
DbApp application.Db `inject:""`
}
func (m *Dashbord) Dashbord(rc *req.Ctx) {
accountId := rc.GetLoginAccount().Id
dbNum := len(m.TagTreeApp.GetAccountResourceCodes(accountId, consts.TagResourceTypeDb, ""))
rc.ResData = collx.M{
"dbNum": dbNum,
}
}

View File

@@ -43,9 +43,9 @@ type Db interface {
type dbAppImpl struct {
base.AppImpl[*entity.Db, repository.Db]
DbSqlRepo repository.DbSql `inject:""`
DbInstanceApp Instance `inject:""`
TagApp tagapp.TagTree `inject:"TagTreeApp"`
dbSqlRepo repository.DbSql `inject:"DbSqlRepo"`
dbInstanceApp Instance `inject:"DbInstanceApp"`
tagApp tagapp.TagTree `inject:"TagTreeApp"`
}
// 注入DbRepo
@@ -78,7 +78,7 @@ func (d *dbAppImpl) SaveDb(ctx context.Context, dbEntity *entity.Db, tagIds ...u
return d.Tx(ctx, func(ctx context.Context) error {
return d.Insert(ctx, dbEntity)
}, func(ctx context.Context) error {
return d.TagApp.RelateResource(ctx, resouceCode, consts.TagResourceTypeDb, tagIds)
return d.tagApp.RelateResource(ctx, resouceCode, consts.TagResourceTypeDb, tagIds)
})
}
@@ -106,13 +106,13 @@ func (d *dbAppImpl) SaveDb(ctx context.Context, dbEntity *entity.Db, tagIds ...u
for _, v := range delDb {
// 删除该库关联的所有sql记录
d.DbSqlRepo.DeleteByCond(ctx, &entity.DbSql{DbId: dbId, Db: v})
d.dbSqlRepo.DeleteByCond(ctx, &entity.DbSql{DbId: dbId, Db: v})
}
return d.Tx(ctx, func(ctx context.Context) error {
return d.UpdateById(ctx, dbEntity)
}, func(ctx context.Context) error {
return d.TagApp.RelateResource(ctx, old.Code, consts.TagResourceTypeDb, tagIds)
return d.tagApp.RelateResource(ctx, old.Code, consts.TagResourceTypeDb, tagIds)
})
}
@@ -133,10 +133,10 @@ func (d *dbAppImpl) Delete(ctx context.Context, id uint64) error {
},
func(ctx context.Context) error {
// 删除该库下用户保存的所有sql信息
return d.DbSqlRepo.DeleteByCond(ctx, &entity.DbSql{DbId: id})
return d.dbSqlRepo.DeleteByCond(ctx, &entity.DbSql{DbId: id})
}, func(ctx context.Context) error {
var tagIds []uint64
return d.TagApp.RelateResource(ctx, db.Code, consts.TagResourceTypeDb, tagIds)
return d.tagApp.RelateResource(ctx, db.Code, consts.TagResourceTypeDb, tagIds)
})
}
@@ -147,7 +147,7 @@ func (d *dbAppImpl) GetDbConn(dbId uint64, dbName string) (*dbi.DbConn, error) {
return nil, errorx.NewBiz("数据库信息不存在")
}
instance, err := d.DbInstanceApp.GetById(new(entity.DbInstance), db.InstanceId)
instance, err := d.dbInstanceApp.GetById(new(entity.DbInstance), db.InstanceId)
if err != nil {
return nil, errorx.NewBiz("数据库实例不存在")
}
@@ -168,7 +168,7 @@ func (d *dbAppImpl) GetDbConn(dbId uint64, dbName string) (*dbi.DbConn, error) {
if err := instance.PwdDecrypt(); err != nil {
return nil, errorx.NewBiz(err.Error())
}
return toDbInfo(instance, dbId, dbName, d.TagApp.ListTagPathByResource(consts.TagResourceTypeDb, db.Code)...), nil
return toDbInfo(instance, dbId, dbName, d.tagApp.ListTagPathByResource(consts.TagResourceTypeDb, db.Code)...), nil
})
}

View File

@@ -3,16 +3,15 @@ package application
import (
"context"
"encoding/binary"
"github.com/google/uuid"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/pkg/model"
"github.com/google/uuid"
)
type DbBackupApp struct {
dbApp Db `inject:"DbApp"`
scheduler *dbScheduler `inject:"DbScheduler"`
instanceRepo repository.Instance `inject:"DbInstanceRepo"`
backupRepo repository.DbBackup `inject:"DbBackupRepo"`
backupHistoryRepo repository.DbBackupHistory `inject:"DbBackupHistoryRepo"`
}

View File

@@ -11,15 +11,13 @@ import (
)
type DbBinlogApp struct {
dbApp Db `inject:"DbApp"`
scheduler *dbScheduler `inject:"DbScheduler"`
binlogRepo repository.DbBinlog `inject:"DbBinlogRepo"`
binlogHistoryRepo repository.DbBinlogHistory `inject:"DbBinlogHistoryRepo"`
backupRepo repository.DbBackup `inject:"DbBackupRepo"`
backupHistoryRepo repository.DbBackupHistory `inject:"DbBackupHistoryRepo"`
context context.Context
cancel context.CancelFunc
waitGroup sync.WaitGroup
scheduler *dbScheduler `inject:"DbScheduler"`
binlogRepo repository.DbBinlog `inject:"DbBinlogRepo"`
backupRepo repository.DbBackup `inject:"DbBackupRepo"`
context context.Context
cancel context.CancelFunc
waitGroup sync.WaitGroup
}
func newDbBinlogApp() *DbBinlogApp {

View File

@@ -41,7 +41,7 @@ type DataSyncTask interface {
type dataSyncAppImpl struct {
base.AppImpl[*entity.DataSyncTask, repository.DataSyncTask]
DbDataSyncLogRepo repository.DataSyncLog `inject:""`
dbDataSyncLogRepo repository.DataSyncLog `inject:"DbDataSyncLogRepo"`
}
func (d *dataSyncAppImpl) InjectDbDataSyncTaskRepo(repo repository.DataSyncTask) {
@@ -325,7 +325,7 @@ func (app *dataSyncAppImpl) endRunning(taskEntity *entity.DataSyncTask, log *ent
}
func (app *dataSyncAppImpl) saveLog(log *entity.DataSyncLog) {
app.DbDataSyncLogRepo.Save(context.Background(), log)
app.dbDataSyncLogRepo.Save(context.Background(), log)
}
func (app *dataSyncAppImpl) InitCronJob() {
@@ -371,5 +371,5 @@ func (app *dataSyncAppImpl) InitCronJob() {
}
func (app *dataSyncAppImpl) GetTaskLogList(condition *entity.DataSyncLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return app.DbDataSyncLogRepo.GetTaskLogList(condition, pageParam, toEntity, orderBy...)
return app.dbDataSyncLogRepo.GetTaskLogList(condition, pageParam, toEntity, orderBy...)
}

View File

@@ -8,13 +8,9 @@ import (
)
type DbRestoreApp struct {
dbApp Db `inject:"DbApp"`
scheduler *dbScheduler `inject:"DbScheduler"`
instanceRepo repository.Instance `inject:"DbInstanceRepo"`
backupHistoryRepo repository.DbBackupHistory `inject:"DbBackupHistoryRepo"`
restoreRepo repository.DbRestore `inject:"DbRestoreRepo"`
restoreHistoryRepo repository.DbRestoreHistory `inject:"DbRestoreHistoryRepo"`
binlogHistoryRepo repository.DbBinlogHistory `inject:"DbBinlogHistoryRepo"`
}
func (app *DbRestoreApp) Init() error {

View File

@@ -28,7 +28,6 @@ type dbScheduler struct {
restoreHistoryRepo repository.DbRestoreHistory `inject:"DbRestoreHistoryRepo"`
binlogRepo repository.DbBinlog `inject:"DbBinlogRepo"`
binlogHistoryRepo repository.DbBinlogHistory `inject:"DbBinlogHistoryRepo"`
binlogTimes map[uint64]time.Time
}
func newDbScheduler() *dbScheduler {
@@ -53,7 +52,7 @@ func (s *dbScheduler) repo(typ entity.DbJobType) repository.DbJob {
case entity.DbJobTypeBinlog:
return s.binlogRepo
default:
panic(errors.New(fmt.Sprintf("无效的数据库任务类型: %v", typ)))
panic(fmt.Errorf("无效的数据库任务类型: %v", typ))
}
}
@@ -281,7 +280,7 @@ func (s *dbScheduler) runJob(ctx context.Context, job entity.DbJob) {
case entity.DbJobTypeBinlog:
errRun = s.fetchBinlogMysql(ctx, job)
default:
errRun = errors.New(fmt.Sprintf("无效的数据库任务类型: %v", typ))
errRun = fmt.Errorf("无效的数据库任务类型: %v", typ)
}
status := entity.DbJobSuccess
if errRun != nil {

View File

@@ -57,7 +57,7 @@ type DbSqlExec interface {
}
type dbSqlExecAppImpl struct {
DbSqlExecRepo repository.DbSqlExec `inject:""`
dbSqlExecRepo repository.DbSqlExec `inject:"DbSqlExecRepo"`
}
func createSqlExecRecord(ctx context.Context, execSqlReq *DbSqlExecReq) *entity.DbSqlExec {
@@ -138,23 +138,23 @@ func (d *dbSqlExecAppImpl) Exec(ctx context.Context, execSqlReq *DbSqlExecReq) (
// 保存sql执行记录如果是查询类则根据系统配置判断是否保存
func (d *dbSqlExecAppImpl) saveSqlExecLog(isQuery bool, dbSqlExecRecord *entity.DbSqlExec) {
if !isQuery {
d.DbSqlExecRepo.Insert(context.TODO(), dbSqlExecRecord)
d.dbSqlExecRepo.Insert(context.TODO(), dbSqlExecRecord)
return
}
if config.GetDbSaveQuerySql() {
dbSqlExecRecord.Table = "-"
dbSqlExecRecord.OldValue = "-"
dbSqlExecRecord.Type = entity.DbSqlExecTypeQuery
d.DbSqlExecRepo.Insert(context.TODO(), dbSqlExecRecord)
d.dbSqlExecRepo.Insert(context.TODO(), dbSqlExecRecord)
}
}
func (d *dbSqlExecAppImpl) DeleteBy(ctx context.Context, condition *entity.DbSqlExec) {
d.DbSqlExecRepo.DeleteByCond(ctx, condition)
d.dbSqlExecRepo.DeleteByCond(ctx, condition)
}
func (d *dbSqlExecAppImpl) GetPageList(condition *entity.DbSqlExecQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return d.DbSqlExecRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
return d.dbSqlExecRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
}
func doSelect(ctx context.Context, selectStmt *sqlparser.Select, execSqlReq *DbSqlExecReq) (*DbSqlExecRes, error) {

View File

@@ -15,7 +15,12 @@ func InitDbRouter(router *gin.RouterGroup) {
d := new(api.Db)
biz.ErrIsNil(ioc.Inject(d))
dashbord := new(api.Dashbord)
biz.ErrIsNil(ioc.Inject(dashbord))
reqs := [...]*req.Conf{
req.NewGet("dashbord", dashbord.Dashbord),
// 获取数据库列表
req.NewGet("", d.Dbs),