Skip to content

Commit

Permalink
code review with coderabbit
Browse files Browse the repository at this point in the history
  • Loading branch information
daroczig committed Oct 12, 2024
1 parent 3a72aa0 commit 91299ad
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
16 changes: 8 additions & 8 deletions src/sc_keeper/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def search_servers(
if len(order_obj) == 0:
raise HTTPException(status_code=400, detail="Unknown order_by field.")
if len(order_obj) > 1:
raise HTTPException(status_code=400, detail="Unambiguous order_by field.")
raise HTTPException(status_code=400, detail="Ambiguous order_by field.")
order_field = getattr(order_obj[0], order_by)
if OrderDir(order_dir) == OrderDir.ASC:
query = query.order_by(order_field)
Expand Down Expand Up @@ -553,7 +553,7 @@ def search_server_prices(
if OrderDir(order_dir) == OrderDir.ASC:
query = query.order_by(ServerExtra.score / ServerPrice.price)
else:
query = query.order_by(ServerExtra.score / ServerPrice.price * -1)
query = query.order_by(ServerExtra.score / ServerPrice.price).desc()
else:
order_obj = [
o
Expand All @@ -563,9 +563,7 @@ def search_server_prices(
if len(order_obj) == 0:
raise HTTPException(status_code=400, detail="Unknown order_by field.")
if len(order_obj) > 1:
raise HTTPException(
status_code=400, detail="Unambiguous order_by field."
)
raise HTTPException(status_code=400, detail="Ambiguous order_by field.")
order_field = getattr(order_obj[0], order_by)
if OrderDir(order_dir) == OrderDir.ASC:
query = query.order_by(order_field)
Expand Down Expand Up @@ -622,7 +620,9 @@ def search_server_prices(
if OrderDir(order_dir) == OrderDir.ASC:
query = query.order_by(ServerExtra.score / subquery_aliased.price)
else:
query = query.order_by(ServerExtra.score / subquery_aliased.price * -1)
query = query.order_by(
ServerExtra.score / subquery_aliased.price
).desc()
else:
order_obj = [
o
Expand Down Expand Up @@ -756,7 +756,7 @@ def search_storage_prices(
if len(order_obj) == 0:
raise HTTPException(status_code=400, detail="Unknown order_by field.")
if len(order_obj) > 1:
raise HTTPException(status_code=400, detail="Unambiguous order_by field.")
raise HTTPException(status_code=400, detail="Ambiguous order_by field.")
order_field = getattr(order_obj[0], order_by)
if OrderDir(order_dir) == OrderDir.ASC:
query = query.order_by(order_field)
Expand Down Expand Up @@ -870,7 +870,7 @@ def search_traffic_prices(
if len(order_obj) == 0:
raise HTTPException(status_code=400, detail="Unknown order_by field.")
if len(order_obj) > 1:
raise HTTPException(status_code=400, detail="Unambiguous order_by field.")
raise HTTPException(status_code=400, detail="Ambiguous order_by field.")
order_field = getattr(order_obj[0], order_by)
if OrderDir(order_dir) == OrderDir.ASC:
query = query.order_by(order_field)
Expand Down
8 changes: 6 additions & 2 deletions src/sc_keeper/routers/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def get_similar_servers(
select(ServerExtra.score)
.where(ServerExtra.vendor_id == serverobj.vendor_id)
.where(ServerExtra.server_id == serverobj.server_id)
).one()
).first()
if target_score is None:
return []
query = query.where(ServerExtra.score.isnot(None)).order_by(
func.abs(ServerExtra.score - target_score)
)
Expand All @@ -113,7 +115,9 @@ def get_similar_servers(
select(ServerExtra.score_per_price)
.where(ServerExtra.vendor_id == serverobj.vendor_id)
.where(ServerExtra.server_id == serverobj.server_id)
).one()
).first()
if target_score_per_price is None:
return []
query = query.where(ServerExtra.score_per_price.isnot(None)).order_by(
func.abs(ServerExtra.score_per_price - target_score_per_price)
)
Expand Down
34 changes: 14 additions & 20 deletions src/sc_keeper/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ def insert(cls, session: Session):
items = []
for base in currencies:
for quote in currencies:
items.append(
{
"base": base,
"quote": quote,
"rate": cc.convert(1, base, quote),
}
)
items.append({
"base": base,
"quote": quote,
"rate": cc.convert(1, base, quote),
})
insert_items(cls, items, session=session)


Expand Down Expand Up @@ -76,20 +74,16 @@ def query():
"min_price"
),
func.min(
case(
(
ServerPrice.allocation == Allocation.SPOT,
func.round(ServerPrice.price * Currency.rate, 4),
)
)
case((
ServerPrice.allocation == Allocation.SPOT,
func.round(ServerPrice.price * Currency.rate, 4),
))
).label("min_price_spot"),
func.min(
case(
(
ServerPrice.allocation == Allocation.ONDEMAND,
func.round(ServerPrice.price * Currency.rate, 4),
)
)
case((
ServerPrice.allocation == Allocation.ONDEMAND,
func.round(ServerPrice.price * Currency.rate, 4),
))
).label("min_price_ondemand"),
)
.where(ServerPrice.status == Status.ACTIVE)
Expand All @@ -106,7 +100,7 @@ def query():
price.c.server_id,
scoren.c.score,
case(
(price.c.min_price.is_(None), None),
((price.c.min_price.is_(None)) | (price.c.min_price == 0), None),
else_=func.round(scoren.c.score / price.c.min_price, 4),
).label("score_per_price"),
score1.c.score1,
Expand Down

0 comments on commit 91299ad

Please sign in to comment.