Skip to content

Commit

Permalink
fix: lint ruff errors
Browse files Browse the repository at this point in the history
  • Loading branch information
khalsz committed Nov 27, 2024
1 parent 9aeb214 commit 7d3d25c
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 147 deletions.
25 changes: 17 additions & 8 deletions src/api/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np
import pandas as pd
import pyarrow.parquet as pq
from psycopg2.extras import Json
from sqlalchemy import MetaData, create_engine, text
from sqlalchemy_utils.functions import (
create_database,
Expand All @@ -17,7 +18,6 @@
)
from sqlmodel import SQLModel
from tqdm import tqdm
from psycopg2.extras import Json

# Do not remove. Sqlalchemy needs this import to create tables
from . import models # noqa: F401
Expand All @@ -36,14 +36,16 @@ class Context(str, Enum):
DQV = "http://www.w3.org/ns/dqv#"
SDMX = "http://purl.org/linked-data/sdmx/2009/measure#"


base_context = {
"dct": Context.DCT,
"dct": Context.DCT,
"schema": Context.SCHEMA,
"dqv": Context.DQV,
"sdmx-measure": Context.SDMX,
"dcat": Context.DCAT,
"dqv": Context.DQV,
"sdmx-measure": Context.SDMX,
"dcat": Context.DCAT,
}


class URLType(Enum):
"""Enum type for different types of storage endpoint"""

Expand Down Expand Up @@ -130,9 +132,8 @@ def create_cpf_summary(self, data_path: Path):
dfs = [pd.read_parquet(path) for path in paths]
df = pd.concat(dfs).reset_index(drop=True)
df["context"] = [Json(base_context)] * len(df)
df = df.drop_duplicates(subset=['name'])
df = df.drop_duplicates(subset=["name"])
df.to_sql("cpf_summary", self.uri, if_exists="append")


def create_scenarios(self, data_path: Path):
"""Create the scenarios metadata table"""
Expand Down Expand Up @@ -238,7 +239,15 @@ def create_sources(self, data_path: Path):
+ ".zarr/"
+ source_metadata["name"]
)
column_names = ["uuid", "shot_id", "name", "description", "quality", "url", "context"]
column_names = [
"uuid",
"shot_id",
"name",
"description",
"quality",
"url",
"context",
]
source_metadata = source_metadata[column_names]
source_metadata.to_sql("sources", self.uri, if_exists="append", index=False)

Expand Down
2 changes: 1 addition & 1 deletion src/api/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def apply_filters(query: Query, filters: str) -> Query:
def apply_sorting(query: Query, sort: t.Optional[str] = None) -> Query:
if sort is None:
return query

if sort.startswith("-"):
sort = sort[1:]
order = desc(column(sort))
Expand Down
92 changes: 49 additions & 43 deletions src/api/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import datetime
import io
import json
import os
import re
import uuid
from typing import List, Optional

import pandas as pd
import sqlmodel
import ujson
import re
import json

from fastapi import (
Depends,
FastAPI,
Expand All @@ -22,8 +21,6 @@
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse


from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi_pagination import add_pagination
Expand All @@ -36,12 +33,10 @@

from . import crud, graphql, models
from .database import get_db
from .models import CPFSummaryModel, ScenarioModel, ShotModel, SignalModel, SourceModel

templates = Jinja2Templates(directory="src/api/templates")



class JSONLDGraphQL(GraphQL):
async def process_result(
self, request: Request, result: ExecutionResult
Expand Down Expand Up @@ -196,24 +191,24 @@ def __init__(

class CustomJSONResponse(JSONResponse):
"""
serializes the result of a database query (a dictionary) into a JSON-readable format
serializes the result of a database query (a dictionary) into a JSON-readable format
"""

media_type = "application/json"
def render(self, content)-> bytes:

def render(self, content) -> bytes:
"""
renders the output of the request
"""
content = self.convert_to_jsonld_terms(content)
extracted_dict = {}
edited_content = self.extract_meta_key(content, extracted_dict)

# merge content with extracted context by placing context at the top
merged_content = {**extracted_dict, **edited_content}

return json.dumps(merged_content).encode()

def convert_to_jsonld_terms(self, items):
"""
Replaces '__' with ':', and [A-Za-z_] with [@A-Za-z] in the mapping of terms (column names) to
Expand All @@ -222,7 +217,6 @@ def convert_to_jsonld_terms(self, items):
if not isinstance(items, dict):
return items
for key, val in list(items.items()):

# Recursive key modification if value is a dictionary or list object
if isinstance(val, list):
items[key] = [self.convert_to_jsonld_terms(item) for item in val]
Expand All @@ -237,9 +231,9 @@ def convert_to_jsonld_terms(self, items):

def extract_meta_key(self, content, extracted_dict):
"""
Extract keys and values of @context and @type from the dictionary to
return them at the top of the dictionary as one entity for the whole dictionary,
rather than each for each item since they contain the same key and values
Extract keys and values of @context and @type from the dictionary to
return them at the top of the dictionary as one entity for the whole dictionary,
rather than each for each item since they contain the same key and values
"""
target_keys = ["@context", "@type", "dct:title"]
for k, v in list(content.items()):
Expand All @@ -255,7 +249,6 @@ def extract_meta_key(self, content, extracted_dict):
self.extract_meta_key(item, extracted_dict)
# return popped content
return content



def apply_pagination(
Expand Down Expand Up @@ -306,18 +299,19 @@ def query_aggregate(
"/json/shots",
description="Get information about experimental shots",
response_model=CursorPage[models.ShotModel],
response_class=CustomJSONResponse
response_class=CustomJSONResponse,
)
def get_shots(
db: Session = Depends(get_db), params: QueryParams = Depends()
) -> CursorPage[models.ShotModel]:
if params.sort is None:
params.sort = "shot_id"

query = crud.select_query(models.ShotModel, params.fields, params.filters, params.sort)

return paginate(db, query)
query = crud.select_query(
models.ShotModel, params.fields, params.filters, params.sort
)

return paginate(db, query)


@app.get("/json/shots/aggregate")
Expand All @@ -335,7 +329,7 @@ def get_shots_aggregate(
"/json/shots/{shot_id}",
description="Get information about a single experimental shot",
response_model=models.ShotModel,
response_class=CustomJSONResponse
response_class=CustomJSONResponse,
)
def get_shot(db: Session = Depends(get_db), shot_id: int = None) -> models.ShotModel:
shot = crud.get_shot(shot_id)
Expand All @@ -347,7 +341,7 @@ def get_shot(db: Session = Depends(get_db), shot_id: int = None) -> models.ShotM
"/json/shots/{shot_id}/signals",
description="Get information all signals for a single experimental shot",
response_model=CursorPage[models.SignalModel],
response_class=CustomJSONResponse
response_class=CustomJSONResponse,
)
def get_signals_for_shot(
db: Session = Depends(get_db),
Expand All @@ -362,21 +356,26 @@ def get_signals_for_shot(

# Get signals for this shot
params.filters.append(f"shot_id$eq:{shot['shot_id']}")
query = crud.select_query(models.SignalModel, params.fields, params.filters, params.sort)
query = crud.select_query(
models.SignalModel, params.fields, params.filters, params.sort
)
return paginate(db, query)


@app.get(
"/json/signals",
description="Get information about specific signals.",
response_model=CursorPage[models.SignalModel],
response_class=CustomJSONResponse
)
response_class=CustomJSONResponse,
)
def get_signals(
db: Session = Depends(get_db), params: QueryParams = Depends()
) -> CursorPage[models.SignalModel]:
) -> CursorPage[models.SignalModel]:
if params.sort is None:
params.sort = "uuid"
query = crud.select_query(models.SignalModel, params.fields, params.filters, params.sort)
query = crud.select_query(
models.SignalModel, params.fields, params.filters, params.sort
)

return paginate(db, query)

Expand All @@ -397,23 +396,23 @@ def get_signals_aggregate(
description="Get information about a single signal",
response_model_exclude_unset=True,
response_model=models.SignalModel,
response_class=CustomJSONResponse
response_class=CustomJSONResponse,
)
def get_signal(
db: Session = Depends(get_db), uuid_: uuid.UUID = None) -> models.SignalModel:
db: Session = Depends(get_db), uuid_: uuid.UUID = None
) -> models.SignalModel:
signal = crud.get_signal(uuid_)
signal = crud.execute_query_one(db, signal)

return signal



@app.get(
"/json/signals/{uuid_}/shot",
description="Get information about the shot for a single signal",
response_model_exclude_unset=True,
response_model=models.ShotModel,
response_class=CustomJSONResponse
response_class=CustomJSONResponse,
)
def get_shot_for_signal(
db: Session = Depends(get_db), uuid_: uuid.UUID = None
Expand All @@ -429,7 +428,7 @@ def get_shot_for_signal(
"/json/cpf_summary",
description="Get descriptions of CPF summary variables.",
response_model=CursorPage[models.CPFSummaryModel],
response_class=CustomJSONResponse
response_class=CustomJSONResponse,
)
def get_cpf_summary(
db: Session = Depends(get_db), params: QueryParams = Depends()
Expand All @@ -447,37 +446,43 @@ def get_cpf_summary(
"/json/scenarios",
description="Get information on different scenarios.",
response_model=CursorPage[models.ScenarioModel],
response_class=CustomJSONResponse
response_class=CustomJSONResponse,
)
def get_scenarios(
db: Session = Depends(get_db), params: QueryParams = Depends()
) -> CursorPage[models.ScenarioModel]:
if params.sort is None:
params.sort = "id"

query = crud.select_query(models.ScenarioModel, params.fields, params.filters, params.sort)
query = crud.select_query(
models.ScenarioModel, params.fields, params.filters, params.sort
)
return paginate(db, query)


@app.get(
"/json/sources",
description="Get information on different sources.",
response_model=CursorPage[models.SourceModel],
response_class=CustomJSONResponse
response_class=CustomJSONResponse,
)
def get_sources(
db: Session = Depends(get_db), params: QueryParams = Depends()
) -> CursorPage[models.SourceModel]:
if params.sort is None:
params.sort = "name"

query = crud.select_query(models.SourceModel, params.fields, params.filters, params.sort)
query = crud.select_query(
models.SourceModel, params.fields, params.filters, params.sort
)
return paginate(db, query)


@app.get("/json/sources/aggregate",
response_model=models.SourceModel,
response_class=CustomJSONResponse)
@app.get(
"/json/sources/aggregate",
response_model=models.SourceModel,
response_class=CustomJSONResponse,
)
def get_sources_aggregate(
request: Request,
response: Response,
Expand All @@ -492,7 +497,7 @@ def get_sources_aggregate(
"/json/sources/{name}",
description="Get information about a single signal",
response_model=models.SourceModel,
response_class=CustomJSONResponse
response_class=CustomJSONResponse,
)
def get_single_source(
db: Session = Depends(get_db), name: str = None
Expand All @@ -501,6 +506,7 @@ def get_single_source(
source = db.execute(source).one()[0]
return source


def ndjson_stream_query(db, query):
STREAM_SIZE = 1000
offset = 0
Expand Down
Loading

0 comments on commit 7d3d25c

Please sign in to comment.