fix(typesense): make fetching task positions per view more efficient
continuous-integration/drone/push Build is failing Details

This commit is contained in:
kolaente 2024-04-13 17:26:38 +02:00
parent 3519b8b2fe
commit a5d02380a3
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 41 additions and 19 deletions

View File

@ -529,7 +529,21 @@ func (l *AddTaskToTypesense) Handle(msg *message.Message) (err error) {
s := db.NewSession()
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 {
return err
}

View File

@ -240,23 +240,8 @@ func ReindexAllTasks() (err error) {
return
}
type TaskPositionWithView struct {
ProjectView `xorm:"extends"`
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)
func getTypesenseTaskForTask(s *xorm.Session, task *Task, projectsCache map[int64]*Project, taskPositionCache map[int64][]*TaskPositionWithView) (ttask *typesenseTask, err error) {
ttask = convertTaskToTypesenseTask(task, taskPositionCache[task.ID])
var p *Project
if projectsCache == nil {
@ -285,6 +270,11 @@ func getTypesenseTaskForTask(s *xorm.Session, task *Task, projectsCache map[int6
return
}
type TaskPositionWithView struct {
ProjectView `xorm:"extends"`
TaskPosition `xorm:"extends"`
}
func reindexTasksInTypesense(s *xorm.Session, tasks map[int64]*Task) (err error) {
if len(tasks) == 0 {
@ -300,9 +290,27 @@ func reindexTasksInTypesense(s *xorm.Session, tasks map[int64]*Task) (err error)
projects := make(map[int64]*Project)
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 {
ttask, err := getTypesenseTaskForTask(s, task, projects)
ttask, err := getTypesenseTaskForTask(s, task, projects, positionsByTask)
if err != nil {
return err
}