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 1 commit
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
11 changes: 11 additions & 0 deletions keep/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ 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 = ",") -> str:
if isinstance(iterable, str):
try:
iterable = json.loads(iterable)
except json.JSONDecodeError:
iterable = json.loads(iterable.replace("'", '"'))
talboren marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(iterable, dict):
return delimiter.join([f"{k}={v}" for k, v in iterable.items()])
return delimiter.join([str(item) for item in iterable])

talboren marked this conversation as resolved.
Show resolved Hide resolved

def dict_pop(data: str | dict, *args) -> dict:
if isinstance(data, str):
data = json.loads(data)
Expand Down
13 changes: 2 additions & 11 deletions keep/iohandler/iohandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,18 +520,9 @@ 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,
)
res = iohandler.render('https://www.keephq.dev?keep.join("{{alert.tags}}", "&")')
from asteval import Interpreter

aeval = 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
Loading