Skip to content

Commit

Permalink
Add Pagination to API responses (#659)
Browse files Browse the repository at this point in the history
* updates for pagination for raw api response

Signed-off-by: Chloe Ching <[email protected]>

* update models for pagination

Signed-off-by: Chloe Ching <[email protected]>

* added pagination logic to raw

Signed-off-by: Chloe Ching <[email protected]>

* add pagination logic to all time series apis

Signed-off-by: Chloe Ching <[email protected]>

* updates

Signed-off-by: Chloe Ching <[email protected]>

* updated unit test for api pagination

Signed-off-by: Chloe Ching <[email protected]>

---------

Signed-off-by: Chloe Ching <[email protected]>
  • Loading branch information
cching95 authored Feb 12, 2024
1 parent f997c1a commit 019990f
Show file tree
Hide file tree
Showing 21 changed files with 248 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/api/v1/circular_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
CircularAverageQueryParams,
PivotQueryParams,
LimitOffsetQueryParams,
PaginationRow,
)
import src.api.v1.common

Expand Down Expand Up @@ -61,15 +62,35 @@ 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,
)
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,
)
except Exception as e:
logging.error(str(e))
Expand Down
22 changes: 22 additions & 0 deletions src/api/v1/circular_standard_deviation.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
PivotQueryParams,
LimitOffsetQueryParams,
CircularAverageQueryParams,
PaginationRow,
)
import src.api.v1.common

Expand Down Expand Up @@ -61,16 +62,37 @@ 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,
)
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,
)

except Exception as e:
logging.error(str(e))
raise HTTPException(status_code=400, detail=str(e))
Expand Down
21 changes: 21 additions & 0 deletions src/api/v1/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
InterpolateQueryParams,
PivotQueryParams,
LimitOffsetQueryParams,
PaginationRow,
)
import src.api.v1.common

Expand Down Expand Up @@ -62,15 +63,35 @@ 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,
)
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,
)
except Exception as e:
logging.error(str(e))
Expand Down
21 changes: 21 additions & 0 deletions src/api/v1/interpolation_at_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
InterpolationAtTimeQueryParams,
PivotQueryParams,
LimitOffsetQueryParams,
PaginationRow,
)
import src.api.v1.common

Expand All @@ -56,15 +57,35 @@ 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,
)
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,
)
except Exception as e:
logging.error(str(e))
Expand Down
20 changes: 20 additions & 0 deletions src/api/v1/latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
LatestResponse,
LimitOffsetQueryParams,
HTTPError,
PaginationRow,
)
from src.api.auth.azuread import oauth2_scheme
from src.api.FastAPIApp import api_v1_router
Expand All @@ -47,9 +48,28 @@ 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,
)
except Exception as e:
logging.error(str(e))
Expand Down
21 changes: 21 additions & 0 deletions src/api/v1/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
MetadataResponse,
LimitOffsetQueryParams,
HTTPError,
PaginationRow,
)
from src.api.auth.azuread import oauth2_scheme
from src.api.FastAPIApp import api_v1_router
Expand All @@ -47,10 +48,30 @@ 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,
)

except Exception as e:
logging.error(str(e))
raise HTTPException(status_code=400, detail=str(e))
Expand Down
12 changes: 12 additions & 0 deletions src/api/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ class LatestRow(BaseModel):
GoodValueType: Union[str, None]


class PaginationRow(BaseModel):
limit: Union[int, None]
offset: Union[int, None]
next: Union[int, None]


class RawRow(BaseModel):
EventTime: datetime
TagName: str
Expand All @@ -90,20 +96,23 @@ class MetadataResponse(BaseModel):
None, alias="schema", serialization_alias="schema"
)
data: List[MetadataRow]
pagination: Union[PaginationRow, None]


class LatestResponse(BaseModel):
field_schema: FieldSchema = Field(
None, alias="schema", serialization_alias="schema"
)
data: List[LatestRow]
pagination: Union[PaginationRow, None]


class RawResponse(BaseModel):
field_schema: FieldSchema = Field(
None, alias="schema", serialization_alias="schema"
)
data: List[RawRow]
pagination: Union[PaginationRow, None]


class ResampleInterpolateRow(BaseModel):
Expand All @@ -123,20 +132,23 @@ class ResampleInterpolateResponse(BaseModel):
None, alias="schema", serialization_alias="schema"
)
data: List[ResampleInterpolateRow]
pagination: Union[PaginationRow, None]


class SummaryResponse(BaseModel):
field_schema: FieldSchema = Field(
None, alias="schema", serialization_alias="schema"
)
data: List[SummaryRow]
pagination: Union[PaginationRow, None]


class PivotResponse(BaseModel):
field_schema: FieldSchema = Field(
None, alias="schema", serialization_alias="schema"
)
data: List[PivotRow]
pagination: Union[PaginationRow, None]


class HTTPError(BaseModel):
Expand Down
22 changes: 21 additions & 1 deletion src/api/v1/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pandas.io.json import build_table_schema
from fastapi import Query, HTTPException, Depends, Body
import nest_asyncio
from src.sdk.python.rtdip_sdk.queries.time_series import raw
from src.sdk.python.rtdip_sdk.queries.time_series import raw, summary
from src.api.v1.models import (
BaseHeaders,
BaseQueryParams,
Expand All @@ -28,6 +28,7 @@
TagsBodyParams,
LimitOffsetQueryParams,
HTTPError,
PaginationRow,
)
from src.api.auth.azuread import oauth2_scheme
from src.api.FastAPIApp import api_v1_router
Expand All @@ -53,9 +54,28 @@ def raw_events_get(
)

data = raw.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 RawResponse(
schema=build_table_schema(data, index=False, primary_key=False),
data=data.replace({np.nan: None}).to_dict(orient="records"),
pagination=pagination,
)
except Exception as e:
logging.error(str(e))
Expand Down
Loading

0 comments on commit 019990f

Please sign in to comment.