-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add filterset class to room source, to clean the fields and return every datail of the field on a single class * feat: update agents, flows, queues, sectors, rooms, tags sources with the new filterset pattern * feat: update rooms and flowruns * feat: fix flows query executor * feat: fix imports * feat: minor fixes * feat: minor fixes * feat: minor fixes * feat: remove comments
- Loading branch information
1 parent
23daf0b
commit d084b36
Showing
32 changed files
with
496 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# use stub files to represent it on other parts of the code | ||
# Use django_filters Filter class as a reference | ||
from insights.sources.filtersets import GenericSQLFilter | ||
|
||
|
||
class AgentFilterSet: | ||
project = GenericSQLFilter( | ||
source_field="project_id", | ||
table_alias="pp", | ||
) | ||
project_id = project | ||
|
||
def get_field(self, field_name): | ||
try: | ||
return getattr(self, field_name) | ||
except AttributeError: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
class GenericSQLQueryGenerator: | ||
default_query_type = "count" | ||
|
||
def __init__( | ||
self, | ||
filter_strategy, | ||
query_builder, | ||
filterset, | ||
filters: dict, | ||
query_type: str = "", | ||
query_kwargs: dict = {}, | ||
) -> None: | ||
self.filter_strategy = filter_strategy | ||
self.query_builder = query_builder | ||
self.filterset = filterset | ||
self.filters = filters | ||
self.query_type = query_type or self.default_query_type | ||
self.query_kwargs = query_kwargs | ||
|
||
def generate(self): | ||
strategy = self.filter_strategy() | ||
builder = self.query_builder() | ||
filterset = self.filterset() | ||
|
||
for key, value in self.filters.items(): | ||
if "__" in key: | ||
field, operation = key.split("__", 1) | ||
elif type(value) is list: | ||
field = key.split("__", 1)[0] | ||
operation = "in" | ||
else: | ||
field, operation = key, "eq" | ||
field_object = filterset.get_field(field) | ||
if field_object is None: | ||
continue | ||
source_field = field_object.source_field | ||
join_clause = field_object.join_clause | ||
if join_clause != {}: | ||
builder.add_joins(join_clause) | ||
builder.add_filter( | ||
strategy, source_field, operation, value, field_object.table_alias | ||
) | ||
builder.build_query() | ||
|
||
return getattr(builder, self.query_type)(**self.query_kwargs) | ||
|
||
|
||
class GenericElasticSearchQueryGenerator: | ||
default_query_type = "count" | ||
|
||
def __init__( | ||
self, | ||
filter_strategy, | ||
query_builder, | ||
filterset, | ||
filters: dict, | ||
query_type: str = "", | ||
query_kwargs: dict = {}, | ||
) -> None: | ||
self.filter_strategy = filter_strategy | ||
self.query_builder = query_builder | ||
self.filterset = filterset | ||
self.filters = filters | ||
self.query_type = query_type or self.default_query_type | ||
self.query_kwargs = query_kwargs | ||
|
||
def generate(self): | ||
strategy = self.filter_strategy() | ||
builder = self.query_builder() | ||
filterset = self.filterset() | ||
|
||
for key, value in self.filters.items(): | ||
if "__" in key: | ||
field, operation = key.split("__", 1) | ||
elif type(value) is list: | ||
field = key.split("__", 1)[0] | ||
operation = "in" | ||
else: | ||
field, operation = key, "eq" | ||
field_object = filterset.get_field(field) | ||
if field_object is None: | ||
continue | ||
source_field = field_object.source_field | ||
builder.add_filter(strategy, source_field, operation, value) | ||
builder.build_query() | ||
|
||
return getattr(builder, self.query_type)(**self.query_kwargs) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class GenericSQLFilter: | ||
"""Responsible for cleaning and validating Filter data""" | ||
|
||
def __init__( | ||
self, | ||
source_field: str, | ||
table_alias: str, | ||
join_clause: dict = {}, | ||
value: any = None, | ||
) -> None: | ||
self.source_field = source_field | ||
self.table_alias = table_alias | ||
self.join_clause = join_clause | ||
|
||
|
||
class GenericElasticSearchFilter: | ||
"""Responsible for cleaning and validating Filter data""" | ||
|
||
def __init__( | ||
self, | ||
source_field: str, | ||
field_type: str, | ||
) -> None: | ||
self.source_field = source_field | ||
self.field_type = field_type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# use stub files to represent it on other parts of the code | ||
# Use django_filters Filter class as a reference | ||
from insights.sources.filtersets import GenericElasticSearchFilter | ||
|
||
|
||
class FlowRunFilterSet: | ||
created_on = GenericElasticSearchFilter( | ||
source_field="created_on", | ||
field_type="date", | ||
) | ||
exited_on = GenericElasticSearchFilter( | ||
source_field="exited_on", | ||
field_type="date", | ||
) | ||
ended_at = GenericElasticSearchFilter( | ||
source_field="exited_on", | ||
field_type="date", | ||
) | ||
project = GenericElasticSearchFilter( | ||
source_field="project_uuid", | ||
field_type="string", | ||
) | ||
flow = GenericElasticSearchFilter( | ||
source_field="flow_uuid", | ||
field_type="string", | ||
) | ||
|
||
def get_field(self, field_name): | ||
try: | ||
return getattr(self, field_name) | ||
except AttributeError: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.