Skip to content

Commit

Permalink
Merge pull request #115 from CrowdStrike/online-state-batch-fix
Browse files Browse the repository at this point in the history
Online State Endpoints Batch Data Size Fix
  • Loading branch information
kenoel authored Aug 22, 2023
2 parents b3c257d + 3ee6bce commit cef519b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions caracara/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Caracara: Common functions and data imports."""
__all__ = [
"DATA_BATCH_SIZE",
"DEFAULT_DATA_BATCH_SIZE",
"FalconApiModule",
"Policy",
"SCROLL_BATCH_SIZE",
"user_agent_string",
]

from caracara.common.constants import SCROLL_BATCH_SIZE, DATA_BATCH_SIZE
from caracara.common.constants import SCROLL_BATCH_SIZE, DEFAULT_DATA_BATCH_SIZE
from caracara.common.policy_wrapper import Policy
from caracara.common.meta import user_agent_string
from caracara.common.module import FalconApiModule
7 changes: 4 additions & 3 deletions caracara/common/batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from threading import current_thread
from typing import Callable, Dict, List, Tuple

from caracara.common.constants import DATA_BATCH_SIZE
from caracara.common.constants import DEFAULT_DATA_BATCH_SIZE


BATCH_LOGGER = logging.getLogger(__name__)
Expand All @@ -37,6 +37,7 @@ def batch_data_pull_threads() -> int:
def batch_get_data(
lookup_ids: str,
func: Callable[[object, List[str], str], Dict],
data_batch_size: int = DEFAULT_DATA_BATCH_SIZE,
) -> Dict[str, Dict]:
"""Retrieve details for the list of AIDs provided.
Expand All @@ -57,8 +58,8 @@ def batch_get_data(
BATCH_LOGGER.info("Batch data retrieval for %s (%d items)", func.__name__, len(lookup_ids))
BATCH_LOGGER.debug(str(lookup_ids))

# Divide the list of item IDs into a list of lists, each of size DATA_BATCH_SIZE
batches = [lookup_ids[i:i+DATA_BATCH_SIZE] for i in range(0, len(lookup_ids), DATA_BATCH_SIZE)]
# Divide the list of item IDs into a list of lists, each of size data_batch_size
batches = [lookup_ids[i:i+data_batch_size] for i in range(0, len(lookup_ids), data_batch_size)]
BATCH_LOGGER.info("Divided the item IDs into %d batches", len(batches))

threads = batch_data_pull_threads()
Expand Down
5 changes: 4 additions & 1 deletion caracara/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from enum import Enum, EnumMeta

# Batch size of data downloaded via a multi-threaded data pull
DATA_BATCH_SIZE = 500
DEFAULT_DATA_BATCH_SIZE = 500

# Batch size of data downloaded via a multi-threaded data pull from the online state endpoint
ONLINE_STATE_DATA_BATCH_SIZE = 100

# Default pagination limit
PAGINATION_LIMIT = 100
Expand Down
8 changes: 6 additions & 2 deletions caracara/modules/hosts/_online_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)

from caracara.common.batching import batch_get_data
from caracara.common.constants import OnlineState
from caracara.common.constants import OnlineState, ONLINE_STATE_DATA_BATCH_SIZE
from caracara.common.exceptions import InvalidOnlineState

if TYPE_CHECKING:
Expand Down Expand Up @@ -56,7 +56,11 @@ def get_online_state(
dict: A dictionary containing online state details for every device listed.
"""
self.logger.info("Obtaining online state data for %s devices", len(device_ids))
device_online_state_data = batch_get_data(device_ids, self.hosts_api.get_online_state)
device_online_state_data = batch_get_data(
device_ids,
self.hosts_api.get_online_state,
data_batch_size=ONLINE_STATE_DATA_BATCH_SIZE,
)
return device_online_state_data


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "caracara"
version = "0.5.0"
version = "0.5.1"
description = "The CrowdStrike Falcon Developer Toolkit"
authors = [ "CrowdStrike <falconpy@crowdstrike.com>" ]
readme = "README.md"
Expand Down

0 comments on commit cef519b

Please sign in to comment.