async def get_postcode_areas(request, postcode: str, **kwargs): loop = get_running_loop() async with Connection(loop=loop) as conn: area_codes = await conn.fetch(locations_query, postcode) return area_codes
async def get_data(request, area_type, area_id, timestamp, loop=None): partition_names = { "overview": "other", "nation": "other", "region": "other", "nhsRegion": "other", "utla": "utla", "ltla": "ltla", "nhsTrust": "nhstrust", "msoa": "msoa", } partition_name = partition_names[area_type] numeric_metrics = ["%Percentage%", "%Rate%"] local_metrics = query_data["local_data"]["metrics"] msoa_metric = [f'{query_data["local_data"]["msoa_metric"]}%'] if area_type == "msoa": query = msoa_query args = [area_id, local_metrics, msoa_metric] else: query = single_query args = [numeric_metrics, area_id, local_metrics] query = query.format(partition_id=f"{timestamp}_{partition_name}") async with Connection(loop=loop) as conn: result = await conn.fetch(query, *args) df = DataFrame( result, columns=query_data["local_data"]["column_names"], ).astype(query_data["local_data"]["column_types"]) return df
async def _get_whats_new_banners(timestamp: str): loop = get_running_loop() async with Connection(loop=loop) as conn: response = await conn.fetch(query, datetime.fromisoformat(timestamp[:-2])) results = list() for banner in map(dict, response): banner["formatted_date"] = f"{banner['date']:%-d %B %Y}" banner["date"] = banner['date'].isoformat() results.append(banner) return results
async def _get_banners(request, timestamp: str): loop = get_running_loop() async with Connection(loop=loop) as conn: response = await conn.fetch(query) results = [ { "timestamp": banner["date"], "display_timestamp": f"{banner['date']:%-d %B %Y}", "body": markdown(banner["body"]) } for banner in response ] return results
async def get_landing_data(timestamp): ts = datetime.fromisoformat(timestamp.replace("5Z", "")) query = overview_data_query.format(partition=f"{ts:%Y_%-m_%-d}_other") loop = get_running_loop() async with Connection() as conn, Lock(loop=loop): values = await conn.fetch(query, ts, metrics) df = DataFrame(values, columns=[ "areaCode", "areaType", "areaName", "date", "metric", "value", "rank" ]) df = df.assign(formatted_date=df.date.map(lambda x: f"{x:%-d %B %Y}")) return df
async def test_db(): async with Connection() as conn: db_active = await conn.fetchval("SELECT NOW() AS timestamp;") return {"db": f"healthy - {db_active}"}
async def test_db(): async with Connection() as conn: db_active = await conn.fetchval("SELECT TRUE AS passed;") return {"db": db_active is True}