Log usage

Closes: #6
This commit is contained in:
Edward Betts 2023-11-24 15:13:00 +00:00
parent 6dd4ac1115
commit 4720814e1f
2 changed files with 27 additions and 9 deletions

View file

@ -9,9 +9,9 @@ from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import column_property from sqlalchemy.orm import column_property
from sqlalchemy.schema import Column from sqlalchemy.schema import Column
from sqlalchemy.types import Float, Integer, Numeric, String from sqlalchemy.types import DateTime, Float, Integer, Numeric, String
from .database import session from .database import now_utc, session
Base = declarative_base() Base = declarative_base()
Base.query = session.query_property() Base.query = session.query_property()
@ -81,3 +81,16 @@ class Scotland(Base):
name = Column(String(50)) name = Column(String(50))
geom = Column(Geometry("MULTIPOLYGON", srid=27700)) geom = Column(Geometry("MULTIPOLYGON", srid=27700))
class LookupLog(Base):
"""Log lookups."""
__tablename__ = "lookup_log"
id = Column(Integer, primary_key=True)
dt = Column(DateTime, default=now_utc())
lat = Column(Float)
lon = Column(Float)
remote_addr = Column(String)
result = Column(postgresql.JSONB)

View file

@ -186,15 +186,20 @@ def index() -> str | Response:
lat, lon = request.args.get("lat"), request.args.get("lon") lat, lon = request.args.get("lat"), request.args.get("lon")
if lat is not None and lon is not None: if lat is None or lon is None:
samples = sorted(geocode.samples, key=lambda row: row[2])
return render_template("index.html", samples=samples)
result = lat_lon_to_wikidata(lat, lon)["result"] result = lat_lon_to_wikidata(lat, lon)["result"]
result.pop("element", None) result.pop("element", None)
result.pop("geojson", None) result.pop("geojson", None)
log = model.LookupLog(
lat=lat, lon=lon, remote_addr=request.remote_addr, result=result
)
database.session.add(log)
database.session.commit()
return jsonify(result) return jsonify(result)
samples = sorted(geocode.samples, key=lambda row: row[2])
return render_template("index.html", samples=samples)
@app.route("/random") @app.route("/random")
def random_location() -> str | Response: def random_location() -> str | Response: