Expose all generated JSON files in one endpoint, and use actual track data for rendering the blue line (fixes #72, fixes #64)

This commit is contained in:
Paul Bienkowski 2021-05-13 20:32:55 +02:00
parent cf30037a85
commit ce2a27ed51
4 changed files with 39 additions and 27 deletions

View file

@ -206,7 +206,6 @@ router.post(
await track.queueProcessing(); await track.queueProcessing();
} }
// console.log(track.author);
return res.json({ track: track.toJSONFor(req.user) }); return res.json({ track: track.toJSONFor(req.user) });
}), }),
); );
@ -353,37 +352,43 @@ router.delete(
// return an track's generated data // return an track's generated data
router.get( router.get(
'/:track/data/:filename', '/:track/data',
auth.optional, auth.optional,
wrapRoute(async (req, res) => { wrapRoute(async (req, res) => {
const {filename} = req.params const FILE_BY_KEY = {
allMeasurements: 'all_measurements.json',
if (!['statistics', 'all_measurements'].includes(filename)) { confirmedMeasurements: 'confirmed_measurements.json',
return res.sendStatus(404); track: 'track.json',
} }
console.log(req.track.author, req.user)
if (!req.track.isVisibleTo(req.user)) { if (!req.track.isVisibleTo(req.user)) {
return res.sendStatus(403); return res.sendStatus(403);
} }
const filePath = path.join(PROCESSING_OUTPUT_DIR, req.track.filePath, filename + '.json') const result = {}
for (const [key, filename] of Object.entries(FILE_BY_KEY)) {
const filePath = path.join(PROCESSING_OUTPUT_DIR, req.track.filePath, filename)
let stats let stats
try { try {
stats = await fs.promises.stat(filePath) stats = await fs.promises.stat(filePath)
} catch(err) { } catch(err) {
return res.sendStatus(404); continue
}
if (!stats.isFile()) {
// file does not exist (yet)
continue
}
const content = await fs.promises.readFile(filePath)
const contentJson = JSON.parse(content)
result[key] = contentJson
} }
if (!stats.isFile()) { return res.json(result);
// file does not exist (yet)
return res.sendStatus(404);
}
const content = await fs.promises.readFile(filePath)
return res.json(JSON.parse(content));
}), }),
); );

View file

@ -102,8 +102,7 @@ export default function TrackMap({trackData, show, ...props}: {trackData: TrackD
const trackPointsD2: Feature<Point>[] = [] const trackPointsD2: Feature<Point>[] = []
const trackPointsUntaggedD1: Feature<Point>[] = [] const trackPointsUntaggedD1: Feature<Point>[] = []
const trackPointsUntaggedD2: Feature<Point>[] = [] const trackPointsUntaggedD2: Feature<Point>[] = []
const points: Coordinate[] = [] const filteredPoints: TrackPoint[] = trackData?.allMeasurements?.features.filter(isValidTrackPoint) ?? []
const filteredPoints: TrackPoint[] = trackData?.features.filter(isValidTrackPoint) ?? []
for (const feature of filteredPoints) { for (const feature of filteredPoints) {
const { const {
@ -114,7 +113,6 @@ export default function TrackMap({trackData, show, ...props}: {trackData: TrackD
} = feature } = feature
const p = fromLonLat([longitude, latitude]) const p = fromLonLat([longitude, latitude])
points.push(p)
const geometry = new Point(p) const geometry = new Point(p)
@ -135,6 +133,10 @@ export default function TrackMap({trackData, show, ...props}: {trackData: TrackD
} }
} }
const points: Coordinate[] = trackData?.track.geometry.coordinates.map(([latitude, longitude]) => {
return fromLonLat([longitude, latitude])
}) ?? []
//Simplify to 1 point per 2 meter //Simplify to 1 point per 2 meter
const trackVectorSource = new VectorSource({ const trackVectorSource = new VectorSource({
features: [new Feature(new LineString(points).simplify(2))], features: [new Feature(new LineString(points).simplify(2))],
@ -142,7 +144,7 @@ export default function TrackMap({trackData, show, ...props}: {trackData: TrackD
const viewExtent = points.length ? trackVectorSource.getExtent() : null const viewExtent = points.length ? trackVectorSource.getExtent() : null
return {trackVectorSource, trackPointsD1, trackPointsD2, trackPointsUntaggedD1, trackPointsUntaggedD2, viewExtent} return {trackVectorSource, trackPointsD1, trackPointsD2, trackPointsUntaggedD1, trackPointsUntaggedD2, viewExtent}
}, [trackData?.features]) }, [trackData?.allMeasurements?.features])
const trackLayerStyle = React.useMemo( const trackLayerStyle = React.useMemo(
() => () =>

View file

@ -51,7 +51,7 @@ const TrackPage = connect((state) => ({login: state.login}))(function TrackPage(
) )
const trackData$ = slug$.pipe( const trackData$ = slug$.pipe(
map((slug) => `/tracks/${slug}/data/all_measurements`), map((slug) => `/tracks/${slug}/data`),
switchMap((url) => switchMap((url) =>
concat( concat(
of(undefined), of(undefined),

View file

@ -1,4 +1,4 @@
import type {FeatureCollection, Point} from 'geojson' import type {FeatureCollection, Feature, LineString, Point} from 'geojson'
export type UserProfile = { export type UserProfile = {
username: string username: string
@ -6,7 +6,12 @@ export type UserProfile = {
bio?: string | null bio?: string | null
} }
export type TrackData = FeatureCollection export type TrackData = {
track: Feature<LineString>,
allMeasurements: FeatureCollection,
confirmedMeasurements: FeatureCollection,
}
export type TrackStatistics = { export type TrackStatistics = {
recordedAt?: Date recordedAt?: Date
recordedUntil?: Date recordedUntil?: Date