fix(typesense): make fetching task positions per view more efficient
This commit is contained in:
parent
3519b8b2fe
commit
a5d02380a3
@ -529,7 +529,21 @@ func (l *AddTaskToTypesense) Handle(msg *message.Message) (err error) {
|
|||||||
|
|
||||||
s := db.NewSession()
|
s := db.NewSession()
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
ttask, err := getTypesenseTaskForTask(s, event.Task, nil)
|
|
||||||
|
positions := []*TaskPositionWithView{}
|
||||||
|
err = s.
|
||||||
|
Table("project_views").
|
||||||
|
Where("project_views.project_id = ?", event.Task.ProjectID).
|
||||||
|
Join("LEFT", "task_positions", "project_views.id = task_positions.project_view_id AND task_positions.task_id = ?", event.Task.ID).
|
||||||
|
Find(&positions)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
positionsMap := make(map[int64][]*TaskPositionWithView, 1)
|
||||||
|
positionsMap[event.Task.ID] = positions
|
||||||
|
|
||||||
|
ttask, err := getTypesenseTaskForTask(s, event.Task, nil, positionsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -240,23 +240,8 @@ func ReindexAllTasks() (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type TaskPositionWithView struct {
|
func getTypesenseTaskForTask(s *xorm.Session, task *Task, projectsCache map[int64]*Project, taskPositionCache map[int64][]*TaskPositionWithView) (ttask *typesenseTask, err error) {
|
||||||
ProjectView `xorm:"extends"`
|
ttask = convertTaskToTypesenseTask(task, taskPositionCache[task.ID])
|
||||||
TaskPosition `xorm:"extends"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func getTypesenseTaskForTask(s *xorm.Session, task *Task, projectsCache map[int64]*Project) (ttask *typesenseTask, err error) {
|
|
||||||
positions := []*TaskPositionWithView{}
|
|
||||||
err = s.
|
|
||||||
Table("project_views").
|
|
||||||
Where("project_views.project_id = ?", task.ProjectID).
|
|
||||||
Join("LEFT", "task_positions", "project_views.id = task_positions.project_view_id AND task_positions.task_id = ?", task.ID).
|
|
||||||
Find(&positions)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ttask = convertTaskToTypesenseTask(task, positions)
|
|
||||||
|
|
||||||
var p *Project
|
var p *Project
|
||||||
if projectsCache == nil {
|
if projectsCache == nil {
|
||||||
@ -285,6 +270,11 @@ func getTypesenseTaskForTask(s *xorm.Session, task *Task, projectsCache map[int6
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TaskPositionWithView struct {
|
||||||
|
ProjectView `xorm:"extends"`
|
||||||
|
TaskPosition `xorm:"extends"`
|
||||||
|
}
|
||||||
|
|
||||||
func reindexTasksInTypesense(s *xorm.Session, tasks map[int64]*Task) (err error) {
|
func reindexTasksInTypesense(s *xorm.Session, tasks map[int64]*Task) (err error) {
|
||||||
|
|
||||||
if len(tasks) == 0 {
|
if len(tasks) == 0 {
|
||||||
@ -300,9 +290,27 @@ func reindexTasksInTypesense(s *xorm.Session, tasks map[int64]*Task) (err error)
|
|||||||
projects := make(map[int64]*Project)
|
projects := make(map[int64]*Project)
|
||||||
typesenseTasks := []interface{}{}
|
typesenseTasks := []interface{}{}
|
||||||
|
|
||||||
|
rawPositions := []*TaskPositionWithView{}
|
||||||
|
err = s.
|
||||||
|
Table("project_views").
|
||||||
|
Join("LEFT", "task_positions", "project_views.id = task_positions.project_view_id").
|
||||||
|
Find(&rawPositions)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
positionsByTask := make(map[int64][]*TaskPositionWithView, len(rawPositions))
|
||||||
|
for _, p := range rawPositions {
|
||||||
|
_, has := positionsByTask[p.TaskID]
|
||||||
|
if !has {
|
||||||
|
positionsByTask[p.TaskID] = []*TaskPositionWithView{}
|
||||||
|
}
|
||||||
|
positionsByTask[p.TaskID] = append(positionsByTask[p.TaskID], p)
|
||||||
|
}
|
||||||
|
|
||||||
for _, task := range tasks {
|
for _, task := range tasks {
|
||||||
|
|
||||||
ttask, err := getTypesenseTaskForTask(s, task, projects)
|
ttask, err := getTypesenseTaskForTask(s, task, projects, positionsByTask)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user