Re-read already-uploaded tracks once so their profiles get power and temperature
This commit is contained in:
1 parent
ebd4852e98
commit
3e487539bc
4 files changed
+66
-1
No files matched your search
@@ -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
|
||||
|
||||
+37
-1
@@ -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
|
||||
|
||||
Reference in new issue
Block a user