Skip to content

Commit

Permalink
Merge pull request #940 from hackforla/mattyweb/issue933
Browse files Browse the repository at this point in the history
Added stats route to /types endpoint (and new index)
  • Loading branch information
mattyweb authored Feb 10, 2021
2 parents fc49a5a + eda1e9b commit 799bacd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
CREATE UNIQUE INDEX ON service_requests(request_id);
CREATE UNIQUE INDEX ON service_requests(srnumber);
CREATE INDEX ON service_requests(created_date);
CREATE INDEX ON service_requests(closed_date);
CREATE INDEX ON service_requests(type_id);
CREATE INDEX ON service_requests(council_id);
CREATE INDEX ON service_requests(region_id);
Expand Down
35 changes: 35 additions & 0 deletions server/api/code/lacity_data_api/models/request_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,41 @@ class RequestType(db.Model):
color = db.Column(db.String)
data_code = db.Column(db.String)

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

query = db.text("""
SELECT
service_requests.type_id,
request_types.type_name,
min(closed_date - created_date),
percentile_disc(0.25) within group
(order by closed_date - created_date) as q1,
percentile_disc(0.5) within group
(order by closed_date - created_date) as median,
percentile_disc(0.75) within group
(order by closed_date - created_date) as q3,
max(closed_date - created_date)
FROM
service_requests
JOIN
request_types ON request_types.type_id = service_requests.type_id
WHERE
service_requests.closed_date <= CURRENT_DATE AND
service_requests.closed_date >= service_requests.created_date
GROUP BY
service_requests.type_id, request_types.type_name
""")

result = await db.all(query)
return result


@cached(cache=Cache.REDIS,
endpoint=CACHE_ENDPOINT,
Expand Down
6 changes: 6 additions & 0 deletions server/api/code/lacity_data_api/routers/request_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ async def get_all_request_types():
return await RequestType.query.gino.all()


@router.get("/stats")
async def get_request_stats():
result = await RequestType.get_type_stats()
return result


@router.get("/{id}")
async def get_request_type(id: int):
result = await RequestType.get_or_404(id)
Expand Down

0 comments on commit 799bacd

Please sign in to comment.