Skip to content

Commit

Permalink
fix missing start variable and return empty string for order (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
russbiggs authored Sep 27, 2023
1 parent fd2ec69 commit 8f39d78
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 10 additions & 7 deletions openaq_api/openaq_api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@

logger = logging.getLogger("db")

allowed_config_params = ['work_mem']
allowed_config_params = ["work_mem"]


def default(obj):
return str(obj)


# config is required as a placeholder here because of this
# function is used in the `cached` decorator and without it
# we will get a number of arguments error
def dbkey(m, f, query, args, config = None):
def dbkey(m, f, query, args, config=None):
j = orjson.dumps(
args, option=orjson.OPT_OMIT_MICROSECONDS, default=default
).decode()
Expand Down Expand Up @@ -74,7 +76,7 @@ async def init(con):
class DB:
def __init__(self, request: Request):
self.request = request
request.state.timer.mark('db')
request.state.timer.mark("db")

async def acquire(self):
pool = await self.pool()
Expand All @@ -87,9 +89,10 @@ async def pool(self):
return self.request.app.state.pool

@cached(settings.API_CACHE_TIMEOUT, **cache_config)
async def fetch(self, query, kwargs, config = None):
async def fetch(self, query, kwargs, config=None):
pool = await self.pool()
self.request.state.timer.mark('pooled')
self.request.state.timer.mark("pooled")
start = time.time()
logger.debug("Start time: %s\nQuery: %s \nArgs:%s\n", start, query, kwargs)
rquery, args = render(query, **kwargs)
async with pool.acquire() as con:
Expand Down Expand Up @@ -124,7 +127,7 @@ async def fetch(self, query, kwargs, config = None):
raise HTTPException(status_code=500, detail=f"{e}")
logger.debug(
"query took: %s and returned:%s\n -- results_firstrow: %s",
self.request.state.timer.mark('fetched', 'since'),
self.request.state.timer.mark("fetched", "since"),
len(r),
str(r and r[0])[0:1000],
)
Expand All @@ -142,7 +145,7 @@ async def fetchval(self, query, kwargs):
return r[0]
return None

async def fetchPage(self, query, kwargs, config = None) -> OpenAQResult:
async def fetchPage(self, query, kwargs, config=None) -> OpenAQResult:
page = kwargs.get("page", 1)
limit = kwargs.get("limit", 1000)
kwargs["offset"] = abs((page - 1) * limit)
Expand Down
4 changes: 3 additions & 1 deletion openaq_api/openaq_api/v3/models/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,8 @@ def where(self) -> str:
else:
return ""

def order_by(self) -> str | None:
def order_by(self) -> str:
if self._sortable:
return f"ORDER BY {self.query.order_by.lower()} {self.query.sort_order.upper()}"
else:
return ""

0 comments on commit 8f39d78

Please sign in to comment.