-
Notifications
You must be signed in to change notification settings - Fork 1
Usage stats tile route #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6827d2f
Update gitignore
davidsklar99 6b54822
Beginning of new route
davidsklar99 974e97b
Merge branch 'fossils' into stats-tile
davidsklar99 7cd810b
Need to test today
davidsklar99 8f23375
Rockd works too
davidsklar99 26bbb3a
Format code and sort imports
davidsklar99 12689f8
Rename tables
davidsklar99 9e778a2
Update gitignore
davidsklar99 6d4a97b
Update git ignore again
davidsklar99 cb8b63f
Update makefile
davidsklar99 ca75dfa
Rename function
davidsklar99 498f82e
Simplify function
davidsklar99 1de5da6
Merge branch 'main' into stats-tile
davidsklar99 a92754d
Format code and sort imports
davidsklar99 b9efe74
Merge branch 'main' into stats-tile
davidsklar99 e62057f
Fix makefile
davidsklar99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
services/tileserver/macrostrat/tileserver/usage_stats/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
31 changes: 31 additions & 0 deletions
31
services/tileserver/macrostrat/tileserver/usage_stats/queries/macrostrat.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
32 changes: 32 additions & 0 deletions
32
services/tileserver/macrostrat/tileserver/usage_stats/queries/macrostrat_today.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
31 changes: 31 additions & 0 deletions
31
services/tileserver/macrostrat/tileserver/usage_stats/queries/rockd.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
32 changes: 32 additions & 0 deletions
32
services/tileserver/macrostrat/tileserver/usage_stats/queries/rockd_today.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.