Skip to content

Commit

Permalink
Refactor and fix nitpicks
Browse files Browse the repository at this point in the history
  • Loading branch information
upadhyeammit committed Sep 26, 2023
1 parent b7adeaf commit 9ed4ba8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 36 deletions.
16 changes: 16 additions & 0 deletions ros/api/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ros.extensions import db
from sqlalchemy import exc
from ros.api.common.add_group_filter import group_filtered_query
from sqlalchemy import asc, desc

LOG = logging.getLogger(__name__)
prefix = "VALIDATE REQUEST"
Expand Down Expand Up @@ -90,3 +91,18 @@ def validate_request(*args, **kwargs):
return func(*args, **new_kwargs)

return validate_request


def sorting_order(order_how):
"""Sorting order method."""
method_name = None
if order_how == 'asc':
method_name = asc
elif order_how == 'desc':
method_name = desc
else:
abort(
403,
message="Incorrect sorting order. Possible values - ASC/DESC"
)
return method_name
20 changes: 3 additions & 17 deletions ros/api/v1/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from ros.lib.constants import SubStates

from datetime import datetime, timedelta, timezone
from sqlalchemy import asc, desc, nullslast, nullsfirst
from sqlalchemy import asc, nullslast, nullsfirst
from flask_restful import Resource, abort, fields, marshal_with
from ros.api.common.add_group_filter import group_filtered_query
from ros.api.common.utils import sorting_order

from ros.lib.models import (
db,
Expand Down Expand Up @@ -208,24 +209,9 @@ def build_system_filters():
filters.append(System.groups[0]['name'].astext.in_(group_names))
return filters

@staticmethod
def sorting_order(order_how):
"""Sorting order method."""
method_name = None
if order_how == 'asc':
method_name = asc
elif order_how == 'desc':
method_name = desc
else:
abort(
403,
message="Incorrect sorting order. Possible values - ASC/DESC"
)
return method_name

def build_sort_expression(self, order_how, order_method):
"""Build sort expression."""
sort_order = self.sorting_order(order_how)
sort_order = sorting_order(order_how)

if order_method == 'display_name':
return (sort_order(System.display_name),
Expand Down
22 changes: 4 additions & 18 deletions ros/api/v1/suggested_instance_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from flask import request
from sqlalchemy import func, asc, desc
from sqlalchemy import func
from flask_restful import Resource, fields, marshal_with, abort
from ros.api.common.add_group_filter import group_filtered_query
from ros.lib.utils import (
Expand All @@ -18,6 +18,7 @@
build_paginated_system_list_response
)
from ros.api.common.instance_types_helper import instance_types_desc_dict
from ros.api.common.utils import sorting_order


def non_null_suggested_instance_types():
Expand Down Expand Up @@ -80,27 +81,12 @@ def build_instance_filters():
filters = []
if filter_instance_type := request.args.get('instance_type'):
filters.append(PerformanceProfile.top_candidate.ilike(f'%{filter_instance_type}'))
# To Do: once we have multi cloud support add filter for cloud provider
# TODO: once ROS has multi cloud support, add cloud provider filter
return filters

@staticmethod
def sorting_order(order_how):
"""Sorting order method."""
method_name = None
if order_how == 'asc':
method_name = asc
elif order_how == 'desc':
method_name = desc
else:
abort(
403,
message="Incorrect sorting order. Possible values - ASC/DESC"
)
return method_name

def build_sort_expression(self, order_how, order_method):
"""Build sort expression."""
sort_order = self.sorting_order(order_how)
sort_order = sorting_order(order_how)

if order_method == 'instance_type':
return (sort_order(PerformanceProfile.top_candidate),)
Expand Down
43 changes: 42 additions & 1 deletion ros/openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,25 @@
"/suggested_instance_types": {
"get": {
"summary": "List all suggested instance types for the system",
"description": "Lists the systems and their suggested instance type as per the Resource Optimization Rules",
"description": "Lists suggested instance types as per the suggestions",
"operationId": "getSuggestedInstanceTypes",
"parameters": [
{
"$ref": "#/components/parameters/limitParam"
},
{
"$ref": "#/components/parameters/offsetParam"
},
{
"$ref": "#/components/parameters/orderByParamSuggestedInstanceType"
},
{
"$ref": "#/components/parameters/orderHowParam"
},
{
"$ref":"#/components/parameters/filterInstanceType"
}
],
"responses": {
"200": {
"description": "OK",
Expand Down Expand Up @@ -989,6 +1006,30 @@
"type": "string"
}
}
},
"filterInstanceType": {
"name": "instance_type",
"in": "query",
"required": false,
"description": "Filter suggested instances by instance type",
"schema": {
"type": "string"
}

},
"orderByParamSuggestedInstanceType": {
"name": "order_by",
"in": "query",
"required": false,
"description": "Ordering field name, defaults to system count",
"schema": {
"type": "string",
"default": "system_count",
"enum": [
"instance_type",
"system_count"
]
}
}

},
Expand Down

0 comments on commit 9ed4ba8

Please sign in to comment.