-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_dashboards.py
More file actions
executable file
·275 lines (229 loc) · 10.5 KB
/
Copy pathgenerate_dashboards.py
File metadata and controls
executable file
·275 lines (229 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python3
"""
Kibana Dashboard Generator - New Clean Architecture
This is the new main entry point using the modular architecture.
Generates Kibana dashboards from Swagger API endpoints.
"""
import argparse
import json
import logging
import os
import sys
from datetime import datetime
from pathlib import Path
# Import new modules only
from src.core.config import AppConfig
from src.services.kibana_client import KibanaClient
from src.services.swagger_service import SwaggerService
from src.analyzers.endpoint_analyzer import EndpointAnalyzer
from src.generators.dashboard_generator import DashboardGenerator
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def main():
"""Main entry point for dashboard generation"""
parser = argparse.ArgumentParser(
description='Generate Kibana dashboards from Swagger API endpoints (New Architecture)',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Using environment variables
export KIBANA_URL="http://localhost:5601"
export KIBANA_API_KEY="your-api-key"
python generate_dashboards.py swagger.json
# Generate only main dashboard
python generate_dashboards.py swagger.json http://localhost:5601 \\
--username elastic --password PASSWORD --api-key YOUR_KEY \\
--index-pattern "logs*" --mode main --verbose
# Generate all controller dashboards only
python generate_dashboards.py swagger.json \\
--kibana-url http://localhost:5601 \\
--api-key YOUR_KEY \\
--index-pattern "logs*" \\
--mode all \\
--verbose
# Generate both main and controller dashboards (default)
python generate_dashboards.py swagger.json \\
--kibana-url http://localhost:5601 \\
--api-key YOUR_KEY \\
--index-pattern "logs*" \\
--mode both \\
--output results.json \\
--verbose
"""
)
parser.add_argument(
'swagger_file',
help='Path to Swagger JSON file or URL'
)
parser.add_argument(
'kibana_url',
nargs='?',
help='Kibana URL (e.g., http://localhost:5601). Can also use --kibana-url or KIBANA_URL env var'
)
parser.add_argument(
'--kibana-url',
dest='kibana_url_flag',
help='Kibana URL (alternative to positional argument). Can also use KIBANA_URL env var'
)
parser.add_argument(
'--username',
help='Kibana username (or use KIBANA_USERNAME env var)'
)
parser.add_argument(
'--password',
help='Kibana password (or use KIBANA_PASSWORD env var)'
)
parser.add_argument(
'--api-key',
help='Kibana API key (or use KIBANA_API_KEY env var)'
)
parser.add_argument(
'--index-pattern',
default='logs*',
help='Elasticsearch index pattern (default: logs*)'
)
parser.add_argument(
'--output',
help='Output file for generation results (JSON format)'
)
parser.add_argument(
'--mode',
choices=['all', 'main', 'both'],
default='both',
help='Dashboard generation mode: "all" (controller dashboards only), "main" (main dashboard only), or "both" (default: both)'
)
parser.add_argument(
'--verbose', '-v',
action='store_true',
help='Enable verbose logging'
)
args = parser.parse_args()
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
logger.debug("Verbose logging enabled")
try:
logger.info("Loading configuration...")
kibana_url = args.kibana_url or args.kibana_url_flag
config = AppConfig.load(
kibana_url=kibana_url,
kibana_username=args.username,
kibana_password=args.password,
kibana_api_key=args.api_key,
index_pattern=args.index_pattern
)
if not config.kibana.url:
logger.error("Kibana URL is required. Provide it via --kibana-url or set KIBANA_URL environment variable.")
return 1
logger.info(f"Kibana URL: {config.kibana.url}")
logger.info(f"Index pattern: {config.elasticsearch.index_pattern}")
logger.info(f"Application zone: {config.application.application_zone}")
logger.info(f"Parsing Swagger file: {args.swagger_file}")
swagger_service = SwaggerService(args.swagger_file)
api_info = swagger_service.get_api_info()
logger.info(f"API: {api_info['title']} v{api_info['version']}")
endpoints = swagger_service.extract_endpoints()
logger.info(f"Extracted {len(endpoints)} API endpoints")
logger.info("Connecting to Kibana...")
with KibanaClient(config.kibana) as client:
if not client.test_connection():
logger.error("Failed to connect to Kibana. Please check your credentials and URL.")
return 1
logger.info(f"Checking Elasticsearch indices for pattern: {config.elasticsearch.index_pattern}")
if not client.check_elasticsearch_indices(config.elasticsearch.index_pattern):
logger.warning("No matching indices found. Dashboards may not show data until logs are present.")
logger.info("Grouping endpoints by tag...")
analyzer = EndpointAnalyzer()
grouped_endpoints = analyzer.group_endpoints_by_controller(endpoints)
logger.info(f"Found {len(grouped_endpoints)} tag groups")
stats = analyzer.get_statistics(endpoints)
logger.info(f"Endpoint statistics: {stats['by_method']}")
logger.info(f"Generating dashboards (mode: {args.mode})...")
generator = DashboardGenerator(client, config.application)
results = {}
if args.mode in ['all', 'both']:
logger.info("Generating controller dashboards...")
for controller, controller_endpoints in grouped_endpoints.items():
logger.info(f"Processing controller '{controller}' with {len(controller_endpoints)} endpoints")
dashboard_result = generator.generate_dashboard_per_controller(
controller,
controller_endpoints,
config.elasticsearch.index_pattern
)
if dashboard_result:
results[controller] = dashboard_result
else:
logger.warning(f"Failed to generate dashboard for tag: {controller}")
else:
logger.info("Skipping controller dashboards (mode: main)")
summary = {
'api_info': api_info,
'generation_timestamp': datetime.now().isoformat(),
'configuration': {
'kibana_url': config.kibana.url,
'index_pattern': config.elasticsearch.index_pattern,
'application_zone': config.application.application_zone
},
'statistics': stats,
'total_tags': len(results),
'total_dashboards': len(results),
'total_visualizations': sum(
len(r['visualizations']) for r in results.values()
),
'results_by_tag': {}
}
for controller, result in results.items():
summary['results_by_tag'][controller] = {
'endpoint_count': result['endpoint_count'],
'visualization_count': len(result['visualizations']),
'dashboard_created': result['dashboard'] is not None
}
if args.output:
output_path = Path(args.output)
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(summary, f, indent=2, ensure_ascii=False)
logger.info(f"Results saved to {output_path}")
else:
print(json.dumps(summary, indent=2, ensure_ascii=False))
logger.info("=" * 60)
logger.info("Dashboard generation complete!")
logger.info("=" * 60)
if args.mode in ['all', 'both']:
logger.info(f" Total tags processed: {summary['total_tags']}")
logger.info(f" Total dashboards created: {summary['total_dashboards']}")
logger.info(f" Total visualizations created: {summary['total_visualizations']}")
logger.info("")
main_dashboard_result = None
if args.mode in ['main', 'both']:
logger.info("Creating main dashboard...")
controllers = swagger_service.extract_controllers()
main_dashboard_result = generator.create_main_dashboard(
controllers,
config.elasticsearch.index_pattern
)
if main_dashboard_result:
logger.info(f"Main dashboard created successfully with {main_dashboard_result.get('visualizations_count', 0)} visualizations")
else:
logger.warning("Failed to create main dashboard")
else:
logger.info("Skipping main dashboard (mode: all)")
if args.mode in ['all', 'both'] and results:
for controller, info in summary['results_by_tag'].items():
status = "✓" if info['dashboard_created'] else "✗"
logger.info(
f" {status} {controller}: {info['endpoint_count']} endpoints, "
f"{info['visualization_count']} visualizations"
)
logger.info("=" * 60)
return 0
except KeyboardInterrupt:
logger.info("\nOperation cancelled by user")
return 130
except Exception as e:
logger.error(f"Error during dashboard generation: {e}", exc_info=args.verbose)
return 1
if __name__ == '__main__':
sys.exit(main())