Skip to content

Commit b638943

Browse files
author
Jongmin Kim
authored
Merge pull request #143 from whdalsrnt/master
Refactor group_by option in analyze method
2 parents ba0d3f5 + d3c4c09 commit b638943

File tree

1 file changed

+80
-63
lines changed

1 file changed

+80
-63
lines changed

src/spaceone/core/model/mongo_model/__init__.py

Lines changed: 80 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ def _make_condition(cls, condition):
479479
if operator not in FILTER_OPERATORS:
480480
raise ERROR_DB_QUERY(
481481
reason=f"Filter operator is not supported. (operator = "
482-
f"{FILTER_OPERATORS.keys()})"
482+
f"{FILTER_OPERATORS.keys()})"
483483
)
484484

485485
resolver, mongo_operator, is_multiple = FILTER_OPERATORS.get(operator)
@@ -566,14 +566,14 @@ def _make_unwind_project_stage(only: list):
566566

567567
@classmethod
568568
def _stat_with_unwind(
569-
cls,
570-
unwind: list,
571-
only: list = None,
572-
filter: list = None,
573-
filter_or: list = None,
574-
sort: list = None,
575-
page: dict = None,
576-
target: str = None,
569+
cls,
570+
unwind: list,
571+
only: list = None,
572+
filter: list = None,
573+
filter_or: list = None,
574+
sort: list = None,
575+
page: dict = None,
576+
target: str = None,
577577
):
578578
if only is None:
579579
raise ERROR_DB_QUERY(reason="unwind option requires only option.")
@@ -641,19 +641,19 @@ def _stat_with_unwind(
641641

642642
@classmethod
643643
def query(
644-
cls,
645-
*args,
646-
only=None,
647-
exclude=None,
648-
filter=None,
649-
filter_or=None,
650-
sort=None,
651-
page=None,
652-
minimal=False,
653-
count_only=False,
654-
unwind=None,
655-
target=None,
656-
**kwargs,
644+
cls,
645+
*args,
646+
only=None,
647+
exclude=None,
648+
filter=None,
649+
filter_or=None,
650+
sort=None,
651+
page=None,
652+
minimal=False,
653+
count_only=False,
654+
unwind=None,
655+
target=None,
656+
**kwargs,
657657
):
658658
filter = filter or []
659659
filter_or = filter_or or []
@@ -715,7 +715,7 @@ def query(
715715
if start < 1:
716716
start = 1
717717

718-
vos = vos[start - 1 : start + page["limit"] - 1]
718+
vos = vos[start - 1: start + page["limit"] - 1]
719719

720720
return vos, total_count
721721

@@ -786,7 +786,7 @@ def _make_sub_conditions(cls, sub_conditions, _before_group_keys):
786786
if operator not in _SUPPORTED_OPERATOR:
787787
raise ERROR_DB_QUERY(
788788
reason=f"'aggregate.group.fields.conditions.operator' condition's {operator} operator is not "
789-
f"supported. (supported_operator = {_SUPPORTED_OPERATOR})"
789+
f"supported. (supported_operator = {_SUPPORTED_OPERATOR})"
790790
)
791791

792792
if key in _before_group_keys:
@@ -808,7 +808,7 @@ def _get_group_fields(cls, condition, _before_group_keys):
808808
if operator not in STAT_GROUP_OPERATORS:
809809
raise ERROR_DB_QUERY(
810810
reason=f"'aggregate.group.fields' condition's {operator} operator is not supported. "
811-
f"(supported_operator = {list(STAT_GROUP_OPERATORS.keys())})"
811+
f"(supported_operator = {list(STAT_GROUP_OPERATORS.keys())})"
812812
)
813813

814814
if name is None:
@@ -927,7 +927,7 @@ def _get_project_fields(cls, condition):
927927
if operator and operator not in STAT_PROJECT_OPERATORS:
928928
raise ERROR_DB_QUERY(
929929
reason=f"'aggregate.project.fields' condition's {operator} operator is not supported. "
930-
f"(supported_operator = {list(STAT_PROJECT_OPERATORS.keys())})"
930+
f"(supported_operator = {list(STAT_PROJECT_OPERATORS.keys())})"
931931
)
932932

933933
if name is None:
@@ -1085,9 +1085,9 @@ def _make_aggregate_rules(cls, aggregate):
10851085
else:
10861086
raise ERROR_REQUIRED_PARAMETER(
10871087
key="aggregate.unwind or aggregate.group or "
1088-
"aggregate.count or aggregate.sort or "
1089-
"aggregate.project or aggregate.limit or "
1090-
"aggregate.skip"
1088+
"aggregate.count or aggregate.sort or "
1089+
"aggregate.project or aggregate.limit or "
1090+
"aggregate.skip"
10911091
)
10921092

10931093
return _aggregate_rules
@@ -1141,23 +1141,23 @@ def _stat_distinct(cls, vos, distinct, page):
11411141
start = 1
11421142

11431143
result["total_count"] = len(values)
1144-
values = values[start - 1 : start + page["limit"] - 1]
1144+
values = values[start - 1: start + page["limit"] - 1]
11451145

11461146
result["results"] = cls._make_distinct_values(values)
11471147
return result
11481148

11491149
@classmethod
11501150
def stat(
1151-
cls,
1152-
*args,
1153-
aggregate=None,
1154-
distinct=None,
1155-
filter=None,
1156-
filter_or=None,
1157-
page=None,
1158-
target="SECONDARY_PREFERRED",
1159-
allow_disk_use=False,
1160-
**kwargs,
1151+
cls,
1152+
*args,
1153+
aggregate=None,
1154+
distinct=None,
1155+
filter=None,
1156+
filter_or=None,
1157+
page=None,
1158+
target="SECONDARY_PREFERRED",
1159+
allow_disk_use=False,
1160+
**kwargs,
11611161
):
11621162
filter = filter or []
11631163
filter_or = filter_or or []
@@ -1195,8 +1195,25 @@ def _check_field_group(cls, field_group):
11951195
@classmethod
11961196
def _make_group_keys(cls, group_by, date_field, granularity=None):
11971197
group_keys = []
1198-
for key in group_by:
1199-
name = key.rsplit(".", 1)[-1:][0]
1198+
for group_option in group_by:
1199+
if isinstance(group_option, dict):
1200+
key = group_option.get("key")
1201+
name = group_option.get("name")
1202+
1203+
if not (key and name):
1204+
raise ERROR_INVALID_PARAMETER(
1205+
key="group_by",
1206+
reason="group_by option requires a key and name.",
1207+
)
1208+
elif isinstance(group_option, str):
1209+
key = group_option
1210+
name = key.rsplit(".", 1)[-1:][0]
1211+
else:
1212+
raise ERROR_INVALID_PARAMETER(
1213+
key="group_by",
1214+
reason="group_by option should be dict or str type.",
1215+
)
1216+
12001217
group_keys.append({"key": key, "name": name})
12011218

12021219
if granularity and granularity in ["DAILY", "MONTHLY", "YEARLY"]:
@@ -1249,9 +1266,9 @@ def _check_condition(cls, condition):
12491266
@classmethod
12501267
def _make_field_group_keys(cls, group_keys, field_group):
12511268
field_group_keys = []
1252-
for group_key in group_keys:
1253-
key = group_key["key"].rsplit(".", 1)[-1:][0]
1254-
name = group_key["name"]
1269+
for group_option in group_keys:
1270+
key = group_option["name"]
1271+
name = group_option["name"]
12551272
if name not in field_group:
12561273
if name == "date":
12571274
field_group_keys.append({"key": "date", "name": "date"})
@@ -1436,24 +1453,24 @@ def _convert_date_value(cls, date_value, date_field_format):
14361453

14371454
@classmethod
14381455
def analyze(
1439-
cls,
1440-
*args,
1441-
granularity=None,
1442-
fields=None,
1443-
select=None,
1444-
group_by=None,
1445-
field_group=None,
1446-
filter=None,
1447-
filter_or=None,
1448-
page=None,
1449-
sort=None,
1450-
start=None,
1451-
end=None,
1452-
date_field="date",
1453-
date_field_format="%Y-%m-%d",
1454-
target="SECONDARY_PREFERRED",
1455-
allow_disk_use=False,
1456-
**kwargs,
1456+
cls,
1457+
*args,
1458+
granularity=None,
1459+
fields=None,
1460+
select=None,
1461+
group_by=None,
1462+
field_group=None,
1463+
filter=None,
1464+
filter_or=None,
1465+
page=None,
1466+
sort=None,
1467+
start=None,
1468+
end=None,
1469+
date_field="date",
1470+
date_field_format="%Y-%m-%d",
1471+
target="SECONDARY_PREFERRED",
1472+
allow_disk_use=False,
1473+
**kwargs,
14571474
):
14581475
if fields is None:
14591476
raise ERROR_REQUIRED_PARAMETER(key="fields")

0 commit comments

Comments
 (0)