Skip to content

Commit

Permalink
Merge branch 'main' into feat-app-dynamics
Browse files Browse the repository at this point in the history
  • Loading branch information
talboren authored Apr 16, 2024
2 parents 57689b8 + fd4fff4 commit 34f1974
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
13 changes: 12 additions & 1 deletion keep-ui/app/alerts/alerts-rules-builder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ const sanitizeCELIntoJS = (celExpression: string): string => {

// this pattern is far from robust
const variablePattern = /[a-zA-Z$_][0-9a-zA-Z$_]*/;
const jsReservedWords = new Set([
"break", "case", "catch", "class", "const", "continue", "debugger",
"default", "delete", "do", "else", "export", "extends", "finally",
"for", "function", "if", "import", "in", "instanceof", "new", "return",
"super", "switch", "this", "throw", "try", "typeof", "var", "void",
"while", "with", "yield"
]);

export const evalWithContext = (context: AlertDto, celExpression: string) => {
try {
Expand All @@ -187,10 +194,12 @@ export const evalWithContext = (context: AlertDto, celExpression: string) => {
}

const jsExpression = sanitizeCELIntoJS(celExpression);
const variables = (
let variables = (
getAllMatches(variablePattern, jsExpression) ?? []
).filter((variable) => variable !== "true" && variable !== "false");

// filter reserved words from variables
variables = variables.filter((variable) => !jsReservedWords.has(variable));
const func = new Function(...variables, `return (${jsExpression})`);

const args = variables.map((arg) =>
Expand Down Expand Up @@ -363,6 +372,8 @@ export const AlertsRulesBuilder = ({
celQuery.replace(/\s+/g, "") === celRules.replace(/\s+/g, "") ||
celRules === "";
setIsValidCEL(isValidCEL);
// close the menu
setShowSuggestions(false);
if (isValidCEL) {
onApplyFilter();
updateOutputCEL?.(celRules);
Expand Down
1 change: 1 addition & 0 deletions keep/api/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ def get_last_workflow_execution_by_workflow_id(
session.query(WorkflowExecution)
.filter(WorkflowExecution.workflow_id == workflow_id)
.filter(WorkflowExecution.tenant_id == tenant_id)
.filter(WorkflowExecution.started >= datetime.now() - timedelta(days=7))
.filter(WorkflowExecution.status == "success")
.order_by(WorkflowExecution.started.desc())
.first()
Expand Down
26 changes: 13 additions & 13 deletions keep/contextmanager/contextmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import click
from pympler.asizeof import asizeof

from keep.api.core.db import get_session
from keep.api.core.db import get_last_workflow_execution_by_workflow_id, get_session
from keep.api.logging import WorkflowLoggerAdapter


Expand All @@ -31,18 +31,18 @@ def __init__(self, tenant_id, workflow_id=None, workflow_execution_id=None):
self.click_context = {}
# last workflow context
self.last_workflow_execution_results = {}
# if self.workflow_id:
# try:
# last_workflow_execution = get_last_workflow_execution_by_workflow_id(
# workflow_id, tenant_id
# )
# if last_workflow_execution is not None:
# self.last_workflow_execution_results = (
# last_workflow_execution.results
# )
# except Exception:
# self.logger.exception("Failed to get last workflow execution")
# pass
if self.workflow_id:
try:
last_workflow_execution = get_last_workflow_execution_by_workflow_id(
workflow_id, tenant_id
)
if last_workflow_execution is not None:
self.last_workflow_execution_results = (
last_workflow_execution.results
)
except Exception:
self.logger.exception("Failed to get last workflow execution")
pass
self.aliases = {}
# dependencies are used so iohandler will be able to use the output class of the providers
# e.g. let's say bigquery_provider results are google.cloud.bigquery.Row
Expand Down

0 comments on commit 34f1974

Please sign in to comment.