Skip to content

Commit

Permalink
Update run_analyzers in the api client (#3037)
Browse files Browse the repository at this point in the history
* Update run_analyzers in the api client.
  • Loading branch information
jkppr committed Feb 15, 2024
1 parent 51a7715 commit 769b1d2
Showing 1 changed file with 7 additions and 28 deletions.
35 changes: 7 additions & 28 deletions api_client/python/timesketch_api_client/timeline.py
Expand Up @@ -199,7 +199,7 @@ def run_analyzer(self, analyzer_name, analyzer_kwargs=None, ignore_previous=Fals
if analyzer_kwargs:
if not isinstance(analyzer_kwargs, dict):
raise error.UnableToRunAnalyzer(
"Unable to run analyzer, analyzer kwargs needs to be a " "dict"
"Unable to run analyzer, analyzer kwargs needs to be a dict"
)

if analyzer_name not in analyzer_kwargs:
Expand Down Expand Up @@ -249,34 +249,11 @@ def run_analyzers(
self.api.api_root, self._sketch_id
)

if not ignore_previous:
all_names = {x.lower() for x in analyzer_names}
done_names = set()

response = self.api.fetch_resource_data(
f"sketches/{self._sketch_id}/timelines/{self.id}/analysis/"
)
analyzer_data = response.get("objects", [[]])

if analyzer_data:
for result in analyzer_data[0]:
result_analyzer = result.get("analyzer_name", "N/A")
done_names.add(result_analyzer.lower())

analyzer_names = list(all_names.difference(done_names))
for name in all_names.intersection(done_names):
logger.error(
"Analyzer {0:s} has already been run on the timeline, "
'use "ignore_previous=True" to overwrite'.format(name)
)

if not analyzer_names:
return None

data = {
"timeline_ids": [self.id],
"analyzer_names": analyzer_names,
"analyzer_kwargs": analyzer_kwargs,
"analyzer_force_run": ignore_previous,
}
response = self.api.session.post(resource_url, json=data)

Expand All @@ -290,10 +267,12 @@ def run_analyzers(
data = error.get_response_json(response, logger)
objects = data.get("objects", [])
if not objects:
raise error.UnableToRunAnalyzer(
"No session data returned back, analyzer may have run but "
"unable to verify, please verify manually."
logger.info(
"Analyzers %s were already run on the timeline, use "
"'ignore_previous=True' to overwrite.",
analyzer_names,
)
return None

analyzer_results = []
for session_dict in objects[0]:
Expand Down

0 comments on commit 769b1d2

Please sign in to comment.