Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit 9f29f07

Browse files
[client] Improve sighting ingestion speed removing extra resolution (#728)
1 parent 2e01c16 commit 9f29f07

File tree

1 file changed

+21
-63
lines changed

1 file changed

+21
-63
lines changed

pycti/utils/opencti_stix2.py

Lines changed: 21 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,45 +1315,6 @@ def import_sighting(
13151315

13161316
# Create the sighting
13171317

1318-
### Get the FROM
1319-
if from_id in self.mapping_cache:
1320-
final_from_id = self.mapping_cache[from_id]["id"]
1321-
else:
1322-
stix_object_result = (
1323-
self.opencti.opencti_stix_object_or_stix_relationship.read(id=from_id)
1324-
)
1325-
if stix_object_result is not None:
1326-
final_from_id = stix_object_result["id"]
1327-
self.mapping_cache[from_id] = {
1328-
"id": stix_object_result["id"],
1329-
"entity_type": stix_object_result["entity_type"],
1330-
}
1331-
else:
1332-
self.opencti.app_logger.error(
1333-
"From ref of the sighting not found, doing nothing..."
1334-
)
1335-
return None
1336-
1337-
### Get the TO
1338-
final_to_id = None
1339-
if to_id:
1340-
if to_id in self.mapping_cache:
1341-
final_to_id = self.mapping_cache[to_id]["id"]
1342-
else:
1343-
stix_object_result = (
1344-
self.opencti.opencti_stix_object_or_stix_relationship.read(id=to_id)
1345-
)
1346-
if stix_object_result is not None:
1347-
final_to_id = stix_object_result["id"]
1348-
self.mapping_cache[to_id] = {
1349-
"id": stix_object_result["id"],
1350-
"entity_type": stix_object_result["entity_type"],
1351-
}
1352-
else:
1353-
self.opencti.app_logger.error(
1354-
"To ref of the sighting not found, doing nothing..."
1355-
)
1356-
return None
13571318
if (
13581319
"x_opencti_negative" not in stix_sighting
13591320
and self.opencti.get_attribute_in_extension("negative", stix_sighting)
@@ -1367,8 +1328,8 @@ def import_sighting(
13671328
self.opencti.get_attribute_in_extension("workflow_id", stix_sighting)
13681329
)
13691330
stix_sighting_result = self.opencti.stix_sighting_relationship.create(
1370-
fromId=final_from_id,
1371-
toId=final_to_id,
1331+
fromId=from_id,
1332+
toId=to_id,
13721333
stix_id=stix_sighting["id"] if "id" in stix_sighting else None,
13731334
description=(
13741335
self.convert_markdown(stix_sighting["description"])
@@ -2433,39 +2394,36 @@ def import_item(
24332394
# Import relationship
24342395
self.import_relationship(item, update, types)
24352396
elif item["type"] == "sighting":
2436-
# Resolve the to
2397+
# region Resolve the to
24372398
to_ids = []
2438-
if "where_sighted_refs" in item:
2399+
if "x_opencti_where_sighted_refs" in item:
2400+
for where_sighted_ref in item["_opencti_where_sighted_refs"]:
2401+
to_ids.append(where_sighted_ref)
2402+
elif "where_sighted_refs" in item:
24392403
for where_sighted_ref in item["where_sighted_refs"]:
24402404
to_ids.append(where_sighted_ref)
2441-
# Import sighting_of_ref
2405+
# endregion
2406+
# region Resolve the from
2407+
from_id = None
24422408
if "x_opencti_sighting_of_ref" in item:
24432409
from_id = item["x_opencti_sighting_of_ref"]
2444-
if len(to_ids) > 0:
2445-
for to_id in to_ids:
2446-
self.import_sighting(item, from_id, to_id, update)
2447-
if (
2448-
self.opencti.get_attribute_in_extension("sighting_of_ref", item)
2449-
is not None
2450-
):
2451-
from_id = self.opencti.get_attribute_in_extension(
2452-
"sighting_of_ref", item
2453-
)
2454-
if len(to_ids) > 0:
2410+
elif "sighting_of_ref" in item:
2411+
from_id = item["sighting_of_ref"]
2412+
# endregion
2413+
# region create the sightings
2414+
if len(to_ids) > 0:
2415+
if from_id:
24552416
for to_id in to_ids:
24562417
self.import_sighting(item, from_id, to_id, update)
2457-
from_id = item["sighting_of_ref"]
2458-
if len(to_ids) > 0:
2459-
for to_id in to_ids:
2460-
self.import_sighting(item, from_id, to_id, update)
2461-
# Import observed_data_refs
2462-
if "observed_data_refs" in item:
2463-
for observed_data_ref in item["observed_data_refs"]:
2464-
if len(to_ids) > 0:
2418+
# Import observed_data_refs
2419+
if "observed_data_refs" in item:
2420+
for observed_data_ref in item["observed_data_refs"]:
24652421
for to_id in to_ids:
24662422
self.import_sighting(
24672423
item, observed_data_ref, to_id, update
24682424
)
2425+
# endregion
2426+
24692427
elif item["type"] == "label":
24702428
stix_ids = self.opencti.get_attribute_in_extension("stix_ids", item)
24712429
self.opencti.label.create(

0 commit comments

Comments
 (0)