Skip to content

Commit 89cbbc5

Browse files
authored
[tsctl] searchindex-info improvements (#3368)
* improve tsctl searchindex-info * In all possible execution paths, the result of the initial query is never used. It's immediately overwritten.
1 parent 7eef6d5 commit 89cbbc5

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

docs/guides/admin/admin-cli.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -508,17 +508,26 @@ id status created_at user_id
508508
In some cases, logs present a OpenSearch Index id and it is not easy to find out
509509
which Sketch that index is related to.
510510
511-
Therefore the following command can be used:
511+
Therefore the following command can be used `tsctl searchindex-info`:
512+
513+
Displays detailed information about a specific search index. You can specify the index using either its database ID or its OpenSearch index name.
514+
The command shows the search index ID and name, and lists all timelines associated with the index, including their IDs, names, and associated sketch IDs and names.
512515
513516
```bash
514-
# tsctl searchindex-info --searchindex_id asd
517+
# tsctl searchindex-info --searchindex_id 99
515518
Searchindex: asd not found in database.
516-
# tsctl searchindex-info --searchindex_id 4c5afdf60c6e49499801368b7f238353
519+
# tsctl searchindex-info --searchindex_id 1
520+
Searchindex: 1 Name: sigma_events found in database.
521+
Corresponding Timeline id: 3 in Sketch Id: 2
522+
Corresponding Sketch id: 2 Sketch name: asdasd
523+
# tsctl searchindex-info --index_name 4c5afdf60c6e49499801368b7f238353
517524
Searchindex: 4c5afdf60c6e49499801368b7f238353 Name: sigma_events found in database.
518525
Corresponding Timeline id: 3 in Sketch Id: 2
519526
Corresponding Sketch id: 2 Sketch name: asdasd
520527
```
521528
529+
If neither `searchindex_id` nor `index_name` is provided, an error message is printed. If no matching search index is found, an appropriate message is printed
530+
522531
### Timeline status
523532
524533
The `tsctl timeline-status` command allows to get or set a timeline status.

timesketch/tsctl.py

+46-18
Original file line numberDiff line numberDiff line change
@@ -675,13 +675,13 @@ def sketch_info(sketch_id: int):
675675
type=click.Choice(["ready", "processing", "fail"]),
676676
help="get or set timeline status.",
677677
)
678-
def timeline_status(timeline_id: str, action: str, status: str):
678+
def timeline_status(timeline_id: int, action: str, status: str):
679679
"""Get or set a timeline status.
680680
681681
If "action" is "set", the given value of status will be written in the status.
682682
683683
Args:
684-
timeline_id (str): The ID of the timeline.
684+
timeline_id (int): The ID of the timeline.
685685
action (str): The action to perform ("get" or "set").
686686
status (str): The timeline status to set. Must be one of "ready",
687687
"processing", or "fail".
@@ -839,33 +839,61 @@ def validate_context_links_conf(path):
839839
@cli.command(name="searchindex-info")
840840
@click.option(
841841
"--searchindex_id",
842-
required=True,
843-
help="Searchindex ID to search for e.g. 4c5afdf60c6e49499801368b7f238353.",
842+
required=False,
843+
help="Searchindex database ID to search for e.g. 3.",
844+
)
845+
@click.option(
846+
"--index_name",
847+
required=False,
848+
help="Searchindex name to search for e.g. 4c5afdf60c6e49499801368b7f238353.",
844849
)
845-
def searchindex_info(searchindex_id: str):
850+
def searchindex_info(searchindex_id: int, index_name: str):
846851
"""Search for a searchindex and print information about it.
847-
Especially which sketch the searchindex belongs to.
852+
Especially which sketch the searchindex belongs to. You can either use the
853+
searchindex ID or the index name.
854+
848855
849856
Args:
850-
searchindex_id (str): The search index ID to search for (e.g.,
857+
searchindex_id (int): The searchindex database ID to search for (e.g.,
858+
"3").
859+
index_name (str): The search index ID to search for (e.g.,
851860
"4c5afdf60c6e49499801368b7f238353").
852861
"""
862+
if searchindex_id:
863+
if not searchindex_id.isdigit():
864+
print("Searchindex database ID needs to be an integer.")
865+
return
853866

854-
index_to_search = SearchIndex.query.filter_by(index_name=searchindex_id).first()
867+
index_to_search = None
868+
869+
if searchindex_id:
870+
index_to_search = SearchIndex.query.filter_by(id=searchindex_id).first()
871+
elif index_name:
872+
index_to_search = SearchIndex.query.filter_by(index_name=index_name).first()
873+
else:
874+
print("Please provide either a searchindex ID or an index name")
875+
return
855876

856877
if not index_to_search:
857-
print(f"Searchindex: {searchindex_id} not found in database.")
878+
print("Searchindex not found in database.")
858879
return
859880

860-
print(
861-
f"Searchindex: {searchindex_id} Name: {index_to_search.name} found in database."
862-
)
863-
timeline = Timeline.query.filter_by(id=index_to_search.id).first()
864-
print(
865-
f"Corresponding Timeline id: {timeline.id} in Sketch Id: {timeline.sketch_id}"
866-
)
867-
sketch = Sketch.query.filter_by(id=timeline.sketch_id).first()
868-
print(f"Corresponding Sketch id: {sketch.id} Sketch name: {sketch.name}")
881+
print(f"Searchindex: {index_to_search.id} Name: {index_to_search.name} found")
882+
883+
timelines = index_to_search.timelines
884+
if timelines:
885+
print("Associated Timelines:")
886+
for timeline in timelines:
887+
print(f" ID: {timeline.id}, Name: {timeline.name}")
888+
if timeline.sketch:
889+
print(
890+
f" Sketch ID: {timeline.sketch.id}, Name: {timeline.sketch.name}"
891+
)
892+
else:
893+
print(" No associated sketch found.")
894+
else:
895+
print("No associated timelines found.")
896+
return
869897

870898

871899
@cli.command(name="searchindex-status")

0 commit comments

Comments
 (0)