updates trail filter and sort with elevation loss

This commit is contained in:
Christian Beutel
2024-11-09 14:46:14 +01:00
parent 529cae0b82
commit 50a8b0a87a
11 changed files with 448 additions and 5 deletions

View File

@@ -0,0 +1,342 @@
package migrations
import (
"encoding/json"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/models/schema"
)
func init() {
m.Register(func(db dbx.Builder) error {
dao := daos.New(db);
collection, err := dao.FindCollectionByNameOrId("4wbv9tz5zjdrjh1")
if err != nil {
return err
}
options := map[string]any{}
if err := json.Unmarshal([]byte(`{
"query": "SELECT users.id, COALESCE(MAX(trails.distance), 0) AS max_distance,\n COALESCE(MAX(trails.elevation_gain), 0) AS max_elevation_gain, \n COALESCE(MAX(trails.elevation_loss), 0) AS max_elevation_loss, \n COALESCE(MAX(trails.duration), 0) AS max_duration, \n COALESCE(MIN(trails.distance), 0) AS min_distance, \n COALESCE(MIN(trails.elevation_gain), 0) AS min_elevation_gain, \n COALESCE(MIN(trails.elevation_loss), 0) AS min_elevation_loss, \n COALESCE(MIN(trails.duration), 0) AS min_duration \nFROM users \n LEFT JOIN trails ON \n users.id = trails.author OR \n trails.public = 1 OR \n EXISTS (\n SELECT 1 \n FROM trail_share \n WHERE trail_share.trail = trails.id \n AND trail_share.user = users.id\n ) GROUP BY users.id;"
}`), &options); err != nil {
return err
}
collection.SetOptions(options)
// remove
collection.Schema.RemoveField("ixfa8u8m")
// remove
collection.Schema.RemoveField("bhn0jj40")
// remove
collection.Schema.RemoveField("3kujiwzh")
// remove
collection.Schema.RemoveField("yf3zwzn4")
// remove
collection.Schema.RemoveField("skvtkith")
// remove
collection.Schema.RemoveField("gydvo1ug")
// add
new_max_distance := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "oyvx8oaq",
"name": "max_distance",
"type": "json",
"required": false,
"presentable": false,
"unique": false,
"options": {
"maxSize": 1
}
}`), new_max_distance); err != nil {
return err
}
collection.Schema.AddField(new_max_distance)
// add
new_max_elevation_gain := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "4xu7voeh",
"name": "max_elevation_gain",
"type": "json",
"required": false,
"presentable": false,
"unique": false,
"options": {
"maxSize": 1
}
}`), new_max_elevation_gain); err != nil {
return err
}
collection.Schema.AddField(new_max_elevation_gain)
// add
new_max_elevation_loss := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "sqzilovv",
"name": "max_elevation_loss",
"type": "json",
"required": false,
"presentable": false,
"unique": false,
"options": {
"maxSize": 1
}
}`), new_max_elevation_loss); err != nil {
return err
}
collection.Schema.AddField(new_max_elevation_loss)
// add
new_max_duration := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "rupidtew",
"name": "max_duration",
"type": "json",
"required": false,
"presentable": false,
"unique": false,
"options": {
"maxSize": 1
}
}`), new_max_duration); err != nil {
return err
}
collection.Schema.AddField(new_max_duration)
// add
new_min_distance := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "2y7dbhf9",
"name": "min_distance",
"type": "json",
"required": false,
"presentable": false,
"unique": false,
"options": {
"maxSize": 1
}
}`), new_min_distance); err != nil {
return err
}
collection.Schema.AddField(new_min_distance)
// add
new_min_elevation_gain := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "ooiwexpc",
"name": "min_elevation_gain",
"type": "json",
"required": false,
"presentable": false,
"unique": false,
"options": {
"maxSize": 1
}
}`), new_min_elevation_gain); err != nil {
return err
}
collection.Schema.AddField(new_min_elevation_gain)
// add
new_min_elevation_loss := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "ondmfwce",
"name": "min_elevation_loss",
"type": "json",
"required": false,
"presentable": false,
"unique": false,
"options": {
"maxSize": 1
}
}`), new_min_elevation_loss); err != nil {
return err
}
collection.Schema.AddField(new_min_elevation_loss)
// add
new_min_duration := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "kcrhzmaw",
"name": "min_duration",
"type": "json",
"required": false,
"presentable": false,
"unique": false,
"options": {
"maxSize": 1
}
}`), new_min_duration); err != nil {
return err
}
collection.Schema.AddField(new_min_duration)
return dao.SaveCollection(collection)
}, func(db dbx.Builder) error {
dao := daos.New(db);
collection, err := dao.FindCollectionByNameOrId("4wbv9tz5zjdrjh1")
if err != nil {
return err
}
options := map[string]any{}
if err := json.Unmarshal([]byte(`{
"query": "SELECT users.id, COALESCE(MAX(trails.distance), 0) AS max_distance, COALESCE(MAX(trails.elevation_gain), 0) AS max_elevation_gain, COALESCE(MAX(trails.duration), 0) AS max_duration, COALESCE(MIN(trails.distance), 0) AS min_distance, COALESCE(MIN(trails.elevation_gain), 0) AS min_elevation_gain, COALESCE(MIN(trails.duration), 0) AS min_duration FROM users LEFT JOIN trails ON users.id = trails.author OR trails.public = 1 OR EXISTS (\n SELECT 1 \n FROM trail_share \n WHERE trail_share.trail = trails.id \n AND trail_share.user = users.id\n ) GROUP BY users.id;"
}`), &options); err != nil {
return err
}
collection.SetOptions(options)
// add
del_max_distance := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "ixfa8u8m",
"name": "max_distance",
"type": "json",
"required": false,
"presentable": false,
"unique": false,
"options": {
"maxSize": 1
}
}`), del_max_distance); err != nil {
return err
}
collection.Schema.AddField(del_max_distance)
// add
del_max_elevation_gain := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "bhn0jj40",
"name": "max_elevation_gain",
"type": "json",
"required": false,
"presentable": false,
"unique": false,
"options": {
"maxSize": 1
}
}`), del_max_elevation_gain); err != nil {
return err
}
collection.Schema.AddField(del_max_elevation_gain)
// add
del_max_duration := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "3kujiwzh",
"name": "max_duration",
"type": "json",
"required": false,
"presentable": false,
"unique": false,
"options": {
"maxSize": 1
}
}`), del_max_duration); err != nil {
return err
}
collection.Schema.AddField(del_max_duration)
// add
del_min_distance := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "yf3zwzn4",
"name": "min_distance",
"type": "json",
"required": false,
"presentable": false,
"unique": false,
"options": {
"maxSize": 1
}
}`), del_min_distance); err != nil {
return err
}
collection.Schema.AddField(del_min_distance)
// add
del_min_elevation_gain := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "skvtkith",
"name": "min_elevation_gain",
"type": "json",
"required": false,
"presentable": false,
"unique": false,
"options": {
"maxSize": 1
}
}`), del_min_elevation_gain); err != nil {
return err
}
collection.Schema.AddField(del_min_elevation_gain)
// add
del_min_duration := &schema.SchemaField{}
if err := json.Unmarshal([]byte(`{
"system": false,
"id": "gydvo1ug",
"name": "min_duration",
"type": "json",
"required": false,
"presentable": false,
"unique": false,
"options": {
"maxSize": 1
}
}`), del_min_duration); err != nil {
return err
}
collection.Schema.AddField(del_min_duration)
// remove
collection.Schema.RemoveField("oyvx8oaq")
// remove
collection.Schema.RemoveField("4xu7voeh")
// remove
collection.Schema.RemoveField("sqzilovv")
// remove
collection.Schema.RemoveField("rupidtew")
// remove
collection.Schema.RemoveField("2y7dbhf9")
// remove
collection.Schema.RemoveField("ooiwexpc")
// remove
collection.Schema.RemoveField("ondmfwce")
// remove
collection.Schema.RemoveField("kcrhzmaw")
return dao.SaveCollection(collection)
})
}

View File

@@ -0,0 +1,54 @@
package migrations
import (
"os"
"github.com/meilisearch/meilisearch-go"
"github.com/pocketbase/dbx"
m "github.com/pocketbase/pocketbase/migrations"
)
func init() {
client := meilisearch.NewClient(meilisearch.ClientConfig{
Host: os.Getenv("MEILI_URL"),
APIKey: os.Getenv("MEILI_MASTER_KEY"),
})
m.Register(func(db dbx.Builder) error {
_, err := client.Index("trails").UpdateSortableAttributes(&[]string{
"created", "date", "difficulty", "distance", "elevation_gain", "elevation_loss", "name",
})
if err != nil {
return err
}
_, err = client.Index("trails").UpdateFilterableAttributes(&[]string{
"_geo", "author", "category", "completed", "date", "difficulty", "distance", "elevation_gain", "elevation_loss", "public", "shares",
})
if err != nil {
return err
}
return nil
}, func(db dbx.Builder) error {
_, err := client.Index("trails").UpdateSortableAttributes(&[]string{
"created", "date", "difficulty", "distance", "elevation_gain", "name",
})
if err != nil {
return err
}
_, err = client.Index("trails").UpdateFilterableAttributes(&[]string{
"_geo", "author", "category", "completed", "date", "difficulty", "distance", "elevation_gain", "public", "shares",
})
if err != nil {
return err
}
return nil
})
}

View File

@@ -17,6 +17,7 @@ func documentFromTrailRecord(r *models.Record, includeShares bool) map[string]in
"location": r.GetString("location"),
"distance": r.GetFloat("distance"),
"elevation_gain": r.GetFloat("elevation_gain"),
"elevation_loss": r.GetFloat("elevation_loss"),
"duration": r.GetFloat("duration"),
"difficulty": r.Get("difficulty"),
"category": r.Get("category"),

View File

@@ -3,6 +3,17 @@ title: Changelog
description: What changed in the last patch?
---
## v0.10.1
### Features
- Adds elevation loss to trails. Please note that trails created before this version will have a default elevation loss of 0. Edit & save to update.
### Bug fixes
- Fixes bug that caused auto-added summit logs to not have distance, durtaion etc.
- Fixes error in the auto-upload feature
- Fixes access permissions for profile page
- A summit log is now also created automatically when you upload a trail directly
- Fixes link to the API documentation in footer
## v0.10.0
### Features
- A new summit log entry is now added automatically when uploading a new GPX file for a new or existing trail

View File

@@ -29,7 +29,7 @@ login() {
upload_and_delete() {
local file="$1"
ls $file
ls "$file"
# API call to upload file
response=$(curl -b cookie.txt --location --request PUT "$API_URL/trail/upload" --header 'Content-Type: multipart/form-data' -F "file=@$file" -F "name=$base_name")

View File

@@ -239,17 +239,36 @@
>
</div>
<hr class="my-4 border-separator" />
<p class="text-sm font-medium pb-4">{$_("elevation-loss")}</p>
<DoubleSlider
minValue={filter.elevationLossMin}
maxValue={filter.elevationLossLimit}
bind:currentMin={filter.elevationLossMin}
bind:currentMax={filter.elevationLossMax}
on:set={() => update()}
></DoubleSlider>
<div class="flex justify-between">
<span>{formatElevation(filter.elevationLossMin)}</span>
<span
>{formatElevation(
filter.elevationLossMax,
)}{filter.elevationLossMax == filter.elevationLossLimit
? "+"
: ""}</span
>
</div>
<hr class="my-4 border-separator" />
<div class="space-y-2">
<Datepicker
name="startDate"
label={$_('after')}
label={$_("after")}
bind:value={filter.startDate}
on:change={update}
></Datepicker>
<Datepicker
name="endDate"
label={$_('before')}
label={$_("before")}
bind:value={filter.endDate}
on:change={update}
></Datepicker>

View File

@@ -31,6 +31,7 @@
{ text: $_("difficulty"), value: "difficulty" },
{ text: $_("distance"), value: "distance" },
{ text: $_("elevation-gain"), value: "elevation_gain" },
{ text: $_("elevation-loss"), value: "elevation_loss" },
];
onMount(() => {

View File

@@ -108,6 +108,9 @@ interface TrailFilter {
elevationGainMin: number;
elevationGainMax: number;
elevationGainLimit: number;
elevationLossMin: number;
elevationLossMax: number;
elevationLossLimit: number;
startDate?: string;
endDate?: string;
completed?: boolean;
@@ -120,6 +123,8 @@ interface TrailFilterValues {
max_distance: number,
min_elevation_gain: number,
max_elevation_gain: number,
min_elevation_loss: number,
max_elevation_loss: number,
min_durtation: number,
max_duration: number,
}

View File

@@ -153,7 +153,7 @@ export async function trails_show(id: string, loadGPX?: boolean, f: (url: Reques
}
response.expand.gpx_data = gpxData;
for (const log of response.expand.summit_logs ?? []) {
const gpxData: string = await fetchGPX(log, f);
@@ -387,7 +387,7 @@ export async function fetchGPX(trail: { gpx?: string } & Record<string, any>, f:
function buildFilterText(filter: TrailFilter, includeGeo: boolean): string {
let filterText: string = "";
filterText += `distance >= ${Math.floor(filter.distanceMin)} AND elevation_gain >= ${Math.floor(filter.elevationGainMin)}`
filterText += `distance >= ${Math.floor(filter.distanceMin)} AND elevation_gain >= ${Math.floor(filter.elevationGainMin)} AND elevation_loss >= ${Math.floor(filter.elevationLossMin)}`
if (filter.distanceMax < filter.distanceLimit) {
filterText += ` AND distance <= ${Math.ceil(filter.distanceMax)}`
@@ -397,6 +397,10 @@ function buildFilterText(filter: TrailFilter, includeGeo: boolean): string {
filterText += ` AND elevation_gain <= ${Math.ceil(filter.elevationGainMax)}`
}
if (filter.elevationLossMax < filter.elevationLossLimit) {
filterText += ` AND elevation_loss <= ${Math.ceil(filter.elevationLossMax)}`
}
filterText += ` AND difficulty IN [${filter.difficulty.join(",")}]`
if (filter.startDate) {

View File

@@ -20,6 +20,9 @@ export const load: ServerLoad = async ({ params, locals, fetch }) => {
elevationGainMin: 0,
elevationGainMax: filterValues.max_elevation_gain,
elevationGainLimit: filterValues.max_elevation_gain,
elevationLossMin: 0,
elevationLossMax: filterValues.max_elevation_loss,
elevationLossLimit: filterValues.max_elevation_gain,
sort: "created",
sortOrder: "+",
};

View File

@@ -19,6 +19,9 @@ export const load: ServerLoad = async ({ params, locals, url, fetch }) => {
elevationGainMin: 0,
elevationGainMax: filterValues.max_elevation_gain,
elevationGainLimit: filterValues.max_elevation_gain,
elevationLossMin: 0,
elevationLossMax: filterValues.max_elevation_loss,
elevationLossLimit: filterValues.max_elevation_loss,
sort: "created",
sortOrder: "+",
};