Repository: pingcap/tidb
File: pkg/domain/plan_replayer_dump.go (Go)

Code before:
		if err = dumpPlanReplayerExplain(ctx, sctx, zw, task, &records); err != nil {
			errMsgs = append(errMsgs, err.Error())
		}
	}

	if err = dumpDebugTrace(zw, task.DebugTrace); err != nil {
		return err
	}

	if len(errMsgs) > 0 {
		if err = dumpErrorMsgs(zw, errMsgs); err != nil {
			return err
		}
	}
	return nil
}

func generateRecords(ctx context.Context, task *PlanReplayerDumpTask) []PlanReplayerStatusRecord {
	records := make([]PlanReplayerStatusRecord, 0)
	if !task.IsCapture {
		if storage, err := extstore.GetGlobalExtStorage(ctx); err == nil {
			if url, err := storage.PresignFile(ctx, filepath.Join(replayer.GetPlanReplayerDirName(), task.FileName), 24*time.Hour); err == nil {
				task.PresignedURL = url
			}
		}
	}
	if len(task.ExecStmts) > 0 {
		for _, execStmt := range task.ExecStmts {
			records = append(records, PlanReplayerStatusRecord{
				SQLDigest:  task.SQLDigest,
				PlanDigest: task.PlanDigest,
				OriginSQL:  execStmt.Text(),
				Token:      task.FileName,
			})
		}
	}
	return records
}

func dumpSQLMeta(zw *zip.Writer, task *PlanReplayerDumpTask) error {
	cf, err := zw.Create(PlanReplayerSQLMetaFile)
	if err != nil {
		return errors.AddStack(err)
	}
	varMap := make(map[string]string)
	varMap[PlanReplayerSQLMetaStartTS] = strconv.FormatUint(task.StartTS, 10)
	varMap[PlanReplayerTaskMetaIsCapture] = strconv.FormatBool(task.IsCapture)
	varMap[PlanReplayerTaskMetaIsContinues] = strconv.FormatBool(task.IsContinuesCapture)
	varMap[PlanReplayerTaskMetaSQLDigest] = task.SQLDigest
	varMap[PlanReplayerTaskMetaPlanDigest] = task.PlanDigest
	varMap[PlanReplayerTaskEnableHistoricalStats] = strconv.FormatBool(vardef.EnableHistoricalStatsForCapture.Load())

Review comment (refactor):
duplicate with below one maybe encapsulate into a method, and log the returned error
```golang
func getPresignedURL(task) (string, error) {
```

Code after:
		if err = dumpPlanReplayerExplain(ctx, sctx, zw, task, &records); err != nil {
			errMsgs = append(errMsgs, err.Error())
		}
	}

	if err = dumpDebugTrace(zw, task.DebugTrace); err != nil {
		return err
	}

	if len(errMsgs) > 0 {
		if err = dumpErrorMsgs(zw, errMsgs); err != nil {
			return err
		}
	}
	return nil
}

func generateRecords(ctx context.Context, task *PlanReplayerDumpTask) []PlanReplayerStatusRecord {
	records := make([]PlanReplayerStatusRecord, 0)
	setTaskPresignedURL(ctx, task)
	if len(task.ExecStmts) > 0 {
		for _, execStmt := range task.ExecStmts {
			records = append(records, PlanReplayerStatusRecord{
				SQLDigest:  task.SQLDigest,
				PlanDigest: task.PlanDigest,
				OriginSQL:  execStmt.Text(),
				Token:      task.FileName,
			})
		}
	}
	return records
}

func setTaskPresignedURL(ctx context.Context, task *PlanReplayerDumpTask) {
	if task.IsCapture {
		return
	}
	url, err := getPresignedURL(ctx, task)
	if err != nil {
		logutil.BgLogger().Warn("failed to get plan replayer presigned URL", zap.String("category", "plan-replayer-dump"), zap.Error(err), zap.String("filename", task.FileName))
		return
	}
	task.PresignedURL = url
}

func getPresignedURL(ctx context.Context, task *PlanReplayerDumpTask) (string, error) {
	storage, err := extstore.GetGlobalExtStorage(ctx)
	if err != nil {
		return "", err
	}
	return storage.PresignFile(ctx, filepath.Join(replayer.GetPlanReplayerDirName(), task.FileName), 1*time.Hour)