|
2 | 2 | # SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | 4 | import json |
| 5 | +from typing import Any |
| 6 | + |
5 | 7 | from .tool_params import ( |
6 | 8 | GetAllocationArgs, |
7 | 9 | GetClusterStateArgs, |
|
35 | 37 | get_query_insights, |
36 | 38 | get_segments, |
37 | 39 | get_shards, |
| 40 | + list_aliases, |
| 41 | + list_data_streams, |
38 | 42 | list_indices, |
39 | 43 | search_index, |
40 | 44 | ) |
@@ -76,26 +80,99 @@ async def list_indices_tool(args: ListIndicesArgs) -> list[dict]: |
76 | 80 | {'type': 'text', 'text': f'Index information for {args.index}:\n{formatted_info}'} |
77 | 81 | ] |
78 | 82 |
|
79 | | - # Otherwise, list all indices |
| 83 | + # Otherwise, list all indices, aliases, and data streams |
80 | 84 | indices = list_indices(args) |
81 | | - |
82 | | - # If include_detail is False, return only pure list of index names |
83 | | - if not args.include_detail: |
84 | | - index_names = [ |
85 | | - item.get('index') |
86 | | - for item in indices |
87 | | - if isinstance(item, dict) and 'index' in item |
88 | | - ] |
89 | | - formatted_names = json.dumps(index_names, indent=2) |
90 | | - return [{'type': 'text', 'text': f'Indices:\n{formatted_names}'}] |
91 | | - |
92 | | - # include_detail is True: return full information |
93 | | - formatted_indices = json.dumps(indices, indent=2) |
94 | | - return [{'type': 'text', 'text': f'All indices information:\n{formatted_indices}'}] |
| 85 | + aliases = list_aliases(args) |
| 86 | + data_streams = list_data_streams(args) |
| 87 | + |
| 88 | + indices_payload = _format_indices(indices, aliases, data_streams, args.include_detail) |
| 89 | + aliases_payload = _format_aliases(aliases, args.include_detail) |
| 90 | + data_streams_payload = _format_data_streams(data_streams, args.include_detail) |
| 91 | + |
| 92 | + combined_payload = { |
| 93 | + 'indices': indices_payload, |
| 94 | + 'aliases': aliases_payload, |
| 95 | + 'data_streams': data_streams_payload, |
| 96 | + } |
| 97 | + |
| 98 | + formatted_payload = json.dumps(combined_payload, indent=2) |
| 99 | + response_text = 'Indices, aliases, and data streams information:\n' + formatted_payload |
| 100 | + return [{'type': 'text', 'text': response_text}] |
95 | 101 | except Exception as e: |
96 | 102 | return [{'type': 'text', 'text': f'Error listing indices: {str(e)}'}] |
97 | 103 |
|
98 | 104 |
|
| 105 | +def _format_indices( |
| 106 | + indices: list[dict], |
| 107 | + aliases: dict[str, list[str]], |
| 108 | + data_streams: list[dict], |
| 109 | + include_detail: bool, |
| 110 | +) -> list[Any]: |
| 111 | + excluded_indices: set[str] = set() |
| 112 | + |
| 113 | + for index_list in aliases.values(): |
| 114 | + excluded_indices.update(index_list) |
| 115 | + |
| 116 | + for stream in data_streams: |
| 117 | + indices_payload = stream.get('indices', []) |
| 118 | + if isinstance(indices_payload, list): |
| 119 | + for entry in indices_payload: |
| 120 | + if not isinstance(entry, dict): |
| 121 | + continue |
| 122 | + index_name = entry.get('index_name') |
| 123 | + if isinstance(index_name, str): |
| 124 | + excluded_indices.add(index_name) |
| 125 | + |
| 126 | + filtered = [ |
| 127 | + item for item in indices if item.get('index') not in excluded_indices |
| 128 | + ] |
| 129 | + if include_detail: |
| 130 | + return filtered |
| 131 | + return [item['index'] for item in filtered if 'index' in item] |
| 132 | + |
| 133 | + |
| 134 | +def _format_aliases( |
| 135 | + aliases: dict[str, list[str]], include_detail: bool |
| 136 | +) -> list[dict[str, Any]]: |
| 137 | + sorted_alias_items = sorted(aliases.items()) |
| 138 | + |
| 139 | + if include_detail: |
| 140 | + payload = [ |
| 141 | + {'alias': alias_name, 'indices': index_list} |
| 142 | + for alias_name, index_list in sorted_alias_items |
| 143 | + ] |
| 144 | + else: |
| 145 | + payload = [ |
| 146 | + {'alias': alias_name, 'index_count': len(index_list)} |
| 147 | + for alias_name, index_list in sorted_alias_items |
| 148 | + ] |
| 149 | + |
| 150 | + return payload |
| 151 | + |
| 152 | + |
| 153 | +def _format_data_streams( |
| 154 | + data_streams: list[dict], include_detail: bool |
| 155 | +) -> list[dict[str, Any]]: |
| 156 | + if include_detail: |
| 157 | + return data_streams |
| 158 | + |
| 159 | + payload = [] |
| 160 | + for stream in data_streams: |
| 161 | + simplified = {k: v for k, v in stream.items() if k != 'indices'} |
| 162 | + indices_payload = stream.get('indices', []) |
| 163 | + index_count = 0 |
| 164 | + if isinstance(indices_payload, list): |
| 165 | + index_count = sum( |
| 166 | + 1 |
| 167 | + for entry in indices_payload |
| 168 | + if isinstance(entry, dict) and isinstance(entry.get('index_name'), str) |
| 169 | + ) |
| 170 | + simplified['index_count'] = index_count |
| 171 | + payload.append(simplified) |
| 172 | + |
| 173 | + return payload |
| 174 | + |
| 175 | + |
99 | 176 | async def get_index_mapping_tool(args: GetIndexMappingArgs) -> list[dict]: |
100 | 177 | try: |
101 | 178 | check_tool_compatibility('IndexMappingTool', args) |
|
0 commit comments