fix: reduce Meilisearch load, debounce federation sync (#1012)

* optimize meili trail index

* several fixes

---------

Co-authored-by: Flomp <Flomp@users.noreply.github.com>
This commit is contained in:
slothful-vassal
2026-06-01 16:01:59 +02:00
committed by GitHub
parent 79b86b6219
commit 65c2e3d932
8 changed files with 199 additions and 63 deletions

View File

@@ -13,8 +13,6 @@ import (
"github.com/meilisearch/meilisearch-go"
"github.com/pocketbase/pocketbase/core"
"github.com/tkrajina/gpxgo/gpx"
"github.com/twpayne/go-polyline"
)
func documentFromTrailRecord(app core.App, r *core.Record, author *core.Record, includeShares bool) (map[string]interface{}, error) {
@@ -41,11 +39,6 @@ func documentFromTrailRecord(app core.App, r *core.Record, author *core.Record,
category = trailCategory.GetString("name")
}
polyline, err := getPolyline(app, r)
if err != nil {
polyline = ""
}
domain := ""
if !author.GetBool("isLocal") {
domain = author.GetString("domain")
@@ -72,7 +65,7 @@ func documentFromTrailRecord(app core.App, r *core.Record, author *core.Record,
"thumbnail": thumbnail,
"gpx": r.GetString("gpx"),
"tags": tags,
"polyline": polyline,
"polyline": r.GetString("polyline"),
"domain": domain,
"iri": r.GetString("iri"),
"_geo": map[string]float64{
@@ -128,46 +121,6 @@ func difficultyToNumber(difficulty string) int32 {
return 0
}
func getPolyline(app core.App, r *core.Record) (string, error) {
gpxPath := r.GetString("gpx")
if len(gpxPath) == 0 {
return "", nil
}
avatarKey := r.BaseFilesPath() + "/" + gpxPath
fsys, err := app.NewFilesystem()
if err != nil {
return "", err
}
defer fsys.Close()
gpxFile, err := fsys.GetReader(avatarKey)
if err != nil {
return "", err
}
defer gpxFile.Close()
content := new(bytes.Buffer)
_, err = io.Copy(content, gpxFile)
if err != nil {
return "", err
}
gpxData, err := gpx.Parse(content)
if err != nil {
return "", err
}
gpxData.SimplifyTracks(50)
coordinates := make([][]float64, 4)
for _, trk := range gpxData.Tracks {
for _, seg := range trk.Segments {
for _, pt := range seg.Points {
coordinates = append(coordinates, []float64{pt.Latitude, pt.Longitude})
}
}
}
return string(polyline.EncodeCoords(coordinates)), nil
}
func documentFromListRecord(r *core.Record, author *core.Record, includeShares bool) (map[string]any, error) {
totalElevationGain := 0.0
@@ -359,18 +312,10 @@ func UpdateTrail(app core.App, r *core.Record, author *core.Record, client meili
}
documents := []map[string]interface{}{doc}
task, err := client.Index("trails").UpdateDocuments(documents, nil)
if err != nil {
if _, err = client.Index("trails").UpdateDocuments(documents, nil); err != nil {
return err
}
interval := 500 * time.Millisecond
_, err = client.WaitForTask(task.TaskUID, interval)
if err != nil {
return fmt.Errorf("meilisearch update trail: error waiting for task completion: %v", err)
}
return nil
}

64
db/util/polyline.go Normal file
View File

@@ -0,0 +1,64 @@
package util
import (
"bytes"
"fmt"
"io"
"github.com/pocketbase/pocketbase/core"
"github.com/tkrajina/gpxgo/gpx"
"github.com/twpayne/go-polyline"
)
func ComputePolyline(app core.App, r *core.Record) (string, error) {
gpxPath := r.GetString("gpx")
if len(gpxPath) == 0 {
return "", nil
}
fsys, err := app.NewFilesystem()
if err != nil {
return "", fmt.Errorf("open filesystem: %w", err)
}
defer fsys.Close()
gpxFilePath := r.BaseFilesPath() + "/" + gpxPath
gpxFile, err := fsys.GetReader(gpxFilePath)
if err != nil {
return "", fmt.Errorf("open gpx file %q: %w", gpxFilePath, err)
}
defer gpxFile.Close()
content := new(bytes.Buffer)
if _, err = io.Copy(content, gpxFile); err != nil {
return "", fmt.Errorf("read gpx file %q: %w", gpxFilePath, err)
}
gpxData, err := gpx.Parse(content)
if err != nil {
return "", fmt.Errorf("parse gpx file %q: %w", gpxFilePath, err)
}
gpxData.SimplifyTracks(50)
coordinates := make([][]float64, 0)
for _, trk := range gpxData.Tracks {
for _, seg := range trk.Segments {
for _, pt := range seg.Points {
coordinates = append(coordinates, []float64{pt.Latitude, pt.Longitude})
}
}
}
return string(polyline.EncodeCoords(coordinates)), nil
}
func SavePolyline(app core.App, r *core.Record) error {
encoded, err := ComputePolyline(app, r)
if err != nil {
return err
}
r.Set("polyline", encoded)
if err := app.UnsafeWithoutHooks().Save(r); err != nil {
return fmt.Errorf("save trail polyline: %w", err)
}
return nil
}