"""
Chart Generator for Agents
"""
import json
import math
from pathlib import Path
from typing import Dict
class ChartGenerator:
"""
کلاس تولید نمودارهای SVG برای نمایش پیشرفت
"""
def __init__(self, base_path: str = "/workspace/unified_agent"):
self.base_path = Path(base_path)
def generate_doughnut_chart(self, app_name: str) -> str:
"""
تولید نمودار دونات پیشرفت
Args:
app_name: نام اپلیکیشن
Returns:
محتوای فایل SVG
"""
app_path = self.base_path / "apps" / app_name
progress_file = app_path / "PROGRESS.json"
# خواندن دادههای پیشرفت
if not progress_file.exists():
return self._create_empty_chart()
with open(progress_file, 'r', encoding='utf-8') as f:
data = json.load(f)
overall_progress = data.get('overall_progress', 0)
tasks = data.get('tasks', [])
# محاسبه آمار وضعیتها
status_counts = {
'completed': 0,
'in_progress': 0,
'not_started': 0,
'blocked': 0,
'cancelled': 0
}
for task in tasks:
status = task.get('status', 'not_started')
if status in status_counts:
status_counts[status] += 1
# تولید SVG
svg_content = self._create_doughnut_svg(
overall_progress,
status_counts,
app_name
)
# ذخیره در فایل
chart_path = app_path / "charts" / "progress_doughnut.svg"
chart_path.parent.mkdir(parents=True, exist_ok=True)
with open(chart_path, 'w', encoding='utf-8') as f:
f.write(svg_content)
return svg_content
def _create_doughnut_svg(self, progress: int, status_counts: Dict[str, int],
app_name: str) -> str:
"""ایجاد SVG نمودار دونات"""
# تنظیمات نمودار
width = 400
height = 400
cx = width // 2
cy = height // 2
outer_radius = 120
inner_radius = 60
# رنگها
colors = {
'completed': '#27ae60',
'in_progress': '#3498db',
'not_started': '#95a5a6',
'blocked': '#e74c3c',
'cancelled': '#e67e22'
}
# محاسبه مجموع
total = sum(status_counts.values())
if total == 0:
return self._create_empty_chart()
svg = f''''
return svg
def _create_arc_path(self, cx: int, cy: int, outer_radius: int,
inner_radius: int, start_angle: float,
end_angle: float) -> str:
"""ایجاد مسیر SVG برای قوس"""
# تبدیل زاویه به رادیان
start_rad = math.radians(start_angle)
end_rad = math.radians(end_angle)
# محاسبه نقاط
x1_outer = cx + outer_radius * math.cos(start_rad)
y1_outer = cy + outer_radius * math.sin(start_rad)
x2_outer = cx + outer_radius * math.cos(end_rad)
y2_outer = cy + outer_radius * math.sin(end_rad)
x1_inner = cx + inner_radius * math.cos(start_rad)
y1_inner = cy + inner_radius * math.sin(start_rad)
x2_inner = cx + inner_radius * math.cos(end_rad)
y2_inner = cy + inner_radius * math.sin(end_rad)
# تعیین large-arc-flag
large_arc = 1 if (end_angle - start_angle) > 180 else 0
# ساخت مسیر
path = f"M {x1_outer:.2f},{y1_outer:.2f} "
path += f"A {outer_radius},{outer_radius} 0 {large_arc},1 {x2_outer:.2f},{y2_outer:.2f} "
path += f"L {x2_inner:.2f},{y2_inner:.2f} "
path += f"A {inner_radius},{inner_radius} 0 {large_arc},0 {x1_inner:.2f},{y1_inner:.2f} "
path += "Z"
return path
def _create_empty_chart(self) -> str:
"""ایجاد نمودار خالی"""
return ''''''
def generate_gantt_chart(self, app_name: str) -> str:
"""
تولید نمودار گانت برای تسکها
Args:
app_name: نام اپلیکیشن
Returns:
محتوای فایل SVG
"""
app_path = self.base_path / "apps" / app_name
progress_file = app_path / "PROGRESS.json"
if not progress_file.exists():
return self._create_empty_chart()
with open(progress_file, 'r', encoding='utf-8') as f:
data = json.load(f)
tasks = data.get('tasks', [])
if not tasks:
return self._create_empty_chart()
# تنظیمات نمودار
width = 800
row_height = 40
header_height = 60
height = header_height + (len(tasks) * row_height) + 40
svg = f''''
return svg
def generate_burndown_chart(self, app_name: str) -> str:
"""
تولید نمودار Burndown
Args:
app_name: نام اپلیکیشن
Returns:
محتوای فایل SVG
"""
# این متد میتواند در آینده پیادهسازی شود
# برای نمایش روند کاهش تسکهای باقیمانده در طول زمان
pass
def save_all_charts(self, app_name: str):
"""ذخیره تمام نمودارها برای یک اپلیکیشن"""
# نمودار دونات
self.generate_doughnut_chart(app_name)
# نمودار گانت
gantt_svg = self.generate_gantt_chart(app_name)
gantt_path = self.base_path / "apps" / app_name / "charts" / "gantt_chart.svg"
gantt_path.parent.mkdir(parents=True, exist_ok=True)
with open(gantt_path, 'w', encoding='utf-8') as f:
f.write(gantt_svg)
# نمونه استفاده
if __name__ == "__main__":
generator = ChartGenerator()
# تولید نمودارها برای patient_chatbot
generator.save_all_charts("patient_chatbot")
print("نمودارها با موفقیت ایجاد شدند")