Skip to content

Commit

Permalink
feat: filter testing
Browse files Browse the repository at this point in the history
  • Loading branch information
WilsonNet committed Sep 20, 2024
1 parent a584ded commit 5e01a8e
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 137 deletions.
6 changes: 3 additions & 3 deletions backend/kernelCI/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ def __getitem__(self, item):

CORS_ALLOW_ALL_ORIGINS = False

CACHE_TIMEOUT = int(get_json_env_var("CACHE_TIMEOUT", "600"))

if DEBUG:
CORS_ALLOWED_ORIGIN_REGEXES = [
r"^http://localhost", # dashboard dev server
Expand All @@ -201,6 +203,7 @@ def __getitem__(self, item):
CSRF_COOKIE_SECURE = False
SECURE_SSL_REDIRECT = False
SECURE_HSTS_SECONDS = 3600
CACHE_TIMEOUT = 0

if DEBUG_SQL_QUERY:
LOGGING = {
Expand Down Expand Up @@ -229,6 +232,3 @@ def __getitem__(self, item):
},
},
}


CACHE_TIMEOUT = int(get_json_env_var("CACHE_TIMEOUT", "600"))
60 changes: 33 additions & 27 deletions backend/kernelCI_app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import timedelta
import re

DEFAULT_QUERY_TIME_INTERVAL = {'days': 7}
DEFAULT_QUERY_TIME_INTERVAL = {"days": 7}


def toIntOrDefault(value, default):
Expand All @@ -25,7 +25,7 @@ def extract_platform(misc_environment: Union[str, dict, None]):
platform = parsedEnvMisc.get("platform")
if platform:
return platform
print("unknown platform in misc_environment", misc_environment)
# print("unknown platform in misc_environment", misc_environment)
return "unknown"


Expand All @@ -41,7 +41,7 @@ def extract_error_message(misc: Union[str, dict, None]):
error_message = parsedEnv.get("error_msg")
if error_message:
return error_message
print("unknown error_msg in misc", misc)
# print("unknown error_msg in misc", misc)
return "unknown error"


Expand All @@ -55,50 +55,54 @@ def getErrorResponseBody(reason: str):
return json.dumps({"error": True, "reason": reason})


class InvalidComparisonOP(Exception,):
class InvalidComparisonOP(
Exception,
):
pass


class FilterParams():
'''
class FilterParams:
"""
The param field form has two forms:
- with a comparison operator ?filter_<field>_[<comparison_op>]=<value>
- wit no comparison operator ?filter_<field>=<value>, the comparison operator `exact` will then be applied
- Lists
example: filter_category=cat1&ffilter_category=cat2
example: filter_category=cat1&filter_category=cat2
if a list is received then the comparison operator `in` is applied
'''
"""

filter_reg = re.compile(r"^(.*)_\[(.*)\]$")
filter_param_prefix = 'filter_'
comparison_op_type_idx = {
'orm': 0,
'raw': 1
}
filter_param_prefix = "filter_"
comparison_op_type_idx = {"orm": 0, "raw": 1}

comparison_ops = {
'exact': ['exact', '='],
'in': ['in', 'IN'],
'gt': ['gt', '>'],
'gte': ['gte', '>='],
'lt': ['lt', '<'],
'lte': ['lte', '<='],
"exact": ["exact", "="],
"in": ["in", "IN"],
"gt": ["gt", ">"],
"gte": ["gte", ">="],
"lt": ["lt", "<"],
"lte": ["lte", "<="],
}

def __init__(self, request):
self.filters = []
self.create_filters_from_req(request)

def create_filters_from_req(self, request):
print(request.GET.keys())
print(request.GET)
for k in request.GET.keys():
if not k.startswith(self.filter_param_prefix):
continue
filter_term = k[len(self.filter_param_prefix):]
# HACK: Flake8 will always bug with (): so we define a variable here
filter_param_prefix_length = len(self.filter_param_prefix)
filter_term = k[filter_param_prefix_length:]

# filter as list
if len(request.GET.getlist(k)) > 1:
field = filter_term
value = request.GET.getlist(k)
self.add_filter(field, value, 'in')
self.add_filter(field, value, "in")
continue

match = self.filter_reg.match(filter_term)
Expand All @@ -108,18 +112,20 @@ def create_filters_from_req(self, request):
self.add_filter(field, request.GET.get(k), comparison_op)
continue

self.add_filter(filter_term, request.GET.get(k), 'exact')
self.add_filter(filter_term, request.GET.get(k), "exact")

def add_filter(self, field, value, comparison_op):
self.validate_comparison_op(comparison_op)
self.filters.append({'field': field, 'value': value,
'comparison_op': comparison_op})
self.filters.append(
{"field": field, "value": value, "comparison_op": comparison_op}
)

def validate_comparison_op(self, op):
if op not in self.comparison_ops.keys():
raise InvalidComparisonOP(
f'Filter with invalid comparison operator `{op}` found`')
f"Filter with invalid comparison operator `{op}` found`"
)

def get_comparison_op(self, filter, op_type='orm'):
def get_comparison_op(self, filter, op_type="orm"):
idx = self.comparison_op_type_idx[op_type]
return self.comparison_ops[filter['comparison_op']][idx]
return self.comparison_ops[filter["comparison_op"]][idx]
Loading

0 comments on commit 5e01a8e

Please sign in to comment.