Skip to content

关于本页

本页介绍更夫所基于的开源内核 Keep 的通用能力。更夫在其上提供国内化增强:钉钉/企业微信/飞书通知、国产大模型接入、LDAP 认证与值班管理。部署与上手请看快速上手。

Keep 工作流引擎中的函数是可在工作流中用于操作数据、检查条件或执行转换的实用工具。本文档为每个可用函数提供简要概述和使用示例。


数学函数 ​

add ​

描述: 将所有提供的数字相加。所有参数都转换为整数。

示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.add(1, 2, 3) # Output: 6
        message2: keep.add(10, 20, 30) # Output: 60

sub ​

描述: 从第一个数字中减去所有后续数字。所有参数都转换为整数。

示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.sub(10, 2, 3) # Output: 5
        message2: keep.sub(100, 20, 30) # Output: 50

mul ​

描述: 将所有提供的数字相乘。所有参数都转换为整数。

示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.mul(2, 3, 4) # Output: 24
        message2: keep.mul(5, 6, 7) # Output: 210

div ​

描述: 用第一个数字除以所有后续数字。所有参数都转换为整数。如果除法结果是整数则返回整数,否则返回浮点数。

示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.div(10, 2) # Output: 5
        message2: keep.div(10, 3) # Output: 3.3333333333333335
        message3: keep.div(100, 2, 5) # Output: 10

mod ​

描述: 计算将第一个数字除以所有后续数字的余数(按顺序)。所有参数都转换为整数。

示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.mod(10, 3) # Output: 1
        message2: keep.mod(100, 30, 7) # Output: 2

exp ​

描述: 将第一个数字提升到等于所有后续数字乘积的幂。所有参数都转换为整数。

示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.exp(2, 3) # Output: 8
        message2: keep.exp(2, 3, 2) # Output: 64

fdiv ​

描述: 对第一个数字和所有后续数字执行整数除法。所有参数都转换为整数。

示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.fdiv(10, 3) # Output: 3
        message2: keep.fdiv(100, 3, 2) # Output: 16

eq ​

描述: 检查两个值是否相等。

示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.eq(5, 5) # Output: true
        message2: keep.eq("hello", "world") # Output: false
        message3: keep.eq([1, 2, 3], [1, 2, 3]) # Output: true

字符串函数 ​

uppercase ​

描述: 将字符串转换为大写。

示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: "keep.uppercase('hello world')" # Output: "HELLO WORLD"

lowercase ​

描述: 将字符串转换为小写。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: "keep.lowercase('HELLO WORLD')" # Output: "hello world"

capitalize ​

描述: 将字符串的第一个字符大写。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.capitalize("hello world") # Output: "Hello world"

title ​

描述: 将字符串转换为首字母大写形式(每个单词的首字母大写)。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.title("hello world") # Output: "Hello World"

split ​

描述: 使用分隔符将字符串拆分为列表。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: "keep.split('a,b,c', ',')" # Output: ["a", "b", "c"]

strip ​

描述: 移除字符串开头和结尾的空白字符。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.strip("  hello world  ") # Output: "hello world"

replace ​

描述: 用另一个字符串替换子字符串的所有出现。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.replace("hello world", "world", "Keep") # Output: "hello Keep"

remove_newlines ​

描述: 从字符串中移除所有换行符和制表符。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.remove_newlines("hello\nworld\t!") # Output: "helloworld!"

encode ​

描述: 对字符串进行 URL 编码。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.encode("hello world") # Output: "hello%20world"

slice ​

描述: 基于起始和结束索引提取字符串的一部分。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.slice("hello world", 0, 5) # Output: "hello"

列表和字典函数 ​

first ​

描述: 从列表中检索第一个元素。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.first([1, 2, 3]) # Output: 1

last ​

描述: 从列表中检索最后一个元素。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.last([1, 2, 3]) # Output: 3

index ​

描述: 从列表中检索特定索引处的元素。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.index(["a", "b", "c"], 1) # Output: "b"

join ​

描述: 使用分隔符将元素列表连接成字符串。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.join(["a", "b", "c"], ",") # Output: "a,b,c"

len ​

描述: 返回列表的长度。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.len([1, 2, 3]) # Output: 3

dict_to_key_value_list ​

描述: 将字典转换为键值对列表。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.dict_to_key_value_list({"a": 1, "b": 2}) # Output: ["a:1", "b:2"]

dict_pop ​

描述: 从字典中移除指定的键。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.dict_pop({"a": 1, "b": 2, "c": 3}, "a", "b") # Output: {"c": 3}

dict_pop_prefix ​

描述: 从字典中移除以指定前缀开头的所有键。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.dict_pop_prefix({"a_1": 1, "a_2": 2, "b_1": 3}, "a_") # Output: {"b_1": 3}

dict_filter_by_prefix ​

描述: 仅返回键以指定前缀开头的字典条目。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.dict_filter_by_prefix({"a_1": 1, "a_2": 2, "b_1": 3}, "a_") # Output: {"a_1": 1, "a_2": 2}

dictget ​

描述: 从字典中获取值,带有默认回退值。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.dictget({"a": 1, "b": 2}, "c", "default") # Output: "default"

日期和时间函数 ​

from_timestamp ​

描述: 将 unix 时间戳 int、float 或 string 转换为 datetime 对象,带有可选的时区选项。

示例:

yaml
steps:
  - name: example-step
    provider:
      type: console
      with:
        message: keep.from_timestamp(1717244449.0) # will print "2024-06-01 12:20:49+00:00"
        # or with timezone
        # message: keep.from_timestamp(1717244449.0, "Europe/Berlin") # will print "2024-06-01 14:20:49+02:00"

utcnow ​

描述: 返回当前 UTC 日期时间。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.utcnow()

utcnowtimestamp ​

描述: 将当前 UTC 日期时间作为 Unix 时间戳返回(自 epoch 以来的秒数)。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.utcnowtimestamp() # Output: 1704067200

utcnowiso ​

描述: 以 ISO 格式返回当前 UTC 日期时间。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.utcnowiso()

to_utc ​

描述: 将日期时间字符串或对象转换为 UTC。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.to_utc("2024-01-01T00:00:00")

to_timestamp ​

描述: 将日期时间对象或字符串转换为 Unix 时间戳。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.to_timestamp("2024-01-01T00:00:00")

datetime_compare ​

描述: 比较两个日期时间对象并返回小时差。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.datetime_compare("2024-01-01T10:00:00", "2024-01-01T00:00:00") # Output: 10.0

is_business_hours ​

描述: 检查给定时间是否在营业时间内。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.is_business_hours(
          time_to_check="2024-01-01T14:00:00Z",
          start_hour=8,
          end_hour=20,
          business_days=[0,1,2,3,4],
          timezone="America/New_York"
        )

JSON 函数 ​

json_dumps ​

描述: 将字典或字符串转换为格式化的 JSON 字符串。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.json_dumps({"key": "value"})

json_loads ​

描述: 将 JSON 字符串解析为字典。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.json_loads('{"key": "value"}')

实用函数 ​

get_firing_time ​

描述: 以指定的时间单位计算告警的触发持续时间。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.get_firing_time(alert, "m", tenant_id="tenant-id") # Output: "15.0"

add_time_to_date ​

描述: 根据指定的时间单位向日期字符串添加时间。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.add_time_to_date("2024-01-01", "%Y-%m-%d", "1w 2d") # Output: "2024-01-10"

timestamp_delta ​

描述: 向日期时间添加或减去时间增量。使用负值来减去时间。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        # Add 2 hours to the current time
        add_hours: keep.timestamp_delta(keep.utcnow(), 2, "hours")

        # Subtract 30 minutes from a specific datetime
        subtract_minutes: keep.timestamp_delta("2024-01-01T12:00:00Z", -30, "minutes") # Output: 2024-01-01T11:30:00Z

        # Add 1 week to a datetime
        add_week: keep.timestamp_delta("2024-01-01T00:00:00Z", 1, "weeks") # Output: 2024-01-08T00:00:00Z

is_first_time ​

描述: 检查具有给定指纹的告警是否首次触发或在指定时间段内首次触发。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        # Check if this is the first time the alert is firing
        first_time: keep.is_first_time(alert.fingerprint, tenant_id="tenant-id")

        # Check if this is the first time the alert is firing in the last 24 hours
        first_time_24h: keep.is_first_time(alert.fingerprint, "24h", tenant_id="tenant-id")

all ​

描述: 检查可迭代对象中的所有元素是否相同。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.all([1, 1, 1]) # Output: true

diff ​

描述: 检查可迭代对象中是否有任何元素不同(与 all 相反)。 示例:

yaml
steps:
  - name: example-step
    provider:
      type: mock
      with:
        message: keep.diff([1, 2, 1]) # Output: true

基于 MIT 协议开源的 Keep · 由更夫社区维护