Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a handler that can list all jobs detail #73

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pkg/service/http_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/storage/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions pkg/storage/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions pkg/storage/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading