sample_rate
支持的语言:
optimization.sample_rate(byteOrString, rateNumerator, rateDenominator)
说明
此函数可根据确定性抽样策略确定是否包含某个事件。此函数会返回:
true表示一部分输入值,相当于 (rateNumerator/rateDenominator),表示相应事件应纳入样本中。false,表示不应将相应事件纳入样本中。
此函数适用于您只想处理部分事件的优化方案。等效于:
hash.fingerprint2011(byteOrString) % rateDenominator < rateNumerator
形参数据类型
- byteOrString:计算结果为
BYTE或STRING的表达式。 - rateNumerator: 'INT'
- rateDenominator: 'INT'
返回类型
BOOL
代码示例
events:
$e.metadata.event_type = "NETWORK_CONNECTION"
$asset_id = $e.principal.asset.asset_id
optimization.sample_rate($e.metadata.id, 1, 5) // Only 1 out of every 5 events
match:
$asset_id over 1h
outcome:
$event_count = count_distinct($e.metadata.id)
// estimate the usage by multiplying by the inverse of the sample rate
$usage_past_hour = sum(5.0 * $e.network.sent_bytes)
condition:
// Requiring a certain number of events after sampling avoids bias (e.g. a
// device with just 1 connection will still show up 20% of the time and
// if we multiply that traffic by 5, we'll get an incorrect estimate)
$e and ($usage_past_hour > 1000000000) and $event_count >= 100