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();
}
// console.log(track.author);
return res.json({ track: track.toJSONFor(req.user) });
}),
);
@ -353,37 +352,43 @@ router.delete(
// return an track's generated data
router.get(
'/:track/data/:filename',
'/:track/data',
auth.optional,
wrapRoute(async (req, res) => {
const {filename} = req.params
if (!['statistics', 'all_measurements'].includes(filename)) {
return res.sendStatus(404);
const FILE_BY_KEY = {
allMeasurements: 'all_measurements.json',
confirmedMeasurements: 'confirmed_measurements.json',
track: 'track.json',
}
console.log(req.track.author, req.user)
if (!req.track.isVisibleTo(req.user)) {
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 {
stats = await fs.promises.stat(filePath)
} catch(err) {
return res.sendStatus(404);
try {
stats = await fs.promises.stat(filePath)
} catch(err) {
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()) {
// file does not exist (yet)
return res.sendStatus(404);
}
const content = await fs.promises.readFile(filePath)
return res.json(JSON.parse(content));
return res.json(result);
}),
);

View file

@ -102,8 +102,7 @@ export default function TrackMap({trackData, show, ...props}: {trackData: TrackD
const trackPointsD2: Feature<Point>[] = []
const trackPointsUntaggedD1: Feature<Point>[] = []
const trackPointsUntaggedD2: Feature<Point>[] = []
const points: Coordinate[] = []
const filteredPoints: TrackPoint[] = trackData?.features.filter(isValidTrackPoint) ?? []
const filteredPoints: TrackPoint[] = trackData?.allMeasurements?.features.filter(isValidTrackPoint) ?? []
for (const feature of filteredPoints) {
const {
@ -114,7 +113,6 @@ export default function TrackMap({trackData, show, ...props}: {trackData: TrackD
} = feature
const p = fromLonLat([longitude, latitude])
points.push(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
const trackVectorSource = new VectorSource({
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
return {trackVectorSource, trackPointsD1, trackPointsD2, trackPointsUntaggedD1, trackPointsUntaggedD2, viewExtent}
}, [trackData?.features])
}, [trackData?.allMeasurements?.features])
const trackLayerStyle = React.useMemo(
() =>

View file

@ -51,7 +51,7 @@ const TrackPage = connect((state) => ({login: state.login}))(function TrackPage(
)
const trackData$ = slug$.pipe(
map((slug) => `/tracks/${slug}/data/all_measurements`),
map((slug) => `/tracks/${slug}/data`),
switchMap((url) =>
concat(
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 = {
username: string
@ -6,7 +6,12 @@ export type UserProfile = {
bio?: string | null
}
export type TrackData = FeatureCollection
export type TrackData = {
track: Feature<LineString>,
allMeasurements: FeatureCollection,
confirmedMeasurements: FeatureCollection,
}
export type TrackStatistics = {
recordedAt?: Date
recordedUntil?: Date