forked from openreplay/openreplay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chalice): cache autocomplete-top-10 responses
feat(DB): support Spot login
- Loading branch information
Showing
13 changed files
with
214 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .or_cache import CachedResponse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import functools | ||
import inspect | ||
import json | ||
import logging | ||
from chalicelib.utils import pg_client | ||
import time | ||
from fastapi.encoders import jsonable_encoder | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CachedResponse: | ||
def __init__(self, table, ttl): | ||
self.table = table | ||
self.ttl = ttl | ||
|
||
def __call__(self, func): | ||
self.param_names = {i: param for i, param in enumerate(inspect.signature(func).parameters)} | ||
|
||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs): | ||
values = dict() | ||
for i, param in self.param_names.items(): | ||
if i < len(args): | ||
values[param] = args[i] | ||
elif param in kwargs: | ||
values[param] = kwargs[param] | ||
else: | ||
values[param] = None | ||
result = self.__get(values) | ||
if result is None or result["expired"] \ | ||
or result["result"] is None or len(result["result"]) == 0: | ||
now = time.time() | ||
result = func(*args, **kwargs) | ||
now = time.time() - now | ||
if result is not None and len(result) > 0: | ||
self.__add(values, result, now) | ||
result[0]["cached"] = False | ||
else: | ||
logger.info(f"using cached response for " | ||
f"{func.__name__}({','.join([f'{key}={val}' for key, val in enumerate(values)])})") | ||
result = result["result"] | ||
result[0]["cached"] = True | ||
|
||
return result | ||
|
||
return wrapper | ||
|
||
def __get(self, values): | ||
with pg_client.PostgresClient() as cur: | ||
sub_constraints = [] | ||
for key, value in values.items(): | ||
if value is not None: | ||
sub_constraints.append(f"{key}=%({key})s") | ||
else: | ||
sub_constraints.append(f"{key} IS NULL") | ||
query = f"""SELECT result, | ||
(%(ttl)s>0 | ||
AND EXTRACT(EPOCH FROM (timezone('utc'::text, now()) - created_at - INTERVAL %(interval)s)) > 0) AS expired | ||
FROM {self.table} | ||
WHERE {" AND ".join(sub_constraints)}""" | ||
query = cur.mogrify(query, {**values, 'ttl': self.ttl, 'interval': f'{self.ttl} seconds'}) | ||
logger.debug("------") | ||
logger.debug(query) | ||
logger.debug("------") | ||
cur.execute(query) | ||
result = cur.fetchone() | ||
return result | ||
|
||
def __add(self, values, result, execution_time): | ||
with pg_client.PostgresClient() as cur: | ||
query = f"""INSERT INTO {self.table} ({",".join(values.keys())},result,execution_time) | ||
VALUES ({",".join([f"%({param})s" for param in values.keys()])},%(result)s,%(execution_time)s) | ||
ON CONFLICT ({",".join(values.keys())}) DO UPDATE SET result=%(result)s, | ||
execution_time=%(execution_time)s, | ||
created_at=timezone('utc'::text, now());""" | ||
query = cur.mogrify(query, {**values, | ||
"result": json.dumps(jsonable_encoder(result)), | ||
"execution_time": execution_time}) | ||
logger.debug("------") | ||
logger.debug(query) | ||
logger.debug("------") | ||
cur.execute(query) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
ee/scripts/schema/db/rollback_dbs/postgresql/1.20.0/1.20.0.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
\set previous_version 'v1.20.0-ee' | ||
\set next_version 'v1.19.0-ee' | ||
SELECT openreplay_version() AS current_version, | ||
openreplay_version() = :'previous_version' AS valid_previous, | ||
openreplay_version() = :'next_version' AS is_next | ||
\gset | ||
|
||
\if :valid_previous | ||
\echo valid previous DB version :'previous_version', starting DB downgrade to :'next_version' | ||
BEGIN; | ||
SELECT format($fn_def$ | ||
CREATE OR REPLACE FUNCTION openreplay_version() | ||
RETURNS text AS | ||
$$ | ||
SELECT '%1$s' | ||
$$ LANGUAGE sql IMMUTABLE; | ||
$fn_def$, :'next_version') | ||
\gexec | ||
|
||
-- | ||
ALTER TABLE IF EXISTS public.users | ||
DROP COLUMN IF EXISTS spot_jwt_iat, | ||
DROP COLUMN IF EXISTS spot_jwt_refresh_jti, | ||
DROP COLUMN IF EXISTS spot_jwt_refresh_iat; | ||
|
||
DROP SCHEMA or_cache CASCADE; | ||
|
||
COMMIT; | ||
|
||
\elif :is_next | ||
\echo new version detected :'next_version', nothing to do | ||
\else | ||
\warn skipping DB downgrade of :'next_version', expected previous version :'previous_version', found :'current_version' | ||
\endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
scripts/schema/db/rollback_dbs/postgresql/1.20.0/1.20.0.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
\set previous_version 'v1.20.0' | ||
\set next_version 'v1.19.0' | ||
SELECT openreplay_version() AS current_version, | ||
openreplay_version() = :'previous_version' AS valid_previous, | ||
openreplay_version() = :'next_version' AS is_next | ||
\gset | ||
|
||
\if :valid_previous | ||
\echo valid previous DB version :'previous_version', starting DB downgrade to :'next_version' | ||
BEGIN; | ||
SELECT format($fn_def$ | ||
CREATE OR REPLACE FUNCTION openreplay_version() | ||
RETURNS text AS | ||
$$ | ||
SELECT '%1$s' | ||
$$ LANGUAGE sql IMMUTABLE; | ||
$fn_def$, :'next_version') | ||
\gexec | ||
|
||
-- | ||
ALTER TABLE IF EXISTS public.users | ||
DROP COLUMN IF EXISTS spot_jwt_iat, | ||
DROP COLUMN IF EXISTS spot_jwt_refresh_jti, | ||
DROP COLUMN IF EXISTS spot_jwt_refresh_iat; | ||
|
||
DROP SCHEMA or_cache CASCADE; | ||
|
||
COMMIT; | ||
|
||
\elif :is_next | ||
\echo new version detected :'next_version', nothing to do | ||
\else | ||
\warn skipping DB downgrade of :'next_version', expected previous version :'previous_version', found :'current_version' | ||
\endif |