Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
26 changes: 4 additions & 22 deletions src/api/v1/circular_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@
CircularAverageQueryParams,
PivotQueryParams,
LimitOffsetQueryParams,
PaginationRow,
)
import src.api.v1.common
from src.api.v1.common import common_api_setup_tasks, pagination

nest_asyncio.apply()

Expand All @@ -51,7 +50,7 @@ def circular_average_events_get(
base_headers,
):
try:
(connection, parameters) = src.api.v1.common.common_api_setup_tasks(
(connection, parameters) = common_api_setup_tasks(
base_query_parameters,
raw_query_parameters=raw_query_parameters,
tag_query_parameters=tag_query_parameters,
Expand All @@ -63,34 +62,17 @@ def circular_average_events_get(

data = circular_average.get(connection, parameters)

pagination = None

if (
limit_offset_parameters.limit is not None
and limit_offset_parameters.offset is not None
):
next = None

if len(data.index) == limit_offset_parameters.limit:
next = limit_offset_parameters.offset + limit_offset_parameters.limit

pagination = PaginationRow(
limit=limit_offset_parameters.limit,
offset=limit_offset_parameters.offset,
next=next,
)

if parameters.get("pivot") == True:
return PivotResponse(
schema=build_table_schema(data, index=False, primary_key=False),
data=data.replace({np.nan: None}).to_dict(orient="records"),
pagination=pagination,
pagination=pagination(limit_offset_parameters, data),
)
else:
return ResampleInterpolateResponse(
schema=build_table_schema(data, index=False, primary_key=False),
data=data.replace({np.nan: None}).to_dict(orient="records"),
pagination=pagination,
pagination=pagination(limit_offset_parameters, data),
)
except Exception as e:
logging.error(str(e))
Expand Down
26 changes: 4 additions & 22 deletions src/api/v1/circular_standard_deviation.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@
PivotQueryParams,
LimitOffsetQueryParams,
CircularAverageQueryParams,
PaginationRow,
)
import src.api.v1.common
from src.api.v1.common import common_api_setup_tasks, pagination

nest_asyncio.apply()

Expand All @@ -51,7 +50,7 @@ def circular_standard_deviation_events_get(
base_headers,
):
try:
(connection, parameters) = src.api.v1.common.common_api_setup_tasks(
(connection, parameters) = common_api_setup_tasks(
base_query_parameters,
raw_query_parameters=raw_query_parameters,
tag_query_parameters=tag_query_parameters,
Expand All @@ -63,34 +62,17 @@ def circular_standard_deviation_events_get(

data = circular_standard_deviation.get(connection, parameters)

pagination = None

if (
limit_offset_parameters.limit is not None
and limit_offset_parameters.offset is not None
):
next = None

if len(data.index) == limit_offset_parameters.limit:
next = limit_offset_parameters.offset + limit_offset_parameters.limit

pagination = PaginationRow(
limit=limit_offset_parameters.limit,
offset=limit_offset_parameters.offset,
next=next,
)

if parameters.get("pivot") == True:
return PivotResponse(
schema=build_table_schema(data, index=False, primary_key=False),
data=data.replace({np.nan: None}).to_dict(orient="records"),
pagination=pagination,
pagination=pagination(limit_offset_parameters, data),
)
else:
return ResampleInterpolateResponse(
schema=build_table_schema(data, index=False, primary_key=False),
data=data.replace({np.nan: None}).to_dict(orient="records"),
pagination=pagination,
pagination=pagination(limit_offset_parameters, data),
)

except Exception as e:
Expand Down
25 changes: 24 additions & 1 deletion src/api/v1/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@

import os
import importlib.util

from pandas import DataFrame
from src.sdk.python.rtdip_sdk.connectors import DatabricksSQLConnection

if importlib.util.find_spec("turbodbc") != None:
from src.sdk.python.rtdip_sdk.connectors import TURBODBCSQLConnection
from src.api.auth import azuread
from src.api.v1.models import BaseHeaders
from .models import BaseHeaders, LimitOffsetQueryParams, PaginationRow


def common_api_setup_tasks( # NOSONAR
Expand Down Expand Up @@ -121,3 +123,24 @@ def common_api_setup_tasks( # NOSONAR
parameters = dict(parameters, **limit_offset_query_parameters.__dict__)

return connection, parameters


def pagination(limit_offset_parameters: LimitOffsetQueryParams, data: DataFrame):
pagination = None

if (
limit_offset_parameters.limit is not None
and limit_offset_parameters.offset is not None
):
next = None

if len(data.index) == limit_offset_parameters.limit:
next = limit_offset_parameters.offset + limit_offset_parameters.limit

pagination = PaginationRow(
limit=limit_offset_parameters.limit,
offset=limit_offset_parameters.offset,
next=next,
)

return pagination
26 changes: 4 additions & 22 deletions src/api/v1/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@
InterpolateQueryParams,
PivotQueryParams,
LimitOffsetQueryParams,
PaginationRow,
)
import src.api.v1.common
from src.api.v1.common import common_api_setup_tasks, pagination

nest_asyncio.apply()

Expand All @@ -51,7 +50,7 @@ def interpolate_events_get(
base_headers,
):
try:
(connection, parameters) = src.api.v1.common.common_api_setup_tasks(
(connection, parameters) = common_api_setup_tasks(
base_query_parameters,
raw_query_parameters=raw_query_parameters,
resample_query_parameters=resample_parameters,
Expand All @@ -64,34 +63,17 @@ def interpolate_events_get(

data = interpolate.get(connection, parameters)

pagination = None

if (
limit_offset_parameters.limit is not None
and limit_offset_parameters.offset is not None
):
next = None

if len(data.index) == limit_offset_parameters.limit:
next = limit_offset_parameters.offset + limit_offset_parameters.limit

pagination = PaginationRow(
limit=limit_offset_parameters.limit,
offset=limit_offset_parameters.offset,
next=next,
)

if parameters.get("pivot") == True:
return PivotResponse(
schema=build_table_schema(data, index=False, primary_key=False),
data=data.replace({np.nan: None}).to_dict(orient="records"),
pagination=pagination,
pagination=pagination(limit_offset_parameters, data),
)
else:
return ResampleInterpolateResponse(
schema=build_table_schema(data, index=False, primary_key=False),
data=data.replace({np.nan: None}).to_dict(orient="records"),
pagination=pagination,
pagination=pagination(limit_offset_parameters, data),
)
except Exception as e:
logging.error(str(e))
Expand Down
26 changes: 4 additions & 22 deletions src/api/v1/interpolation_at_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
InterpolationAtTimeQueryParams,
PivotQueryParams,
LimitOffsetQueryParams,
PaginationRow,
)
import src.api.v1.common
from src.api.v1.common import common_api_setup_tasks, pagination

nest_asyncio.apply()

Expand All @@ -47,7 +46,7 @@ def interpolation_at_time_events_get(
base_headers,
):
try:
(connection, parameters) = src.api.v1.common.common_api_setup_tasks(
(connection, parameters) = common_api_setup_tasks(
base_query_parameters,
tag_query_parameters=tag_query_parameters,
interpolation_at_time_query_parameters=interpolation_at_time_query_parameters,
Expand All @@ -58,34 +57,17 @@ def interpolation_at_time_events_get(

data = interpolation_at_time.get(connection, parameters)

pagination = None

if (
limit_offset_parameters.limit is not None
and limit_offset_parameters.offset is not None
):
next = None

if len(data.index) == limit_offset_parameters.limit:
next = limit_offset_parameters.offset + limit_offset_parameters.limit

pagination = PaginationRow(
limit=limit_offset_parameters.limit,
offset=limit_offset_parameters.offset,
next=next,
)

if parameters.get("pivot") == True:
return PivotResponse(
schema=build_table_schema(data, index=False, primary_key=False),
data=data.replace({np.nan: None}).to_dict(orient="records"),
pagination=pagination,
pagination=pagination(limit_offset_parameters, data),
)
else:
return ResampleInterpolateResponse(
schema=build_table_schema(data, index=False, primary_key=False),
data=data.replace({np.nan: None}).to_dict(orient="records"),
pagination=pagination,
pagination=pagination(limit_offset_parameters, data),
)
except Exception as e:
logging.error(str(e))
Expand Down
24 changes: 3 additions & 21 deletions src/api/v1/latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@
LatestResponse,
LimitOffsetQueryParams,
HTTPError,
PaginationRow,
)
from src.api.auth.azuread import oauth2_scheme
from src.api.v1.common import common_api_setup_tasks, pagination
from src.api.FastAPIApp import api_v1_router
from src.api.v1 import common

nest_asyncio.apply()

Expand All @@ -40,7 +39,7 @@ def latest_retrieval_get(
query_parameters, metadata_query_parameters, limit_offset_parameters, base_headers
):
try:
(connection, parameters) = common.common_api_setup_tasks(
(connection, parameters) = common_api_setup_tasks(
query_parameters,
metadata_query_parameters=metadata_query_parameters,
limit_offset_query_parameters=limit_offset_parameters,
Expand All @@ -49,27 +48,10 @@ def latest_retrieval_get(

data = latest.get(connection, parameters)

pagination = None

if (
limit_offset_parameters.limit is not None
and limit_offset_parameters.offset is not None
):
next = None

if len(data.index) == limit_offset_parameters.limit:
next = limit_offset_parameters.offset + limit_offset_parameters.limit

pagination = PaginationRow(
limit=limit_offset_parameters.limit,
offset=limit_offset_parameters.offset,
next=next,
)

return LatestResponse(
schema=build_table_schema(data, index=False, primary_key=False),
data=data.replace({np.nan: None}).to_dict(orient="records"),
pagination=pagination,
pagination=pagination(limit_offset_parameters, data),
)
except Exception as e:
logging.error(str(e))
Expand Down
24 changes: 3 additions & 21 deletions src/api/v1/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@
MetadataResponse,
LimitOffsetQueryParams,
HTTPError,
PaginationRow,
)
from src.api.auth.azuread import oauth2_scheme
from src.api.v1.common import common_api_setup_tasks, pagination
from src.api.FastAPIApp import api_v1_router
from src.api.v1 import common

nest_asyncio.apply()

Expand All @@ -40,7 +39,7 @@ def metadata_retrieval_get(
query_parameters, metadata_query_parameters, limit_offset_parameters, base_headers
):
try:
(connection, parameters) = common.common_api_setup_tasks(
(connection, parameters) = common_api_setup_tasks(
query_parameters,
metadata_query_parameters=metadata_query_parameters,
limit_offset_query_parameters=limit_offset_parameters,
Expand All @@ -49,27 +48,10 @@ def metadata_retrieval_get(

data = metadata.get(connection, parameters)

pagination = None

if (
limit_offset_parameters.limit is not None
and limit_offset_parameters.offset is not None
):
next = None

if len(data.index) == limit_offset_parameters.limit:
next = limit_offset_parameters.offset + limit_offset_parameters.limit

pagination = PaginationRow(
limit=limit_offset_parameters.limit,
offset=limit_offset_parameters.offset,
next=next,
)

return MetadataResponse(
schema=build_table_schema(data, index=False, primary_key=False),
data=data.replace({np.nan: None}).to_dict(orient="records"),
pagination=pagination,
pagination=pagination(limit_offset_parameters, data),
)

except Exception as e:
Expand Down
12 changes: 0 additions & 12 deletions src/api/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,18 +257,6 @@ def __init__(
self.end_date = end_date


class SqlQueryParams:
def __init__(
self,
sql_statement: str = Query(
...,
description="SQL Statement to be executed",
examples=["select * from 1"],
),
):
self.sql_statement = sql_statement


class SqlBodyParams(BaseModel):
sql_statement: str

Expand Down
Loading