Re-read already-uploaded tracks once so their profiles get power and temperature
CI / check (push) Successful in 19s
CI / deploy (push) Successful in 22s

This commit is contained in:
randy committed 2026-11-16 18:46:58 +01:00
1 parent ebd4852e98
commit 3e487539bc
4 files changed
+66 -1

No files matched your search

+6
View File
@@ -107,6 +107,12 @@ module Maptool
GPX_STORE = GpxStore.new(File.join(DATA_DIR, "gpx")) GPX_STORE = GpxStore.new(File.join(DATA_DIR, "gpx"))
LIBRARY = Library.new(File.join(DATA_DIR, "library.sqlite"), gpx_store: GPX_STORE, LIBRARY = Library.new(File.join(DATA_DIR, "library.sqlite"), gpx_store: GPX_STORE,
sources_dir: File.join(DATA_DIR, "library", "sources")) 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")) LIVETRACK_STORE = LiveTrackStore.new(File.join(DATA_DIR, "livetrack.json"))
# Not under DATA_DIR: unlike everything else there (uploads, PMTiles, # Not under DATA_DIR: unlike everything else there (uploads, PMTiles,
+6
View File
@@ -128,6 +128,12 @@ module Maptool
File.read(cache) File.read(cache)
end 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 # 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 # the FeatureCollection at `path` — or, if `path` is nil, to a new or
# existing file named `name` inside `folder`. Deliberately overwrites in # existing file named `name` inside `folder`. Deliberately overwrites in
+37 -1
View File
@@ -35,6 +35,9 @@ module Maptool
ROOT = "root" # folder id in a node key for items with no folder ROOT = "root" # folder id in a node key for items with no folder
EXTENSIONS = %w[.gpx .fit .tcx .geojson].freeze 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:) def initialize(db_path, gpx_store:, sources_dir:)
@path = db_path @path = db_path
@@ -46,7 +49,7 @@ module Maptool
db.execute("PRAGMA journal_mode = WAL") db.execute("PRAGMA journal_mode = WAL")
db.execute_batch(<<~SQL) db.execute_batch(<<~SQL)
CREATE TABLE IF NOT EXISTS meta (rev INTEGER NOT NULL); 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 ( CREATE TABLE IF NOT EXISTS folders (
id TEXT PRIMARY KEY, parent_id TEXT, name TEXT NOT NULL, 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 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 items_fingerprint ON items(folder_id, fingerprint);
CREATE INDEX IF NOT EXISTS folders_rev ON folders(rev); CREATE INDEX IF NOT EXISTS folders_rev ON folders(rev);
SQL 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 end
migrate if fresh migrate if fresh
end 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 def rev
connect { |db| db.get_first_value("SELECT rev FROM meta") } connect { |db| db.get_first_value("SELECT rev FROM meta") }
end end
+17
View File
@@ -177,6 +177,23 @@ class LibraryTest < Minitest::Test
assert_raises(Maptool::Library::NotFound) { library.export_gpx("nope") } assert_raises(Maptool::Library::NotFound) { library.export_gpx("nope") }
end 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 private
def write_tmp(content) def write_tmp(content)