mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-01-22 17:48:41 +00:00
improve grouping statistics performance
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { ListItem, ListLevel } from '$lib/components/file-list/file-list';
|
||||
import { GPXFile, GPXStatistics, type Track } from 'gpx';
|
||||
import { GPXFile, GPXStatistics, GPXStatisticsGroup, type Track } from 'gpx';
|
||||
|
||||
export class GPXStatisticsTree {
|
||||
level: ListLevel;
|
||||
@@ -21,35 +21,26 @@ export class GPXStatisticsTree {
|
||||
}
|
||||
}
|
||||
|
||||
getStatisticsFor(item: ListItem): GPXStatistics {
|
||||
let statistics = [];
|
||||
getStatisticsFor(item: ListItem): GPXStatisticsGroup {
|
||||
let statistics = new GPXStatisticsGroup();
|
||||
let id = item.getIdAtLevel(this.level);
|
||||
if (id === undefined || id === 'waypoints') {
|
||||
Object.keys(this.statistics).forEach((key) => {
|
||||
if (this.statistics[key] instanceof GPXStatistics) {
|
||||
statistics.push(this.statistics[key]);
|
||||
statistics.add(this.statistics[key]);
|
||||
} else {
|
||||
statistics.push(this.statistics[key].getStatisticsFor(item));
|
||||
statistics.add(this.statistics[key].getStatisticsFor(item));
|
||||
}
|
||||
});
|
||||
} else {
|
||||
let child = this.statistics[id];
|
||||
if (child instanceof GPXStatistics) {
|
||||
statistics.push(child);
|
||||
statistics.add(child);
|
||||
} else if (child !== undefined) {
|
||||
statistics.push(child.getStatisticsFor(item));
|
||||
statistics.add(child.getStatisticsFor(item));
|
||||
}
|
||||
}
|
||||
if (statistics.length === 0) {
|
||||
return new GPXStatistics();
|
||||
} else if (statistics.length === 1) {
|
||||
return statistics[0];
|
||||
} else {
|
||||
return statistics.reduce((acc, curr) => {
|
||||
acc.mergeWith(curr);
|
||||
return acc;
|
||||
}, new GPXStatistics());
|
||||
}
|
||||
return statistics;
|
||||
}
|
||||
}
|
||||
export type GPXFileWithStatistics = { file: GPXFile; statistics: GPXStatisticsTree };
|
||||
|
||||
Reference in New Issue
Block a user