From 3e487539bc36e08b7088340fc1226f544a77770d Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Mon, 16 Nov 2026 18:46:58 +0100 Subject: [PATCH] Re-read already-uploaded tracks once so their profiles get power and temperature --- app.rb | 6 ++++++ lib/maptool/gpx_store.rb | 6 ++++++ lib/maptool/library.rb | 38 +++++++++++++++++++++++++++++++++++++- test/test_library.rb | 17 +++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/app.rb b/app.rb index b64b828..67abf88 100644 --- a/app.rb +++ b/app.rb @@ -107,6 +107,12 @@ module Maptool GPX_STORE = GpxStore.new(File.join(DATA_DIR, "gpx")) LIBRARY = Library.new(File.join(DATA_DIR, "library.sqlite"), gpx_store: GPX_STORE, sources_dir: File.join(DATA_DIR, "library", "sources")) + # Once per parser upgrade, in the background so boot isn't held up. + Thread.new do + warn "library: re-read #{LIBRARY.reparse} track profiles" + rescue StandardError => e + warn "library reparse failed: #{e.message}" + end LIVETRACK_STORE = LiveTrackStore.new(File.join(DATA_DIR, "livetrack.json")) # Not under DATA_DIR: unlike everything else there (uploads, PMTiles, diff --git a/lib/maptool/gpx_store.rb b/lib/maptool/gpx_store.rb index 1864337..15cd767 100644 --- a/lib/maptool/gpx_store.rb +++ b/lib/maptool/gpx_store.rb @@ -128,6 +128,12 @@ module Maptool File.read(cache) end + # Drops a file's cached GeoJSON so the next read converts it again + # (Library#reparse, after a parser learned to read more). + def refresh(relative_path) + FileUtils.rm_f(geojson_cache_path(resolve_file(relative_path))) + end + # Appends one GeoJSON Feature (a hand-drawn point/note, see notes.js) to # the FeatureCollection at `path` — or, if `path` is nil, to a new or # existing file named `name` inside `folder`. Deliberately overwrites in diff --git a/lib/maptool/library.rb b/lib/maptool/library.rb index f3a0dcb..3a9d4b6 100644 --- a/lib/maptool/library.rb +++ b/lib/maptool/library.rb @@ -35,6 +35,9 @@ module Maptool ROOT = "root" # folder id in a node key for items with no folder 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 = 1 def initialize(db_path, gpx_store:, sources_dir:) @path = db_path @@ -46,7 +49,7 @@ module Maptool db.execute("PRAGMA journal_mode = WAL") db.execute_batch(<<~SQL) CREATE TABLE IF NOT EXISTS meta (rev INTEGER NOT NULL); - INSERT INTO meta SELECT 0 WHERE NOT EXISTS (SELECT 1 FROM meta); + INSERT INTO meta (rev) SELECT 0 WHERE NOT EXISTS (SELECT 1 FROM meta); CREATE TABLE IF NOT EXISTS folders ( id TEXT PRIMARY KEY, parent_id TEXT, name TEXT NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL, deleted_at TEXT, rev INTEGER NOT NULL @@ -62,10 +65,43 @@ module Maptool CREATE INDEX IF NOT EXISTS items_fingerprint ON items(folder_id, fingerprint); CREATE INDEX IF NOT EXISTS folders_rev ON folders(rev); SQL + db.execute("ALTER TABLE meta ADD COLUMN parser INTEGER NOT NULL DEFAULT 0") unless db.execute("PRAGMA table_info(meta)").any? { |c| c[1] == "parser" } end migrate if fresh end + # Re-reads every imported track's/route's profile from its source file + # (and drops migrated layers' cached GeoJSON) when PARSER_VERSION went + # up; name, color and the rest stay. Returns how many were updated. + 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')") } + updated = 0 + rows.each do |id, type, geometry, props, source| + if type == "layer" + @gpx_store.refresh(source) + next + end + path = File.join(@sources_dir, source) + next if !File.file?(path) || path.downcase.end_with?(".geojson") + + geometry = round(JSON.parse(geometry)) + feature = parse(path).find { |f| item_type(f) == type && round(f["geometry"]) == geometry } + next unless feature&.dig("properties", "profile") + + write do |db, rev, now| + db.execute("UPDATE items SET properties = ?, updated_at = ?, rev = ? WHERE id = ?", + [JSON.generate(JSON.parse(props).merge("profile" => feature["properties"]["profile"])), now, rev, id]) + end + updated += 1 + rescue Gpx::ParseError, Fit::ParseError, Errno::ENOENT, JSON::ParserError, GpxStore::InvalidPath + next + end + connect { |db| db.execute("UPDATE meta SET parser = ?", [PARSER_VERSION]) } + updated + end + def rev connect { |db| db.get_first_value("SELECT rev FROM meta") } end diff --git a/test/test_library.rb b/test/test_library.rb index 8e04fce..4dafccc 100644 --- a/test/test_library.rb +++ b/test/test_library.rb @@ -177,6 +177,23 @@ class LibraryTest < Minitest::Test assert_raises(Maptool::Library::NotFound) { library.export_gpx("nope") } end + def test_reparse_rereads_old_profiles_once_and_keeps_the_name + 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. + 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 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 0, library.reparse # done once + end + private def write_tmp(content)