File size: 13,337 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
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
import React, { useState, useEffect, useCallback, useMemo } from "react";
import { useDashboardData } from "@/hooks/useDashboardData";
import { DashboardVisualization } from "../dashboard/DashboardVisualization";
import { EntityRelationAnalysisModal } from "../dashboard/modals/EntityRelationAnalysisModal";
import { TinyTrendChart } from "@/components/shared/TinyTrendChart";
import { EntityRelationTreeChart } from "@/components/shared/EntityRelationTreeChart";
import {
  generateTrendData,
  calculateFailureCount,
} from "@/lib/trend-data-generator";
import { useAgentGraph } from "@/context/AgentGraphContext";
import { api } from "@/lib/api";

import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Skeleton } from "@/components/ui/skeleton";
import {
  FileText,
  AlertCircle,
  Clock,
  Users,
  AlertTriangle,
} from "lucide-react";

export function DashboardView() {
  const { state, actions } = useAgentGraph();
  const { stats, recentActivity, isLoading, error, refresh } =
    useDashboardData();

  // Modal states
  const [isEntityModalOpen, setIsEntityModalOpen] = useState(false);

  // Generate realistic trend data based on current traces
  const trendData = useMemo(
    () => generateTrendData(state.traces),
    [state.traces]
  );

  // Load traces data when dashboard mounts (if not already loaded)
  const loadTracesData = useCallback(async () => {
    if (state.traces.length === 0 && !state.isLoading) {
      actions.setLoading(true);
      try {
        const tracesData = await api.traces.list();
        actions.setTraces(Array.isArray(tracesData) ? tracesData : []);
      } catch (error) {
        actions.setError(
          error instanceof Error ? error.message : "Failed to load traces"
        );
        actions.setTraces([]);
      } finally {
        actions.setLoading(false);
      }
    }
  }, [state.traces.length, state.isLoading, actions]);

  useEffect(() => {
    loadTracesData();
  }, [loadTracesData]);

  const handleUploadTrace = () => {
    actions.setActiveView("upload");
  };

  return (
    <div className="flex-1 flex flex-col p-6 gap-6 min-h-0 relative">
      {/* Glassmorphism background gradient */}
      <div className="absolute inset-0 bg-gradient-to-br from-blue-50/30 via-purple-50/20 to-green-50/30 dark:from-blue-950/20 dark:via-purple-950/10 dark:to-green-950/20 pointer-events-none" />

      {/* Content with relative positioning */}
      <div className="relative z-10 flex flex-col gap-6 h-full">
        {/* Error State */}
        {error && (
          <Card className="border-destructive/50 bg-destructive/10">
            <CardContent className="p-4">
              <div className="flex items-center gap-2 text-destructive">
                <AlertCircle className="h-4 w-4" />
                <span className="text-sm">{error}</span>
                <Button
                  variant="outline"
                  size="sm"
                  onClick={refresh}
                  className="ml-auto"
                >
                  Try Again
                </Button>
              </div>
            </CardContent>
          </Card>
        )}

        {/* Stats Grid */}
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3">
          {/* Traces & Agent Graphs - Blue & Green Theme */}
          <div className="glass-card glass-card-blue rounded-2xl p-4 cursor-pointer group">
            <div className="flex items-center justify-between mb-3">
              <div className="flex items-center gap-3">
                <div className="glass-icon rounded-xl p-2">
                  <FileText className="h-5 w-5 text-blue-500" />
                </div>
                <div>
                  <h3 className="text-sm font-medium text-foreground/90">
                    Traces & Graphs
                  </h3>
                  {isLoading ? (
                    <div className="flex gap-2">
                      <Skeleton className="h-6 w-8 mt-1" />
                      <span className="text-xs text-muted-foreground mt-2">

                      </span>
                      <Skeleton className="h-6 w-8 mt-1" />
                    </div>
                  ) : (
                    <div className="flex items-center gap-2">
                      <div className="text-xl font-bold glass-number text-blue-600 dark:text-blue-400">
                        {stats.totalTraces}
                      </div>
                      <span className="text-xs text-muted-foreground"></span>
                      <div className="text-xl font-bold glass-number text-green-600 dark:text-green-400">
                        {stats.totalAgentGraphs}
                      </div>
                    </div>
                  )}
                </div>
              </div>
            </div>

            {/* Dual-line trend chart */}
            <div className="mb-2">
              <TinyTrendChart
                data={trendData.tracesAndGraphs}
                color="#3b82f6"
                color2="#22c55e"
                isDual={true}
              />
            </div>

            {stats.totalTraces > 0 ? (
              <p className="text-xs text-muted-foreground">
                <span className="text-blue-600">Traces</span> ready •{" "}
                <span className="text-green-600">Graphs</span> generated
              </p>
            ) : (
              <Button
                variant="link"
                size="sm"
                onClick={handleUploadTrace}
                className="text-xs text-blue-600 hover:text-blue-500 p-0 h-auto font-normal"
              >
                Upload your first trace
              </Button>
            )}
          </div>

          {/* Entities & Relations - Purple & Orange Theme */}
          <div
            className="glass-card glass-card-purple rounded-2xl p-4 cursor-pointer group hover:scale-105 transition-all duration-300"
            onClick={() => setIsEntityModalOpen(true)}
          >
            <div className="flex items-center justify-between mb-3">
              <div className="flex items-center gap-3">
                <div className="glass-icon rounded-xl p-2">
                  <Users className="h-5 w-5 text-purple-500" />
                </div>
                <div>
                  <h3 className="text-sm font-medium text-foreground/90">
                    Entities & Relations
                  </h3>
                  {isLoading ? (
                    <div className="flex gap-2">
                      <Skeleton className="h-6 w-12 mt-1" />
                      <span className="text-xs text-muted-foreground mt-2">

                      </span>
                      <Skeleton className="h-6 w-12 mt-1" />
                    </div>
                  ) : (
                    <div className="flex items-center gap-2">
                      <div className="text-xl font-bold glass-number text-purple-600 dark:text-purple-400">
                        {stats.totalEntities}
                      </div>
                      <span className="text-xs text-muted-foreground"></span>
                      <div className="text-xl font-bold glass-number text-orange-600 dark:text-orange-400">
                        {stats.totalRelations}
                      </div>
                    </div>
                  )}
                </div>
              </div>
            </div>

            {/* Entity-Relation Tree Visualization */}
            <div className="mb-2">
              <EntityRelationTreeChart />
            </div>

            <p className="text-xs text-muted-foreground">
              <span className="text-purple-600">Entities</span> extracted •{" "}
              <span className="text-orange-600">Relations</span> connected
            </p>
          </div>

          {/* Failures - Red Theme */}
          <div className="glass-card-red rounded-2xl p-4 cursor-pointer group backdrop-blur-md">
            <div className="flex items-center justify-between mb-3">
              <div className="flex items-center gap-3">
                <div className="glass-icon rounded-xl p-2 bg-red-500/10">
                  <AlertTriangle className="h-5 w-5 text-red-500" />
                </div>
                <div>
                  <h3 className="text-sm font-medium text-foreground/90">
                    System Failures
                  </h3>
                  {isLoading ? (
                    <Skeleton className="h-6 w-12 mt-1" />
                  ) : (
                    <div className="text-xl font-bold glass-number text-red-600 dark:text-red-400">
                      {calculateFailureCount(state.traces)}
                    </div>
                  )}
                </div>
              </div>
            </div>

            {/* Failures trend chart */}
            <div className="mb-2">
              <TinyTrendChart data={trendData.failures} color="#dc2626" />
            </div>

            <p className="text-xs text-muted-foreground">
              Processing errors tracked
            </p>
          </div>
        </div>

        {/* Dashboard Visualization */}
        <DashboardVisualization className="mt-6" />

        {/* Main Content Area - Recent Activity */}
        <div className="flex-1 flex flex-col gap-6 min-h-0 overflow-auto">
          {/* Recent Activity - Now takes full available space */}
          <Card className="flex-1 flex flex-col min-h-0">
            <CardHeader className="pb-4">
              <CardTitle className="flex items-center gap-2">
                <Clock className="h-4 w-4" />
                Recent Activity
              </CardTitle>
              <CardDescription>
                Latest system events and processing updates
              </CardDescription>
            </CardHeader>
            <CardContent className="flex-1 overflow-auto">
              {isLoading ? (
                <div className="space-y-4">
                  {[...Array(3)].map((_, i) => (
                    <div key={i} className="flex items-center justify-between">
                      <div className="space-y-2">
                        <Skeleton className="h-4 w-32" />
                        <Skeleton className="h-3 w-24" />
                      </div>
                      <Skeleton className="h-6 w-16" />
                    </div>
                  ))}
                </div>
              ) : recentActivity.length > 0 ? (
                <div className="space-y-4">
                  {recentActivity.map((activity) => (
                    <div
                      key={activity.id}
                      className="flex items-center justify-between"
                    >
                      <div className="space-y-1">
                        <p className="text-sm font-medium">{activity.action}</p>
                        <p className="text-xs text-muted-foreground">
                          {activity.trace}
                        </p>
                      </div>
                      <div className="text-right space-y-1">
                        <Badge
                          variant={
                            activity.status === "success"
                              ? "default"
                              : activity.status === "error"
                              ? "destructive"
                              : activity.status === "processing"
                              ? "secondary"
                              : "secondary"
                          }
                          className={`text-xs ${
                            activity.status === "success"
                              ? "bg-green-100 text-green-800 border-green-200 dark:bg-green-900 dark:text-green-300"
                              : activity.status === "error"
                              ? "bg-red-100 text-red-800 border-red-200 dark:bg-red-900 dark:text-red-300"
                              : activity.status === "processing"
                              ? "bg-blue-100 text-blue-800 border-blue-200 dark:bg-blue-900 dark:text-blue-300"
                              : "bg-gray-100 text-gray-800 border-gray-200 dark:bg-gray-900 dark:text-gray-300"
                          }`}
                        >
                          {activity.status}
                        </Badge>
                        <p className="text-xs text-muted-foreground">
                          {activity.timestamp}
                        </p>
                      </div>
                    </div>
                  ))}
                </div>
              ) : (
                <div className="text-center py-8">
                  <Clock className="h-8 w-8 mx-auto mb-2 text-muted-foreground" />
                  <p className="text-sm text-muted-foreground">
                    No recent activity
                  </p>
                </div>
              )}
            </CardContent>
          </Card>
        </div>

        {/* Analysis Modals */}
        <EntityRelationAnalysisModal
          open={isEntityModalOpen}
          onOpenChange={setIsEntityModalOpen}
        />
      </div>
    </div>
  );
}