Skip to content

Commit

Permalink
Added timer (#280)
Browse files Browse the repository at this point in the history
* included order_by in v1 and v2 measurements (#263)

Co-authored-by: Gabriel Fosse <[email protected]>

* manufacturers resource (#273)

* manufacturers resource resolves #252
---------

Co-authored-by: Gabriel Fosse <[email protected]>

* Adding `v3/instruments` resource (#271)

* instruments resource resolves #270 

---------

Co-authored-by: Gabriel Fosse <[email protected]>

* Added timer

* Added owner router

---------

Co-authored-by: Gabriel Fosse <[email protected]>
Co-authored-by: Gabriel Fosse <[email protected]>
Co-authored-by: Russ Biggs <[email protected]>
  • Loading branch information
4 people authored Sep 27, 2023
1 parent c23796a commit 29d0b7a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
10 changes: 5 additions & 5 deletions openaq_api/openaq_api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def init(con):
class DB:
def __init__(self, request: Request):
self.request = request
logger.debug(f"New db: {request.app.state}")
request.state.timer.mark('db')

async def acquire(self):
pool = await self.pool()
Expand All @@ -89,20 +89,20 @@ async def pool(self):
@cached(settings.API_CACHE_TIMEOUT, **cache_config)
async def fetch(self, query, kwargs, config = None):
pool = await self.pool()
start = time.time()
self.request.state.timer.mark('pooled')
logger.debug("Start time: %s\nQuery: %s \nArgs:%s\n", start, query, kwargs)
rquery, args = render(query, **kwargs)
async with pool.acquire() as con:
try:
# a transaction is required to prevent auto-commit
self.request.state.timer.mark('connected')
# a transaction is required to prevent auto-commit
tr = con.transaction()
if config is not None:
await tr.start()
for param, value in config.items():
if param in allowed_config_params:
q = f"SELECT set_config('{param}', $1, TRUE)"
s = await con.execute(q, value)

r = await con.fetch(rquery, *args)
await tr.commit()
except asyncpg.exceptions.UndefinedColumnError as e:
Expand All @@ -125,7 +125,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",
time.time() - start,
self.request.state.timer.mark('fetched', 'since'),
len(r),
str(r and r[0])[0:1000],
)
Expand Down
2 changes: 1 addition & 1 deletion openaq_api/openaq_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
locations,
manufacturers,
measurements,
owners,
owners,
parameters,
providers,
sensors,
Expand Down
28 changes: 23 additions & 5 deletions openaq_api/openaq_api/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,34 @@ async def dispatch(self, request: Request, call_next):
return response


class Timer():
def __init__(self):
self.start_time = time.time()
self.last_mark = self.start_time
self.marks = []

def mark(self, key: str, return_time: str = 'total') -> float:
now = time.time()
mrk = {
"key": key,
"since": round((now - self.last_mark)*1000, 1),
"total": round((now - self.start_time)*1000, 1),
}
self.last_make = now
self.marks.append(mrk)
logger.debug(f"TIMER ({key}): {mrk['since']}")
return mrk.get(return_time)


class LoggingMiddleware(BaseHTTPMiddleware):
"""MiddleWare to set servers url on App with current url."""

async def dispatch(self, request: Request, call_next):
start_time = time.time()
request.state.timer = Timer()
response = await call_next(request)
process_time = time.time() - start_time
timing = round(process_time * 1000, 2)
if hasattr(request.state, "rate_limiter"):
rate_limiter = request.state.rate_limiter
timing = request.state.timer.mark('process')
if hasattr(request.app.state, "rate_limiter"):
rate_limiter = request.app.state.rate_limiter
else:
rate_limiter = None

Expand Down

0 comments on commit 29d0b7a

Please sign in to comment.