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: add settings for duplicate event time #134

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/spaceone/monitoring/conf/global_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
JOB_TIMEOUT = 600

# Event Settings
SAME_EVENT_TIME = 600
DUPLICATE_EVENT_TIME = 600

INSTALLED_DATA_SOURCE_PLUGINS = [
# {
Expand Down
19 changes: 15 additions & 4 deletions src/spaceone/monitoring/manager/event_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,20 @@ def list_events(self, query: dict) -> dict:
def stat_events(self, query: dict) -> dict:
return self.event_model.stat(**query)

def get_event_by_key(self, event_key, domain_id, project_id, workspace_id):
same_event_time = config.get_global("SAME_EVENT_TIME", 600)
same_event_datetime = datetime.utcnow() - timedelta(seconds=same_event_time)
def get_event_by_key(
self,
event_key,
domain_id,
project_id,
workspace_id,
duplicate_event_time,
):
if not isinstance(duplicate_event_time, int):
duplicate_event_time = config.get_global("DUPLICATE_EVENT_TIME", 600)

duplicate_event_datetime = datetime.utcnow() - timedelta(
seconds=duplicate_event_time
)

query = {
"filter": [
Expand All @@ -66,7 +77,7 @@ def get_event_by_key(self, event_key, domain_id, project_id, workspace_id):
{"k": "workspace_id", "v": workspace_id, "o": "eq"},
{"k": "project_id", "v": project_id, "o": "eq"},
{"k": "event_type", "v": "RECOVERY", "o": "not"},
{"k": "created_at", "v": same_event_datetime, "o": "gte"},
{"k": "created_at", "v": duplicate_event_datetime, "o": "gte"},
],
"sort": [{"key": "created_at", "desc": True}],
}
Expand Down
6 changes: 6 additions & 0 deletions src/spaceone/monitoring/service/event_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def create(self, params: dict) -> None:
webhook_id = webhook_data["webhook_id"]
domain_id = webhook_data["domain_id"]
workspace_id = webhook_data["workspace_id"]
plugin_metadata = webhook_data["plugin_metadata"]

webhook_vo = self.webhook_mgr.get_webhook(webhook_id, domain_id, workspace_id)
webhook_vo.increment("requests.total")
Expand Down Expand Up @@ -101,6 +102,7 @@ def create(self, params: dict) -> None:
)
plugin_info["version"] = updated_version
plugin_info["metadata"] = plugin_metadata
webhook_data["metadata"] = plugin_metadata
self.webhook_mgr.update_webhook_by_vo(
{"plugin_info": plugin_info}, webhook_vo
)
Expand Down Expand Up @@ -238,6 +240,7 @@ def _get_webhook_data(self, webhook_id):
"plugin_version": webhook_vo.plugin_info.version,
"plugin_upgrade_mode": webhook_vo.plugin_info.upgrade_mode,
"plugin_options": webhook_vo.plugin_info.options,
"plugin_metadata": webhook_vo.plugin_info.metadata,
}

@staticmethod
Expand All @@ -251,6 +254,8 @@ def _check_webhook_state(webhook_data):
raise ERROR_WEBHOOK_STATE_DISABLED(webhook_id=webhook_data["webhook_id"])

def _create_event(self, event_data, raw_data, webhook_data):
plugin_metadata = webhook_data.get("plugin_metadata", {})

event_data["raw_data"] = copy.deepcopy(raw_data)
event_data["occurred_at"] = utils.iso8601_to_datetime(
event_data.get("occurred_at")
Expand Down Expand Up @@ -287,6 +292,7 @@ def _create_event(self, event_data, raw_data, webhook_data):
event_data["domain_id"],
event_data["project_id"],
event_data["workspace_id"],
plugin_metadata.get("duplicate_event_time"),
)

if event_vo and event_vo.alert.state != "RESOLVED":
Expand Down
Loading