|
1 | 1 | import datetime |
2 | 2 | import logging |
| 3 | +import warnings |
3 | 4 | from dataclasses import dataclass, fields |
4 | 5 | from typing import Any |
5 | 6 |
|
6 | 7 | import sqlalchemy as sa |
7 | 8 | from aiopg.sa.connection import SAConnection |
8 | 9 | from aiopg.sa.result import RowProxy |
| 10 | +from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine |
9 | 11 |
|
10 | 12 | from .models.groups import GroupType, groups, user_to_groups |
11 | 13 | from .models.groups_extra_properties import groups_extra_properties |
12 | 14 | from .utils_models import FromRowMixin |
| 15 | +from .utils_repos import pass_or_acquire_connection |
13 | 16 |
|
14 | 17 | _logger = logging.getLogger(__name__) |
15 | 18 |
|
@@ -103,31 +106,52 @@ def _merge_extra_properties_booleans( |
103 | 106 |
|
104 | 107 | @dataclass(frozen=True, slots=True, kw_only=True) |
105 | 108 | class GroupExtraPropertiesRepo: |
| 109 | + @staticmethod |
| 110 | + def _get_stmt(gid: int, product_name: str): |
| 111 | + return sa.select(groups_extra_properties).where( |
| 112 | + (groups_extra_properties.c.group_id == gid) |
| 113 | + & (groups_extra_properties.c.product_name == product_name) |
| 114 | + ) |
| 115 | + |
106 | 116 | @staticmethod |
107 | 117 | async def get( |
108 | 118 | connection: SAConnection, *, gid: int, product_name: str |
109 | 119 | ) -> GroupExtraProperties: |
110 | | - get_stmt = sa.select(groups_extra_properties).where( |
111 | | - (groups_extra_properties.c.group_id == gid) |
112 | | - & (groups_extra_properties.c.product_name == product_name) |
| 120 | + warnings.warn( |
| 121 | + f"{__name__}.get_v2 uses aiopg which has been deprecated in this repo." |
| 122 | + "Use get_v2 instead. " |
| 123 | + "See https://github.com/ITISFoundation/osparc-simcore/issues/4529", |
| 124 | + DeprecationWarning, |
| 125 | + stacklevel=1, |
113 | 126 | ) |
114 | | - result = await connection.execute(get_stmt) |
| 127 | + |
| 128 | + query = GroupExtraPropertiesRepo._get_stmt(gid, product_name) |
| 129 | + result = await connection.execute(query) |
115 | 130 | assert result # nosec |
116 | 131 | if row := await result.first(): |
117 | 132 | return GroupExtraProperties.from_row_proxy(row) |
118 | 133 | msg = f"Properties for group {gid} not found" |
119 | 134 | raise GroupExtraPropertiesNotFoundError(msg) |
120 | 135 |
|
121 | 136 | @staticmethod |
122 | | - async def get_aggregated_properties_for_user( |
123 | | - connection: SAConnection, |
| 137 | + async def get_v2( |
| 138 | + engine: AsyncEngine, |
| 139 | + connection: AsyncConnection | None = None, |
124 | 140 | *, |
125 | | - user_id: int, |
| 141 | + gid: int, |
126 | 142 | product_name: str, |
127 | 143 | ) -> GroupExtraProperties: |
128 | | - rows = await _list_table_entries_ordered_by_group_type( |
129 | | - connection, user_id, product_name |
130 | | - ) |
| 144 | + async with pass_or_acquire_connection(engine, connection) as conn: |
| 145 | + query = GroupExtraPropertiesRepo._get_stmt(gid, product_name) |
| 146 | + result = await conn.stream(query) |
| 147 | + assert result # nosec |
| 148 | + if row := await result.first(): |
| 149 | + return GroupExtraProperties.from_orm(row) |
| 150 | + msg = f"Properties for group {gid} not found" |
| 151 | + raise GroupExtraPropertiesNotFoundError(msg) |
| 152 | + |
| 153 | + @staticmethod |
| 154 | + def _aggregate(rows, user_id, product_name): |
131 | 155 | merged_standard_extra_properties = None |
132 | 156 | for row in rows: |
133 | 157 | group_extra_properties = GroupExtraProperties.from_row_proxy(row) |
@@ -161,3 +185,42 @@ async def get_aggregated_properties_for_user( |
161 | 185 | return merged_standard_extra_properties |
162 | 186 | msg = f"Properties for user {user_id} in {product_name} not found" |
163 | 187 | raise GroupExtraPropertiesNotFoundError(msg) |
| 188 | + |
| 189 | + @staticmethod |
| 190 | + async def get_aggregated_properties_for_user( |
| 191 | + connection: SAConnection, |
| 192 | + *, |
| 193 | + user_id: int, |
| 194 | + product_name: str, |
| 195 | + ) -> GroupExtraProperties: |
| 196 | + warnings.warn( |
| 197 | + f"{__name__}.get_aggregated_properties_for_user uses aiopg which has been deprecated in this repo. " |
| 198 | + "Use get_aggregated_properties_for_user_v2 instead. " |
| 199 | + "See https://github.com/ITISFoundation/osparc-simcore/issues/4529", |
| 200 | + DeprecationWarning, |
| 201 | + stacklevel=1, |
| 202 | + ) |
| 203 | + |
| 204 | + rows = await _list_table_entries_ordered_by_group_type( |
| 205 | + connection, user_id, product_name |
| 206 | + ) |
| 207 | + return GroupExtraPropertiesRepo._aggregate(rows, user_id, product_name) |
| 208 | + |
| 209 | + @staticmethod |
| 210 | + async def get_aggregated_properties_for_user_v2( |
| 211 | + engine: AsyncEngine, |
| 212 | + connection: AsyncConnection | None = None, |
| 213 | + *, |
| 214 | + user_id: int, |
| 215 | + product_name: str, |
| 216 | + ) -> GroupExtraProperties: |
| 217 | + async with pass_or_acquire_connection(engine, connection) as conn: |
| 218 | + |
| 219 | + list_stmt = _list_table_entries_ordered_by_group_type_query( |
| 220 | + user_id=user_id, product_name=product_name |
| 221 | + ) |
| 222 | + result = await conn.stream( |
| 223 | + sa.select(list_stmt).order_by(list_stmt.c.type_order) |
| 224 | + ) |
| 225 | + rows = [row async for row in result] |
| 226 | + return GroupExtraPropertiesRepo._aggregate(rows, user_id, product_name) |
0 commit comments