Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions cli/macrostrat/cli/database/rockd/integration_tokens.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* 1 ─ Add the new column (type timestamptz is the usual choice) */
ALTER TABLE public.people
ADD COLUMN token_exp timestamptz;

INSERT INTO public.people (
first_name,
last_name,
email,
password,
public,
created,
save_photos,
oauth_type,
oauth_id,
autocorrect,
affiliation,
orc_id,
last_notification_check,
last_data_dump,
token_exp
)
VALUES (
'Internal ML Model', -- first_name
'Integrations', -- last_name
'[email protected]', -- email
'$2a$08$iaME/tEwdU.0Rz8ZsCr5gejANP2r2Pldy4a06lF.jvP4jVjg5Pdni', -- bcrypt
TRUE, -- public
NOW(), -- created
TRUE, -- save_photos
NULL, -- oauth_type
NULL, -- oauth_id
FALSE, -- autocorrect
'Internal', -- affiliation
'', -- orc_id
NOW(), -- last_notification_check
NULL, -- last_data_dump
NOW() + INTERVAL '1 year' -- token_exp
);
5 changes: 2 additions & 3 deletions cli/macrostrat/cli/database/rockd/model-feedback-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ CREATE TABLE IF NOT EXISTS public.model_feedback (
id serial PRIMARY KEY,
checkin_id integer NOT NULL REFERENCES public.checkins(checkin_id) ON DELETE CASCADE,
observation_id integer REFERENCES public.observations(obs_id),
photo_id integer NOT NULL,
text_relevancy float NOT NULL,
text_relevancy float,
image_relevancy float NOT NULL,
text_appropriateness float NOT NULL,
text_appropriateness float,
image_appropriateness float NOT NULL,
status_code integer NOT NULL,
date_created timestamp with time zone NOT NULL DEFAULT now()
Expand Down
19 changes: 11 additions & 8 deletions services/legacy-tileserver/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

FROM python:3.9@sha256:c0dcc146710fed0a6d62cb55b92f00bfbfc3b931fff6218f4958bab58333c37b

ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
POETRY_NO_INTERACTION=1 \
POETRY_CACHE_DIR=/tmp/poetry-cache \
POETRY_VIRTUALENVS_CREATE=false

WORKDIR /app
RUN python3 -m venv /venv
ENV PATH="/venv/bin:$PATH"
ENV VIRTUAL_ENV=/venv

# MAPNIK
# Install mapnik for compiling legacy image tiles
RUN apt-get update -y && \
Expand Down Expand Up @@ -47,24 +57,17 @@ RUN npm install -g carto
RUN rm -rf /tmp/*

# The rest of this (for vector tile generation and the server itself) should be easier.
ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
POETRY_NO_INTERACTION=1 \
POETRY_CACHE_DIR=/tmp/poetry-cache \
POETRY_VIRTUALENVS_CREATE=false


RUN pip install --no-cache-dir "pip==25.1.1" && pip install --no-cache-dir "poetry==2.1.1"

WORKDIR /app/

# Copy only requirements to cache them in docker layer
# Right now, Poetry lock file must exist to avoid hanging on dependency resolution
COPY ./pyproject.toml ./poetry.lock /app/

# Create and activate our own virtual envrionment so that we can keep
# our application dependencies separate from Poetry's
RUN python3 -m venv /venv
ENV PATH="/venv/bin:$PATH"
ENV VIRTUAL_ENV=/venv

RUN --mount=type=cache,target=$POETRY_CACHE_DIR poetry install --no-root --without dev --without local

Expand Down
4 changes: 4 additions & 0 deletions services/tileserver/macrostrat/tileserver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ async def shutdown_event():

app.include_router(integrations_router, tags=["Integrations"], prefix="/integrations")

from .usage_stats import router as usage_stats_router

app.include_router(usage_stats_router, tags=["Usage stats"], prefix="/usage-stats")


@app.get("/carto/rotation-models")
async def rotation_models():
Expand Down
62 changes: 62 additions & 0 deletions services/tileserver/macrostrat/tileserver/usage_stats/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from pathlib import Path
from typing import List

from buildpg import V, render
from fastapi import APIRouter, Request, Response
from timvt.resources.enums import MimeTypes

from macrostrat.tileserver_utils import VectorTileResponse
from macrostrat.utils import get_logger

from ..utils import get_layer_sql

log = get_logger(__name__)

router = APIRouter()

__here__ = Path(__file__).parent


@router.get("/{slug}/{z}/{x}/{y}")
async def get_tile(
request: Request,
slug: str,
z: int,
x: int,
y: int,
):
"""Get a tile from the tileserver."""
pool = request.app.state.pool

params = dict(
z=z,
x=x,
y=y,
)

"""Get a tile from the tileserver."""
pool = request.app.state.pool

if slug == "macrostrat":
if "today" in request.query_params:
query = __here__ / "queries" / "macrostrat_today.sql"
else:
query = __here__ / "queries" / "macrostrat.sql"
else:
if "today" in request.query_params:
query = __here__ / "queries" / "rockd_today.sql"
else:
query = __here__ / "queries" / "rockd.sql"

query = query.read_text()
query = query.strip()
if query.endswith(";"):
query = query[:-1]

q, p = render(query, **params)
q = q.replace("textarray", "text[]")

async with pool.acquire() as con:
data = await con.fetchval(q, *p)

return Response(data, media_type=MimeTypes.pbf.value)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
WITH
tile AS (
SELECT ST_TileEnvelope(:z, :x, :y) AS envelope,
tile_layers.geographic_envelope(:x, :y, :z, 0.01) AS envelope_4326
),
points AS (
SELECT
id,
tile_layers.tile_geom(
ST_Intersection(
ST_SetSRID(ST_MakePoint(lng, lat), 4326),
envelope_4326
),
envelope
) AS geom
FROM usage_stats.macrostrat_stats, tile
WHERE
lat IS NOT NULL AND lng IS NOT NULL
AND ST_Intersects(
ST_SetSRID(ST_MakePoint(lng, lat), 4326),
envelope_4326
)
)

SELECT ST_AsMVT(
points.*,
'default',
4096,
'geom'
) AS mvt
FROM points;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
WITH
tile AS (
SELECT ST_TileEnvelope(:z, :x, :y) AS envelope,
tile_layers.geographic_envelope(:x, :y, :z, 0.01) AS envelope_4326
),
points AS (
SELECT
id,
tile_layers.tile_geom(
ST_Intersection(
ST_SetSRID(ST_MakePoint(lng, lat), 4326),
envelope_4326
),
envelope
) AS geom
FROM usage_stats.macrostrat_stats, tile
WHERE
lat IS NOT NULL AND lng IS NOT NULL
AND ST_Intersects(
ST_SetSRID(ST_MakePoint(lng, lat), 4326),
envelope_4326
)
AND date >= (NOW() - INTERVAL '24 hours')
)

SELECT ST_AsMVT(
points.*,
'default',
4096,
'geom'
) AS mvt
FROM points;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
WITH
tile AS (
SELECT ST_TileEnvelope(:z, :x, :y) AS envelope,
tile_layers.geographic_envelope(:x, :y, :z, 0.01) AS envelope_4326
),
points AS (
SELECT
id,
tile_layers.tile_geom(
ST_Intersection(
ST_SetSRID(ST_MakePoint(lng, lat), 4326),
envelope_4326
),
envelope
) AS geom
FROM usage_stats.rockd_stats, tile
WHERE
lat IS NOT NULL AND lng IS NOT NULL
AND ST_Intersects(
ST_SetSRID(ST_MakePoint(lng, lat), 4326),
envelope_4326
)
)

SELECT ST_AsMVT(
points.*,
'default',
4096,
'geom'
) AS mvt
FROM points;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
WITH
tile AS (
SELECT ST_TileEnvelope(:z, :x, :y) AS envelope,
tile_layers.geographic_envelope(:x, :y, :z, 0.01) AS envelope_4326
),
points AS (
SELECT
id,
tile_layers.tile_geom(
ST_Intersection(
ST_SetSRID(ST_MakePoint(lng, lat), 4326),
envelope_4326
),
envelope
) AS geom
FROM usage_stats.rockd_stats, tile
WHERE
lat IS NOT NULL AND lng IS NOT NULL
AND ST_Intersects(
ST_SetSRID(ST_MakePoint(lng, lat), 4326),
envelope_4326
)
AND date >= (NOW() - INTERVAL '24 hours')
)

SELECT ST_AsMVT(
points.*,
'default',
4096,
'geom'
) AS mvt
FROM points;