Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(QueryObject): add QueryObjectFactory to meet SRP #17466

Merged
merged 1 commit into from
Nov 18, 2021

Conversation

ofekisr
Copy link
Contributor

@ofekisr ofekisr commented Nov 17, 2021

Background

When we have worked on #16991 we wanted to test the new functionalities in concrete and accurate unittest.
All chartData flows and its components are too couple to superset so it is impossible to create unittests.
The flows are not testable and so many components do not meet the very important principle SRP and the code became so dirty

So I've started to refactor it (#17344 ) but many changes were added and it was hard to review so I decided to split those changes into small PRs so will be easier to follow

This is the eighth PR in a sequence of PRs to meet these
The next PR is #17479

PR description

Creating QueryObject is not only an assignment task, it contains some logic.
To meet the SRP, The creation task should be assigned to an external object.
After the PR the QueryObject constructor is decoupled from Superset so in the next PR, the module can be decoupled from Superset

Test plans

There is no logic added so new tests are not required

Previous PRs

  1. refactor(ChartData): move ChartDataResult enums to common #17399
  2. refactor(ChartData): move chart_data_apis from ChartRestApi ChartDataRestApi #17400
  3. refactor(ChartDataCommand): separate loading query_context form cache into different module #17405
  4. refactor(TestChartApi): move chart data api tests into TestChartDataApi #17407
  5. refactor(ChartDataCommand): into two separate commands #17425
  6. refactor(ChartDataCommand): remove create queryContext command's responsibly  #17461
  7. refactor(QueryObject): decouple from queryContext and clean code #17465

Copy link
Member

@zhaoyongjie zhaoyongjie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the refactor, a few factory patterns in Python suggestion

Comment on lines 79 to 102
class QueryObjectFactory:
def create(self,
result_type: ChartDataResultType,
datasource: Optional[DatasourceDict] = None,
extras: Optional[Dict[str, Any]] = None,
row_limit: Optional[int] = None,
time_range: Optional[str] = None,
time_shift: Optional[str] = None,
**kwargs) -> QueryObject:
datasource_model_instance = None
if datasource:
datasource_model_instance = self._convert_to_model(datasource)
extras = self._process_extras(extras)
row_limit = self._process_row_limit(row_limit, result_type)
from_dttm, to_dttm = self._get_dttms(time_range, time_shift, extras)
return QueryObject(result_type,
datasource=datasource_model_instance,
extras=extras,
row_limit=row_limit,
from_dttm=from_dttm,
to_dttm=to_dttm,
time_range=time_range,
time_shift=time_shift,
**kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    @classmethod
    def create(cls,
               result_type: ChartDataResultType,
               datasource: Optional[DatasourceDict] = None,
               extras: Optional[Dict[str, Any]] = None,
               row_limit: Optional[int] = None,
               time_range: Optional[str] = None,
               time_shift: Optional[str] = None,
               **kwargs) -> QueryObject:
        query_object_instance = cls()
        datasource_model_instance = None
        if datasource:
            datasource_model_instance = query_object_instance._convert_to_model(datasource)
        extras = query_object_instance._process_extras(extras)
        row_limit = query_object_instance._process_row_limit(row_limit, result_type)
        from_dttm, to_dttm = query_object_instance._get_dttms(time_range, time_shift, extras)
        return QueryObject(result_type,
                           datasource=datasource_model_instance,
                           extras=extras,
                           row_limit=row_limit,
                           from_dttm=from_dttm,
                           to_dttm=to_dttm,
                           time_range=time_range,
                           time_shift=time_shift,
                           **kwargs)

Comment on lines 107 to 108
query_object_factory = QueryObjectFactory()
self.queries = [query_object_factory.create(**query_obj) for query_obj in queries]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

       self.queries = [QueryObjectFactory.create(**query_obj) for query_obj in queries]

Copy link
Contributor Author

@ofekisr ofekisr Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. In general, using class methods should be avoided, hard to test.
  2. this is only a temp change, after that PR will be another PRs so the Factory will move to another module and config will be injected to it so it wont be couple to superset

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I will take a closer look tomorrow.

@ofekisr ofekisr marked this pull request as ready for review November 18, 2021 13:17
@ofekisr ofekisr changed the title refactor(QueryObject) refactor(QueryObject): add QueryObjectFactory to meet SRP Nov 18, 2021
@codecov
Copy link

codecov bot commented Nov 18, 2021

Codecov Report

Merging #17466 (8dd6248) into master (b914e2d) will increase coverage by 0.08%.
The diff coverage is 91.89%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #17466      +/-   ##
==========================================
+ Coverage   76.87%   76.95%   +0.08%     
==========================================
  Files        1042     1042              
  Lines       56331    56345      +14     
  Branches     7793     7793              
==========================================
+ Hits        43304    43360      +56     
+ Misses      12771    12729      -42     
  Partials      256      256              
Flag Coverage Δ
hive 81.53% <91.89%> (+<0.01%) ⬆️
mysql 81.95% <91.89%> (+<0.01%) ⬆️
postgres 81.96% <91.89%> (+<0.01%) ⬆️
presto 81.83% <91.89%> (?)
python 82.46% <91.89%> (+0.15%) ⬆️
sqlite 81.64% <91.89%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
superset/common/query_object.py 93.03% <91.42%> (-0.59%) ⬇️
superset/common/query_context.py 91.89% <100.00%> (+0.03%) ⬆️
superset/models/core.py 90.00% <0.00%> (+0.73%) ⬆️
superset/connectors/sqla/models.py 88.37% <0.00%> (+1.35%) ⬆️
superset/db_engine_specs/presto.py 90.37% <0.00%> (+6.06%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b914e2d...8dd6248. Read the comment docs.

Copy link
Member

@amitmiran137 amitmiran137 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/L 🚢 1.5.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants