mirror of
https://github.com/semaphoreui/semaphore.git
synced 2024-11-21 17:01:04 +01:00
feat(db): change algo for filling last_task of template model
This commit is contained in:
parent
63204b27b5
commit
a8a95495d8
@ -47,7 +47,7 @@ func GetTasksList(w http.ResponseWriter, r *http.Request, limit int) {
|
||||
var tasks []db.TaskWithTpl
|
||||
|
||||
if tpl != nil {
|
||||
tasks, err = helpers.Store(r).GetTemplateTasks(tpl.(db.Template).ProjectID, []int{tpl.(db.Template).ID}, db.RetrieveQueryParams{
|
||||
tasks, err = helpers.Store(r).GetTemplateTasks(tpl.(db.Template).ProjectID, tpl.(db.Template).ID, db.RetrieveQueryParams{
|
||||
Count: limit,
|
||||
})
|
||||
} else {
|
||||
|
@ -237,7 +237,7 @@ type Store interface {
|
||||
CreateTask(task Task, maxTasks int) (Task, error)
|
||||
UpdateTask(task Task) error
|
||||
|
||||
GetTemplateTasks(projectID int, templateIDs []int, params RetrieveQueryParams) ([]TaskWithTpl, error)
|
||||
GetTemplateTasks(projectID int, templateID int, params RetrieveQueryParams) ([]TaskWithTpl, error)
|
||||
GetProjectTasks(projectID int, params RetrieveQueryParams) ([]TaskWithTpl, error)
|
||||
GetTask(projectID int, taskID int) (Task, error)
|
||||
DeleteTaskWithOutputs(projectID int, taskID int) error
|
||||
|
@ -127,40 +127,6 @@ func (tpl *Template) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func FillTemplates(d Store, templates []Template) (err error) {
|
||||
|
||||
var templateIDs []int
|
||||
var projectID int
|
||||
|
||||
if len(templates) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
projectID = templates[0].ProjectID
|
||||
|
||||
for _, tpl := range templates {
|
||||
if tpl.ProjectID != projectID {
|
||||
err = &ValidationError{"templates must be from the same project"}
|
||||
return
|
||||
}
|
||||
|
||||
templateIDs = append(templateIDs, tpl.ID)
|
||||
}
|
||||
|
||||
var tasks []TaskWithTpl
|
||||
tasks, err = d.GetTemplateTasks(projectID, templateIDs, RetrieveQueryParams{Count: 1})
|
||||
|
||||
for _, task := range tasks {
|
||||
i := findIntIndex(templateIDs, task.TemplateID)
|
||||
if i >= 0 {
|
||||
templates[i].LastTask = &TaskWithTpl{}
|
||||
*templates[i].LastTask = task
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func FillTemplate(d Store, template *Template) (err error) {
|
||||
if template.VaultKeyID != nil {
|
||||
template.VaultKey, err = d.GetAccessKey(template.ProjectID, *template.VaultKeyID)
|
||||
@ -170,14 +136,8 @@ func FillTemplate(d Store, template *Template) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
err = FillTemplates(d, []Template{*template})
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var tasks []TaskWithTpl
|
||||
tasks, err = d.GetTemplateTasks(template.ProjectID, []int{template.ID}, RetrieveQueryParams{Count: 1})
|
||||
tasks, err = d.GetTemplateTasks(template.ProjectID, template.ID, RetrieveQueryParams{Count: 1})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -55,7 +55,13 @@ func (d *BoltDb) GetTemplates(projectID int, filter db.TemplateFilter, params db
|
||||
return
|
||||
}
|
||||
|
||||
err = db.FillTemplates(d, templates)
|
||||
var tasks []db.Task
|
||||
|
||||
err = d.getObjects(projectID, db.TaskProps, db.RetrieveQueryParams{}, func(i interface{}) bool {
|
||||
return true
|
||||
}, &tasks)
|
||||
|
||||
//err = db.FillTemplates(d, templates)
|
||||
|
||||
return
|
||||
}
|
||||
@ -85,7 +91,7 @@ func (d *BoltDb) deleteTemplate(projectID int, templateID int, tx *bbolt.Tx) (er
|
||||
return db.ErrInvalidOperation
|
||||
}
|
||||
|
||||
tasks, err := d.GetTemplateTasks(projectID, []int{templateID}, db.RetrieveQueryParams{})
|
||||
tasks, err := d.GetTemplateTasks(projectID, templateID, db.RetrieveQueryParams{})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -191,8 +191,8 @@ func (d *SqlDb) GetTask(projectID int, taskID int) (task db.Task, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetTemplateTasks(projectID int, templateIDs []int, params db.RetrieveQueryParams) (tasks []db.TaskWithTpl, err error) {
|
||||
err = d.getTasks(projectID, templateIDs, params, &tasks)
|
||||
func (d *SqlDb) GetTemplateTasks(projectID int, templateID int, params db.RetrieveQueryParams) (tasks []db.TaskWithTpl, err error) {
|
||||
err = d.getTasks(projectID, templateID, params, &tasks)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -104,7 +104,21 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) getProjectTasksByIDs(projectID int, taskIDs []int) (tasks []db.Task, err error) {
|
||||
err = d.getObjects(projectID, db.TaskProps, db.RetrieveQueryParams{}, func(builder squirrel.SelectBuilder) squirrel.SelectBuilder {
|
||||
return builder.Where("`id` IN ?", taskIDs)
|
||||
}, &tasks)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetTemplates(projectID int, filter db.TemplateFilter, params db.RetrieveQueryParams) (templates []db.Template, err error) {
|
||||
|
||||
type templateWithLastTask struct {
|
||||
db.Template
|
||||
LastTaskID *int `db:"last_task_id"`
|
||||
}
|
||||
|
||||
q := squirrel.Select("pt.id",
|
||||
"pt.project_id",
|
||||
"pt.inventory_id",
|
||||
@ -122,7 +136,8 @@ func (d *SqlDb) GetTemplates(projectID int, filter db.TemplateFilter, params db.
|
||||
"pt.survey_vars",
|
||||
"pt.start_version",
|
||||
"pt.`type`",
|
||||
"pt.`tasks`").
|
||||
"pt.`tasks`",
|
||||
"(SELECT `id` FROM `task` WHERE template_id = pt.id) last_task_id").
|
||||
From("project__template pt")
|
||||
|
||||
if filter.ViewID != nil {
|
||||
@ -168,13 +183,46 @@ func (d *SqlDb) GetTemplates(projectID int, filter db.TemplateFilter, params db.
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.selectAll(&templates, query, args...)
|
||||
var tpls []templateWithLastTask
|
||||
|
||||
_, err = d.selectAll(&tpls, query, args...)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = db.FillTemplates(d, templates)
|
||||
var taskIDs []int
|
||||
|
||||
for _, tpl := range tpls {
|
||||
if tpl.LastTaskID != nil {
|
||||
taskIDs = append(taskIDs, *tpl.LastTaskID)
|
||||
}
|
||||
}
|
||||
|
||||
tasks, err := d.getProjectTasksByIDs(projectID, taskIDs)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, tpl := range tpls {
|
||||
template := tpl.Template
|
||||
templates = append(templates, template)
|
||||
if tpl.LastTaskID == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, tsk := range tasks {
|
||||
if tsk.TemplateID != *tpl.LastTaskID {
|
||||
continue
|
||||
}
|
||||
|
||||
template.LastTask = &db.TaskWithTpl{
|
||||
Task: tsk,
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -340,7 +340,7 @@ func (p *TaskPool) AddTask(taskObj db.Task, userID *int, projectID int) (newTask
|
||||
|
||||
if tpl.Type == db.TemplateBuild { // get next version for TaskRunner if it is a Build
|
||||
var builds []db.TaskWithTpl
|
||||
builds, err = p.store.GetTemplateTasks(tpl.ProjectID, []int{tpl.ID}, db.RetrieveQueryParams{Count: 1})
|
||||
builds, err = p.store.GetTemplateTasks(tpl.ProjectID, tpl.ID, db.RetrieveQueryParams{Count: 1})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user