Reviewed-on: https://git.openi.org.cn/OpenI/aiforge/pulls/1029 Reviewed-by: lewis <747342561@qq.com>pull/1031/head
@@ -91,6 +91,7 @@ type Cloudbrain struct { | |||||
DeletedAt time.Time `xorm:"deleted"` | DeletedAt time.Time `xorm:"deleted"` | ||||
CanDebug bool `xorm:"-"` | CanDebug bool `xorm:"-"` | ||||
CanDel bool `xorm:"-"` | CanDel bool `xorm:"-"` | ||||
CanModify bool `xorm:"-"` | |||||
Type int | Type int | ||||
VersionID int64 //版本id | VersionID int64 //版本id | ||||
@@ -29,9 +29,7 @@ var ( | |||||
ResourceSpecs *models.ResourceSpecs | ResourceSpecs *models.ResourceSpecs | ||||
) | ) | ||||
func isAdminOrOwnerOrJobCreater(ctx *context.Context, jobId string) bool { | |||||
job, err := models.GetCloudbrainByJobID(jobId) | |||||
func isAdminOrOwnerOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool { | |||||
if err != nil { | if err != nil { | ||||
return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin() | return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin() | ||||
@@ -41,9 +39,29 @@ func isAdminOrOwnerOrJobCreater(ctx *context.Context, jobId string) bool { | |||||
} | } | ||||
func isAdminOrJobCreater(ctx *context.Context, jobId string) bool { | |||||
func CanDeleteDebugJob(ctx *context.Context, job *models.Cloudbrain) bool { | |||||
if job.Status != string(models.JobStopped) && job.Status != string(models.JobFailed) && job.Status != string(models.ModelArtsStartFailed) && job.Status != string(models.ModelArtsCreateFailed) { | |||||
return false | |||||
} | |||||
return isAdminOrOwnerOrJobCreater(ctx, job, nil) | |||||
} | |||||
func CanDeleteTrainJob(ctx *context.Context, job *models.Cloudbrain) bool { | |||||
return isAdminOrOwnerOrJobCreater(ctx, job, nil) | |||||
} | |||||
func CanCreateOrDebugJob(ctx *context.Context) bool { | |||||
return ctx.Repo.CanWrite(models.UnitTypeCloudBrain) | |||||
} | |||||
func CanModifyJob(ctx *context.Context, job *models.Cloudbrain) bool { | |||||
job, err := models.GetCloudbrainByJobID(jobId) | |||||
return isAdminOrJobCreater(ctx, job, nil) | |||||
} | |||||
func isAdminOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool { | |||||
if err != nil { | if err != nil { | ||||
return ctx.IsUserSiteAdmin() | return ctx.IsUserSiteAdmin() | ||||
@@ -57,7 +75,9 @@ func AdminOrOwnerOrJobCreaterRight(ctx *context.Context) { | |||||
var jobID = ctx.Params(":jobid") | var jobID = ctx.Params(":jobid") | ||||
if !isAdminOrOwnerOrJobCreater(ctx, jobID) { | |||||
job, err := models.GetCloudbrainByJobID(jobID) | |||||
if !isAdminOrOwnerOrJobCreater(ctx, job, err) { | |||||
ctx.NotFound(ctx.Req.URL.RequestURI(), nil) | ctx.NotFound(ctx.Req.URL.RequestURI(), nil) | ||||
} | } | ||||
@@ -67,8 +87,8 @@ func AdminOrOwnerOrJobCreaterRight(ctx *context.Context) { | |||||
func AdminOrJobCreaterRight(ctx *context.Context) { | func AdminOrJobCreaterRight(ctx *context.Context) { | ||||
var jobID = ctx.Params(":jobid") | var jobID = ctx.Params(":jobid") | ||||
if !isAdminOrJobCreater(ctx, jobID) { | |||||
job, err := models.GetCloudbrainByJobID(jobID) | |||||
if !isAdminOrJobCreater(ctx, job, err) { | |||||
ctx.NotFound(ctx.Req.URL.RequestURI(), nil) | ctx.NotFound(ctx.Req.URL.RequestURI(), nil) | ||||
} | } | ||||
@@ -74,12 +74,13 @@ func CloudBrainIndex(ctx *context.Context) { | |||||
timestamp := time.Now().Unix() | timestamp := time.Now().Unix() | ||||
for i, task := range ciTasks { | for i, task := range ciTasks { | ||||
if task.Status == string(models.JobRunning) && (timestamp-int64(task.Cloudbrain.CreatedUnix) > 10) { | if task.Status == string(models.JobRunning) && (timestamp-int64(task.Cloudbrain.CreatedUnix) > 10) { | ||||
ciTasks[i].CanDebug = true | |||||
ciTasks[i].CanDebug = cloudbrain.CanCreateOrDebugJob(ctx) | |||||
} else { | } else { | ||||
ciTasks[i].CanDebug = false | ciTasks[i].CanDebug = false | ||||
} | } | ||||
ciTasks[i].CanDel = models.CanDelJob(ctx.IsSigned, ctx.User, task) | |||||
ciTasks[i].CanDel = cloudbrain.CanDeleteDebugJob(ctx, &task.Cloudbrain) | |||||
} | } | ||||
pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5) | pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5) | ||||
@@ -88,6 +89,7 @@ func CloudBrainIndex(ctx *context.Context) { | |||||
ctx.Data["PageIsCloudBrain"] = true | ctx.Data["PageIsCloudBrain"] = true | ||||
ctx.Data["Tasks"] = ciTasks | ctx.Data["Tasks"] = ciTasks | ||||
ctx.Data["CanCreate"] = cloudbrain.CanCreateOrDebugJob(ctx) | |||||
ctx.HTML(200, tplCloudBrainIndex) | ctx.HTML(200, tplCloudBrainIndex) | ||||
} | } | ||||
@@ -4,6 +4,7 @@ import ( | |||||
"encoding/json" | "encoding/json" | ||||
"errors" | "errors" | ||||
"io" | "io" | ||||
"io/ioutil" | |||||
"net/http" | "net/http" | ||||
"os" | "os" | ||||
"path" | "path" | ||||
@@ -11,6 +12,8 @@ import ( | |||||
"strings" | "strings" | ||||
"time" | "time" | ||||
"code.gitea.io/gitea/modules/cloudbrain" | |||||
"code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
"code.gitea.io/gitea/modules/auth" | "code.gitea.io/gitea/modules/auth" | ||||
"code.gitea.io/gitea/modules/base" | "code.gitea.io/gitea/modules/base" | ||||
@@ -68,10 +71,11 @@ func NotebookIndex(ctx *context.Context) { | |||||
for i, task := range ciTasks { | for i, task := range ciTasks { | ||||
if task.Status == string(models.JobRunning) { | if task.Status == string(models.JobRunning) { | ||||
ciTasks[i].CanDebug = true | |||||
ciTasks[i].CanDebug = cloudbrain.CanCreateOrDebugJob(ctx) | |||||
} else { | } else { | ||||
ciTasks[i].CanDebug = false | ciTasks[i].CanDebug = false | ||||
} | } | ||||
ciTasks[i].CanDel = cloudbrain.CanDeleteDebugJob(ctx, &task.Cloudbrain) | |||||
} | } | ||||
pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5) | pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5) | ||||
@@ -80,6 +84,7 @@ func NotebookIndex(ctx *context.Context) { | |||||
ctx.Data["PageIsCloudBrain"] = true | ctx.Data["PageIsCloudBrain"] = true | ||||
ctx.Data["Tasks"] = ciTasks | ctx.Data["Tasks"] = ciTasks | ||||
ctx.Data["CanCreate"] = cloudbrain.CanCreateOrDebugJob(ctx) | |||||
ctx.HTML(200, tplModelArtsNotebookIndex) | ctx.HTML(200, tplModelArtsNotebookIndex) | ||||
} | } | ||||
@@ -301,12 +306,18 @@ func TrainJobIndex(ctx *context.Context) { | |||||
return | return | ||||
} | } | ||||
for i, task := range tasks { | |||||
tasks[i].CanDel = cloudbrain.CanDeleteTrainJob(ctx, &task.Cloudbrain) | |||||
tasks[i].CanModify = cloudbrain.CanModifyJob(ctx, &task.Cloudbrain) | |||||
} | |||||
pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5) | pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5) | ||||
pager.SetDefaultParams(ctx) | pager.SetDefaultParams(ctx) | ||||
ctx.Data["Page"] = pager | ctx.Data["Page"] = pager | ||||
ctx.Data["PageIsCloudBrain"] = true | ctx.Data["PageIsCloudBrain"] = true | ||||
ctx.Data["Tasks"] = tasks | ctx.Data["Tasks"] = tasks | ||||
ctx.Data["CanCreate"] = cloudbrain.CanCreateOrDebugJob(ctx) | |||||
ctx.HTML(200, tplModelArtsTrainJobIndex) | ctx.HTML(200, tplModelArtsTrainJobIndex) | ||||
} | } | ||||