Merge pull request #349 from ywk253100/new_ui

filter jobs by policy ID and repository
This commit is contained in:
Wenkai Yin 2016-06-17 11:55:45 +08:00 committed by GitHub
commit 38388b25cb
4 changed files with 91 additions and 34 deletions

View File

@ -55,17 +55,25 @@ func (ra *RepJobAPI) Prepare() {
}
// Get gets all the jobs according to the policy
func (ra *RepJobAPI) Get() {
policyID, err := ra.GetInt64("policy_id")
if err != nil {
log.Errorf("Failed to get policy id, error: %v", err)
ra.RenderError(http.StatusBadRequest, "Invalid policy id")
return
// List filters jobs according to the policy and repository
func (ra *RepJobAPI) List() {
var policyID int64
var repository string
var err error
policyIDStr := ra.GetString("policy_id")
if len(policyIDStr) != 0 {
policyID, err = strconv.ParseInt(policyIDStr, 10, 64)
if err != nil || policyID <= 0 {
ra.CustomAbort(http.StatusBadRequest, fmt.Sprintf("invalid policy ID: %s", policyIDStr))
}
jobs, err := dao.GetRepJobByPolicy(policyID)
}
repository = ra.GetString("repository")
jobs, err := dao.FilterRepJobs(repository, policyID)
if err != nil {
log.Errorf("Failed to query job from db, error: %v", err)
log.Errorf("failed to filter jobs according policy ID %d and repository %s: %v", policyID, repository, err)
ra.RenderError(http.StatusInternalServerError, "Failed to query job")
return
}

View File

@ -1107,6 +1107,22 @@ func TestGetRepJobByPolicy(t *testing.T) {
}
}
func TestFilterRepJobs(t *testing.T) {
jobs, err := FilterRepJobs("", policyID)
if err != nil {
log.Errorf("Error occured in FilterRepJobs: %v, policy ID: %d", err, policyID)
return
}
if len(jobs) != 1 {
log.Errorf("Unexpected length of jobs, expected: 1, in fact: %d", len(jobs))
return
}
if jobs[0].ID != jobID {
log.Errorf("Unexpected job ID in the result, expected: %d, in fact: %d", jobID, jobs[0].ID)
return
}
}
func TestGetRepoJobToStop(t *testing.T) {
jobs := [...]models.RepJob{
models.RepJob{

View File

@ -273,6 +273,38 @@ func GetRepJobByPolicy(policyID int64) ([]*models.RepJob, error) {
return res, err
}
// FilterRepJobs filters jobs by repo and policy ID
func FilterRepJobs(repo string, policyID int64) ([]*models.RepJob, error) {
o := GetOrmer()
var args []interface{}
sql := `select * from replication_job `
if len(repo) != 0 && policyID != 0 {
sql += `where repository like ? and policy_id = ? `
args = append(args, "%"+repo+"%")
args = append(args, policyID)
} else if len(repo) != 0 {
sql += `where repository like ? `
args = append(args, "%"+repo+"%")
} else if policyID != 0 {
sql += `where policy_id = ? `
args = append(args, policyID)
}
sql += `order by creation_time`
var jobs []*models.RepJob
if _, err := o.Raw(sql, args).QueryRows(&jobs); err != nil {
return nil, err
}
genTagListForJob(jobs...)
return jobs, nil
}
// GetRepJobToStop get jobs that are possibly being handled by workers of a certain policy.
func GetRepJobToStop(policyID int64) ([]*models.RepJob, error) {
var res []*models.RepJob

View File

@ -66,7 +66,8 @@ func initRouters() {
beego.Router("/api/repositories", &api.RepositoryAPI{})
beego.Router("/api/repositories/tags", &api.RepositoryAPI{}, "get:GetTags")
beego.Router("/api/repositories/manifests", &api.RepositoryAPI{}, "get:GetManifests")
beego.Router("/api/jobs/replication/?:id([0-9]+)", &api.RepJobAPI{})
beego.Router("/api/jobs/replication/", &api.RepJobAPI{}, "get:List")
beego.Router("/api/jobs/replication/:id([0-9]+)", &api.RepJobAPI{})
beego.Router("/api/jobs/replication/:id([0-9]+)/log", &api.RepJobAPI{}, "get:GetLog")
beego.Router("/api/policies/replication/:id([0-9]+)", &api.RepPolicyAPI{})
beego.Router("/api/policies/replication", &api.RepPolicyAPI{}, "get:List")