diff --git a/pkg/service/http_service.go b/pkg/service/http_service.go index 1e85acb9..e0f343da 100644 --- a/pkg/service/http_service.go +++ b/pkg/service/http_service.go @@ -599,6 +599,31 @@ func (s *HttpService) jobDetailHandler(w http.ResponseWriter, r *http.Request) { } } +// get jobs details +func (s *HttpService) jobsDetailHandler(w http.ResponseWriter, r *http.Request) { + log.Infof("list jobs") + + type result struct { + *defaultResult + Jobs map[string]string `json:"jobs,omitempty"` + } + + var jobResult *result + defer func() { writeJson(w, jobResult) }() + if jobInfoMap, err := s.db.GetJobsInfo(); err != nil { + log.Warnf("get jobs' detail failed: %+v", err) + + jobResult = &result{ + defaultResult: newErrorResult(err.Error()), + } + } else { + jobResult = &result{ + defaultResult: newSuccessResult(), + Jobs: jobInfoMap, + } + } +} + func (s *HttpService) RegisterHandlers() { s.mux.HandleFunc("/version", s.versionHandler) s.mux.HandleFunc("/create_ccr", s.createHandler) @@ -612,6 +637,7 @@ func (s *HttpService) RegisterHandlers() { s.mux.HandleFunc("/list_jobs", s.listJobsHandler) s.mux.HandleFunc("/job_detail", s.jobDetailHandler) s.mux.HandleFunc("/job_progress", s.jobProgressHandler) + s.mux.HandleFunc("/all_jobs_detail", s.jobsDetailHandler) s.mux.Handle("/metrics", promhttp.Handler()) } diff --git a/pkg/storage/db.go b/pkg/storage/db.go index 668e0e98..2f26205a 100644 --- a/pkg/storage/db.go +++ b/pkg/storage/db.go @@ -25,6 +25,8 @@ type DB interface { GetJobInfo(jobName string) (string, error) // Get job_belong GetJobBelong(jobName string) (string, error) + // Get jobs detail info + GetJobsInfo() (map[string]string, error) // Update ccr sync progress UpdateProgress(jobName string, progress string) error diff --git a/pkg/storage/mysql.go b/pkg/storage/mysql.go index 8ced7268..5c75e3cf 100644 --- a/pkg/storage/mysql.go +++ b/pkg/storage/mysql.go @@ -160,6 +160,27 @@ func (s *MysqlDB) UpdateProgress(jobName string, progress string) error { return nil } +func (s *MysqlDB) GetJobsInfo() (map[string]string, error) { + jobRows, err := s.db.Query("SELECT job_info,job_name FROM jobs") + if err != nil { + return nil, xerror.Wrap(err, xerror.DB, "mysql: get jobs detail data failed.") + } + + jobDataMap := make(map[string]string) + for jobRows.Next() { + var jobInfo string + var jobName string + + if err := jobRows.Scan(&jobInfo, &jobName); err != nil { + return nil, xerror.Wrap(err, xerror.DB, "mysql: scan jobs row failed.") + } + + jobDataMap[jobName] = jobInfo + } + + return jobDataMap, nil +} + func (s *MysqlDB) IsProgressExist(jobName string) (bool, error) { var count int if err := s.db.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM progresses WHERE job_name = '%s'", jobName)).Scan(&count); err != nil { diff --git a/pkg/storage/sqlite.go b/pkg/storage/sqlite.go index a9a6967d..0ecb8d4e 100644 --- a/pkg/storage/sqlite.go +++ b/pkg/storage/sqlite.go @@ -143,6 +143,26 @@ func (s *SQLiteDB) UpdateProgress(jobName string, progress string) error { return nil } +func (s *SQLiteDB) GetJobsInfo() (map[string]string, error) { + jobRows, err := s.db.Query("SELECT job_info,job_name FROM jobs") + if err != nil { + return nil, xerror.Wrap(err, xerror.DB, "sqlite: get jobs detail data failed.") + } + + jobDataMap := make(map[string]string) + for jobRows.Next() { + var jobInfo string + var jobName string + if err := jobRows.Scan(&jobInfo, &jobName); err != nil { + return nil, xerror.Wrap(err, xerror.DB, "sqlite: scan jobs row failed.") + } + + jobDataMap[jobName] = jobInfo + } + + return jobDataMap, nil +} + func (s *SQLiteDB) IsProgressExist(jobName string) (bool, error) { var count int if err := s.db.QueryRow("SELECT COUNT(*) FROM progresses WHERE job_name = ?", jobName).Scan(&count); err != nil {