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

feat: keep.join function for workflows #2547

Merged
merged 4 commits into from
Nov 20, 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
4 changes: 2 additions & 2 deletions keep/api/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ def get_last_workflow_executions(tenant_id: str, limit=20):
)

return execution_with_logs


def get_workflow_executions_count(tenant_id: str):
with Session(engine) as session:
Expand Down Expand Up @@ -1745,7 +1745,7 @@ def get_incident_for_grouping_rule(
# Create and add a new incident if it doesn't exist
incident = Incident(
tenant_id=tenant_id,
user_generated_name=f"Incident generated by rule {rule.name}",
user_generated_name=f"{rule.name}",
rule_id=rule.id,
rule_fingerprint=rule_fingerprint,
is_predicted=False,
Expand Down
18 changes: 17 additions & 1 deletion keep/functions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import copy
import datetime
import json
import re
import urllib.parse
from datetime import timedelta
from itertools import groupby

import json5 as json
import pytz
from dateutil import parser
from dateutil.parser import ParserError
Expand Down Expand Up @@ -175,6 +175,22 @@ def slice(str_to_slice: str, start: int = 0, end: int = 0) -> str:
return str_to_slice[int(start) : int(end)]


def join(
iterable: list | dict | str, delimiter: str = ",", prefix: str | None = None
) -> str:
if isinstance(iterable, str):
iterable = json.loads(iterable)

if isinstance(iterable, dict):
if prefix:
return delimiter.join([f"{prefix}{k}={v}" for k, v in iterable.items()])
return delimiter.join([f"{k}={v}" for k, v in iterable.items()])

if prefix:
return delimiter.join([f"{prefix}{item}" for item in iterable])
return delimiter.join([str(item) for item in iterable])


def dict_pop(data: str | dict, *args) -> dict:
if isinstance(data, str):
data = json.loads(data)
Expand Down
11 changes: 2 additions & 9 deletions keep/iohandler/iohandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,17 +520,10 @@ def __get_short_urls(self, urls: list) -> dict:
if __name__ == "__main__":
# debug & test
context_manager = ContextManager("keep")
context_manager.event_context = {
"ticket_id": "1234",
"severity": "high",
"ticket_created_at": "2021-09-01T00:00:00Z",
}
context_manager.event_context = {"tags": {"k1": "v1", "k2": "v2"}}
iohandler = IOHandler(context_manager)
res = iohandler.render(
iohandler.quote(
"not '{{ alert.ticket_id }}' or (('{{ alert.ticket_status }}' in ['Resolved', 'Closed', 'Canceled']) and ('{{ alert.severity }}' == 'critical' or keep.datetime_compare(keep.utcnow(), keep.to_utc('{{ alert.ticket_created_at }}')) > 168))"
),
safe=False,
'https://www.keephq.dev?keep.join("{{alert.tags}}", "&", "prefix_")'
)
from asteval import Interpreter

Expand Down
3 changes: 3 additions & 0 deletions keep/providers/cloudwatch_provider/cloudwatch_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def validate_scopes(self):

# 4. validate start query
logs_client = self.__generate_client("logs")
query = False
try:
query = logs_client.start_query(
logGroupName="keepTest",
Expand All @@ -277,6 +278,8 @@ def validate_scopes(self):
else:
self.logger.info("Error validating AWS logs:StartQuery scope")
scopes["logs:StartQuery"] = str(e)

query_id = False
if query:
try:
query_id = logs_client.describe_queries().get("queries")[0]["queryId"]
Expand Down
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "keep"
version = "0.29.1"
version = "0.29.2"
description = "Alerting. for developers, by developers."
authors = ["Keep Alerting LTD"]
readme = "README.md"
Expand Down Expand Up @@ -95,6 +95,7 @@ networkx = "^3.3"
google-auth = "2.34.0"
clickhouse-driver = "^0.2.9"
google-cloud-logging = "^3.11.3"
json5 = "^0.9.28"
[tool.poetry.group.dev.dependencies]
pre-commit = "^3.0.4"
pre-commit-hooks = "^4.4.0"
Expand Down
Loading