Skip to content

Commit

Permalink
feat: fixed orderby and limit and alembic
Browse files Browse the repository at this point in the history
  • Loading branch information
SteZamboni committed Oct 17, 2024
1 parent fc28910 commit c5e0e37
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 190 deletions.
14 changes: 10 additions & 4 deletions api/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@ def run_migrations_offline() -> None:
include_name=include_name
)

# Here we need to enforce public if schema target_metadata.schema is None, which is default schema (public for postgres) for alembic
target_schema = 'public' if target_metadata.schema is None else target_metadata.schema

with context.begin_transaction():
context.execute(f'create schema if not exists "{target_metadata.schema}";')
context.execute(f'set search_path to "{target_metadata.schema}"')
context.execute(f'create schema if not exists "{target_schema}";')
context.execute(f'set search_path to "{target_schema}"')
context.run_migrations()


Expand All @@ -99,9 +102,12 @@ def run_migrations_online() -> None:
include_name=include_name
)

# Here we need to enforce public if schema target_metadata.schema is None, which is default schema (public for postgres) for alembic
target_schema = 'public' if target_metadata.schema is None else target_metadata.schema

with context.begin_transaction():
context.execute(f'create schema if not exists "{target_metadata.schema}";')
context.execute(f'set search_path to "{target_metadata.schema}"')
context.execute(f'create schema if not exists "{target_schema}";')
context.execute(f'set search_path to "{target_schema}"')
context.run_migrations()


Expand Down
97 changes: 0 additions & 97 deletions api/alembic/versions/750ad194c81a_add_percentage_column.py

This file was deleted.

30 changes: 30 additions & 0 deletions api/alembic/versions/dccb82489f4d_add_percentage_column.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""add percentage column
Revision ID: dccb82489f4d
Revises: 6edab3f23907
Create Date: 2024-10-17 09:03:48.063883
"""
from typing import Sequence, Union, Text

from alembic import op
import sqlalchemy as sa
from app.db.tables.commons.json_encoded_dict import JSONEncodedDict

# revision identifiers, used by Alembic.
revision: str = 'dccb82489f4d'
down_revision: Union[str, None] = '6edab3f23907'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('current_dataset_metrics', sa.Column('PERCENTAGES', JSONEncodedDict(astext_type=Text()), nullable=True))
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('current_dataset_metrics', 'PERCENTAGES')
# ### end Alembic commands ###
4 changes: 3 additions & 1 deletion api/app/db/dao/model_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_all(
with self.db.begin_session() as session:
return session.query(Model).where(Model.deleted.is_(False))

def get_all_percentages(self):
def get_last_n_percentages(self, n_models):
with self.db.begin_session() as session:
subq = (
session.query(
Expand All @@ -84,6 +84,8 @@ def get_all_percentages(self):
CurrentDatasetMetrics,
CurrentDatasetMetrics.current_uuid == CurrentDataset.uuid,
)
.order_by(Model.updated_at.desc())
.limit(n_models)
.all()
)

Expand Down
4 changes: 2 additions & 2 deletions api/app/db/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class Reflected(DeferredReflection):

# https://github.com/sqlalchemy/alembic/discussions/1351
# If the schema is the default, Alembic needs None otherwise migrations are messed up
schema_name = (
fixed_schema = (
None
if get_config().db_config.db_schema == 'public'
else get_config().db_config.db_schema
)
BaseTable = declarative_base(
metadata=MetaData(schema=schema_name, naming_convention=naming_convention)
metadata=MetaData(schema=fixed_schema, naming_convention=naming_convention)
)


Expand Down
5 changes: 2 additions & 3 deletions api/app/services/model_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get_all_models(
return model_out_list

def get_last_n_models_percentages(self, n_models) -> List[ModelOut]:
models = self.model_dao.get_all_percentages()
models = self.model_dao.get_last_n_percentages(n_models)
model_out_list_tmp = []
for model, metrics in models:
latest_reference_dataset, latest_current_dataset = self.get_latest_datasets(
Expand All @@ -99,8 +99,7 @@ def get_last_n_models_percentages(self, n_models) -> List[ModelOut]:
percentages=metrics.percentages,
)
model_out_list_tmp.append(model_out)
model_out_list_tmp.sort(key=lambda m: m.updated_at, reverse=True)
return model_out_list_tmp[:n_models]
return model_out_list_tmp

def get_all_models_paginated(
self,
Expand Down
Loading

0 comments on commit c5e0e37

Please sign in to comment.