forked from longtao.wu/droboticflow
69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Callable
|
|
|
|
|
|
HelperMap = dict[str, Callable[..., Any]]
|
|
|
|
|
|
def run(
|
|
run_id: str,
|
|
node_id: str,
|
|
output_dir: Any,
|
|
node_data: dict[str, Any],
|
|
inputs: list[dict[str, Any]],
|
|
helpers: HelperMap,
|
|
) -> list[dict[str, Any]]:
|
|
params = helpers["hyperparams"](node_data)
|
|
payload = helpers["first_payload_with_keys"](inputs, "packageCount", "items")
|
|
if payload is None:
|
|
raise RuntimeError("抽检采样需要上游成品数据输入")
|
|
rules = helpers["normalize_sampling_rules"](helpers["parse_json_param"](params, "RULES_JSON", [], list))
|
|
sampled_items = []
|
|
summaries = []
|
|
for rule in rules:
|
|
rule_id = rule["id"]
|
|
field_path = rule["field"]
|
|
operator = rule["operator"]
|
|
value = rule.get("value")
|
|
percent = rule["samplePercent"]
|
|
matched = []
|
|
sampled = []
|
|
for item in payload.get("items", []):
|
|
if not isinstance(item, dict):
|
|
continue
|
|
if not helpers["compare_values"](helpers["get_field_value"](item, field_path), operator, value):
|
|
continue
|
|
matched.append(item)
|
|
package_name = str(item.get("packageName", "")).strip() or str(item.get("clipId", "")).strip()
|
|
if helpers["stable_sample_hit"](f"{rule_id}:{package_name}", percent):
|
|
sampled.append(item)
|
|
sampled_items.append({**item, "sampleRuleId": rule_id})
|
|
summaries.append(
|
|
{
|
|
"id": rule_id,
|
|
"field": field_path,
|
|
"operator": operator,
|
|
"value": value,
|
|
"samplePercent": percent,
|
|
"matchedCount": len(matched),
|
|
"sampledCount": len(sampled),
|
|
}
|
|
)
|
|
manifest = {
|
|
"datasetType": "x-platform-audit-samples",
|
|
"rules": summaries,
|
|
"sampledCount": len(sampled_items),
|
|
"items": sampled_items,
|
|
}
|
|
return [
|
|
helpers["write_json_artifact"](
|
|
run_id,
|
|
node_id,
|
|
output_dir,
|
|
"audit-samples.json",
|
|
manifest,
|
|
output=helpers["output_binding"](node_data),
|
|
)
|
|
]
|