Unnamed imported tracks (FIT, ...) take the file's name, backfilled once for earlier imports; split and best-effort times with seconds
This commit is contained in:
1 parent
21c5c2f21b
commit
d87b8b8f80
5 files changed
+30
-11
No files matched your search
+15
-5
@@ -37,7 +37,7 @@ module Maptool
|
|||||||
EXTENSIONS = %w[.gpx .fit .tcx .geojson].freeze
|
EXTENSIONS = %w[.gpx .fit .tcx .geojson].freeze
|
||||||
# Bumped when a parser starts reading more (power, temperature, ...):
|
# Bumped when a parser starts reading more (power, temperature, ...):
|
||||||
# tracks imported before get their profile re-read once (reparse).
|
# tracks imported before get their profile re-read once (reparse).
|
||||||
PARSER_VERSION = 2
|
PARSER_VERSION = 3
|
||||||
|
|
||||||
def initialize(db_path, gpx_store:, sources_dir:)
|
def initialize(db_path, gpx_store:, sources_dir:)
|
||||||
@path = db_path
|
@path = db_path
|
||||||
@@ -76,9 +76,9 @@ module Maptool
|
|||||||
def reparse
|
def reparse
|
||||||
return 0 if connect { |db| db.get_first_value("SELECT parser FROM meta") } >= PARSER_VERSION
|
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
|
updated = 0
|
||||||
rows.each do |id, type, geometry, props, source|
|
rows.each do |id, type, name, geometry, props, source|
|
||||||
if type == "layer"
|
if type == "layer"
|
||||||
@gpx_store.refresh(source)
|
@gpx_store.refresh(source)
|
||||||
next
|
next
|
||||||
@@ -90,9 +90,12 @@ module Maptool
|
|||||||
feature = parse(path).find { |f| item_type(f) == type && round(f["geometry"]) == geometry }
|
feature = parse(path).find { |f| item_type(f) == type && round(f["geometry"]) == geometry }
|
||||||
next unless feature&.dig("properties", "profile")
|
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|
|
write do |db, rev, now|
|
||||||
db.execute("UPDATE items SET properties = ?, updated_at = ?, rev = ? WHERE id = ?",
|
db.execute("UPDATE items SET name = ?, properties = ?, updated_at = ?, rev = ? WHERE id = ?",
|
||||||
[JSON.generate(JSON.parse(props).merge(feature["properties"].slice("profile", "laps"))), now, rev, id])
|
[name, JSON.generate(props), now, rev, id])
|
||||||
end
|
end
|
||||||
updated += 1
|
updated += 1
|
||||||
rescue Gpx::ParseError, Fit::ParseError, Errno::ENOENT, JSON::ParserError, GpxStore::InvalidPath
|
rescue Gpx::ParseError, Fit::ParseError, Errno::ENOENT, JSON::ParserError, GpxStore::InvalidPath
|
||||||
@@ -228,6 +231,12 @@ module Maptool
|
|||||||
add_features(features, folder, source: relative)
|
add_features(features, folder, source: relative)
|
||||||
end
|
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.
|
# One hand-drawn note (notes.js) into `folder`; returns its node key.
|
||||||
def add_note(feature, folder)
|
def add_note(feature, folder)
|
||||||
add_features([feature], folder).first
|
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|
|
features.sort_by { |f| f.dig("geometry", "type") == "Point" ? 1 : 0 }.each do |f|
|
||||||
type = item_type(f)
|
type = item_type(f)
|
||||||
props = (f["properties"] || {}).compact
|
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"])]))
|
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])
|
existing = db.get_first_value("SELECT id FROM items WHERE folder_id IS ? AND fingerprint = ? AND deleted_at IS NULL", [target, fingerprint])
|
||||||
id = existing
|
id = existing
|
||||||
|
|||||||
@@ -371,7 +371,7 @@ function statsTables(profile, bounds, laps, ride) {
|
|||||||
|
|
||||||
const rows = segmentStats(profile, bounds).map((s, i) => {
|
const rows = segmentStats(profile, bounds).map((s, i) => {
|
||||||
const cells = [laps ? `${i + 1}` : km(s.to), ...(laps ? [km(s.meters)] : [])];
|
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`);
|
cells.push(`↑ ${Math.round(s.gain)} m`, `↓ ${Math.round(s.loss)} m`);
|
||||||
if (hasHr) cells.push(s.hr ? `${Math.round(s.hr)} bpm` : "");
|
if (hasHr) cells.push(s.hr ? `${Math.round(s.hr)} bpm` : "");
|
||||||
if (hasPower) cells.push(s.power ? `${Math.round(s.power)} W` : "");
|
if (hasPower) cells.push(s.power ? `${Math.round(s.power)} W` : "");
|
||||||
@@ -385,7 +385,7 @@ function statsTables(profile, bounds, laps, ride) {
|
|||||||
if (hasTime) {
|
if (hasTime) {
|
||||||
BEST_DISTANCES_KM.filter((km) => km * 1000 <= total).forEach((km) => {
|
BEST_DISTANCES_KM.filter((km) => km * 1000 <= total).forEach((km) => {
|
||||||
const secs = fastest(timed, km * 1000);
|
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) {
|
if (hasPower) {
|
||||||
@@ -551,6 +551,13 @@ function formatDuration(totalSeconds) {
|
|||||||
return hours > 0 ? `${hours}h ${minutes}m` : `${minutes}m`;
|
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) {
|
function escapeHtml(text) {
|
||||||
const div = document.createElement("div");
|
const div = document.createElement("div");
|
||||||
div.textContent = text;
|
div.textContent = text;
|
||||||
|
|||||||
@@ -167,6 +167,7 @@ class AppTest < Minitest::Test
|
|||||||
properties = json_body(request.get(node_url(key)))["features"][0]["properties"]
|
properties = json_body(request.get(node_url(key)))["features"][0]["properties"]
|
||||||
assert_equal "cycling", properties["activity_type"]
|
assert_equal "cycling", properties["activity_type"]
|
||||||
assert_equal [0, 1], properties["laps"]
|
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)
|
fit = request.get("#{node_url(key)}?download=fit&name=Ride", **logged_in_env)
|
||||||
assert_equal 200, fit.status
|
assert_equal 200, fit.status
|
||||||
|
|||||||
@@ -181,16 +181,17 @@ class LibraryTest < Minitest::Test
|
|||||||
library = open_library
|
library = open_library
|
||||||
key = library.import(folder: "", filename: "ride.gpx", io: File.open(fixture_path("sample.gpx"))).find { |k| k.start_with?("item:") }
|
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:")
|
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"))
|
db = SQLite3::Database.new(File.join(@dir, "library.sqlite"))
|
||||||
props = JSON.parse(db.get_first_value("SELECT properties FROM items WHERE id = ?", [id]))
|
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")
|
db.execute("UPDATE meta SET parser = 0")
|
||||||
|
|
||||||
assert_equal 1, library.reparse
|
assert_equal 1, library.reparse
|
||||||
props = JSON.parse(db.get_first_value("SELECT properties FROM items WHERE id = ?", [id]))
|
props = JSON.parse(db.get_first_value("SELECT properties FROM items WHERE id = ?", [id]))
|
||||||
assert props["profile"].any?
|
assert props["profile"].any?
|
||||||
assert_equal "#ff0000", props["color"]
|
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
|
assert_equal 0, library.reparse # done once
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -25,7 +25,7 @@ class SearchTest < Minitest::Test
|
|||||||
|
|
||||||
def test_finds_across_sources_ignoring_case_and_diacritics_and_says_where
|
def test_finds_across_sources_ignoring_case_and_diacritics_and_says_where
|
||||||
layer = search("kosice")
|
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])
|
assert_match(/\Aitem:/, layer.first[:ref])
|
||||||
|
|
||||||
trail = search("snp hrdinov").first
|
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
|
def test_matches_description_and_falls_back_to_node_label_with_bbox
|
||||||
assert_equal ["Kemp Košice"], search("voda").map { |r| r[:name] }
|
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]
|
assert_equal [17.0, 48.0, 18.0, 49.0], track[:bbox]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user