Skip to content

Commit e4e0210

Browse files
authored
Merge pull request #78 from DataIntegrationGroup/jir-transfer
BDMS-47 transfer
2 parents 74131eb + 4582b83 commit e4e0210

20 files changed

Lines changed: 4465 additions & 199 deletions

api/observation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ def get_groundwater_level_observations(
110110
sql = sql.where(Observation.observation_datetime <= end_time)
111111

112112
sql = order_sort_filter(sql, Observation, sort, order, filter_)
113+
114+
if not order:
115+
sql = sql.order_by(Observation.observation_datetime.desc())
116+
113117
return paginate(query=sql, conn=session)
114118

115119

api/search.py

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
from fastapi import APIRouter, Depends
1717
from sqlalchemy import select
1818
from sqlalchemy.orm import Session
19+
from api.pagination import CustomPage
20+
from fastapi_pagination import paginate
21+
from fastapi_pagination.utils import disable_installed_extensions_check
1922

23+
from core.dependencies import session_dependency
2024
from db import (
2125
Contact,
2226
Email,
@@ -27,12 +31,13 @@
2731
AssetThingAssociation,
2832
search,
2933
)
30-
from db.engine import get_db_session
3134

35+
36+
disable_installed_extensions_check()
3237
router = APIRouter(prefix="/search", tags=["search"])
3338

3439

35-
def _get_contact_results(session: Session, q: str) -> list[dict]:
40+
def _get_contact_results(session: Session, q: str, limit: int) -> list[dict]:
3641
vector = (
3742
Contact.search_vector
3843
| Email.search_vector
@@ -41,7 +46,10 @@ def _get_contact_results(session: Session, q: str) -> list[dict]:
4146
)
4247

4348
query = search(
44-
select(Contact).join(Email).join(Phone).join(Address), q, vector=vector
49+
select(Contact).join(Email).join(Phone).join(Address),
50+
q,
51+
vector=vector,
52+
limit=limit,
4553
)
4654
contacts = session.scalars(query).all()
4755
results = [
@@ -62,31 +70,71 @@ def _get_contact_results(session: Session, q: str) -> list[dict]:
6270
return results
6371

6472

65-
def _get_thing_results(session: Session, q: str) -> list[dict]:
73+
def _get_thing_results(session: Session, q: str, limit: int) -> list[dict]:
6674
vector = Thing.search_vector
67-
query = search(select(Thing), q, vector=vector)
75+
water_well_query = search(
76+
select(Thing).where(Thing.thing_type == "water well"),
77+
q,
78+
vector=vector,
79+
limit=limit,
80+
)
81+
spring_well_query = search(
82+
select(Thing).where(Thing.thing_type == "spring"), q, vector=vector, limit=limit
83+
)
6884

69-
things = session.scalars(query).all()
70-
results = [
71-
{
72-
"label": t.name,
73-
"group": "Things",
74-
"properties": {
75-
"well_type": t.well_type,
76-
"thing_type": t.thing_type,
77-
"id": t.id,
78-
},
85+
wells = session.scalars(water_well_query).all()
86+
springs = session.scalars(spring_well_query).all()
87+
88+
def _make_response(group: str, thing: Thing, properties: dict) -> dict:
89+
90+
if properties is None:
91+
properties = {}
92+
93+
properties["thing_type"] = thing.thing_type
94+
properties["id"] = thing.id
95+
return {
96+
"label": thing.name,
97+
"group": group,
98+
"properties": properties,
7999
}
80-
for t in things
81-
]
82100

83-
return results
101+
def make_well_response(thing: Thing) -> dict:
102+
return _make_response(
103+
"Wells",
104+
thing,
105+
{
106+
"well_type": thing.well_type,
107+
"well_depth": thing.well_depth,
108+
"hole_depth": thing.hole_depth,
109+
},
110+
)
111+
112+
def make_spring_response(thing: Thing) -> dict:
113+
return _make_response(
114+
"Springs",
115+
thing,
116+
{
117+
"spring_type": thing.spring_type,
118+
},
119+
)
120+
121+
return [
122+
func(item)
123+
for items, func in (
124+
(wells, make_well_response),
125+
(springs, make_spring_response),
126+
)
127+
for item in items
128+
]
84129

85130

86-
def _get_asset_results(session: Session, q: str) -> list[dict]:
131+
def _get_asset_results(session: Session, q: str, limit: int) -> list[dict]:
87132
vector = Asset.search_vector
88133
query = search(
89-
select(Asset).join(AssetThingAssociation).join(Thing), q, vector=vector
134+
select(Asset).join(AssetThingAssociation).join(Thing),
135+
q,
136+
vector=vector,
137+
limit=limit,
90138
)
91139

92140
assets = session.scalars(query).all()
@@ -109,16 +157,21 @@ def _get_asset_results(session: Session, q: str) -> list[dict]:
109157

110158

111159
@router.get("")
112-
def search_api(q: str, session: Session = Depends(get_db_session)):
160+
def search_api(
161+
session: session_dependency,
162+
q: str,
163+
limit: int = 25,
164+
) -> CustomPage[dict]:
113165
"""
114166
Search endpoint for the collaborative network.
115167
"""
116168

117-
results = _get_contact_results(session, q)
118-
results.extend(_get_thing_results(session, q))
119-
results.extend(_get_asset_results(session, q))
169+
results = _get_contact_results(session, q, limit)
170+
results.extend(_get_thing_results(session, q, limit))
171+
results.extend(_get_asset_results(session, q, limit))
120172

121-
return results
173+
return paginate(results)
174+
# return {"items": results, "total": len(results)}
122175

123176

124177
# ============= EOF =============================================

0 commit comments

Comments
 (0)