Skip to content

Commit

Permalink
Merge pull request #139 from stat-kwon/master
Browse files Browse the repository at this point in the history
Change cache logic when loading Widget
  • Loading branch information
stat-kwon authored Dec 24, 2024
2 parents cd876d0 + 71f7915 commit 0379372
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 81 deletions.
17 changes: 11 additions & 6 deletions src/spaceone/dashboard/manager/data_table_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def __init__(self, *args, **kwargs):
self.jinja_variables = None
self.state = None
self.error_message = None
self.cache_hash_key = None

def get_data_and_labels_info(self) -> Tuple[dict, dict]:
raise NotImplementedError()
Expand Down Expand Up @@ -60,8 +59,8 @@ def load_from_widget(
data_table_id, granularity, start, end, group_by, sort
)

self.cache_hash_key = utils.dict_to_hash(query_data)
response = self._get_cached_response()
cache_hash_key = utils.dict_to_hash(query_data)
response = self._get_cached_response(cache_hash_key)

if not response:

Expand All @@ -79,9 +78,13 @@ def load_from_widget(
"results": self.df.copy(deep=True).to_dict(orient="records")
}
cache.set(
f"dashboard:Widget:load:{self.cache_hash_key}", response, expire=600
f"dashboard:widget:load:{self.domain_id}:{self.widget_id}:{cache_hash_key}",
response,
expire=600,
)

self.df = None

if column_sum:
return self.response_sum_data_from_widget(response)

Expand Down Expand Up @@ -317,8 +320,10 @@ def _prepare_query_data(

return query_data

def _get_cached_response(self) -> dict:
cache_data = cache.get(f"dashboard:Widget:load:{self.cache_hash_key}")
def _get_cached_response(self, cache_hash_key) -> dict:
cache_data = cache.get(
f"dashboard:widget:load:{self.domain_id}:{self.widget_id}:{cache_hash_key}"
)
return cache_data if cache_data else None

def _apply_group_by(self, group_by: list):
Expand Down
2 changes: 0 additions & 2 deletions src/spaceone/dashboard/model/private_data_table/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class PrivateDataTable(MongoModel):
labels_info = DictField(default=None)
data_info = DictField(default=None)
sort_keys = ListField(default=None)
cache_key = StringField(max_length=255, default=None, null=True)
dashboard_id = StringField(max_length=40)
widget_id = StringField(max_length=40)
resource_group = StringField(
Expand All @@ -39,7 +38,6 @@ class PrivateDataTable(MongoModel):
"labels_info",
"data_info",
"sort_keys",
"cache_key",
],
"minimal_fields": [
"data_table_id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class PrivateDataTableResponse(BaseModel):
labels_info: Union[dict, None] = None
data_info: Union[dict, None] = None
sort_keys: Union[List[str], None] = None
cache_key: Union[str, None] = None
error_message: Union[str, None] = None
dashboard_id: Union[str, None] = None
widget_id: Union[str, None] = None
Expand Down
2 changes: 0 additions & 2 deletions src/spaceone/dashboard/model/public_data_table/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class PublicDataTable(MongoModel):
labels_info = DictField(default=None)
data_info = DictField(default=None)
sort_keys = ListField(default=None)
cache_key = StringField(max_length=255, default=None, null=True)
dashboard_id = StringField(max_length=40)
widget_id = StringField(max_length=40)
resource_group = StringField(
Expand All @@ -40,7 +39,6 @@ class PublicDataTable(MongoModel):
"labels_info",
"data_info",
"sort_keys",
"cache_key",
"project_id",
"workspace_id",
],
Expand Down
1 change: 0 additions & 1 deletion src/spaceone/dashboard/model/public_data_table/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class PublicDataTableResponse(BaseModel):
labels_info: Union[dict, None] = None
data_info: Union[dict, None] = None
sort_keys: Union[List[str], None] = None
cache_key: Union[str, None] = None
error_message: Union[str, None] = None
dashboard_id: Union[str, None] = None
widget_id: Union[str, None] = None
Expand Down
18 changes: 8 additions & 10 deletions src/spaceone/dashboard/service/private_data_table_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,10 @@ def update(
if raw_filter:
params_dict["options"]["filter"] = raw_filter

if pri_data_table_vo.cache_key:
cache_key = f"dashboard:Widget:load:{pri_data_table_vo.cache_key}"
if cache.get(cache_key):
cache.delete(cache_key)

params_dict["cache_key"] = None
cache_key = (
f"dashboard:widget:load:{params.domain_id}:{pri_data_table_vo.widget_id}:*"
)
cache.delete_pattern(cache_key)

pri_data_table_vo = self.pri_data_table_mgr.update_private_data_table_by_vo(
params_dict, pri_data_table_vo
Expand Down Expand Up @@ -353,10 +351,10 @@ def delete(self, params: PrivateDataTableDeleteRequest) -> None:
)
)

if pri_data_table_vo.cache_key:
cache_key = f"dashboard:Widget:load:{pri_data_table_vo.cache_key}"
if cache.get(cache_key):
cache.delete(cache_key)
cache_key = (
f"dashboard:widget:load:{params.domain_id}:{pri_data_table_vo.widget_id}:*"
)
cache.delete_pattern(cache_key)

self.pri_data_table_mgr.delete_private_data_table_by_vo(pri_data_table_vo)

Expand Down
28 changes: 4 additions & 24 deletions src/spaceone/dashboard/service/private_widget_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def load(self, params: PrivateWidgetLoadRequest) -> dict:
pri_data_table_vo.widget_id,
pri_data_table_vo.domain_id,
)
response = ds_mgr.load_from_widget(
return ds_mgr.load_from_widget(
pri_data_table_vo.data_table_id,
params.granularity,
params.start,
Expand All @@ -394,7 +394,6 @@ def load(self, params: PrivateWidgetLoadRequest) -> dict:
params.vars,
)

cached_hash_key = ds_mgr.cache_hash_key
else:
operator = pri_data_table_vo.operator
options = pri_data_table_vo.options.get(operator, {})
Expand All @@ -406,7 +405,7 @@ def load(self, params: PrivateWidgetLoadRequest) -> dict:
pri_data_table_vo.widget_id,
pri_data_table_vo.domain_id,
)
response = dt_mgr.load_from_widget(
return dt_mgr.load_from_widget(
pri_data_table_vo.data_table_id,
params.granularity,
params.start,
Expand All @@ -417,14 +416,6 @@ def load(self, params: PrivateWidgetLoadRequest) -> dict:
params.vars,
)

cached_hash_key = dt_mgr.cache_hash_key
if cached_hash_key and pri_data_table_vo.cache_key != cached_hash_key:
pri_data_table_mgr.update_private_data_table_by_vo(
{"cache_key": cached_hash_key}, pri_data_table_vo
)

return response

@transaction(
permission="dashboard:PrivateWidget.read",
role_types=["USER"],
Expand Down Expand Up @@ -476,7 +467,7 @@ def load_sum(self, params: PrivateWidgetLoadSumRequest) -> dict:
pri_data_table_vo.widget_id,
pri_data_table_vo.domain_id,
)
response = ds_mgr.load_from_widget(
return ds_mgr.load_from_widget(
pri_data_table_vo.data_table_id,
params.granularity,
params.start,
Expand All @@ -485,8 +476,6 @@ def load_sum(self, params: PrivateWidgetLoadSumRequest) -> dict:
column_sum=True,
)

cached_hash_key = ds_mgr.cache_hash_key

else:
operator = pri_data_table_vo.operator
options = pri_data_table_vo.options.get(operator, {})
Expand All @@ -498,7 +487,7 @@ def load_sum(self, params: PrivateWidgetLoadSumRequest) -> dict:
pri_data_table_vo.widget_id,
pri_data_table_vo.domain_id,
)
response = dt_mgr.load_from_widget(
return dt_mgr.load_from_widget(
pri_data_table_vo.data_table_id,
params.granularity,
params.start,
Expand All @@ -507,15 +496,6 @@ def load_sum(self, params: PrivateWidgetLoadSumRequest) -> dict:
column_sum=True,
)

cached_hash_key = dt_mgr.cache_hash_key

if cached_hash_key and pri_data_table_vo.cache_key != cached_hash_key:
pri_data_table_mgr.update_private_data_table_by_vo(
{"cache_key": cached_hash_key}, pri_data_table_vo
)

return response

@transaction(
permission="dashboard:PrivateWidget.read",
role_types=["USER"],
Expand Down
18 changes: 8 additions & 10 deletions src/spaceone/dashboard/service/public_data_table_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,10 @@ def update(
if raw_filter:
params_dict["options"]["filter"] = raw_filter

if pub_data_table_vo.cache_key:
cache_key = f"dashboard:Widget:load:{pub_data_table_vo.cache_key}"
if cache.get(cache_key):
cache.delete(cache_key)

params_dict["cache_key"] = None
cache_key = (
f"dashboard:widget:load:{params.domain_id}:{pub_data_table_vo.widget_id}:*"
)
cache.delete_pattern(cache_key)

pub_data_table_vo = self.pub_data_table_mgr.update_public_data_table_by_vo(
params_dict, pub_data_table_vo
Expand Down Expand Up @@ -372,10 +370,10 @@ def delete(self, params: PublicDataTableDeleteRequest) -> None:
)
)

if pub_data_table_vo.cache_key:
cache_key = f"dashboard:Widget:load:{pub_data_table_vo.cache_key}"
if cache.get(cache_key):
cache.delete(cache_key)
cache_key = (
f"dashboard:Widget:load:{params.domain_id}:{pub_data_table_vo.widget_id}:*"
)
cache.delete_pattern(cache_key)

self.pub_data_table_mgr.delete_public_data_table_by_vo(pub_data_table_vo)

Expand Down
29 changes: 4 additions & 25 deletions src/spaceone/dashboard/service/public_widget_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def load(self, params: PublicWidgetLoadRequest) -> dict:
pub_data_table_vo.widget_id,
pub_data_table_vo.domain_id,
)
response = ds_mgr.load_from_widget(
return ds_mgr.load_from_widget(
pub_widget_vo.data_table_id,
params.granularity,
params.start,
Expand All @@ -408,8 +408,6 @@ def load(self, params: PublicWidgetLoadRequest) -> dict:
params.vars,
)

cached_hash_key = ds_mgr.cache_hash_key

else:
operator = pub_data_table_vo.operator
options = pub_data_table_vo.options.get(operator, {})
Expand All @@ -421,7 +419,7 @@ def load(self, params: PublicWidgetLoadRequest) -> dict:
pub_data_table_vo.widget_id,
pub_data_table_vo.domain_id,
)
response = dt_mgr.load_from_widget(
return dt_mgr.load_from_widget(
pub_widget_vo.data_table_id,
params.granularity,
params.start,
Expand All @@ -432,14 +430,6 @@ def load(self, params: PublicWidgetLoadRequest) -> dict:
params.vars,
)

cached_hash_key = dt_mgr.cache_hash_key
if cached_hash_key and pub_data_table_vo.cache_key != cached_hash_key:
pub_data_table_mgr.update_public_data_table_by_vo(
{"cache_key": cached_hash_key}, pub_data_table_vo
)

return response

@transaction(
permission="dashboard:PublicWidget.write",
role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"],
Expand Down Expand Up @@ -494,7 +484,7 @@ def load_sum(self, params: PublicWidgetLoadSumRequest) -> dict:
pub_data_table_vo.widget_id,
pub_data_table_vo.domain_id,
)
response = ds_mgr.load_from_widget(
return ds_mgr.load_from_widget(
pub_data_table_vo.data_table_id,
params.granularity,
params.start,
Expand All @@ -503,8 +493,6 @@ def load_sum(self, params: PublicWidgetLoadSumRequest) -> dict:
column_sum=True,
)

cached_hash_key = ds_mgr.cache_hash_key

else:
operator = pub_data_table_vo.operator
options = pub_data_table_vo.options.get(operator, {})
Expand All @@ -516,7 +504,7 @@ def load_sum(self, params: PublicWidgetLoadSumRequest) -> dict:
pub_data_table_vo.widget_id,
pub_data_table_vo.domain_id,
)
response = dt_mgr.load_from_widget(
return dt_mgr.load_from_widget(
pub_data_table_vo.data_table_id,
params.granularity,
params.start,
Expand All @@ -525,15 +513,6 @@ def load_sum(self, params: PublicWidgetLoadSumRequest) -> dict:
column_sum=True,
)

cached_hash_key = dt_mgr.cache_hash_key

if cached_hash_key and pub_data_table_vo.cache_key != cached_hash_key:
pub_data_table_mgr.update_public_data_table_by_vo(
{"cache_key": cached_hash_key}, pub_data_table_vo
)

return response

@transaction(
permission="dashboard:PublicWidget.read",
role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"],
Expand Down

0 comments on commit 0379372

Please sign in to comment.