Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seans version #2585

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions augur/api/view/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,15 @@ def repo_table_view():
pagination_offset = config.get_value("frontend", "pagination_offset")

if current_user.is_authenticated:
data = load_repos_test(user = current_user, search = query, page = page, sort = sorting, direction = direction, source = "user")
page_count = load_repos_test(user = current_user, search = query, count = True, source = "user")
# data = current_user.get_repos(page = page, sort = sorting, direction = direction, search=query)[0]
# page_count = (current_user.get_repo_count(search = query)[0] or 0) // pagination_offset
data = current_user.get_repos(page = page, sort = sorting, direction = direction, search=query)[0]
repos_count = (current_user.get_repo_count(search = query)[0] or 0)
else:
data = load_repos_test(search = query, page = page, sort = sorting, direction = direction)
page_count = load_repos_test(search = query, count = True)
# data = get_all_repos(page = page, sort = sorting, direction = direction, search=query)[0]
# page_count = (get_all_repos_count(search = query)[0] or 0) // pagination_offset
data = get_all_repos(page = page, sort = sorting, direction = direction, search=query)[0]
repos_count = (get_all_repos_count(search = query)[0] or 0)

page_count = math.ceil(repos_count / pagination_offset) - 1

if not data.count():
if not data:
data = None


Expand Down
66 changes: 3 additions & 63 deletions augur/api/view/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
from augur.application.db.session import DatabaseSession
from augur.application.db.engine import DatabaseEngine
from augur.application.db.models import User, Repo, RepoGroup, UserGroup, UserRepo
from sqlalchemy import Column, Table, Integer, MetaData, or_, Label
from sqlalchemy import Column, Table, Integer, MetaData, or_
from sqlalchemy.sql.operators import ilike_op, distinct_op
from sqlalchemy.sql.functions import coalesce
from augur.application.db.models.base import Base

from sqlalchemy.orm import Query

init_logging()

from .init import logger
Expand Down Expand Up @@ -310,66 +312,4 @@ def render_module(module, **args):
return render_template('index.j2', **args)

""" ----------------------------------------------------------------
No longer used
"""
# My attempt at a loading page
def renderLoading(dest, query, request):
cache_files_requested.append(request)
return render_template('index.j2', body="loading", title="Loading", d=dest, query_key=query, api_url=getSetting('serving'))

with DatabaseEngine() as engine:
augur_data_schema = MetaData(schema = "augur_data")
augur_data_schema.reflect(bind = engine, views = True)

commits_materialized_view: Table = augur_data_schema.tables["augur_data.api_get_all_repos_commits"]
issues_materialized_view: Table = augur_data_schema.tables["augur_data.api_get_all_repos_issues"]

""" ----------------------------------------------------------------
"""
def load_repos_test(count = False, source = None, **kwargs):
columns: list[Label] = [
Repo.repo_id.distinct().label("repo_id"),
Repo.description.label("description"),
Repo.repo_git.label("url"),
coalesce(commits_materialized_view.columns.commits_all_time, 0).label("commits_all_time"),
coalesce(issues_materialized_view.columns.issues_all_time, 0).label("issues_all_time"),
RepoGroup.rg_name.label("rg_name"),
Repo.repo_git.regexp_replace('.*github\.com\/[A-Za-z0-9 \- _]+\/([A-Za-z0-9 \- _ .]+)$', "\\1").label("repo_name"),
Repo.repo_git.regexp_replace('.*github\.com\/([A-Za-z0-9 \- _]+)\/[A-Za-z0-9 \- _ .]+$', "\\1").label("repo_owner"),
RepoGroup.repo_group_id.label("repo_group_id")
]

def get_colum_by_label(label: str)-> Label:
for column in columns:
if column.name == label:
return column

repos = db_session.query(*columns)\
.outerjoin(commits_materialized_view, Repo.repo_id == commits_materialized_view.columns.repo_id)\
.outerjoin(issues_materialized_view, Repo.repo_id == issues_materialized_view.columns.repo_id)\
.join(RepoGroup, Repo.repo_group_id == RepoGroup.repo_group_id)

user: User = kwargs.get("user")
if user:
repos = repos.join(UserRepo, Repo.repo_id == UserRepo.repo_id)\
.join(UserGroup, UserGroup.group_id == UserRepo.group_id)\
.filter(UserGroup.user_id == user.user_id)

search = kwargs.get("search")
qkey = kwargs.get("query_key") or ["repo_name", "repo_owner"]
if search:
if isinstance(qkey, list) and len(qkey) > 0:
repos = repos.filter(or_(ilike_op(get_colum_by_label(filter_column), f"%{search}%") for filter_column in qkey))
else:
repos = repos.filter(ilike_op(get_colum_by_label(qkey), f"%{search}%"))

page_size: int = kwargs.get("page_size") or 25
if count:
c = repos.count()
return math.ceil(c / page_size) - 1

page: int = kwargs.get("page") or 0
offset = page * page_size

return repos.slice(offset, offset + page_size)

10 changes: 4 additions & 6 deletions augur/application/db/models/augur_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ class RepoGroup(Base):
data_source = Column(String)
data_collection_date = Column(TIMESTAMP(precision=0))

repo = relationship("Repo", back_populates="repo_group")

@staticmethod
def is_valid_repo_group_id(session, repo_group_id: int) -> bool:
"""Deterime is repo_group_id exists.
Expand Down Expand Up @@ -866,8 +868,8 @@ class Repo(Base):
TIMESTAMP(precision=0), server_default=text("CURRENT_TIMESTAMP")
)

repo_group = relationship("RepoGroup")
user_repo = relationship("UserRepo")
repo_group = relationship("RepoGroup", back_populates="repo")
user_repo = relationship("UserRepo", back_populates="repo")
collection_status = relationship("CollectionStatus", back_populates="repo")
issues = relationship("Issue", back_populates="repo")
prs = relationship("PullRequest", back_populates="repo")
Expand Down Expand Up @@ -1208,10 +1210,6 @@ class Commit(Base):
primaryjoin="Commit.cmt_author_platform_username == Contributor.cntrb_login",
back_populates="commits"
)
contributor1 = relationship(
"Contributor",
primaryjoin="Commit.cmt_author_platform_username == Contributor.cntrb_login",
)
repo = relationship("Repo", back_populates="commits")
message_ref = relationship("CommitCommentRef", back_populates="cmt")

Expand Down
32 changes: 16 additions & 16 deletions augur/application/db/models/augur_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ class User(Base):
{"schema": "augur_operations"}
)

groups = relationship("UserGroup")
tokens = relationship("UserSessionToken")
applications = relationship("ClientApplication")
groups = relationship("UserGroup", back_populates="user")
tokens = relationship("UserSessionToken", back_populates="user")
applications = relationship("ClientApplication", back_populates="user")

_is_authenticated = False
_is_active = True
Expand Down Expand Up @@ -628,8 +628,8 @@ class UserGroup(Base):
{"schema": "augur_operations"}
)

user = relationship("User")
repos = relationship("UserRepo")
user = relationship("User", back_populates="groups")
repos = relationship("UserRepo", back_populates="group")

@staticmethod
def insert(session, user_id:int, group_name:str) -> dict:
Expand Down Expand Up @@ -739,8 +739,8 @@ class UserRepo(Base):
ForeignKey("augur_data.repo.repo_id", name="user_repo_user_id_fkey"), primary_key=True, nullable=False
)

repo = relationship("Repo")
group = relationship("UserGroup")
repo = relationship("Repo", back_populates="user_repo")
group = relationship("UserGroup", back_populates="repos")

@staticmethod
def insert(session, repo_id: int, group_id:int = 1) -> bool:
Expand Down Expand Up @@ -949,9 +949,9 @@ class UserSessionToken(Base):
application_id = Column(ForeignKey("augur_operations.client_applications.id", name="user_session_token_application_id_fkey"), nullable=False)
created_at = Column(BigInteger)

user = relationship("User")
application = relationship("ClientApplication")
refresh_tokens = relationship("RefreshToken")
user = relationship("User", back_populates="tokens")
application = relationship("ClientApplication", back_populates="sessions")
refresh_tokens = relationship("RefreshToken", back_populates="user_session")

@staticmethod
def create(session, user_id, application_id, seconds_to_expire=86400):
Expand Down Expand Up @@ -991,9 +991,9 @@ class ClientApplication(Base):
redirect_url = Column(String, nullable=False)
api_key = Column(String, nullable=False)

user = relationship("User")
user = relationship("User", back_populates="applications")
sessions = relationship("UserSessionToken")
subscriptions = relationship("Subscription")
subscriptions = relationship("Subscription", back_populates="application")

def __eq__(self, other):
return isinstance(other, ClientApplication) and str(self.id) == str(other.id)
Expand All @@ -1013,8 +1013,8 @@ class Subscription(Base):
application_id = Column(ForeignKey("augur_operations.client_applications.id", name="subscriptions_application_id_fkey"), primary_key=True)
type_id = Column(ForeignKey("augur_operations.subscription_types.id", name="subscriptions_type_id_fkey"), primary_key=True)

application = relationship("ClientApplication")
type = relationship("SubscriptionType")
application = relationship("ClientApplication", back_populates="subscriptions")
type = relationship("SubscriptionType", back_populates="subscriptions")

class SubscriptionType(Base):
__tablename__ = "subscription_types"
Expand All @@ -1027,7 +1027,7 @@ class SubscriptionType(Base):
id = Column(BigInteger, primary_key=True)
name = Column(String, nullable=False)

subscriptions = relationship("Subscription")
subscriptions = relationship("Subscription", back_populates="type")


class RefreshToken(Base):
Expand All @@ -1040,7 +1040,7 @@ class RefreshToken(Base):
id = Column(String, primary_key=True)
user_session_token = Column(ForeignKey("augur_operations.user_session_tokens.token", name="refresh_token_session_token_id_fkey"), nullable=False)

user_session = relationship("UserSessionToken")
user_session = relationship("UserSessionToken", back_populates="refresh_tokens")

@staticmethod
def create(session, user_session_token_id):
Expand Down
Loading
Loading