Skip to content
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

Mattyweb/issue990 #992

Merged
merged 2 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 5 additions & 13 deletions server/api/code/lacity_data_api/models/council.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from aiocache import cached, Cache, serializers
from aiocache import cached

from sqlalchemy import and_

from . import db
from .service_request import ServiceRequest
from .region import Region
from .request_type import RequestType
from ..config import CACHE_ENDPOINT


class Council(db.Model):
Expand All @@ -19,8 +18,10 @@ class Council(db.Model):
region_id = db.Column(db.SmallInteger, db.ForeignKey('regions.region_id'))
latitude = db.Column(db.Float)
longitude = db.Column(db.Float)
data_code = db.Column(db.SmallInteger)

@classmethod
@cached(key="councils:all", alias="default")
async def all(cls):
result = await (
db.select(
Expand Down Expand Up @@ -53,12 +54,7 @@ async def one(cls, id: int):
return result


@cached(cache=Cache.REDIS,
endpoint=CACHE_ENDPOINT,
namespace="councils",
key="dict",
serializer=serializers.PickleSerializer(),
)
@cached(key="councils:dict", alias="default")
async def get_councils_dict():
result = await db.all(Council.query)
councils_dict = [
Expand All @@ -68,11 +64,7 @@ async def get_councils_dict():
return dict(councils_dict)


@cached(cache=Cache.REDIS,
endpoint=CACHE_ENDPOINT,
namespace="councils",
serializer=serializers.PickleSerializer(),
)
@cached(alias="default")
async def get_open_request_counts(council: int):

result = await (
Expand Down
14 changes: 12 additions & 2 deletions server/api/code/lacity_data_api/models/geometry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from . import db
from geoalchemy2 import Geometry
from aiocache import cached

from . import db
from lacity_data_api.models.council import Council


class Geometry(db.Model):
Expand All @@ -9,14 +12,19 @@ class Geometry(db.Model):
geometry = db.Column(Geometry(geometry_type="MULTIPOLYGON"))

@classmethod
@cached(key="councils:geojson", alias="default")
async def get_council_geojson(cls):

result = await (
db.select(
[
Council.council_id,
Council.council_name,
Geometry.nc_id,
db.func.ST_AsGeoJSON(Geometry.geometry).label("geometry")
]
).select_from(
Geometry.join(Council, Geometry.nc_id == Council.data_code)
).gino.all()
)
return result
Expand All @@ -27,8 +35,10 @@ async def get_enclosing_council(cls, latitude: float, longitude: float):
result = await (
db.select(
[
Geometry.nc_id.label("council_id"),
Council.council_id
]
).select_from(
Geometry.join(Council, Geometry.nc_id == Council.data_code)
).where(
db.func.ST_WITHIN(
db.func.ST_MakePoint(longitude, latitude), Geometry.geometry
Expand Down
10 changes: 2 additions & 8 deletions server/api/code/lacity_data_api/models/region.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from aiocache import cached, Cache, serializers
from aiocache import cached

from ..config import CACHE_ENDPOINT
from . import db


Expand All @@ -13,12 +12,7 @@ class Region(db.Model):
longitude = db.Column(db.Float)


@cached(cache=Cache.REDIS,
endpoint=CACHE_ENDPOINT,
namespace="regions",
key="dict",
serializer=serializers.PickleSerializer(),
)
@cached(key="regions:dict", alias="default")
async def get_regions_dict():
result = await db.all(Region.query)
regions_dict = [
Expand Down
40 changes: 9 additions & 31 deletions server/api/code/lacity_data_api/models/request_type.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from typing import List

from aiocache import cached, Cache, serializers

from ..config import CACHE_ENDPOINT
from aiocache import cached
from . import db


Expand All @@ -17,12 +15,7 @@ class RequestType(db.Model):
data_code = db.Column(db.String)

@classmethod
@cached(cache=Cache.REDIS,
endpoint=CACHE_ENDPOINT,
namespace="types",
key="all",
serializer=serializers.PickleSerializer(),
)
@cached(key="types:all", alias="default")
async def all(cls):
from .agency import Agency

Expand Down Expand Up @@ -59,26 +52,21 @@ async def one(cls, id: int):
return result

@classmethod
@cached(cache=Cache.REDIS,
endpoint=CACHE_ENDPOINT,
namespace="types",
key="stats",
serializer=serializers.PickleSerializer(),
)
@cached(key="types:stats", alias="default")
async def get_type_stats(cls):

query = db.text("""
SELECT
service_requests.type_id,
request_types.type_name,
min(closed_date - created_date),
min(closed_date::date - created_date::date),
percentile_disc(0.25) within group
(order by closed_date - created_date) as q1,
(order by closed_date::date - created_date::date) as q1,
percentile_disc(0.5) within group
(order by closed_date - created_date) as median,
(order by closed_date::date - created_date::date) as median,
percentile_disc(0.75) within group
(order by closed_date - created_date) as q3,
max(closed_date - created_date)
(order by closed_date::date - created_date::date) as q3,
max(closed_date::date - created_date::date)
FROM
service_requests
JOIN
Expand All @@ -94,24 +82,14 @@ async def get_type_stats(cls):
return result


@cached(cache=Cache.REDIS,
endpoint=CACHE_ENDPOINT,
namespace="types",
key="dict",
serializer=serializers.PickleSerializer(),
)
@cached(key="types:dict", alias="default")
async def get_types_dict():
'''This is a shim function to allow types to be retrieved by strings'''
result = await db.all(RequestType.query)
types_dict = [(i.type_id, i.data_code) for i in result]
return dict(types_dict)


@cached(cache=Cache.REDIS,
endpoint=CACHE_ENDPOINT,
namespace="types",
serializer=serializers.PickleSerializer(),
)
async def get_type_ids_by_str_list(str_list: List[str]) -> List[int]:
'''Get a list of RequestType IDs from their type_names using data code'''
result = await db.all(
Expand Down
5 changes: 2 additions & 3 deletions server/api/code/lacity_data_api/models/service_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ async def get_request_reports(
return result

@classmethod
@cached(alias="default")
async def get_recent_requests(cls, start_date: datetime.date):

result = await (
Expand All @@ -109,7 +108,7 @@ async def get_full_request(srnumber: str):
return result


@cached(alias="default")
@cached(key="requests:open", alias="default")
async def get_open_requests() -> List[ServiceRequest]:

result = await (
Expand Down Expand Up @@ -139,7 +138,7 @@ async def get_id_from_srnumber(srnumber: str):
return result


@cached(alias="default")
@cached(key="requests:open_type_counts", alias="default")
async def get_open_request_counts():
result = await (
db.select(
Expand Down
2 changes: 2 additions & 0 deletions server/api/code/lacity_data_api/routers/geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def createFeature(row):
return {
"type": "Feature",
"properties": {
"council_id": item['council_id'],
"council_name": item['council_name'],
"nc_id": item['nc_id']
},
"geometry": json.loads(item['geometry'])
Expand Down
2 changes: 1 addition & 1 deletion server/api/code/lacity_data_api/services/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ..config import CACHE_MAXMEMORY


@cached(alias="default")
@cached(key="status:last_updated", alias="default")
async def get_last_updated():
query = db.text("SELECT max(created_time) as last_pulled FROM log WHERE status = 'INFO'") # noqa
result = await db.first(query)
Expand Down