fix(tasks): make fetching tasks in buckets via typesense work
continuous-integration/drone/push Build is failing Details

This commit is contained in:
kolaente 2024-04-13 17:52:47 +02:00
parent a5d02380a3
commit 77e95642a9
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 101 additions and 28 deletions

View File

@ -530,6 +530,28 @@ func (l *AddTaskToTypesense) Handle(msg *message.Message) (err error) {
s := db.NewSession() s := db.NewSession()
defer s.Close() defer s.Close()
positionsMap, err := getPositionsForTask(s, event)
if err != nil {
return err
}
bucketsMap, err := getBucketsForTask(s, event)
if err != nil {
return err
}
ttask, err := getTypesenseTaskForTask(s, event.Task, nil, positionsMap, bucketsMap)
if err != nil {
return err
}
_, err = typesenseClient.Collection("tasks").
Documents().
Create(context.Background(), ttask)
return
}
func getPositionsForTask(s *xorm.Session, event *TaskCreatedEvent) (positionsMap map[int64][]*TaskPositionWithView, err error) {
positions := []*TaskPositionWithView{} positions := []*TaskPositionWithView{}
err = s. err = s.
Table("project_views"). Table("project_views").
@ -540,17 +562,22 @@ func (l *AddTaskToTypesense) Handle(msg *message.Message) (err error) {
return return
} }
positionsMap := make(map[int64][]*TaskPositionWithView, 1) positionsMap = make(map[int64][]*TaskPositionWithView, 1)
positionsMap[event.Task.ID] = positions positionsMap[event.Task.ID] = positions
return
}
ttask, err := getTypesenseTaskForTask(s, event.Task, nil, positionsMap) func getBucketsForTask(s *xorm.Session, event *TaskCreatedEvent) (bucketsMap map[int64][]*TaskBucket, err error) {
buckets := []*TaskBucket{}
err = s.
Where("task_id = ?", event.Task.ID).
Find(&buckets)
if err != nil { if err != nil {
return err return
} }
_, err = typesenseClient.Collection("tasks"). bucketsMap = make(map[int64][]*TaskBucket, 1)
Documents(). bucketsMap[event.Task.ID] = buckets
Create(context.Background(), ttask)
return return
} }

View File

@ -379,6 +379,10 @@ func convertParsedFilterToTypesense(rawFilters []*taskFilter) (filterBy string,
f.field = "project_id" f.field = "project_id"
} }
if f.field == "bucket_id" {
f.field = "buckets"
}
filter := f.field filter := f.field
switch f.comparator { switch f.comparator {

View File

@ -184,6 +184,10 @@ func CreateTypesenseCollections() error {
Name: "positions", Name: "positions",
Type: "object", Type: "object",
}, },
{
Name: "buckets",
Type: "int64[]",
},
}, },
} }
@ -240,8 +244,8 @@ func ReindexAllTasks() (err error) {
return return
} }
func getTypesenseTaskForTask(s *xorm.Session, task *Task, projectsCache map[int64]*Project, taskPositionCache map[int64][]*TaskPositionWithView) (ttask *typesenseTask, err error) { func getTypesenseTaskForTask(s *xorm.Session, task *Task, projectsCache map[int64]*Project, taskPositionCache map[int64][]*TaskPositionWithView, taskBucketCache map[int64][]*TaskBucket) (ttask *typesenseTask, err error) {
ttask = convertTaskToTypesenseTask(task, taskPositionCache[task.ID]) ttask = convertTaskToTypesenseTask(task, taskPositionCache[task.ID], taskBucketCache[task.ID])
var p *Project var p *Project
if projectsCache == nil { if projectsCache == nil {
@ -270,11 +274,6 @@ 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 {
@ -290,27 +289,19 @@ 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{} positionsByTask, err := getPositionsByTask(s)
err = s.
Table("project_views").
Join("LEFT", "task_positions", "project_views.id = task_positions.project_view_id").
Find(&rawPositions)
if err != nil { if err != nil {
return return err
} }
positionsByTask := make(map[int64][]*TaskPositionWithView, len(rawPositions)) bucketsByTask, err := getBucketsByTask(s)
for _, p := range rawPositions { if err != nil {
_, has := positionsByTask[p.TaskID] return err
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, positionsByTask) ttask, err := getTypesenseTaskForTask(s, task, projects, positionsByTask, bucketsByTask)
if err != nil { if err != nil {
return err return err
} }
@ -334,6 +325,50 @@ func reindexTasksInTypesense(s *xorm.Session, tasks map[int64]*Task) (err error)
return nil return nil
} }
type TaskPositionWithView struct {
ProjectView `xorm:"extends"`
TaskPosition `xorm:"extends"`
}
func getPositionsByTask(s *xorm.Session) (positionsByTask map[int64][]*TaskPositionWithView, err error) {
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)
}
return positionsByTask, nil
}
func getBucketsByTask(s *xorm.Session) (positionsByTask map[int64][]*TaskBucket, err error) {
rawBuckets := []*TaskBucket{}
err = s.Find(&rawBuckets)
if err != nil {
return
}
positionsByTask = make(map[int64][]*TaskBucket, len(rawBuckets))
for _, p := range rawBuckets {
_, has := positionsByTask[p.TaskID]
if !has {
positionsByTask[p.TaskID] = []*TaskBucket{}
}
positionsByTask[p.TaskID] = append(positionsByTask[p.TaskID], p)
}
return positionsByTask, nil
}
func indexDummyTask() (err error) { func indexDummyTask() (err error) {
// The initial sync should contain one dummy task with all related fields populated so that typesense // The initial sync should contain one dummy task with all related fields populated so that typesense
// creates the indexes properly. A little hacky, but gets the job done. // creates the indexes properly. A little hacky, but gets the job done.
@ -401,6 +436,7 @@ func indexDummyTask() (err error) {
"view_3": 5450, "view_3": 5450,
"view_4": 42, "view_4": 42,
}, },
Buckets: []int64{42},
} }
_, err = typesenseClient.Collection("tasks"). _, err = typesenseClient.Collection("tasks").
@ -445,9 +481,10 @@ type typesenseTask struct {
Attachments interface{} `json:"attachments"` Attachments interface{} `json:"attachments"`
Comments interface{} `json:"comments"` Comments interface{} `json:"comments"`
Positions map[string]float64 `json:"positions"` Positions map[string]float64 `json:"positions"`
Buckets []int64 `json:"buckets"`
} }
func convertTaskToTypesenseTask(task *Task, positions []*TaskPositionWithView) *typesenseTask { func convertTaskToTypesenseTask(task *Task, positions []*TaskPositionWithView, buckets []*TaskBucket) *typesenseTask {
tt := &typesenseTask{ tt := &typesenseTask{
ID: fmt.Sprintf("%d", task.ID), ID: fmt.Sprintf("%d", task.ID),
@ -477,6 +514,7 @@ func convertTaskToTypesenseTask(task *Task, positions []*TaskPositionWithView) *
//RelatedTasks: task.RelatedTasks, //RelatedTasks: task.RelatedTasks,
Attachments: task.Attachments, Attachments: task.Attachments,
Positions: make(map[string]float64, len(positions)), Positions: make(map[string]float64, len(positions)),
Buckets: make([]int64, 0, len(buckets)),
} }
if task.DoneAt.IsZero() { if task.DoneAt.IsZero() {
@ -500,6 +538,10 @@ func convertTaskToTypesenseTask(task *Task, positions []*TaskPositionWithView) *
tt.Positions["view_"+strconv.FormatInt(position.ProjectView.ID, 10)] = pos tt.Positions["view_"+strconv.FormatInt(position.ProjectView.ID, 10)] = pos
} }
for _, bucket := range buckets {
tt.Buckets = append(tt.Buckets, bucket.BucketID)
}
return tt return tt
} }