forked from longtao.wu/droboticflow
105 lines
4.3 KiB
Python
105 lines
4.3 KiB
Python
from __future__ import annotations
|
||
|
||
import os
|
||
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)
|
||
mongo_target = params.get("MONGO_TARGET", "").strip().lower() or "business"
|
||
explicit_mongo_uri = params.get("MONGO_URI", "").strip() or os.getenv("MONGO_URI", "").strip()
|
||
explicit_mongo_db = params.get("MONGO_DB", "").strip() or os.getenv("MONGO_DB", "").strip()
|
||
collection_name = params.get("MONGO_COLLECTION", "").strip()
|
||
mode = params.get("MONGO_MODE", "insert_many").strip().lower() or "insert_many"
|
||
upsert_key = params.get("UPSERT_KEY", "_id").strip() or "_id"
|
||
document_path = params.get("DOCUMENT_PATH", "").strip()
|
||
|
||
mongo_uri = explicit_mongo_uri
|
||
mongo_db = explicit_mongo_db
|
||
if not mongo_uri:
|
||
if mongo_target == "platform":
|
||
mongo_uri = os.getenv("PLATFORM_MONGO_URI", "").strip()
|
||
mongo_db = mongo_db or os.getenv("PLATFORM_MONGO_DB", "").strip()
|
||
else:
|
||
mongo_uri = os.getenv("BUSINESS_MONGO_URI", "").strip()
|
||
mongo_db = mongo_db or os.getenv("BUSINESS_MONGO_DB", "").strip()
|
||
|
||
if not mongo_uri:
|
||
raise RuntimeError(f"MongoDB 写入缺少可用连接:target={mongo_target}")
|
||
if not mongo_db:
|
||
raise RuntimeError(f"MongoDB 写入缺少数据库名:target={mongo_target}")
|
||
if not collection_name:
|
||
raise RuntimeError("MONGO_COLLECTION is required for MongoDB 写入")
|
||
if mongo_target == "platform" and collection_name.lower() in {
|
||
item.lower() for item in helpers["RESERVED_MONGO_COLLECTIONS"]
|
||
}:
|
||
raise RuntimeError(f"collection {collection_name} is reserved and cannot be written by workflow operators")
|
||
if mode not in {"insert_many", "upsert"}:
|
||
raise RuntimeError("MONGO_MODE must be insert_many or upsert")
|
||
|
||
documents = helpers["documents_from_inputs"](helpers["payloads_from_inputs"](inputs), document_path)
|
||
if not documents:
|
||
raise RuntimeError("MongoDB 写入未收到可写入的文档")
|
||
|
||
client = helpers["build_mongo_client"](mongo_uri)
|
||
try:
|
||
collection = client[mongo_db][collection_name]
|
||
summary: dict[str, Any] = {
|
||
"target": mongo_target,
|
||
"database": mongo_db,
|
||
"collection": collection_name,
|
||
"mode": mode,
|
||
"documentCount": len(documents),
|
||
}
|
||
if document_path:
|
||
summary["documentPath"] = document_path
|
||
if mode == "insert_many":
|
||
result = collection.insert_many(documents, ordered=False)
|
||
summary["insertedCount"] = len(getattr(result, "inserted_ids", []) or [])
|
||
else:
|
||
processed = 0
|
||
matched_count = 0
|
||
modified_count = 0
|
||
upserted_ids: list[Any] = []
|
||
for document in documents:
|
||
key_value = helpers["get_field_value"](document, upsert_key)
|
||
if key_value is None:
|
||
raise RuntimeError(f"document is missing UPSERT_KEY={upsert_key}")
|
||
result = collection.replace_one({upsert_key: key_value}, document, upsert=True)
|
||
processed += 1
|
||
matched_count += int(getattr(result, "matched_count", 0) or 0)
|
||
modified_count += int(getattr(result, "modified_count", 0) or 0)
|
||
upserted_id = getattr(result, "upserted_id", None)
|
||
if upserted_id is not None:
|
||
upserted_ids.append(helpers["json_compatible"](upserted_id))
|
||
summary["upsertKey"] = upsert_key
|
||
summary["upsertedCount"] = processed
|
||
summary["matchedCount"] = matched_count
|
||
summary["modifiedCount"] = modified_count
|
||
if upserted_ids:
|
||
summary["upsertedIds"] = upserted_ids
|
||
return [
|
||
helpers["write_json_artifact"](
|
||
run_id,
|
||
node_id,
|
||
output_dir,
|
||
"mongodb-write-result.json",
|
||
summary,
|
||
output=helpers["output_binding"](node_data),
|
||
)
|
||
]
|
||
finally:
|
||
close = getattr(client, "close", None)
|
||
if callable(close):
|
||
close()
|