This commit is contained in:
vcoppe
2026-10-04 08:21:39 +02:00
parent f1904bc12f
commit 74b674e8e8
4 changed files with 227 additions and 25 deletions
@@ -1,9 +1,9 @@
use std::{
collections::{HashMap, HashSet, hash_map::Entry},
rc::Rc,
};
use std::collections::{HashMap, HashSet, hash_map::Entry};
use crate::{File, Selection, StackEntry, Statistics, Track, TrackSegment, TrackSegmentRevisionId};
use crate::{
File, GlobalStatistics, Selection, StackEntry, Statistics, Track, TrackSegment,
TrackSegmentRevisionId,
};
#[derive(Debug, Default)]
pub struct StatisticsCache {
@@ -95,7 +95,18 @@ impl StatisticsCache {
stats
}
fn add_file_stats<'a>(&'a self, file: &Rc<File>, stats: &mut Vec<&'a Statistics>) {
/// Global statistics of a file: its segments merged, in order.
pub fn file_global(&self, file: &File) -> GlobalStatistics {
let mut stats = vec![];
self.add_file_stats(file, &mut stats);
let mut global = GlobalStatistics::default();
for s in stats {
global.merge(&s.global);
}
global
}
fn add_file_stats<'a>(&'a self, file: &File, stats: &mut Vec<&'a Statistics>) {
for trk in file.trk.iter() {
self.add_track_stats(trk, stats);
}
@@ -116,7 +127,7 @@ impl StatisticsCache {
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use std::{collections::HashSet, rc::Rc};
use crate::{FileId, parse};
+7 -1
View File
@@ -2,7 +2,7 @@
use crate::{
Action, Apply, Command, CoordinatesCache, Diff, FileId, FileOrder, FileStructure,
FileStructureCache, Selection, Stack, State, StatisticsBuffer, StatisticsCache, TrackSegmentId,
FileStructureCache, GlobalStatistics, Selection, Stack, State, StatisticsBuffer, StatisticsCache, TrackSegmentId,
};
#[derive(Debug, Default)]
@@ -34,6 +34,12 @@ impl Engine {
self.structure_cache.get(id)
}
/// Global statistics (distance, time, elevation, bounds...) of a file.
pub fn file_statistics(&self, id: &FileId) -> Option<GlobalStatistics> {
let file = self.stack.current()?.get(id)?;
Some(self.statistics_cache.file_global(file))
}
/// Which files the last action added, removed or modified.
pub fn last_diff(&self) -> Option<&Diff> {
self.diff.as_ref()
+58
View File
@@ -353,6 +353,25 @@ export interface FileStructure {
/** Changes when the waypoints of the file change: refetch their coordinates. */
waypointsRev: string;
}
/** Global statistics of a file (optional fields are absent when the file has no timestamps). */
export interface FileStatistics {
/** km */
totalDistance: number;
movingDistance?: number;
/** seconds */
totalTime?: number;
movingTime?: number;
elevationGain: number;
elevationLoss: number;
/** ms since epoch */
startTime?: number;
endTime?: number;
/** km/h */
totalSpeed?: number;
movingSpeed?: number;
/** Absent when the file has no trackpoints. */
bounds?: { west: number; south: number; east: number; north: number };
}
export interface TrackNode {
id: string;
name?: string;
@@ -379,6 +398,8 @@ extern "C" {
pub type FilesUpdate;
#[wasm_bindgen(typescript_type = "FileStructure | undefined")]
pub type FileStructure;
#[wasm_bindgen(typescript_type = "FileStatistics | undefined")]
pub type FileStatistics;
#[wasm_bindgen(typescript_type = "string[]")]
pub type FileOrder;
}
@@ -458,6 +479,43 @@ pub fn file_structure(file_id: &str) -> FileStructure {
structure.unchecked_into()
}
/// Global statistics of a file, `undefined` if the id is unknown.
#[wasm_bindgen]
pub fn file_statistics(file_id: &str) -> FileStatistics {
let stats = uuid::Uuid::parse_str(file_id)
.ok()
.and_then(|id| with_engine(|e| e.file_statistics(&FileId(id))).flatten())
.map_or(JsValue::UNDEFINED, |stats| {
let object = Object::new();
let optional = |key: &str, value: Option<f64>| {
if let Some(value) = value {
set(&object, key, value);
}
};
set(&object, "totalDistance", stats.total_distance);
optional("movingDistance", stats.moving_distance);
optional("totalTime", stats.total_time.map(f64::from));
optional("movingTime", stats.moving_time.map(f64::from));
set(&object, "elevationGain", stats.elevation_gain);
set(&object, "elevationLoss", stats.elevation_loss);
optional("startTime", stats.start_time.map(|t| t as f64));
optional("endTime", stats.end_time.map(|t| t as f64));
optional("totalSpeed", stats.total_speed());
optional("movingSpeed", stats.moving_speed());
let (sw, ne) = (&stats.bounds.sw, &stats.bounds.ne);
if sw.lng <= ne.lng && sw.lat <= ne.lat {
let bounds = Object::new();
set(&bounds, "west", sw.lng);
set(&bounds, "south", sw.lat);
set(&bounds, "east", ne.lng);
set(&bounds, "north", ne.lat);
set(&object, "bounds", bounds);
}
object.into()
});
stats.unchecked_into()
}
fn uuids<T>(items: impl IntoIterator<Item = T>, uuid: impl Fn(T) -> uuid::Uuid) -> Array {
items
.into_iter()