2024-08-28 11:25:58 +02:00
|
|
|
import dataclasses
|
2024-07-15 12:58:32 +02:00
|
|
|
import datetime
|
2024-06-07 11:37:53 +02:00
|
|
|
import time
|
|
|
|
from typing import Any, Optional, TYPE_CHECKING
|
|
|
|
from collections.abc import Callable
|
|
|
|
import re
|
|
|
|
import json
|
|
|
|
|
2024-07-15 12:58:32 +02:00
|
|
|
import pytz
|
|
|
|
|
2024-06-07 11:37:53 +02:00
|
|
|
from .print import print_hog_string_output
|
2024-07-15 14:24:23 +02:00
|
|
|
from .date import (
|
|
|
|
now,
|
|
|
|
toUnixTimestamp,
|
|
|
|
fromUnixTimestamp,
|
2024-07-15 17:34:35 +02:00
|
|
|
toUnixTimestampMilli,
|
|
|
|
fromUnixTimestampMilli,
|
2024-07-15 14:24:23 +02:00
|
|
|
toTimeZone,
|
|
|
|
toDate,
|
|
|
|
toDateTime,
|
|
|
|
formatDateTime,
|
|
|
|
is_hog_datetime,
|
|
|
|
is_hog_date,
|
|
|
|
)
|
2024-07-16 15:55:05 +02:00
|
|
|
from .crypto import sha256Hex, md5Hex, sha256HmacChainHex
|
2024-08-29 14:28:50 +02:00
|
|
|
from ..objects import is_hog_error, new_hog_error, is_hog_callable, is_hog_closure
|
2024-09-20 11:36:56 +02:00
|
|
|
from ..utils import like, get_nested_value
|
2024-06-07 11:37:53 +02:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from posthog.models import Team
|
|
|
|
|
|
|
|
|
2024-08-28 11:25:58 +02:00
|
|
|
@dataclasses.dataclass
|
|
|
|
class STLFunction:
|
|
|
|
fn: Callable[[list[Any], Optional["Team"], list[str] | None, float], Any]
|
|
|
|
minArgs: Optional[int] = None
|
|
|
|
maxArgs: Optional[int] = None
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def toString(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float):
|
2024-07-15 12:58:32 +02:00
|
|
|
if isinstance(args[0], dict) and is_hog_datetime(args[0]):
|
|
|
|
dt = datetime.datetime.fromtimestamp(args[0]["dt"], pytz.timezone(args[0]["zone"] or "UTC"))
|
|
|
|
if args[0]["zone"] == "UTC":
|
|
|
|
return dt.isoformat("T", "milliseconds").replace("+00:00", "") + "Z"
|
|
|
|
return dt.isoformat("T", "milliseconds")
|
|
|
|
elif isinstance(args[0], dict) and is_hog_date(args[0]):
|
|
|
|
year = args[0]["year"]
|
|
|
|
month = args[0]["month"]
|
|
|
|
day = args[0]["day"]
|
|
|
|
return f"{year}-{month:02d}-{day:02d}"
|
2024-07-25 16:57:28 +02:00
|
|
|
elif isinstance(args[0], dict) and is_hog_error(args[0]):
|
|
|
|
return (
|
|
|
|
f"{args[0]['name']}({toString(args[0]['message'], team, stdout, timeout)}"
|
|
|
|
+ (f", {toString(args[0]['payload'], team, stdout, timeout)}" if "payload" in args[0] else "")
|
|
|
|
+ ")"
|
|
|
|
)
|
2024-07-15 12:58:32 +02:00
|
|
|
elif args[0] is True:
|
2024-06-07 11:37:53 +02:00
|
|
|
return "true"
|
|
|
|
elif args[0] is False:
|
|
|
|
return "false"
|
|
|
|
elif args[0] is None:
|
|
|
|
return "null"
|
|
|
|
else:
|
|
|
|
return str(args[0])
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def toInt(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float):
|
2024-06-07 11:37:53 +02:00
|
|
|
try:
|
2024-07-15 17:34:35 +02:00
|
|
|
if is_hog_datetime(args[0]):
|
|
|
|
return int(args[0]["dt"])
|
|
|
|
elif is_hog_date(args[0]):
|
|
|
|
return (
|
|
|
|
datetime.datetime(args[0]["year"], args[0]["month"], args[0]["day"]) - datetime.datetime(1970, 1, 1)
|
|
|
|
).days
|
|
|
|
return int(args[0])
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def toFloat(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float):
|
2024-07-15 17:34:35 +02:00
|
|
|
try:
|
|
|
|
if is_hog_datetime(args[0]):
|
|
|
|
return float(args[0]["dt"])
|
|
|
|
elif is_hog_date(args[0]):
|
|
|
|
return float(
|
|
|
|
(
|
|
|
|
datetime.datetime(args[0]["year"], args[0]["month"], args[0]["day"]) - datetime.datetime(1970, 1, 1)
|
|
|
|
).days
|
|
|
|
)
|
|
|
|
return float(args[0])
|
2024-06-07 11:37:53 +02:00
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2024-07-08 16:54:17 +02:00
|
|
|
# ifNull is complied into JUMP instructions. Keeping the function here for backwards compatibility
|
2024-07-25 16:57:28 +02:00
|
|
|
def ifNull(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float):
|
2024-06-07 11:37:53 +02:00
|
|
|
if args[0] is not None:
|
|
|
|
return args[0]
|
|
|
|
else:
|
|
|
|
return args[1]
|
|
|
|
|
|
|
|
|
2024-09-06 11:12:41 +02:00
|
|
|
def empty(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float):
|
|
|
|
if isinstance(args[0], bool) or isinstance(args[0], int) or isinstance(args[0], float):
|
|
|
|
return False
|
|
|
|
return not bool(args[0])
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def sleep(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float):
|
2024-06-07 11:37:53 +02:00
|
|
|
time.sleep(args[0])
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def print(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float):
|
2024-06-07 11:37:53 +02:00
|
|
|
if stdout is not None:
|
|
|
|
value = " ".join(map(print_hog_string_output, args))
|
|
|
|
stdout.append(value)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def run(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> list[Any]:
|
2024-06-07 11:37:53 +02:00
|
|
|
if team is None:
|
|
|
|
return []
|
|
|
|
from posthog.hogql.query import execute_hogql_query
|
|
|
|
|
|
|
|
response = execute_hogql_query(query=args[0], team=team)
|
|
|
|
return response.results
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def jsonParse(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> Any:
|
2024-06-07 11:37:53 +02:00
|
|
|
return json.loads(args[0])
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def jsonStringify(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> str:
|
2024-07-10 15:11:15 +02:00
|
|
|
marked = set()
|
|
|
|
|
|
|
|
def json_safe(obj):
|
|
|
|
if isinstance(obj, dict) or isinstance(obj, list) or isinstance(obj, tuple):
|
2024-08-29 14:28:50 +02:00
|
|
|
if id(obj) in marked and not is_hog_callable(obj) and not is_hog_closure(obj):
|
2024-07-10 15:11:15 +02:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
marked.add(id(obj))
|
|
|
|
try:
|
|
|
|
if isinstance(obj, dict):
|
2024-08-29 14:28:50 +02:00
|
|
|
if is_hog_callable(obj):
|
|
|
|
return f"fn<{obj['name']}({obj['argCount']})>"
|
|
|
|
if is_hog_closure(obj):
|
|
|
|
return f"fn<{obj['callable']['name']}({obj['callable']['argCount']})>"
|
2024-07-10 15:11:15 +02:00
|
|
|
return {json_safe(k): json_safe(v) for k, v in obj.items()}
|
|
|
|
elif isinstance(obj, list):
|
|
|
|
return [json_safe(v) for v in obj]
|
|
|
|
elif isinstance(obj, tuple):
|
|
|
|
return tuple(json_safe(v) for v in obj)
|
|
|
|
finally:
|
|
|
|
marked.remove(id(obj))
|
|
|
|
return obj
|
|
|
|
|
2024-06-07 11:37:53 +02:00
|
|
|
if len(args) > 1 and isinstance(args[1], int) and args[1] > 0:
|
2024-07-10 15:11:15 +02:00
|
|
|
return json.dumps(json_safe(args[0]), indent=args[1])
|
|
|
|
return json.dumps(json_safe(args[0]))
|
2024-06-07 11:37:53 +02:00
|
|
|
|
|
|
|
|
2024-09-20 11:36:56 +02:00
|
|
|
def JSONHas(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> bool:
|
|
|
|
obj = args[0]
|
|
|
|
path = args[1:]
|
|
|
|
current = obj
|
|
|
|
for key in path:
|
|
|
|
currentParsed = current
|
|
|
|
if isinstance(current, str):
|
|
|
|
try:
|
|
|
|
currentParsed = json.loads(current)
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
return False
|
|
|
|
if isinstance(currentParsed, dict):
|
|
|
|
if key not in currentParsed:
|
|
|
|
return False
|
|
|
|
current = currentParsed[key]
|
|
|
|
elif isinstance(currentParsed, list):
|
|
|
|
if isinstance(key, int):
|
|
|
|
if key < 0:
|
|
|
|
if key < -len(currentParsed):
|
|
|
|
return False
|
|
|
|
current = currentParsed[len(currentParsed) + key]
|
|
|
|
elif key == 0:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if key > len(currentParsed):
|
|
|
|
return False
|
|
|
|
current = currentParsed[key - 1]
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def isValidJSON(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> bool:
|
|
|
|
try:
|
|
|
|
json.loads(args[0])
|
|
|
|
return True
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def JSONLength(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> int:
|
|
|
|
obj = args[0]
|
|
|
|
path = args[1:]
|
|
|
|
try:
|
|
|
|
if isinstance(obj, str):
|
|
|
|
obj = json.loads(obj)
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
return 0
|
|
|
|
if not isinstance(obj, dict) and not isinstance(obj, list):
|
|
|
|
return 0
|
|
|
|
current = get_nested_value(obj, path, nullish=True)
|
|
|
|
if isinstance(current, dict) or isinstance(current, list):
|
|
|
|
return len(current)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2024-09-25 10:43:53 +02:00
|
|
|
def JSONExtractBool(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> bool:
|
|
|
|
obj = args[0]
|
|
|
|
path = args[1:]
|
|
|
|
try:
|
|
|
|
if isinstance(obj, str):
|
|
|
|
obj = json.loads(obj)
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
return False
|
|
|
|
if len(path) > 0:
|
|
|
|
obj = get_nested_value(obj, path, nullish=True)
|
|
|
|
if isinstance(obj, bool):
|
|
|
|
return obj
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def base64Encode(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> str:
|
2024-06-14 11:03:57 +02:00
|
|
|
import base64
|
|
|
|
|
|
|
|
return base64.b64encode(args[0].encode()).decode()
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def base64Decode(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> str:
|
2024-06-14 11:03:57 +02:00
|
|
|
import base64
|
|
|
|
|
|
|
|
return base64.b64decode(args[0].encode()).decode()
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def encodeURLComponent(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> str:
|
2024-06-14 11:03:57 +02:00
|
|
|
import urllib.parse
|
|
|
|
|
|
|
|
return urllib.parse.quote(args[0], safe="")
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def decodeURLComponent(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> str:
|
2024-06-14 11:03:57 +02:00
|
|
|
import urllib.parse
|
|
|
|
|
|
|
|
return urllib.parse.unquote(args[0])
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def trim(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> str:
|
2024-08-28 11:25:58 +02:00
|
|
|
char = str(args[1]) if len(args) > 1 and isinstance(args[1], str) else None
|
|
|
|
if len(args) > 1:
|
|
|
|
if char is None:
|
|
|
|
char = " "
|
|
|
|
if len(char) > 1:
|
|
|
|
return ""
|
|
|
|
return args[0].strip(char)
|
2024-07-16 15:55:05 +02:00
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def trimLeft(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> str:
|
2024-08-28 11:25:58 +02:00
|
|
|
char = str(args[1]) if len(args) > 1 and isinstance(args[1], str) else None
|
|
|
|
if len(args) > 1:
|
|
|
|
if char is None:
|
|
|
|
char = " "
|
|
|
|
if len(char) > 1:
|
|
|
|
return ""
|
|
|
|
return args[0].lstrip(char)
|
2024-07-16 15:55:05 +02:00
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def trimRight(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> str:
|
2024-08-28 11:25:58 +02:00
|
|
|
char = str(args[1]) if len(args) > 1 and isinstance(args[1], str) else None
|
|
|
|
if len(args) > 1:
|
|
|
|
if char is None:
|
|
|
|
char = " "
|
|
|
|
if len(char) > 1:
|
|
|
|
return ""
|
|
|
|
return args[0].rstrip(char)
|
2024-07-16 15:55:05 +02:00
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def splitByString(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> list:
|
2024-07-16 15:55:05 +02:00
|
|
|
separator = args[0]
|
|
|
|
string = args[1]
|
2024-08-28 11:25:58 +02:00
|
|
|
if len(args) > 2 and args[2] is not None:
|
2024-07-16 15:55:05 +02:00
|
|
|
parts = string.split(separator, args[2])
|
|
|
|
if len(parts) > args[2]:
|
|
|
|
return parts[: args[2]]
|
|
|
|
return parts
|
|
|
|
return string.split(separator)
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def generateUUIDv4(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> str:
|
2024-06-17 11:07:32 +02:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
return str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def keys(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> list:
|
2024-07-11 18:34:21 +02:00
|
|
|
obj = args[0]
|
|
|
|
if isinstance(obj, dict):
|
|
|
|
return list(obj.keys())
|
|
|
|
if isinstance(obj, list) or isinstance(obj, tuple):
|
|
|
|
return list(range(len(obj)))
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def values(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> list:
|
2024-07-11 18:34:21 +02:00
|
|
|
obj = args[0]
|
|
|
|
if isinstance(obj, dict):
|
|
|
|
return list(obj.values())
|
|
|
|
if isinstance(obj, list) or isinstance(obj, tuple):
|
|
|
|
return list(obj)
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def arrayPushBack(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> list:
|
2024-07-16 15:55:05 +02:00
|
|
|
arr = args[0]
|
|
|
|
item = args[1]
|
|
|
|
if not isinstance(arr, list):
|
|
|
|
return [item]
|
|
|
|
return [*arr, item]
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def arrayPushFront(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> list:
|
2024-07-16 15:55:05 +02:00
|
|
|
arr = args[0]
|
|
|
|
item = args[1]
|
|
|
|
if not isinstance(arr, list):
|
|
|
|
return [item]
|
|
|
|
return [item, *arr]
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def arrayPopBack(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> list:
|
2024-07-16 15:55:05 +02:00
|
|
|
arr = args[0]
|
|
|
|
if not isinstance(arr, list):
|
|
|
|
return []
|
|
|
|
return arr[:-1]
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def arrayPopFront(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> list:
|
2024-07-16 15:55:05 +02:00
|
|
|
arr = args[0]
|
|
|
|
if not isinstance(arr, list):
|
|
|
|
return []
|
|
|
|
return arr[1:]
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def arraySort(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> list:
|
2024-07-16 15:55:05 +02:00
|
|
|
arr = args[0]
|
|
|
|
if not isinstance(arr, list):
|
|
|
|
return []
|
|
|
|
return sorted(arr)
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def arrayReverse(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> list:
|
2024-07-16 15:55:05 +02:00
|
|
|
arr = args[0]
|
|
|
|
if not isinstance(arr, list):
|
|
|
|
return []
|
|
|
|
return arr[::-1]
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def arrayReverseSort(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> list:
|
2024-07-16 15:55:05 +02:00
|
|
|
arr = args[0]
|
|
|
|
if not isinstance(arr, list):
|
|
|
|
return []
|
|
|
|
return sorted(arr, reverse=True)
|
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def arrayStringConcat(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> str:
|
2024-07-16 15:55:05 +02:00
|
|
|
arr = args[0]
|
|
|
|
sep = args[1] if len(args) > 1 else ""
|
|
|
|
if not isinstance(arr, list):
|
|
|
|
return ""
|
|
|
|
return sep.join([str(s) for s in arr])
|
|
|
|
|
|
|
|
|
2024-08-13 11:57:30 +02:00
|
|
|
def has(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> bool:
|
|
|
|
if len(args) < 2 or not isinstance(args[0], list):
|
|
|
|
return False
|
|
|
|
return args[1] in args[0]
|
2024-07-15 12:58:32 +02:00
|
|
|
|
|
|
|
|
2024-07-25 16:57:28 +02:00
|
|
|
def _formatDateTime(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> Any:
|
2024-07-15 14:24:23 +02:00
|
|
|
if len(args) < 2:
|
|
|
|
raise ValueError("formatDateTime requires at least 2 arguments")
|
|
|
|
return formatDateTime(args[0], args[1], args[2] if len(args) > 2 else None)
|
|
|
|
|
|
|
|
|
2024-09-02 14:42:03 +02:00
|
|
|
def _typeof(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> str:
|
|
|
|
if args[0] is None:
|
|
|
|
return "null"
|
|
|
|
elif is_hog_datetime(args[0]):
|
|
|
|
return "datetime"
|
|
|
|
elif is_hog_date(args[0]):
|
|
|
|
return "date"
|
|
|
|
elif is_hog_error(args[0]):
|
|
|
|
return "error"
|
|
|
|
elif is_hog_callable(args[0]) or is_hog_closure(args[0]):
|
|
|
|
return "function"
|
|
|
|
elif isinstance(args[0], list):
|
|
|
|
return "array"
|
|
|
|
elif isinstance(args[0], tuple):
|
|
|
|
return "tuple"
|
|
|
|
elif isinstance(args[0], dict):
|
|
|
|
return "object"
|
|
|
|
elif args[0] is True or args[0] is False:
|
|
|
|
return "boolean"
|
|
|
|
elif isinstance(args[0], int):
|
|
|
|
return "integer"
|
|
|
|
elif isinstance(args[0], float):
|
|
|
|
return "float"
|
|
|
|
elif isinstance(args[0], str):
|
|
|
|
return "string"
|
|
|
|
return "unknown"
|
|
|
|
|
|
|
|
|
2024-08-28 11:25:58 +02:00
|
|
|
STL: dict[str, STLFunction] = {
|
|
|
|
"concat": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: "".join(
|
|
|
|
[print_hog_string_output(arg) if arg is not None else "" for arg in args]
|
|
|
|
),
|
|
|
|
minArgs=1,
|
|
|
|
maxArgs=None,
|
2024-08-13 11:57:30 +02:00
|
|
|
),
|
2024-08-28 11:25:58 +02:00
|
|
|
"match": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: bool(re.search(re.compile(args[1]), args[0])), minArgs=2, maxArgs=2
|
2024-08-13 11:57:30 +02:00
|
|
|
),
|
2024-08-28 11:25:58 +02:00
|
|
|
"like": STLFunction(fn=lambda args, team, stdout, timeout: like(args[0], args[1]), minArgs=2, maxArgs=2),
|
|
|
|
"ilike": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: like(args[0], args[1], re.IGNORECASE), minArgs=2, maxArgs=2
|
2024-08-13 11:57:30 +02:00
|
|
|
),
|
2024-08-28 11:25:58 +02:00
|
|
|
"notLike": STLFunction(fn=lambda args, team, stdout, timeout: not like(args[0], args[1]), minArgs=2, maxArgs=2),
|
|
|
|
"notILike": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: not like(args[0], args[1], re.IGNORECASE), minArgs=2, maxArgs=2
|
|
|
|
),
|
|
|
|
"toString": STLFunction(fn=toString, minArgs=1, maxArgs=1),
|
|
|
|
"toUUID": STLFunction(fn=toString, minArgs=1, maxArgs=1),
|
|
|
|
"toInt": STLFunction(fn=toInt, minArgs=1, maxArgs=1),
|
|
|
|
"toFloat": STLFunction(fn=toFloat, minArgs=1, maxArgs=1),
|
|
|
|
"ifNull": STLFunction(fn=ifNull, minArgs=2, maxArgs=2),
|
|
|
|
"length": STLFunction(fn=lambda args, team, stdout, timeout: len(args[0]), minArgs=1, maxArgs=1),
|
2024-09-06 11:12:41 +02:00
|
|
|
"empty": STLFunction(fn=empty, minArgs=1, maxArgs=1),
|
|
|
|
"notEmpty": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: not empty(args, team, stdout, timeout), minArgs=1, maxArgs=1
|
|
|
|
),
|
2024-08-28 11:25:58 +02:00
|
|
|
"tuple": STLFunction(fn=lambda args, team, stdout, timeout: tuple(args), minArgs=0, maxArgs=None),
|
|
|
|
"lower": STLFunction(fn=lambda args, team, stdout, timeout: args[0].lower(), minArgs=1, maxArgs=1),
|
|
|
|
"upper": STLFunction(fn=lambda args, team, stdout, timeout: args[0].upper(), minArgs=1, maxArgs=1),
|
|
|
|
"reverse": STLFunction(fn=lambda args, team, stdout, timeout: args[0][::-1], minArgs=1, maxArgs=1),
|
|
|
|
"print": STLFunction(fn=print, minArgs=0, maxArgs=None),
|
|
|
|
"jsonParse": STLFunction(fn=jsonParse, minArgs=1, maxArgs=1),
|
|
|
|
"jsonStringify": STLFunction(fn=jsonStringify, minArgs=1, maxArgs=1),
|
2024-09-20 11:36:56 +02:00
|
|
|
"JSONHas": STLFunction(fn=JSONHas, minArgs=2, maxArgs=None),
|
|
|
|
"isValidJSON": STLFunction(fn=isValidJSON, minArgs=1, maxArgs=1),
|
|
|
|
"JSONLength": STLFunction(fn=JSONLength, minArgs=2, maxArgs=None),
|
2024-09-25 10:43:53 +02:00
|
|
|
"JSONExtractBool": STLFunction(fn=JSONExtractBool, minArgs=1, maxArgs=None),
|
2024-08-28 11:25:58 +02:00
|
|
|
"base64Encode": STLFunction(fn=base64Encode, minArgs=1, maxArgs=1),
|
|
|
|
"base64Decode": STLFunction(fn=base64Decode, minArgs=1, maxArgs=1),
|
|
|
|
"encodeURLComponent": STLFunction(fn=encodeURLComponent, minArgs=1, maxArgs=1),
|
|
|
|
"decodeURLComponent": STLFunction(fn=decodeURLComponent, minArgs=1, maxArgs=1),
|
|
|
|
"replaceOne": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: args[0].replace(args[1], args[2], 1), minArgs=3, maxArgs=3
|
|
|
|
),
|
|
|
|
"replaceAll": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: args[0].replace(args[1], args[2]), minArgs=3, maxArgs=3
|
|
|
|
),
|
2024-09-05 12:07:12 +02:00
|
|
|
"position": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: (args[0].index(str(args[1])) + 1)
|
|
|
|
if isinstance(args[0], str) and str(args[1]) in args[0]
|
|
|
|
else 0,
|
|
|
|
minArgs=2,
|
|
|
|
maxArgs=2,
|
|
|
|
),
|
|
|
|
"positionCaseInsensitive": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: (args[0].lower().index(str(args[1]).lower()) + 1)
|
|
|
|
if isinstance(args[0], str) and str(args[1]).lower() in args[0].lower()
|
|
|
|
else 0,
|
|
|
|
minArgs=2,
|
|
|
|
maxArgs=2,
|
|
|
|
),
|
2024-08-28 11:25:58 +02:00
|
|
|
"trim": STLFunction(fn=trim, minArgs=1, maxArgs=2),
|
|
|
|
"trimLeft": STLFunction(fn=trimLeft, minArgs=1, maxArgs=2),
|
|
|
|
"trimRight": STLFunction(fn=trimRight, minArgs=1, maxArgs=2),
|
|
|
|
"splitByString": STLFunction(fn=splitByString, minArgs=2, maxArgs=3),
|
|
|
|
"generateUUIDv4": STLFunction(fn=generateUUIDv4, minArgs=0, maxArgs=0),
|
|
|
|
"sha256Hex": STLFunction(fn=lambda args, team, stdout, timeout: sha256Hex(args[0]), minArgs=1, maxArgs=1),
|
|
|
|
"md5Hex": STLFunction(fn=lambda args, team, stdout, timeout: md5Hex(args[0]), minArgs=1, maxArgs=1),
|
|
|
|
"sha256HmacChainHex": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: sha256HmacChainHex(args[0]), minArgs=1, maxArgs=1
|
|
|
|
),
|
|
|
|
"keys": STLFunction(fn=keys, minArgs=1, maxArgs=1),
|
|
|
|
"values": STLFunction(fn=values, minArgs=1, maxArgs=1),
|
2024-09-05 12:07:12 +02:00
|
|
|
"indexOf": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: (args[0].index(args[1]) + 1)
|
|
|
|
if isinstance(args[0], list) and args[1] in args[0]
|
|
|
|
else 0,
|
|
|
|
minArgs=2,
|
|
|
|
maxArgs=2,
|
|
|
|
),
|
2024-08-28 11:25:58 +02:00
|
|
|
"arrayPushBack": STLFunction(fn=arrayPushBack, minArgs=2, maxArgs=2),
|
|
|
|
"arrayPushFront": STLFunction(fn=arrayPushFront, minArgs=2, maxArgs=2),
|
|
|
|
"arrayPopBack": STLFunction(fn=arrayPopBack, minArgs=1, maxArgs=1),
|
|
|
|
"arrayPopFront": STLFunction(fn=arrayPopFront, minArgs=1, maxArgs=1),
|
|
|
|
"arraySort": STLFunction(fn=arraySort, minArgs=1, maxArgs=1),
|
|
|
|
"arrayReverse": STLFunction(fn=arrayReverse, minArgs=1, maxArgs=1),
|
|
|
|
"arrayReverseSort": STLFunction(fn=arrayReverseSort, minArgs=1, maxArgs=1),
|
|
|
|
"arrayStringConcat": STLFunction(fn=arrayStringConcat, minArgs=1, maxArgs=2),
|
|
|
|
"has": STLFunction(fn=has, minArgs=2, maxArgs=2),
|
|
|
|
"now": STLFunction(fn=lambda args, team, stdout, timeout: now(), minArgs=0, maxArgs=0),
|
|
|
|
"toUnixTimestamp": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: toUnixTimestamp(args[0], args[1] if len(args) > 1 else None),
|
|
|
|
minArgs=1,
|
|
|
|
maxArgs=2,
|
|
|
|
),
|
|
|
|
"fromUnixTimestamp": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: fromUnixTimestamp(args[0]), minArgs=1, maxArgs=1
|
|
|
|
),
|
|
|
|
"toUnixTimestampMilli": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: toUnixTimestampMilli(args[0]), minArgs=1, maxArgs=2
|
|
|
|
),
|
|
|
|
"fromUnixTimestampMilli": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: fromUnixTimestampMilli(args[0]), minArgs=1, maxArgs=1
|
|
|
|
),
|
|
|
|
"toTimeZone": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: toTimeZone(args[0], args[1]), minArgs=2, maxArgs=2
|
|
|
|
),
|
|
|
|
"toDate": STLFunction(fn=lambda args, team, stdout, timeout: toDate(args[0]), minArgs=1, maxArgs=1),
|
|
|
|
"toDateTime": STLFunction(fn=lambda args, team, stdout, timeout: toDateTime(args[0]), minArgs=1, maxArgs=2),
|
|
|
|
"formatDateTime": STLFunction(fn=_formatDateTime, minArgs=2, maxArgs=3),
|
|
|
|
"HogError": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: new_hog_error(args[0], args[1], args[2] if len(args) > 2 else None),
|
|
|
|
minArgs=1,
|
|
|
|
maxArgs=3,
|
|
|
|
),
|
|
|
|
"Error": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: new_hog_error(
|
|
|
|
"Error", args[0] if len(args) > 0 else None, args[1] if len(args) > 1 else None
|
|
|
|
),
|
|
|
|
minArgs=0,
|
|
|
|
maxArgs=2,
|
|
|
|
),
|
|
|
|
"RetryError": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: new_hog_error("RetryError", args[0], args[1] if len(args) > 1 else None),
|
|
|
|
minArgs=0,
|
|
|
|
maxArgs=2,
|
|
|
|
),
|
|
|
|
"NotImplementedError": STLFunction(
|
|
|
|
fn=lambda args, team, stdout, timeout: new_hog_error(
|
|
|
|
"NotImplementedError", args[0], args[1] if len(args) > 1 else None
|
|
|
|
),
|
|
|
|
minArgs=0,
|
|
|
|
maxArgs=2,
|
|
|
|
),
|
2024-09-02 14:42:03 +02:00
|
|
|
"typeof": STLFunction(fn=_typeof, minArgs=1, maxArgs=1),
|
2024-08-28 11:25:58 +02:00
|
|
|
# only in python, async function in nodejs
|
|
|
|
"sleep": STLFunction(fn=sleep, minArgs=1, maxArgs=1),
|
|
|
|
"run": STLFunction(fn=run, minArgs=1, maxArgs=1),
|
2024-06-07 11:37:53 +02:00
|
|
|
}
|