Skip to content

Commit

Permalink
RF: Move database operation to database module, move comments into fu…
Browse files Browse the repository at this point in the history
…nc docstring
  • Loading branch information
mgxd committed Mar 16, 2023
1 parent adbed3c commit 6d73dc5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
35 changes: 28 additions & 7 deletions migas/server/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <project> where version not like '%+%';
- for vers in ^:
- select count(distinct session_id) from <project> where is_ci = false and version = ver;
- select count(distinct session_id) from <project> where is_ci = false and version = ver and status = 'C';
- select count(distinct user_id) from <project> where is_ci = false and version = ver;
- select count(*), date_part('isoyear', timestamp) as year, date_part('week', timestamp) as week from <project> where status = 'C' group by year, week order by year, week;
"""
p, _ = await get_project_tables(project)

async with gen_session() as session:
Expand Down Expand Up @@ -194,10 +205,20 @@ async def get_viz_data(project: str) -> list:
return data


# notes
# select distinct version from <project> where version not like '%+%';
# for vers in ^:
# select count(distinct session_id) from <project> where is_ci = false and version = ver;
# select count(distinct session_id) from <project> where is_ci = false and version = ver and status = 'C';
# select count(distinct user_id) from <project> where is_ci = false and version = ver;
# select count(*), date_part('isoyear', timestamp) as year, date_part('week', timestamp) as week from <project> 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
20 changes: 0 additions & 20 deletions migas/server/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion migas/server/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6d73dc5

Please sign in to comment.