File size: 10,030 Bytes
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useMemo } from "react";
import { Card, CardContent } from "@/components/ui/card";
import { Skeleton } from "@/components/ui/skeleton";
import { TraceAnalyticsData } from "@/hooks/useDashboardVisualizationData";
import { FileText, ArrowRight, Clock, Zap } from "lucide-react";

interface TraceAnalyticsProps {
  data: TraceAnalyticsData[];
  isLoading?: boolean;
  error?: string | null;
  className?: string;
}

export function TraceAnalytics({
  data,
  isLoading = false,
  error,
  className,
}: TraceAnalyticsProps) {
  // Calculate key metrics
  const stats = useMemo(() => {
    if (!data.length) return null;

    const totalTraces = data.length;
    const avgReadingTime =
      data.reduce((sum, item) => sum + item.readingTime, 0) / totalTraces;
    const avgTimeWithoutAgentGraph =
      data.reduce((sum, item) => sum + item.timeWithoutAgentGraph, 0) /
      totalTraces;
    const avgComplexity =
      data.reduce((sum, item) => sum + item.complexity, 0) / totalTraces;

    const efficiencyMultiplier = avgTimeWithoutAgentGraph / avgReadingTime;
    const timeSavedMinutes = (avgTimeWithoutAgentGraph - avgReadingTime) * 3.5;

    // Simplified complexity calculation - 1-10 scale
    const complexityScore = Math.min(
      10,
      Math.max(1, Math.round(3 + (avgComplexity / 100) * 7))
    );
    const readabilityGrade = Math.min(16, Math.max(6, 6 + complexityScore));

    return {
      timeSavedMinutes,
      efficiencyMultiplier,
      complexityScore, // 1-10 scale for red circle
      readabilityGrade,
    };
  }, [data]);

  if (error) {
    return (
      <Card className={className}>
        <CardContent className="pt-6">
          <div className="text-center py-4">
            <p className="text-sm text-destructive">{error}</p>
          </div>
        </CardContent>
      </Card>
    );
  }

  if (isLoading) {
    return (
      <Card className={className}>
        <CardContent className="pt-6">
          <Skeleton className="h-64 w-full" />
        </CardContent>
      </Card>
    );
  }

  if (data.length === 0 || !stats) {
    return (
      <Card className={className}>
        <CardContent className="pt-6">
          <div className="text-center py-4">
            <p className="text-sm text-muted-foreground">No data available</p>
          </div>
        </CardContent>
      </Card>
    );
  }

  return (
    <Card className={className}>
      <CardContent className="pt-6">
        <div className="space-y-6">
          {/* Transformation Example */}
          <div className="grid grid-cols-3 gap-4 items-center">
            {/* Complex Trace Example */}
            <div className="p-4 bg-red-50 border border-red-200 rounded-lg">
              <div className="flex items-center justify-between mb-3">
                <div className="flex items-center gap-2">
                  <FileText className="h-5 w-5 text-red-500" />
                  <span className="text-sm font-medium text-red-700">
                    Raw Agent Trace
                  </span>
                </div>
                <div className="bg-red-500 text-white text-xs rounded-full w-6 h-6 flex items-center justify-center font-bold">
                  {stats.complexityScore}
                </div>
              </div>

              {/* Example trace content */}
              <div className="bg-white p-3 rounded border text-xs font-mono">
                <div className="text-gray-800 mb-2">
                  <span className="text-blue-600">{"{"}</span>
                  <br />
                  <span className="ml-2">"agent_action": "web_search",</span>
                  <br />
                  <span className="ml-2">"tool_calls": [</span>
                  <br />
                  <span className="ml-4">{"{"} "function": "search",</span>
                  <br />
                  <span className="ml-6">"parameters": {"{"}</span>
                  <br />
                  <span className="ml-8">"query": "analyze data"</span>
                  <br />
                  <span className="ml-6">
                    {"}"} {"}"}
                  </span>
                  <br />
                  <span className="ml-2">],</span>
                  <br />
                  <span className="ml-2">"reasoning": "User needs...</span>
                  <br />
                  <span className="text-blue-600">{"}"}</span>
                </div>
                <div className="text-xs text-red-600 mt-2">
                  + 47 more nested objects...
                </div>
              </div>

              <div className="text-xs text-red-600 mt-2 text-center">
                Complexity {stats.complexityScore}/10 • Grade{" "}
                {stats.readabilityGrade}
              </div>
            </div>

            {/* Arrow */}
            <div className="text-center">
              <ArrowRight className="h-8 w-8 text-blue-500 mx-auto mb-2" />
              <div className="text-sm font-bold text-blue-600">AgentGraph</div>
              <div className="text-xs text-blue-500">Auto-transforms</div>
            </div>

            {/* Agent Graph Preview */}
            <div className="p-4 bg-green-50 border border-green-200 rounded-lg">
              <div className="flex items-center justify-between mb-3">
                <div className="flex items-center gap-2">
                  <div className="h-5 w-5 bg-green-500 rounded-sm flex items-center justify-center">
                    <div className="w-2 h-2 bg-white rounded-full"></div>
                  </div>
                  <span className="text-sm font-medium text-green-700">
                    Agent Graph
                  </span>
                </div>
                <div className="bg-green-500 text-white text-xs rounded-full w-6 h-6 flex items-center justify-center">

                </div>
              </div>

              {/* Example graph visualization */}
              <div className="bg-white p-3 rounded border">
                <div className="flex items-center justify-between mb-3">
                  <div className="flex items-center gap-2">
                    <div className="w-3 h-3 bg-blue-400 rounded-full"></div>
                    <span className="text-xs font-medium">User</span>
                  </div>
                  <ArrowRight className="h-3 w-3 text-gray-400" />
                  <div className="flex items-center gap-2">
                    <div className="w-3 h-3 bg-purple-400 rounded-full"></div>
                    <span className="text-xs font-medium">Agent</span>
                  </div>
                </div>

                <div className="flex items-center justify-center mb-2">
                  <ArrowRight className="h-3 w-3 text-gray-400 rotate-90" />
                </div>

                <div className="flex items-center gap-2 justify-center">
                  <div className="w-3 h-3 bg-green-400 rounded-full"></div>
                  <span className="text-xs font-medium">Search Tool</span>
                </div>
              </div>

              <div className="text-xs text-green-600 mt-2 text-center">
                Simplified to Grade 5 • 1/10 complexity
              </div>
            </div>
          </div>

          {/* Impact Metrics */}
          <div className="p-6 bg-gradient-to-r from-blue-50 to-blue-100 rounded-lg border border-blue-200">
            <div className="flex items-center justify-center gap-6">
              <div className="text-center">
                <div className="flex items-center justify-center gap-2 mb-2">
                  <Clock className="h-5 w-5 text-blue-600" />
                  <span className="text-lg font-bold text-blue-800">
                    {stats.timeSavedMinutes.toFixed(0)} min saved
                  </span>
                </div>
                <div className="text-xs text-blue-600">per trace</div>
              </div>

              <div className="w-px h-12 bg-blue-300"></div>

              <div className="text-center">
                <div className="flex items-center justify-center gap-2 mb-2">
                  <Zap className="h-5 w-5 text-blue-600" />
                  <span className="text-lg font-bold text-blue-800">
                    {stats.efficiencyMultiplier.toFixed(1)}× faster
                  </span>
                </div>
                <div className="text-xs text-blue-600">comprehension</div>
              </div>

              <div className="w-px h-12 bg-blue-300"></div>

              <div className="text-center">
                <div className="flex items-center justify-center gap-2 mb-2">
                  <div className="h-5 w-5 bg-red-500 text-white rounded-full text-xs flex items-center justify-center font-bold">
                    {stats.complexityScore}
                  </div>
                  <ArrowRight className="h-4 w-4 text-blue-500" />
                  <div className="h-5 w-5 bg-green-500 text-white rounded-full text-xs flex items-center justify-center font-bold">
                    1
                  </div>
                </div>
                <div className="text-xs text-blue-600">
                  complexity reduction
                </div>
              </div>
            </div>
          </div>

          {/* Complexity Scale Explanation */}
          <div className="p-3 bg-gray-50 rounded-lg border">
            <div className="text-center">
              <div className="text-sm font-medium text-gray-700 mb-2">
                Complexity Scale: What does "{stats.complexityScore}" mean?
              </div>
              <div className="flex items-center justify-center gap-2 text-xs text-gray-600">
                <span>1-3: Simple</span>
                <span></span>
                <span>4-6: Moderate</span>
                <span></span>
                <span className="font-medium text-red-600">7-10: Complex</span>
              </div>
            </div>
          </div>
        </div>
      </CardContent>
    </Card>
  );
}