From 6d73dc50613256420cf07ce1e44f5a36abb799d7 Mon Sep 17 00:00:00 2001 From: mathiasg Date: Wed, 15 Mar 2023 22:50:13 -0400 Subject: [PATCH] RF: Move database operation to database module, move comments into func docstring --- migas/server/database.py | 35 ++++++++++++++++++++++++++++------- migas/server/models.py | 20 -------------------- migas/server/schema.py | 3 ++- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/migas/server/database.py b/migas/server/database.py index b9c7687..7fc2bd8 100644 --- a/migas/server/database.py +++ b/migas/server/database.py @@ -157,6 +157,17 @@ async def project_exists(project: str) -> bool: async def get_viz_data(project: str) -> list: + """ + TODO: Implement bucket sorting. + + Implements the following SQL pseudocode: + - select distinct version from where version not like '%+%'; + - for vers in ^: + - select count(distinct session_id) from where is_ci = false and version = ver; + - select count(distinct session_id) from where is_ci = false and version = ver and status = 'C'; + - select count(distinct user_id) from where is_ci = false and version = ver; + - select count(*), date_part('isoyear', timestamp) as year, date_part('week', timestamp) as week from where status = 'C' group by year, week order by year, week; + """ p, _ = await get_project_tables(project) async with gen_session() as session: @@ -194,10 +205,20 @@ async def get_viz_data(project: str) -> list: return data -# notes -# select distinct version from where version not like '%+%'; -# for vers in ^: -# select count(distinct session_id) from where is_ci = false and version = ver; -# select count(distinct session_id) from where is_ci = false and version = ver and status = 'C'; -# select count(distinct user_id) from where is_ci = false and version = ver; -# select count(*), date_part('isoyear', timestamp) as year, date_part('week', timestamp) as week from where status = 'C' group by year, week order by year, week; +async def verify_token(token: str) -> tuple[bool, list[str]]: + '''Query table for usage access''' + from sqlalchemy import select + from .models import Authentication + + # verify token pertains to project + projects = [] + async with gen_session() as session: + res = await session.execute( + select(Authentication.project).where(Authentication.token == token) + ) + if (project := res.one_or_none()) is not None: + if project[0] == 'master': + projects = await query_projects() + else: + projects = [project[0]] + return bool(project), projects diff --git a/migas/server/models.py b/migas/server/models.py index e63129c..7a4b9e0 100644 --- a/migas/server/models.py +++ b/migas/server/models.py @@ -176,23 +176,3 @@ async def gen_session() -> AsyncGenerator[AsyncSession, None]: yield session finally: await session.close() - - -async def verify_token(token: str) -> tuple[bool, list[str]]: - '''Query table for usage access''' - from sqlalchemy import select - - # verify token pertains to project - projects = [] - async with gen_session() as session: - res = await session.execute( - select(Authentication.project).where(Authentication.token == token) - ) - if project := res.one_or_none(): - if project[0] == 'master': - from .database import query_projects - - projects = await query_projects() - else: - projects = [project[0]] - return bool(project), projects diff --git a/migas/server/schema.py b/migas/server/schema.py index f1ba6bf..dafc099 100644 --- a/migas/server/schema.py +++ b/migas/server/schema.py @@ -16,9 +16,10 @@ project_exists, query_projects, query_usage_by_datetimes, + verify_token, ) from .fetchers import fetch_project_info -from .models import get_project_tables, verify_token +from .models import get_project_tables from .types import ( AuthenticationResult, Context,