Skip to content

Commit

Permalink
milestone API
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddewie committed Jul 21, 2024
1 parent a7607a4 commit a0ab3d8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 7 additions & 2 deletions api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
# SPDX-License-Identifier: GPL-2.0-or-later
import contextlib
import os
from typing import Annotated

import asyncpg
from fastapi import FastAPI
from fastapi import Query

from openrailwaymap_api.facility_api import FacilityAPI
from openrailwaymap_api.milestone_api import MilestoneAPI
Expand Down Expand Up @@ -62,9 +64,12 @@ async def facility():


@app.get("/api/milestone")
async def milestone():
async def milestone(
ref: Annotated[str | None, Query()] = None,
position: Annotated[str | None, Query()] = None,
):
api = MilestoneAPI(app.state.database)
return await api({'ref': 'L36', 'position':'17'})
return await api({'ref': ref, 'position': position})

#
# def connect_db():
Expand Down
8 changes: 4 additions & 4 deletions api/openrailwaymap_api/milestone_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,21 @@ async def get_milestones(self):
FROM openrailwaymap_milestones AS m
JOIN openrailwaymap_tracks_with_ref AS t
ON t.geom && m.geom AND ST_Intersects(t.geom, m.geom) AND t.ref = $2
WHERE m.position BETWEEN ($3 - 10.0)::FLOAT AND ($4 + 10.0)::FLOAT
WHERE m.position BETWEEN ($1 - 10.0)::FLOAT AND ($1 + 10.0)::FLOAT
-- sort by distance from searched location, then osm_id for stable sorting
ORDER BY error ASC, m.osm_id
) AS milestones
GROUP BY position, error, route_ref, operator
) AS unique_milestones
) AS top_of_array
) AS ranked
WHERE grouped_rank <= $5
LIMIT $6;"""
WHERE grouped_rank <= $3
LIMIT $3;"""

async with self.database.acquire() as connection:
statement = await connection.prepare(sql_query)
async with connection.transaction():
data = []
async for record in statement.cursor(self.position, self.route_ref, self.position, self.position, self.limit, self.limit):
async for record in statement.cursor(self.position, self.route_ref, self.limit):
data.append(dict(record))
return data

0 comments on commit a0ab3d8

Please sign in to comment.