File size: 33,602 Bytes
8e4018d |
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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 |
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import datetime
from typing import Dict, List, Any, Union, Optional, Tuple
import calendar
from collections import Counter, defaultdict
from utils.logging import setup_logger
from utils.error_handling import handle_exceptions
from utils.storage import load_data, safe_get
from utils.config import FILE_PATHS, UI_COLORS
# Initialize logger
logger = setup_logger(__name__)
@handle_exceptions
def filter_data_by_time_period(data: List[Dict[str, Any]], time_period: str, timestamp_key: str = "created_at") -> List[Dict[str, Any]]:
"""
Filter data based on the selected time period
Args:
data: List of data items (tasks, notes, goals, etc.)
time_period: Time period to filter by ("Last 7 Days", "Last 30 Days", "Last 90 Days", "All Time")
timestamp_key: Key in the data items that contains the timestamp
Returns:
Filtered data items
"""
logger.debug(f"Filtering data by time period: {time_period}")
if time_period == "All Time" or not data:
return data
now = datetime.datetime.now()
if time_period == "Last 7 Days":
cutoff = now - datetime.timedelta(days=7)
elif time_period == "Last 30 Days":
cutoff = now - datetime.timedelta(days=30)
elif time_period == "Last 90 Days":
cutoff = now - datetime.timedelta(days=90)
else:
logger.warning(f"Unknown time period: {time_period}, returning all data")
return data
# Convert cutoff to timestamp if the data timestamps are stored as timestamps
cutoff_timestamp = cutoff.timestamp()
filtered_data = []
for item in data:
item_timestamp = item.get(timestamp_key)
if not item_timestamp:
continue
# Handle both datetime string and timestamp formats
if isinstance(item_timestamp, str):
try:
item_datetime = datetime.datetime.fromisoformat(item_timestamp.replace('Z', '+00:00'))
item_timestamp = item_datetime.timestamp()
except ValueError:
logger.warning(f"Could not parse timestamp: {item_timestamp}")
continue
if item_timestamp >= cutoff_timestamp:
filtered_data.append(item)
logger.debug(f"Filtered data from {len(data)} to {len(filtered_data)} items")
return filtered_data
@handle_exceptions
def create_completion_rate_chart(data: List[Dict[str, Any]], title: str = "Completion Rate",
completed_key: str = "completed") -> go.Figure:
"""
Create a pie chart showing completion rate
Args:
data: List of data items (tasks, goals, etc.)
title: Chart title
completed_key: Key in the data items that indicates completion status
Returns:
Plotly figure object
"""
logger.debug(f"Creating completion rate chart with {len(data)} items")
# Count completed and incomplete items
completed = sum(1 for item in data if safe_get(item, completed_key, False))
incomplete = len(data) - completed
# Calculate percentages
if data:
completed_pct = completed / len(data) * 100
incomplete_pct = 100 - completed_pct
else:
completed_pct = 0
incomplete_pct = 0
# Create labels with percentages
labels = [f"Completed ({completed_pct:.1f}%)", f"Incomplete ({incomplete_pct:.1f}%)"]
values = [completed, incomplete]
# Create pie chart
fig = go.Figure(data=[go.Pie(
labels=labels,
values=values,
hole=0.4,
marker_colors=[UI_COLORS["success"], UI_COLORS["warning"]]
)])
fig.update_layout(
title=title,
showlegend=True,
legend=dict(orientation="h", yanchor="bottom", y=-0.2, xanchor="center", x=0.5),
margin=dict(l=20, r=20, t=40, b=20),
height=300
)
return fig
@handle_exceptions
def create_status_distribution_chart(tasks: List[Dict[str, Any]], title: str = "Task Status Distribution") -> go.Figure:
"""
Create a bar chart showing the distribution of task statuses
Args:
tasks: List of task items
title: Chart title
Returns:
Plotly figure object
"""
logger.debug(f"Creating status distribution chart with {len(tasks)} tasks")
# Count tasks by status
status_counts = Counter(safe_get(task, "status", "To Do") for task in tasks)
# Define status order and colors
status_order = ["To Do", "In Progress", "Done"]
status_colors = [UI_COLORS["warning"], UI_COLORS["info"], UI_COLORS["success"]]
# Filter and sort statuses
statuses = []
counts = []
colors = []
for status in status_order:
if status in status_counts:
statuses.append(status)
counts.append(status_counts[status])
colors.append(status_colors[status_order.index(status)])
# Add any other statuses not in the predefined order
for status, count in status_counts.items():
if status not in status_order:
statuses.append(status)
counts.append(count)
colors.append(UI_COLORS["secondary"])
# Create bar chart
fig = go.Figure(data=[go.Bar(
x=statuses,
y=counts,
marker_color=colors,
text=counts,
textposition='auto',
)])
fig.update_layout(
title=title,
xaxis_title="Status",
yaxis_title="Number of Tasks",
margin=dict(l=20, r=20, t=40, b=20),
height=300
)
return fig
@handle_exceptions
def create_priority_distribution_chart(tasks: List[Dict[str, Any]], title: str = "Task Priority Distribution") -> go.Figure:
"""
Create a bar chart showing the distribution of task priorities
Args:
tasks: List of task items
title: Chart title
Returns:
Plotly figure object
"""
logger.debug(f"Creating priority distribution chart with {len(tasks)} tasks")
# Count tasks by priority
priority_counts = Counter(safe_get(task, "priority", "Medium") for task in tasks)
# Define priority order and colors
priority_order = ["Low", "Medium", "High", "Urgent"]
priority_colors = [UI_COLORS["success"], UI_COLORS["info"], UI_COLORS["warning"], UI_COLORS["danger"]]
# Filter and sort priorities
priorities = []
counts = []
colors = []
for priority in priority_order:
if priority in priority_counts:
priorities.append(priority)
counts.append(priority_counts[priority])
colors.append(priority_colors[priority_order.index(priority)])
# Add any other priorities not in the predefined order
for priority, count in priority_counts.items():
if priority not in priority_order:
priorities.append(priority)
counts.append(count)
colors.append(UI_COLORS["secondary"])
# Create bar chart
fig = go.Figure(data=[go.Bar(
x=priorities,
y=counts,
marker_color=colors,
text=counts,
textposition='auto',
)])
fig.update_layout(
title=title,
xaxis_title="Priority",
yaxis_title="Number of Tasks",
margin=dict(l=20, r=20, t=40, b=20),
height=300
)
return fig
@handle_exceptions
def create_time_series_chart(data: List[Dict[str, Any]], title: str = "Activity Over Time",
timestamp_key: str = "created_at", group_by: str = "day") -> go.Figure:
"""
Create a time series chart showing activity over time
Args:
data: List of data items (tasks, notes, goals, activity, etc.)
title: Chart title
timestamp_key: Key in the data items that contains the timestamp
group_by: Time grouping ("day", "week", "month")
Returns:
Plotly figure object
"""
logger.debug(f"Creating time series chart with {len(data)} items, grouped by {group_by}")
if not data:
# Return empty chart if no data
fig = go.Figure()
fig.update_layout(title=title, height=300)
return fig
# Convert timestamps to datetime objects
dates = []
for item in data:
timestamp = item.get(timestamp_key)
if not timestamp:
continue
# Handle both datetime string and timestamp formats
if isinstance(timestamp, str):
try:
date = datetime.datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
except ValueError:
logger.warning(f"Could not parse timestamp: {timestamp}")
continue
else:
date = datetime.datetime.fromtimestamp(timestamp)
dates.append(date)
if not dates:
# Return empty chart if no valid dates
fig = go.Figure()
fig.update_layout(title=title, height=300)
return fig
# Group dates by the specified time period
date_counts = defaultdict(int)
for date in dates:
if group_by == "day":
key = date.strftime("%Y-%m-%d")
elif group_by == "week":
# Get the start of the week (Monday)
start_of_week = date - datetime.timedelta(days=date.weekday())
key = start_of_week.strftime("%Y-%m-%d")
elif group_by == "month":
key = date.strftime("%Y-%m")
else:
logger.warning(f"Unknown group_by value: {group_by}, using day")
key = date.strftime("%Y-%m-%d")
date_counts[key] += 1
# Sort dates
sorted_dates = sorted(date_counts.keys())
counts = [date_counts[date] for date in sorted_dates]
# Format x-axis labels based on grouping
if group_by == "day":
x_labels = [datetime.datetime.strptime(d, "%Y-%m-%d").strftime("%b %d") for d in sorted_dates]
elif group_by == "week":
x_labels = [f"Week of {datetime.datetime.strptime(d, '%Y-%m-%d').strftime('%b %d')}" for d in sorted_dates]
elif group_by == "month":
x_labels = [datetime.datetime.strptime(d, "%Y-%m").strftime("%b %Y") for d in sorted_dates]
else:
x_labels = sorted_dates
# Create line chart
fig = go.Figure(data=go.Scatter(
x=x_labels,
y=counts,
mode='lines+markers',
line=dict(color=UI_COLORS["primary"], width=2),
marker=dict(size=8, color=UI_COLORS["primary"]),
fill='tozeroy',
fillcolor=f"rgba({int(UI_COLORS['primary'][1:3], 16)}, {int(UI_COLORS['primary'][3:5], 16)}, {int(UI_COLORS['primary'][5:7], 16)}, 0.2)"
))
fig.update_layout(
title=title,
xaxis_title="Date",
yaxis_title="Count",
margin=dict(l=20, r=20, t=40, b=20),
height=300
)
return fig
@handle_exceptions
def create_activity_heatmap(data: List[Dict[str, Any]], title: str = "Activity Heatmap",
timestamp_key: str = "created_at") -> go.Figure:
"""
Create a heatmap showing activity by day of week and hour of day
Args:
data: List of data items (tasks, notes, goals, activity, etc.)
title: Chart title
timestamp_key: Key in the data items that contains the timestamp
Returns:
Plotly figure object
"""
logger.debug(f"Creating activity heatmap with {len(data)} items")
if not data:
# Return empty chart if no data
fig = go.Figure()
fig.update_layout(title=title, height=400)
return fig
# Convert timestamps to datetime objects and extract day of week and hour
day_hour_counts = np.zeros((7, 24)) # 7 days, 24 hours
for item in data:
timestamp = item.get(timestamp_key)
if not timestamp:
continue
# Handle both datetime string and timestamp formats
if isinstance(timestamp, str):
try:
date = datetime.datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
except ValueError:
logger.warning(f"Could not parse timestamp: {timestamp}")
continue
else:
date = datetime.datetime.fromtimestamp(timestamp)
# Get day of week (0 = Monday, 6 = Sunday) and hour
day_of_week = date.weekday()
hour = date.hour
# Increment count for this day and hour
day_hour_counts[day_of_week, hour] += 1
# Create day and hour labels
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
hours = [f"{h:02d}:00" for h in range(24)]
# Create heatmap
fig = go.Figure(data=go.Heatmap(
z=day_hour_counts,
x=hours,
y=days,
colorscale='Viridis',
hoverongaps=False,
colorbar=dict(title="Count")
))
fig.update_layout(
title=title,
xaxis_title="Hour of Day",
yaxis_title="Day of Week",
margin=dict(l=20, r=20, t=40, b=20),
height=400
)
return fig
@handle_exceptions
def create_calendar_heatmap(data: List[Dict[str, Any]], title: str = "Calendar Heatmap",
timestamp_key: str = "created_at", year: Optional[int] = None) -> go.Figure:
"""
Create a calendar heatmap showing activity by day of year
Args:
data: List of data items (tasks, notes, goals, activity, etc.)
title: Chart title
timestamp_key: Key in the data items that contains the timestamp
year: Year to display (defaults to current year)
Returns:
Plotly figure object
"""
logger.debug(f"Creating calendar heatmap with {len(data)} items")
if not data:
# Return empty chart if no data
fig = go.Figure()
fig.update_layout(title=title, height=500)
return fig
# Use current year if not specified
if year is None:
year = datetime.datetime.now().year
# Convert timestamps to datetime objects and count activities by date
date_counts = defaultdict(int)
for item in data:
timestamp = item.get(timestamp_key)
if not timestamp:
continue
# Handle both datetime string and timestamp formats
if isinstance(timestamp, str):
try:
date = datetime.datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
except ValueError:
logger.warning(f"Could not parse timestamp: {timestamp}")
continue
else:
date = datetime.datetime.fromtimestamp(timestamp)
# Only include dates from the specified year
if date.year == year:
date_key = date.strftime("%Y-%m-%d")
date_counts[date_key] += 1
# Create a DataFrame with all days of the year
start_date = datetime.date(year, 1, 1)
end_date = datetime.date(year, 12, 31)
all_dates = [start_date + datetime.timedelta(days=i) for i in range((end_date - start_date).days + 1)]
dates = [d.strftime("%Y-%m-%d") for d in all_dates]
counts = [date_counts.get(d, 0) for d in dates]
weekdays = [d.weekday() for d in all_dates] # 0 = Monday, 6 = Sunday
months = [d.month for d in all_dates]
days = [d.day for d in all_dates]
# Create a DataFrame for plotting
df = pd.DataFrame({
'date': dates,
'count': counts,
'weekday': weekdays,
'month': months,
'day': days
})
# Create a custom calendar layout
# We'll create a subplot for each month
fig = make_subplots(rows=4, cols=3, subplot_titles=[calendar.month_name[i] for i in range(1, 13)])
# Define color scale
max_count = max(counts) if counts else 1
colorscale = px.colors.sequential.Viridis
# Add data for each month
for month in range(1, 13):
month_data = df[df['month'] == month]
# Create a 7x6 grid for each month (7 days per week, up to 6 weeks per month)
month_grid = np.zeros((7, 6))
month_grid.fill(np.nan) # Fill with NaN to hide empty cells
# Get the first day of the month and its weekday
first_day = month_data.iloc[0]
first_weekday = first_day['weekday']
# Fill the grid with activity counts
for _, row in month_data.iterrows():
day = row['day'] - 1 # 0-indexed day
weekday = row['weekday'] # 0 = Monday, 6 = Sunday
week = (day + first_weekday) // 7
if week < 6: # Only show up to 6 weeks
month_grid[weekday, week] = row['count']
# Add heatmap for this month
row_idx = (month - 1) // 3 + 1
col_idx = (month - 1) % 3 + 1
fig.add_trace(
go.Heatmap(
z=month_grid,
x=list(range(6)), # Weeks
y=['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], # Days
colorscale=colorscale,
showscale=month == 12, # Only show colorbar for December
zmin=0,
zmax=max_count
),
row=row_idx,
col=col_idx
)
# Update layout
fig.update_layout(
title=title,
height=800,
margin=dict(l=20, r=20, t=40, b=20),
)
# Hide x-axis labels and ticks
fig.update_xaxes(showticklabels=False, showgrid=False)
return fig
@handle_exceptions
def create_tags_distribution_chart(data: List[Dict[str, Any]], title: str = "Tags Distribution",
tags_key: str = "tags", top_n: int = 10) -> go.Figure:
"""
Create a bar chart showing the distribution of tags
Args:
data: List of data items (tasks, notes, goals, etc.)
title: Chart title
tags_key: Key in the data items that contains the tags
top_n: Number of top tags to display
Returns:
Plotly figure object
"""
logger.debug(f"Creating tags distribution chart with {len(data)} items")
# Extract all tags
all_tags = []
for item in data:
tags = safe_get(item, tags_key, [])
if tags and isinstance(tags, list):
all_tags.extend(tags)
if not all_tags:
# Return empty chart if no tags
fig = go.Figure()
fig.update_layout(title=title, height=300)
return fig
# Count tags
tag_counts = Counter(all_tags)
# Get top N tags
top_tags = tag_counts.most_common(top_n)
tags = [tag for tag, _ in top_tags]
counts = [count for _, count in top_tags]
# Create bar chart
fig = go.Figure(data=[go.Bar(
x=tags,
y=counts,
marker_color=UI_COLORS["primary"],
text=counts,
textposition='auto',
)])
fig.update_layout(
title=title,
xaxis_title="Tag",
yaxis_title="Count",
margin=dict(l=20, r=20, t=40, b=20),
height=300
)
return fig
@handle_exceptions
def create_sentiment_chart(notes: List[Dict[str, Any]], title: str = "Notes Sentiment Analysis",
content_key: str = "content", timestamp_key: str = "created_at") -> go.Figure:
"""
Create a line chart showing sentiment analysis of notes over time
Args:
notes: List of note items
title: Chart title
content_key: Key in the note items that contains the content
timestamp_key: Key in the note items that contains the timestamp
Returns:
Plotly figure object
"""
logger.debug(f"Creating sentiment chart with {len(notes)} notes")
if not notes:
# Return empty chart if no notes
fig = go.Figure()
fig.update_layout(title=title, height=300)
return fig
# Sort notes by timestamp
sorted_notes = sorted(notes, key=lambda x: safe_get(x, timestamp_key, 0))
# Analyze sentiment for each note
dates = []
sentiments = []
texts = []
for note in sorted_notes:
content = safe_get(note, content_key, "")
timestamp = safe_get(note, timestamp_key, None)
title_text = safe_get(note, "title", "Untitled Note")
if not content or not timestamp:
continue
# Analyze sentiment (returns a value between -1 and 1)
try:
from utils.ai_models import analyze_sentiment
sentiment = analyze_sentiment(content)
except Exception as e:
logger.warning(f"Error analyzing sentiment: {str(e)}")
# Use a random sentiment between -0.5 and 0.5 as fallback
sentiment = (np.random.random() - 0.5)
# Convert timestamp to datetime
if isinstance(timestamp, str):
try:
date = datetime.datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
except ValueError:
logger.warning(f"Could not parse timestamp: {timestamp}")
continue
else:
date = datetime.datetime.fromtimestamp(timestamp)
dates.append(date.strftime("%Y-%m-%d"))
sentiments.append(sentiment)
texts.append(f"<b>{title_text}</b><br>{content[:100]}..." if len(content) > 100 else f"<b>{title_text}</b><br>{content}")
if not dates:
# Return empty chart if no valid notes
fig = go.Figure()
fig.update_layout(title=title, height=300)
return fig
# Create line chart
fig = go.Figure(data=go.Scatter(
x=dates,
y=sentiments,
mode='lines+markers',
line=dict(color=UI_COLORS["primary"], width=2),
marker=dict(
size=10,
color=sentiments,
colorscale=[[0, UI_COLORS["danger"]], [0.5, UI_COLORS["warning"]], [1, UI_COLORS["success"]]],
cmin=-1,
cmax=1,
showscale=True,
colorbar=dict(title="Sentiment")
),
text=texts,
hoverinfo="text+x+y"
))
fig.update_layout(
title=title,
xaxis_title="Date",
yaxis_title="Sentiment Score",
yaxis=dict(range=[-1, 1]),
margin=dict(l=20, r=20, t=40, b=20),
height=400
)
# Add a horizontal line at y=0
fig.add_shape(
type="line",
x0=dates[0],
y0=0,
x1=dates[-1],
y1=0,
line=dict(color="gray", width=1, dash="dash")
)
return fig
@handle_exceptions
def create_model_usage_distribution(activities: List[Dict[str, Any]], title: str = "AI Model Usage Distribution") -> go.Figure:
"""
Create a pie chart showing the distribution of AI model usage
Args:
activities: List of activity items
title: Chart title
Returns:
Plotly figure object
"""
logger.debug(f"Creating model usage distribution chart with {len(activities)} activities")
# Filter activities related to AI model usage
ai_activities = []
for activity in activities:
activity_type = safe_get(activity, "type", "")
if "ai_" in activity_type or "model_" in activity_type:
ai_activities.append(activity)
if not ai_activities:
# Return empty chart if no AI activities
fig = go.Figure()
fig.update_layout(title=title, height=300)
return fig
# Count model usage by type
model_counts = defaultdict(int)
for activity in ai_activities:
activity_type = safe_get(activity, "type", "unknown")
# Clean up the activity type for better display
model_type = activity_type.replace("ai_", "").replace("model_", "").replace("_", " ").title()
model_counts[model_type] += 1
# Prepare data for pie chart
models = list(model_counts.keys())
counts = list(model_counts.values())
# Create color map for models
colors = px.colors.qualitative.Plotly[:len(models)] if len(models) <= 10 else px.colors.qualitative.Alphabet
# Create pie chart
fig = go.Figure(data=[go.Pie(
labels=models,
values=counts,
hole=0.4,
marker=dict(colors=colors),
textinfo="label+percent",
insidetextorientation="radial"
)])
fig.update_layout(
title=title,
margin=dict(l=20, r=20, t=40, b=20),
height=300,
legend=dict(orientation="h", yanchor="bottom", y=-0.2, xanchor="center", x=0.5)
)
return fig
@handle_exceptions
def create_model_usage_over_time(activities: List[Dict[str, Any]], title: str = "AI Model Usage Over Time",
timestamp_key: str = "timestamp", group_by: str = "day") -> go.Figure:
"""
Create a stacked area chart showing AI model usage over time
Args:
activities: List of activity items
title: Chart title
timestamp_key: Key in the activity items that contains the timestamp
group_by: Time grouping ("day", "week", "month")
Returns:
Plotly figure object
"""
logger.debug(f"Creating model usage over time chart with {len(activities)} activities")
# Filter activities related to AI model usage
ai_activities = []
for activity in activities:
activity_type = safe_get(activity, "type", "")
if "ai_" in activity_type or "model_" in activity_type:
ai_activities.append(activity)
if not ai_activities:
# Return empty chart if no AI activities
fig = go.Figure()
fig.update_layout(title=title, height=400)
return fig
# Group activities by date and model type
date_model_counts = defaultdict(lambda: defaultdict(int))
model_types = set()
for activity in ai_activities:
timestamp = safe_get(activity, timestamp_key, None)
if not timestamp:
continue
# Convert timestamp to datetime
if isinstance(timestamp, str):
try:
date = datetime.datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
except ValueError:
logger.warning(f"Could not parse timestamp: {timestamp}")
continue
else:
date = datetime.datetime.fromtimestamp(timestamp)
# Group by the specified time period
if group_by == "day":
date_key = date.strftime("%Y-%m-%d")
elif group_by == "week":
# Get the start of the week (Monday)
start_of_week = date - datetime.timedelta(days=date.weekday())
date_key = start_of_week.strftime("%Y-%m-%d")
elif group_by == "month":
date_key = date.strftime("%Y-%m")
else:
logger.warning(f"Unknown group_by value: {group_by}, using day")
date_key = date.strftime("%Y-%m-%d")
# Clean up the activity type for better display
activity_type = safe_get(activity, "type", "unknown")
model_type = activity_type.replace("ai_", "").replace("model_", "").replace("_", " ").title()
model_types.add(model_type)
# Increment count for this date and model type
date_model_counts[date_key][model_type] += 1
if not date_model_counts:
# Return empty chart if no valid activities
fig = go.Figure()
fig.update_layout(title=title, height=400)
return fig
# Sort dates
sorted_dates = sorted(date_model_counts.keys())
# Format x-axis labels based on grouping
if group_by == "day":
x_labels = [datetime.datetime.strptime(d, "%Y-%m-%d").strftime("%b %d") for d in sorted_dates]
elif group_by == "week":
x_labels = [f"Week of {datetime.datetime.strptime(d, '%Y-%m-%d').strftime('%b %d')}" for d in sorted_dates]
elif group_by == "month":
x_labels = [datetime.datetime.strptime(d, "%Y-%m").strftime("%b %Y") for d in sorted_dates]
else:
x_labels = sorted_dates
# Convert model types to a sorted list
model_types = sorted(model_types)
# Create color map for models
colors = px.colors.qualitative.Plotly[:len(model_types)] if len(model_types) <= 10 else px.colors.qualitative.Alphabet
# Create stacked area chart
fig = go.Figure()
for i, model_type in enumerate(model_types):
y_values = [date_model_counts[date].get(model_type, 0) for date in sorted_dates]
fig.add_trace(go.Scatter(
x=x_labels,
y=y_values,
mode='lines',
stackgroup='one', # This makes it a stacked area chart
name=model_type,
line=dict(width=0.5, color=colors[i % len(colors)]),
fillcolor=colors[i % len(colors)]
))
fig.update_layout(
title=title,
xaxis_title="Date",
yaxis_title="Usage Count",
margin=dict(l=20, r=20, t=40, b=20),
height=400,
legend=dict(orientation="h", yanchor="bottom", y=-0.3, xanchor="center", x=0.5)
)
return fig
@handle_exceptions
def create_completion_time_chart(data: List[Dict[str, Any]], title: str = "Completion Time Distribution",
created_key: str = "created_at", completed_key: str = "completed_at") -> go.Figure:
"""
Create a histogram showing the distribution of completion times
Args:
data: List of data items (tasks, goals, etc.)
title: Chart title
created_key: Key in the data items that contains the creation timestamp
completed_key: Key in the data items that contains the completion timestamp
Returns:
Plotly figure object
"""
logger.debug(f"Creating completion time chart with {len(data)} items")
# Filter completed items
completed_items = []
for item in data:
created_at = safe_get(item, created_key, None)
completed_at = safe_get(item, completed_key, None)
if not created_at or not completed_at:
continue
# Convert timestamps to datetime objects
if isinstance(created_at, str):
try:
created_date = datetime.datetime.fromisoformat(created_at.replace('Z', '+00:00'))
except ValueError:
logger.warning(f"Could not parse timestamp: {created_at}")
continue
else:
created_date = datetime.datetime.fromtimestamp(created_at)
if isinstance(completed_at, str):
try:
completed_date = datetime.datetime.fromisoformat(completed_at.replace('Z', '+00:00'))
except ValueError:
logger.warning(f"Could not parse timestamp: {completed_at}")
continue
else:
completed_date = datetime.datetime.fromtimestamp(completed_at)
# Calculate completion time in days
completion_time = (completed_date - created_date).total_seconds() / (60 * 60 * 24) # Convert to days
# Only include positive completion times
if completion_time > 0:
completed_items.append({
"item": item,
"completion_time": completion_time
})
if not completed_items:
# Return empty chart if no completed items
fig = go.Figure()
fig.update_layout(title=title, height=300)
return fig
# Extract completion times
completion_times = [item["completion_time"] for item in completed_items]
# Create histogram
fig = go.Figure(data=go.Histogram(
x=completion_times,
nbinsx=20,
marker_color=UI_COLORS["primary"],
opacity=0.7
))
# Calculate statistics
avg_time = np.mean(completion_times)
median_time = np.median(completion_times)
# Add vertical lines for average and median
fig.add_shape(
type="line",
x0=avg_time,
y0=0,
x1=avg_time,
y1=1,
yref="paper",
line=dict(color=UI_COLORS["success"], width=2, dash="dash"),
)
fig.add_shape(
type="line",
x0=median_time,
y0=0,
x1=median_time,
y1=1,
yref="paper",
line=dict(color=UI_COLORS["warning"], width=2, dash="dash"),
)
# Add annotations for average and median
fig.add_annotation(
x=avg_time,
y=0.95,
yref="paper",
text=f"Average: {avg_time:.1f} days",
showarrow=True,
arrowhead=1,
ax=40,
ay=-40,
bgcolor=UI_COLORS["success"],
font=dict(color="white")
)
fig.add_annotation(
x=median_time,
y=0.85,
yref="paper",
text=f"Median: {median_time:.1f} days",
showarrow=True,
arrowhead=1,
ax=-40,
ay=-40,
bgcolor=UI_COLORS["warning"],
font=dict(color="white")
)
fig.update_layout(
title=title,
xaxis_title="Completion Time (days)",
yaxis_title="Count",
margin=dict(l=20, r=20, t=40, b=20),
height=300
)
return fig |