From d87b8b8f801b7ad8a7ec9ee05283abade0ec87c3 Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Tue, 17 Nov 2026 23:37:41 +0100 Subject: [PATCH] Unnamed imported tracks (FIT, ...) take the file's name, backfilled once for earlier imports; split and best-effort times with seconds --- lib/maptool/library.rb | 20 +++++++++++++++----- public/js/track_profile.js | 11 +++++++++-- test/test_app.rb | 1 + test/test_library.rb | 5 +++-- test/test_search.rb | 4 ++-- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lib/maptool/library.rb b/lib/maptool/library.rb index 7e157a2..cf3ad5a 100644 --- a/lib/maptool/library.rb +++ b/lib/maptool/library.rb @@ -37,7 +37,7 @@ module Maptool EXTENSIONS = %w[.gpx .fit .tcx .geojson].freeze # Bumped when a parser starts reading more (power, temperature, ...): # tracks imported before get their profile re-read once (reparse). - PARSER_VERSION = 2 + PARSER_VERSION = 3 def initialize(db_path, gpx_store:, sources_dir:) @path = db_path @@ -76,9 +76,9 @@ module Maptool def reparse return 0 if connect { |db| db.get_first_value("SELECT parser FROM meta") } >= PARSER_VERSION - rows = connect { |db| db.execute("SELECT id, type, geometry, properties, source_path FROM items WHERE deleted_at IS NULL AND source_path IS NOT NULL AND type IN ('track', 'route', 'layer')") } + rows = connect { |db| db.execute("SELECT id, type, name, geometry, properties, source_path FROM items WHERE deleted_at IS NULL AND source_path IS NOT NULL AND type IN ('track', 'route', 'layer')") } updated = 0 - rows.each do |id, type, geometry, props, source| + rows.each do |id, type, name, geometry, props, source| if type == "layer" @gpx_store.refresh(source) next @@ -90,9 +90,12 @@ module Maptool feature = parse(path).find { |f| item_type(f) == type && round(f["geometry"]) == geometry } next unless feature&.dig("properties", "profile") + name = default_name(source) if name.to_s.empty? + props = JSON.parse(props).merge(feature["properties"].slice("profile", "laps")) + props["name"] = name if props["name"].to_s.empty? write do |db, rev, now| - db.execute("UPDATE items SET properties = ?, updated_at = ?, rev = ? WHERE id = ?", - [JSON.generate(JSON.parse(props).merge(feature["properties"].slice("profile", "laps"))), now, rev, id]) + db.execute("UPDATE items SET name = ?, properties = ?, updated_at = ?, rev = ? WHERE id = ?", + [name, JSON.generate(props), now, rev, id]) end updated += 1 rescue Gpx::ParseError, Fit::ParseError, Errno::ENOENT, JSON::ParserError, GpxStore::InvalidPath @@ -228,6 +231,12 @@ module Maptool add_features(features, folder, source: relative) end + # FIT (and many TCX/GPX) tracks carry no name of their own: those take + # the file's, so the tree doesn't fill up with "Track". + def default_name(source) + File.basename(source, ".*") + end + # One hand-drawn note (notes.js) into `folder`; returns its node key. def add_note(feature, folder) add_features([feature], folder).first @@ -567,6 +576,7 @@ module Maptool features.sort_by { |f| f.dig("geometry", "type") == "Point" ? 1 : 0 }.each do |f| type = item_type(f) props = (f["properties"] || {}).compact + props["name"] = default_name(source) if source && props["name"].to_s.empty? && %w[track route].include?(type) fingerprint = Digest::SHA1.hexdigest(JSON.generate([type, props["name"], round(f["geometry"])])) existing = db.get_first_value("SELECT id FROM items WHERE folder_id IS ? AND fingerprint = ? AND deleted_at IS NULL", [target, fingerprint]) id = existing diff --git a/public/js/track_profile.js b/public/js/track_profile.js index 97c03c2..6b4a188 100644 --- a/public/js/track_profile.js +++ b/public/js/track_profile.js @@ -371,7 +371,7 @@ function statsTables(profile, bounds, laps, ride) { const rows = segmentStats(profile, bounds).map((s, i) => { const cells = [laps ? `${i + 1}` : km(s.to), ...(laps ? [km(s.meters)] : [])]; - if (hasTime) cells.push(s.secs ? formatDuration(s.secs) : "", s.secs ? pace(s.secs, s.meters) : ""); + if (hasTime) cells.push(s.secs ? formatClock(s.secs) : "", s.secs ? pace(s.secs, s.meters) : ""); cells.push(`↑ ${Math.round(s.gain)} m`, `↓ ${Math.round(s.loss)} m`); if (hasHr) cells.push(s.hr ? `${Math.round(s.hr)} bpm` : ""); if (hasPower) cells.push(s.power ? `${Math.round(s.power)} W` : ""); @@ -385,7 +385,7 @@ function statsTables(profile, bounds, laps, ride) { if (hasTime) { BEST_DISTANCES_KM.filter((km) => km * 1000 <= total).forEach((km) => { const secs = fastest(timed, km * 1000); - if (secs) best.push([`${km === 21.0975 ? "Half marathon" : km === 42.195 ? "Marathon" : `${km} km`}`, `${formatDuration(secs)} · ${pace(secs, km * 1000)}`]); + if (secs) best.push([`${km === 21.0975 ? "Half marathon" : km === 42.195 ? "Marathon" : `${km} km`}`, `${formatClock(secs)} · ${pace(secs, km * 1000)}`]); }); } if (hasPower) { @@ -551,6 +551,13 @@ function formatDuration(totalSeconds) { return hours > 0 ? `${hours}h ${minutes}m` : `${minutes}m`; } +// 4:32, 1:02:05 — with seconds, for splits and best efforts. +function formatClock(totalSeconds) { + const s = Math.round(totalSeconds); + const mmss = `${String(Math.floor((s % 3600) / 60)).padStart(s >= 3600 ? 2 : 1, "0")}:${String(s % 60).padStart(2, "0")}`; + return s >= 3600 ? `${Math.floor(s / 3600)}:${mmss}` : mmss; +} + function escapeHtml(text) { const div = document.createElement("div"); div.textContent = text; diff --git a/test/test_app.rb b/test/test_app.rb index 589acc7..d83a363 100644 --- a/test/test_app.rb +++ b/test/test_app.rb @@ -167,6 +167,7 @@ class AppTest < Minitest::Test properties = json_body(request.get(node_url(key)))["features"][0]["properties"] assert_equal "cycling", properties["activity_type"] assert_equal [0, 1], properties["laps"] + assert_equal "ride", properties["name"] # no name in the file: the file's fit = request.get("#{node_url(key)}?download=fit&name=Ride", **logged_in_env) assert_equal 200, fit.status diff --git a/test/test_library.rb b/test/test_library.rb index 4dafccc..a91f633 100644 --- a/test/test_library.rb +++ b/test/test_library.rb @@ -181,16 +181,17 @@ class LibraryTest < Minitest::Test library = open_library key = library.import(folder: "", filename: "ride.gpx", io: File.open(fixture_path("sample.gpx"))).find { |k| k.start_with?("item:") } id = key.delete_prefix("item:") - # As if imported by an older parser: no profile yet, renamed since. + # As if imported by an older parser: no profile or name yet. db = SQLite3::Database.new(File.join(@dir, "library.sqlite")) props = JSON.parse(db.get_first_value("SELECT properties FROM items WHERE id = ?", [id])) - db.execute("UPDATE items SET properties = ?, name = 'Mine' WHERE id = ?", [JSON.generate(props.except("profile").merge("color" => "#ff0000")), id]) + db.execute("UPDATE items SET properties = ?, name = NULL WHERE id = ?", [JSON.generate(props.except("profile", "name").merge("color" => "#ff0000")), id]) db.execute("UPDATE meta SET parser = 0") assert_equal 1, library.reparse props = JSON.parse(db.get_first_value("SELECT properties FROM items WHERE id = ?", [id])) assert props["profile"].any? assert_equal "#ff0000", props["color"] + assert_equal "ride", db.get_first_value("SELECT name FROM items WHERE id = ?", [id]) # unnamed: the file's assert_equal 0, library.reparse # done once end diff --git a/test/test_search.rb b/test/test_search.rb index aa2bf07..ab67c58 100644 --- a/test/test_search.rb +++ b/test/test_search.rb @@ -25,7 +25,7 @@ class SearchTest < Minitest::Test def test_finds_across_sources_ignoring_case_and_diacritics_and_says_where layer = search("kosice") - assert_equal [["Kemp Košice", "layers", "trips / Track"]], layer.map { |r| r.values_at(:name, :source, :source_label) } + assert_equal [["Kemp Košice", "layers", "trips / camps"]], layer.map { |r| r.values_at(:name, :source, :source_label) } assert_match(/\Aitem:/, layer.first[:ref]) trail = search("snp hrdinov").first @@ -35,7 +35,7 @@ class SearchTest < Minitest::Test def test_matches_description_and_falls_back_to_node_label_with_bbox assert_equal ["Kemp Košice"], search("voda").map { |r| r[:name] } - track = search("trips track").first + track = search("camps").find { |r| r[:name] == "camps" } assert_equal [17.0, 48.0, 18.0, 49.0], track[:bbox] end