Skip to content

Commit

Permalink
map speed improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Beutel committed Feb 10, 2025
1 parent 3173be8 commit 550e3da
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 94 deletions.
27 changes: 20 additions & 7 deletions db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ func registerMigrations(app *pocketbase.PocketBase) {
func setupEventHandlers(app *pocketbase.PocketBase, client meilisearch.ServiceManager) {
app.OnModelAfterCreate("users").Add(createUserHandler(app, client))

app.OnModelAfterCreate("trails").Add(createTrailIndexHandler(client))
app.OnModelAfterCreate("trails").Add(createTrailIndexHandler(app, client))

app.OnRecordAfterCreateRequest("trails").Add(createTrailHandler(app))
app.OnRecordAfterUpdateRequest("trails").Add(updateTrailHandler(client))
app.OnRecordAfterUpdateRequest("trails").Add(updateTrailHandler(app, client))
app.OnRecordAfterDeleteRequest("trails").Add(deleteTrailHandler(client))

app.OnRecordAfterCreateRequest("trail_share").Add(createTrailShareHandler(app, client))
Expand Down Expand Up @@ -128,10 +128,14 @@ func createDefaultUserSettings(app *pocketbase.PocketBase, userId string) error
return app.Dao().SaveRecord(settings)
}

func createTrailIndexHandler(client meilisearch.ServiceManager) func(e *core.ModelEvent) error {
func createTrailIndexHandler(app *pocketbase.PocketBase, client meilisearch.ServiceManager) func(e *core.ModelEvent) error {
return func(e *core.ModelEvent) error {
record := e.Model.(*models.Record)
if err := util.IndexTrail(record, client); err != nil {
author, err := app.Dao().FindRecordById("users", record.GetString(("author")))
if err != nil {
return err
}
if err := util.IndexTrail(record, author, client); err != nil {
return err
}
return nil
Expand All @@ -156,9 +160,13 @@ func createTrailHandler(app *pocketbase.PocketBase) func(e *core.RecordCreateEve
}
}

func updateTrailHandler(client meilisearch.ServiceManager) func(e *core.RecordUpdateEvent) error {
func updateTrailHandler(app *pocketbase.PocketBase, client meilisearch.ServiceManager) func(e *core.RecordUpdateEvent) error {
return func(e *core.RecordUpdateEvent) error {
return util.UpdateTrail(e.Record, client)
author, err := app.Dao().FindRecordById("users", e.Record.GetString(("author")))
if err != nil {
return err
}
return util.UpdateTrail(e.Record, author, client)
}
}

Expand Down Expand Up @@ -727,8 +735,13 @@ func bootstrapMeilisearchTrails(app *pocketbase.PocketBase, client meilisearch.S
return err
}

client.Index("trails").DeleteAllDocuments()
for _, trail := range trails {
if err := util.UpdateTrail(trail, client); err != nil {
author, err := app.Dao().FindRecordById("users", trail.GetString(("author")))
if err != nil {
return err
}
if err := util.IndexTrail(trail, author, client); err != nil {
return err
}
}
Expand Down
20 changes: 15 additions & 5 deletions db/util/meilisearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ import (
"github.com/pocketbase/pocketbase/models"
)

func documentFromTrailRecord(r *models.Record, includeShares bool) map[string]interface{} {
func documentFromTrailRecord(r *models.Record, author *models.Record, includeShares bool) map[string]interface{} {
photos := r.GetStringSlice("photos")
thumbnail := ""
if len(photos) > 0 {
thumbnail = r.GetStringSlice("photos")[r.GetInt("thumbnail")]
}

document := map[string]interface{}{
"id": r.Id,
"author": r.GetString("author"),
"author_name": author.GetString("username"),
"author_avatar": author.GetString("avatar"),
"name": r.GetString("name"),
"description": r.GetString("description"),
"location": r.GetString("location"),
Expand All @@ -25,6 +33,8 @@ func documentFromTrailRecord(r *models.Record, includeShares bool) map[string]in
"date": r.GetDateTime("date").Time().Unix(),
"created": r.GetDateTime("created").Time().Unix(),
"public": r.GetBool("public"),
"thumbnail": thumbnail,
"gpx": r.GetString("gpx"),
"_geo": map[string]float64{
"lat": r.GetFloat("lat"),
"lng": r.GetFloat("lon"),
Expand Down Expand Up @@ -56,8 +66,8 @@ func documentFromListRecord(r *models.Record, includeShares bool) map[string]int
return document
}

func IndexTrail(r *models.Record, client meilisearch.ServiceManager) error {
documents := []map[string]interface{}{documentFromTrailRecord(r, true)}
func IndexTrail(r *models.Record, author *models.Record, client meilisearch.ServiceManager) error {
documents := []map[string]interface{}{documentFromTrailRecord(r, author, true)}

if _, err := client.Index("trails").AddDocuments(documents); err != nil {
return err
Expand All @@ -66,8 +76,8 @@ func IndexTrail(r *models.Record, client meilisearch.ServiceManager) error {
return nil
}

func UpdateTrail(r *models.Record, client meilisearch.ServiceManager) error {
documents := documentFromTrailRecord(r, false)
func UpdateTrail(r *models.Record, author *models.Record, client meilisearch.ServiceManager) error {
documents := documentFromTrailRecord(r, author, false)

if _, err := client.Index("trails").UpdateDocuments(documents); err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions web/src/lib/components/trail/trail_card.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<div
class="relative w-full basis-full max-h-48 overflow-hidden rounded-t-2xl"
>
<img class="h-full w-full" id="header-img" src={thumbnail} alt="" />
<img loading="lazy" class="w-full h-full" id="header-img" src={thumbnail} alt="" />
</div>
{#if (trail.public || trailIsShared) && pb.authStore.model}
<div
Expand All @@ -73,7 +73,9 @@
</span>
{/if}
{#if trail.expand?.trail_share_via_trail?.length}
<ShareInfo type="trail" subject={trail}></ShareInfo>
<span class="tooltip" data-title={$_("shared")}>
<i class="fa fa-share-nodes"></i>
</span>
{/if}
</div>
{/if}
Expand Down
6 changes: 3 additions & 3 deletions web/src/lib/components/trail/trail_info_panel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@
<div
class="flex absolute justify-between items-end w-full bottom-8 left-0 px-8 gap-y-4"
>
<div class="text-white">
<h4 class="text-4xl font-bold">
<div class="text-white overflow-hidden">
<h4 title={trail.name} class="text-4xl font-bold line-clamp-3">
{trail.name}
</h4>
{#if trail.date}
Expand Down Expand Up @@ -370,7 +370,7 @@
<EmptyStateDescription></EmptyStateDescription>
{/if}
<h4 class="text-2xl font-semibold mb-6 mt-12">
{$_("route", { values: { n: 2 } })}
{$_("route", { values: { n: 1 } })}
</h4>
{#if mode === "overview"}
<div
Expand Down
2 changes: 1 addition & 1 deletion web/src/lib/components/trail/trail_timeline.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<div class="p-4">
<h5 class="text-xl font-semibold">{wp.name}</h5>
<span class="text-sm text-gray-500"
>{wp.lat.toFixed(5)}, {wp.lon.toFixed(5)}</span
><i class="fa fa-location-dot mr-1"></i> {wp.lat.toFixed(5)}, {wp.lon.toFixed(5)}</span
>
<p class="whitespace-pre-line">
{wp.description}
Expand Down
37 changes: 33 additions & 4 deletions web/src/lib/models/trail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class Trail {
summit_logs: string[];
expand?: {
category?: Category;
waypoints: Waypoint[]
summit_logs: SummitLog[]
waypoints?: Waypoint[]
summit_logs?: SummitLog[]
author?: UserAnonymous
comments_via_trail: Comment[]
comments_via_trail?: Comment[]
gpx_data?: string
trail_share_via_trail?: TrailShare[]
}
Expand Down Expand Up @@ -142,6 +142,35 @@ interface TrailBoundingBox {
min_lon: number,
}


interface TrailSearchResult {
id: string;
author: string;
author_name: string;
author_avatar: string;
name: string;
description: string;
location: string;
distance: number;
elevation_gain: number;
elevation_loss: number;
duration: number;
difficulty: "easy" | "moderate" | "difficult";
category: string;
completed: boolean;
date: number;
created: number;
public: boolean;
thumbnail: string;
shares?: string[];
gpx: string;
_geo: {
lat: number,
lng: number
};
}


export { Trail };

export type { TrailBoundingBox, TrailFilter, TrailFilterValues };
export type { TrailBoundingBox, TrailFilter, TrailFilterValues, TrailSearchResult };
Loading

0 comments on commit 550e3da

Please sign in to comment.