-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.py
More file actions
440 lines (357 loc) · 14.4 KB
/
run.py
File metadata and controls
440 lines (357 loc) · 14.4 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/env python3
"""
FTEX CLI - Unified Entry Point
==============================
Single command interface for all FTEX operations.
Commands:
python3 run.py test # Test API connection
python3 run.py extract --days 180 # Extract tickets
python3 run.py analyze # Run AI analysis + reports
python3 run.py issues # Issue-centric analysis (streaming)
python3 run.py full --days 180 # Complete pipeline
"""
import argparse
import subprocess
import sys
import os
from pathlib import Path
# Add src to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
# Load environment variables
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass
# Rich for beautiful output
try:
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich import box
console = Console()
RICH = True
except ImportError:
console = None
RICH = False
# Config from environment
FRESHDESK_API_KEY = os.getenv('FRESHDESK_API_KEY', '')
FRESHDESK_GROUP_ID = os.getenv('FRESHDESK_GROUP_ID', '')
FRESHDESK_DOMAIN = os.getenv('FRESHDESK_DOMAIN', '')
def print_header():
if console:
console.print(Panel.fit(
"[bold cyan]FTEX[/bold cyan] - Freshdesk Ticket Export & Analysis\n"
"[dim]Self-Validating AI-Powered Analysis[/dim]",
border_style="cyan"
))
console.print()
else:
print("\n" + "="*60)
print("FTEX - Freshdesk Ticket Export & Analysis")
print("="*60 + "\n")
def print_success(msg):
if console:
console.print(f"[green]✓[/green] {msg}")
else:
print(f"✓ {msg}")
def print_error(msg):
if console:
console.print(f"[red]✗[/red] {msg}")
else:
print(f"✗ {msg}")
def print_step(step, total, msg):
if console:
console.print(f"\n[bold blue]Step {step}/{total}:[/bold blue] {msg}")
else:
print(f"\n[Step {step}/{total}] {msg}")
def run_command(cmd, description=None):
if description and console:
console.print(f" [dim]→ {description}[/dim]")
elif description:
print(f" → {description}")
try:
result = subprocess.run(cmd, capture_output=False)
return result.returncode == 0
except Exception as e:
print_error(f"Command failed: {e}")
return False
def get_script_path(name):
"""Get script path (organized or flat structure)."""
paths = {
'extractor': 'src/extraction/freshdesk_extractor_v2.py',
'test_api': 'src/extraction/test_freshdesk_api.py',
'analyze': 'src/analysis/analyze.py',
}
path = paths.get(name)
if path and Path(path).exists():
return path
# Flat fallback
flat = {
'extractor': 'freshdesk_extractor_v2.py',
'test_api': 'test_freshdesk_api.py',
'analyze': 'analyze.py',
}
return flat.get(name, name)
def cmd_test(args):
"""Test Freshdesk API."""
if not args.api_key:
print_error("API key required. Use --api-key or set FRESHDESK_API_KEY")
return False
script = get_script_path('test_api')
cmd = ['python3', script, '--api-key', args.api_key]
if args.domain:
cmd.extend(['--domain', args.domain])
return run_command(cmd, "Testing Freshdesk API...")
def cmd_extract(args):
"""Extract tickets."""
if not args.api_key:
print_error("API key required. Use --api-key or set FRESHDESK_API_KEY")
return False
script = get_script_path('extractor')
cmd = ['python3', script, '--api-key', args.api_key, '--days', str(args.days)]
if args.group_id:
cmd.extend(['--group-id', str(args.group_id)])
if args.no_attachments:
cmd.append('--no-attachments')
if args.resume:
cmd.append('--resume')
print_step(1, 1, f"Extracting tickets (last {args.days} days)")
return run_command(cmd)
def cmd_analyze(args):
"""Run analysis."""
if not Path(args.input).exists():
print_error(f"File not found: {args.input}")
print_error("Run 'python3 run.py extract' first")
return False
# Try direct import
try:
from analysis.analyze import main as analyze_main
# Call main() with keyword arguments directly
analyze_main(
input_path=args.input,
output_dir=args.output,
use_ai=not args.no_ai,
clear_cache=args.clear_cache
)
return True
except ImportError:
# Subprocess fallback
script = get_script_path('analyze')
cmd = ['python3', script, '--input', args.input, '--output', args.output]
if args.no_ai:
cmd.append('--no-ai')
if args.clear_cache:
cmd.append('--clear-cache')
return run_command(cmd, "Running analysis...")
def cmd_issues(args):
"""Run issue-centric analysis (streaming, conversation-aware)."""
if not Path(args.input).exists():
print_error(f"File not found: {args.input}")
print_error("Run 'python3 run.py extract' first")
return False
# Clear checkpoint if requested
if args.clear_checkpoint:
checkpoint_path = Path(args.output) / 'ai_enrichment_checkpoint.json'
if checkpoint_path.exists():
checkpoint_path.unlink()
if console:
console.print("[yellow]✓ Cleared AI enrichment checkpoint[/yellow]")
else:
print("✓ Cleared AI enrichment checkpoint")
# Try direct import
try:
from analysis.issue_analyzer import IssueAnalyzer
analyzer = IssueAnalyzer(
input_path=args.input,
output_dir=args.output,
use_ai=not args.no_ai,
high_precision=not args.low_precision,
sample_size=args.sample,
)
# Run with progress if Rich available
if console:
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TaskProgressColumn, TimeElapsedColumn
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TaskProgressColumn(),
TimeElapsedColumn(),
console=console
) as progress:
task = progress.add_task("Analyzing issues...", total=100)
def update_progress(msg, pct):
progress.update(task, completed=pct, description=msg)
results = analyzer.run(progress_callback=update_progress)
progress.update(task, completed=100)
else:
results = analyzer.run()
# Print results
stats = results.get('stats', {})
outputs = results.get('outputs', {})
if console:
from rich.table import Table
from rich import box
table = Table(show_header=False, box=box.SIMPLE)
table.add_column("", style="cyan")
table.add_column("", justify="right")
table.add_row("Tickets Processed", f"{stats.get('tickets_processed', 0):,}")
table.add_row("Issue Clusters", f"{stats.get('clusters_created', 0):,}")
table.add_row("Actionable Issues", f"[green]{stats.get('actionable_issues', 0):,}[/green]")
table.add_row("Time", f"{stats.get('elapsed_seconds', 0):.1f}s")
console.print()
console.print(Panel(table, title="📊 Issue Analysis Results", border_style="green"))
if outputs:
console.print()
console.print(Panel(
"\n".join([f" 📄 [cyan]{name}[/cyan]: {path}" for name, path in outputs.items()]),
title="📁 Generated Files",
border_style="green",
))
console.print()
console.print(Panel("[bold green]✓ Issue analysis complete[/bold green]", border_style="green"))
else:
print(f"\nResults: {stats}")
print(f"Outputs: {outputs}")
return True
except ImportError as e:
print_error(f"Import error: {e}")
print_error("Make sure all dependencies are installed: pip install ijson sentence-transformers")
return False
def cmd_full(args):
"""Full pipeline: extract → analyze."""
print_header()
if console:
console.print(Panel("[bold]Full Pipeline[/bold]: Extract → Analyze → Report", border_style="blue"))
# Step 1: Extract
if not args.skip_extract:
print_step(1, 2, f"Extracting tickets (last {args.days} days)")
if not args.api_key:
print_error("API key required")
return False
script = get_script_path('extractor')
cmd = ['python3', script, '--api-key', args.api_key, '--days', str(args.days)]
if args.group_id:
cmd.extend(['--group-id', str(args.group_id)])
if args.no_attachments:
cmd.append('--no-attachments')
if not run_command(cmd):
print_error("Extraction failed")
return False
print_success("Extraction complete")
else:
if console:
console.print(" [yellow]⏭[/yellow] Skipping extraction")
else:
print(" ⏭ Skipping extraction")
# Step 2: Analyze
print_step(2, 2, "Running AI analysis + generating reports")
class AnalyzeArgs:
input = 'output/tickets.json'
output = 'reports'
no_ai = getattr(args, 'no_ai', False)
clear_cache = False
if not cmd_analyze(AnalyzeArgs()):
print_error("Analysis failed")
return False
# Summary
if console:
console.print()
summary = Table(title="Pipeline Complete", box=box.ROUNDED, border_style="green")
summary.add_column("Output", style="cyan")
summary.add_column("Location")
summary.add_row("Extracted Tickets", "output/tickets.json")
summary.add_row("Excel Report", "reports/analysis_report.xlsx")
summary.add_row("Summary (MD)", "reports/analysis_summary.md")
summary.add_row("Summary (PDF)", "reports/analysis_summary.pdf")
summary.add_row("Raw Data", "reports/analysis_data.json")
console.print(summary)
else:
print("\n" + "="*60)
print("PIPELINE COMPLETE")
print("="*60)
print("\nOutputs:")
print(" 📊 output/tickets.json")
print(" 📋 reports/analysis_report.xlsx")
print(" 📄 reports/analysis_summary.md")
print(" 📄 reports/analysis_summary.pdf")
return True
def main():
parser = argparse.ArgumentParser(
description='FTEX - Freshdesk Ticket Export & Analysis',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python3 run.py test --api-key YOUR_KEY
python3 run.py extract --days 180 --group-id 12345
python3 run.py analyze
python3 run.py analyze --no-ai
python3 run.py full --days 180
Environment Variables:
FRESHDESK_DOMAIN Your Freshdesk subdomain
FRESHDESK_API_KEY Your API key
FRESHDESK_GROUP_ID Default group ID
OLLAMA_URL Ollama server URL
OLLAMA_MODEL LLM model name
"""
)
subparsers = parser.add_subparsers(dest='command', help='Commands')
# TEST
test = subparsers.add_parser('test', help='Test Freshdesk API')
test.add_argument('--api-key', '-k', default=FRESHDESK_API_KEY)
test.add_argument('--domain', '-d', default=FRESHDESK_DOMAIN)
# EXTRACT
extract = subparsers.add_parser('extract', help='Extract tickets')
extract.add_argument('--api-key', '-k', default=FRESHDESK_API_KEY)
extract.add_argument('--days', '-d', type=int, default=180)
extract.add_argument('--group-id', '-g', type=int,
default=int(FRESHDESK_GROUP_ID) if FRESHDESK_GROUP_ID else None)
extract.add_argument('--no-attachments', action='store_true')
extract.add_argument('--resume', action='store_true')
# ANALYZE
analyze = subparsers.add_parser('analyze', help='Run AI analysis')
analyze.add_argument('--input', '-i', default='output/tickets.json')
analyze.add_argument('--output', '-o', default='reports')
analyze.add_argument('--no-ai', action='store_true')
analyze.add_argument('--clear-cache', action='store_true')
# ISSUES (streaming, conversation-aware)
issues = subparsers.add_parser('issues', help='Issue-centric analysis (streaming)')
issues.add_argument('--input', '-i', default='output/tickets.json')
issues.add_argument('--output', '-o', default='reports')
issues.add_argument('--sample', '-s', type=int, help='Sample N tickets (for testing)')
issues.add_argument('--no-ai', action='store_true', help='Disable AI enrichment')
issues.add_argument('--low-precision', action='store_true',
help='Use low precision for new issue detection')
issues.add_argument('--clear-checkpoint', action='store_true',
help='Clear AI enrichment checkpoint and start fresh')
# FULL
full = subparsers.add_parser('full', help='Full pipeline')
full.add_argument('--api-key', '-k', default=FRESHDESK_API_KEY)
full.add_argument('--days', '-d', type=int, default=180)
full.add_argument('--group-id', '-g', type=int,
default=int(FRESHDESK_GROUP_ID) if FRESHDESK_GROUP_ID else None)
full.add_argument('--no-attachments', action='store_true')
full.add_argument('--skip-extract', action='store_true')
full.add_argument('--no-ai', action='store_true')
args = parser.parse_args()
if not args.command:
print_header()
parser.print_help()
return
if args.command == 'test':
success = cmd_test(args)
elif args.command == 'extract':
success = cmd_extract(args)
elif args.command == 'analyze':
success = cmd_analyze(args)
elif args.command == 'issues':
success = cmd_issues(args)
elif args.command == 'full':
success = cmd_full(args)
else:
parser.print_help()
success = False
sys.exit(0 if success else 1)
if __name__ == '__main__':
main()