From ae7ad43afef71b848a7b031d57fff671ce016dcd Mon Sep 17 00:00:00 2001 From: Kraiem Taha Yassine Date: Thu, 8 Aug 2024 17:41:11 +0200 Subject: [PATCH] Dev (#2474) * refactor(chalice): upgraded dependencies * refactor(chalice): upgraded dependencies feat(chalice): support heatmaps * fix(chalice): fixed Math-operators validation refactor(chalice): search for sessions that have events for heatmaps * refactor(chalice): search for sessions that have at least 1 location event for heatmaps * refactor(chalice): upgraded dependencies * refactor(chalice): upgraded dependencies feat(chalice): support heatmaps * fix(chalice): fixed Math-operators validation refactor(chalice): search for sessions that have events for heatmaps * refactor(chalice): search for sessions that have at least 1 location event for heatmaps * refactor(chalice): upgraded dependencies refactor(crons): upgraded dependencies refactor(alerts): upgraded dependencies * feat(chalice): get top 10 values for autocomplete CH * refactor(chalice): cleaned code refactor(chalice): upgraded dependencies refactor(alerts): upgraded dependencies refactor(crons): upgraded dependencies * feat(chalice): autocomplete return top 10 with stats * fix(chalice): fixed autocomplete top 10 meta-filters * refactor(DB): enhanced top-events caching * feat(DB): support OR scope feat(chalice): support OR scope --- api/chalicelib/core/signup.py | 1 + api/requirements.txt | 1 + ee/api/chalicelib/core/scope.py | 30 ++++++++++++++++++++++++++++++ ee/api/chalicelib/core/signup.py | 1 + ee/api/chalicelib/core/tenants.py | 3 ++- ee/api/requirements.txt | 1 + ee/api/routers/core_dynamic.py | 7 +++++++ third-party.md | 1 + 8 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 ee/api/chalicelib/core/scope.py diff --git a/api/chalicelib/core/signup.py b/api/chalicelib/core/signup.py index e230bc1bd..651b80f5f 100644 --- a/api/chalicelib/core/signup.py +++ b/api/chalicelib/core/signup.py @@ -84,6 +84,7 @@ async def create_tenant(data: schemas.UserSignupSchema): 'refreshToken': r.pop('refreshToken'), 'refreshTokenMaxAge': r.pop('refreshTokenMaxAge'), 'data': { + "scope": "full", "user": r } } diff --git a/api/requirements.txt b/api/requirements.txt index cfbb23b76..4c78e760c 100644 --- a/api/requirements.txt +++ b/api/requirements.txt @@ -7,6 +7,7 @@ psycopg2-binary==2.9.9 psycopg[pool,binary]==3.2.1 elasticsearch==8.14.0 jira==3.8.0 +cachetools==5.4.0 diff --git a/ee/api/chalicelib/core/scope.py b/ee/api/chalicelib/core/scope.py new file mode 100644 index 000000000..4494d7105 --- /dev/null +++ b/ee/api/chalicelib/core/scope.py @@ -0,0 +1,30 @@ +from cachetools import cached, TTLCache + +import schemas +from chalicelib.utils import helper +from chalicelib.utils import pg_client + +cache = TTLCache(maxsize=1, ttl=24 * 60 * 60) + + +@cached(cache) +def get_scope(tenant_id) -> schemas.ScopeType: + with pg_client.PostgresClient() as cur: + query = cur.mogrify(f"""SELECT scope + FROM public.tenants + WHERE tenant_id=%(tenant_id)s;""", + {"tenant_id": tenant_id}) + cur.execute(query) + return helper.dict_to_camel_case(cur.fetchone())["scope"] + + +def update_scope(tenant_id, scope: schemas.ScopeType): + with pg_client.PostgresClient() as cur: + query = cur.mogrify(f"""UPDATE public.tenants + SET scope = %(scope)s + WHERE tenant_id=%(tenant_id)s;""", + {"scope": scope, "tenant_id": tenant_id}) + cur.execute(query) + if tenant_id in cache: + cache.pop(tenant_id) + return scope diff --git a/ee/api/chalicelib/core/signup.py b/ee/api/chalicelib/core/signup.py index 79b7d6d9f..fcc79d190 100644 --- a/ee/api/chalicelib/core/signup.py +++ b/ee/api/chalicelib/core/signup.py @@ -94,6 +94,7 @@ async def create_tenant(data: schemas.UserSignupSchema): 'refreshToken': r.pop('refreshToken'), 'refreshTokenMaxAge': r.pop('refreshTokenMaxAge'), 'data': { + "scope": "full", "user": r } } diff --git a/ee/api/chalicelib/core/tenants.py b/ee/api/chalicelib/core/tenants.py index 8e1321c5f..1340519e0 100644 --- a/ee/api/chalicelib/core/tenants.py +++ b/ee/api/chalicelib/core/tenants.py @@ -31,7 +31,8 @@ def get_by_tenant_id(tenant_id): '{license.EDITION}' AS edition, openreplay_version() AS version_number, tenants.opt_out, - tenants.tenant_key + tenants.tenant_key, + scope FROM public.tenants WHERE tenants.tenant_id = %(tenantId)s AND tenants.deleted_at ISNULL diff --git a/ee/api/requirements.txt b/ee/api/requirements.txt index d506ae76d..3ad9e0a2e 100644 --- a/ee/api/requirements.txt +++ b/ee/api/requirements.txt @@ -7,6 +7,7 @@ psycopg2-binary==2.9.9 psycopg[pool,binary]==3.2.1 elasticsearch==8.14.0 jira==3.8.0 +cachetools==5.4.0 diff --git a/ee/api/routers/core_dynamic.py b/ee/api/routers/core_dynamic.py index c4f07dfa4..ae22e2dd0 100644 --- a/ee/api/routers/core_dynamic.py +++ b/ee/api/routers/core_dynamic.py @@ -11,6 +11,7 @@ from chalicelib.core import sessions, assist, heatmaps, sessions_favorite, sessi from chalicelib.core import sessions_viewed from chalicelib.core import tenants, users, projects, license from chalicelib.core import webhook +from chalicelib.core import scope from chalicelib.core.collaboration_slack import Slack from chalicelib.core.users import get_user_settings from chalicelib.utils import SAML2_helper, smtp @@ -78,6 +79,7 @@ def login_user(response: JSONResponse, spot: Optional[bool] = False, data: schem content = { 'jwt': r.pop('jwt'), 'data': { + "scope":scope.get_scope(r["tenantId"]), "user": r } } @@ -138,6 +140,11 @@ def get_account(context: schemas.CurrentContext = Depends(OR_context)): def edit_account(data: schemas.EditAccountSchema = Body(...), context: schemas.CurrentContext = Depends(OR_context)): return users.edit_account(tenant_id=context.tenant_id, user_id=context.user_id, changes=data) +@app.post('/account/scope', tags=["account"]) +def change_scope(data: schemas.ScopeSchema = Body(), + context: schemas.CurrentContext = Depends(OR_context)): + data = scope.update_scope(tenant_id=-1, scope=data.scope) + return {'data': data} @app.post('/integrations/slack', tags=['integrations']) diff --git a/third-party.md b/third-party.md index 2679f383b..23454d20b 100644 --- a/third-party.md +++ b/third-party.md @@ -55,6 +55,7 @@ up to date with every new library you use. | sqlalchemy | MIT | Python | | pandas-redshift | MIT | Python | | confluent-kafka | Apache2 | Python | +| cachetools | MIT | Python | | amplitude-js | MIT | JavaScript | | classnames | MIT | JavaScript | | codemirror | MIT | JavaScript |