Spaces:
Running
Running
File size: 11,468 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 337 338 339 |
import React, { useMemo } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { useAgentGraph } from "@/context/AgentGraphContext";
import { GitBranch, ArrowRight, Network, TrendingUp } from "lucide-react";
interface RelationStats {
[relationType: string]: {
count: number;
percentage: number;
sourceTypes: Set<string>;
targetTypes: Set<string>;
};
}
interface FlowPattern {
source: string;
target: string;
relation: string;
count: number;
percentage: number;
}
interface RelationFlowData {
totalRelations: number;
relationStats: RelationStats;
mostCommonRelation: string;
topFlowPatterns: FlowPattern[];
networkComplexity: number;
}
export function RelationFlowAnalysis() {
const { state } = useAgentGraph();
const flowData = useMemo<RelationFlowData>(() => {
const relationStats: RelationStats = {};
const flowPatterns: { [key: string]: FlowPattern } = {};
let totalRelations = 0;
const allEntityTypes = new Set<string>();
// Process all traces and their knowledge graphs
state.traces.forEach((trace) => {
trace.knowledge_graphs?.forEach((kg: any) => {
if (kg.graph_data) {
try {
const graphData =
typeof kg.graph_data === "string"
? JSON.parse(kg.graph_data)
: kg.graph_data;
// First pass: collect all entity types
if (graphData.entities && Array.isArray(graphData.entities)) {
graphData.entities.forEach((entity: any) => {
allEntityTypes.add(entity.type || "Unknown");
});
}
if (graphData.relations && Array.isArray(graphData.relations)) {
// Create entity lookup map
const entityLookup = new Map<string, string>();
if (graphData.entities && Array.isArray(graphData.entities)) {
graphData.entities.forEach((entity: any) => {
entityLookup.set(entity.id, entity.type || "Unknown");
});
}
graphData.relations.forEach((relation: any) => {
const type = relation.type || "Unknown";
const sourceType =
entityLookup.get(relation.source) || "Unknown";
const targetType =
entityLookup.get(relation.target) || "Unknown";
// Track relation statistics
if (!relationStats[type]) {
relationStats[type] = {
count: 0,
percentage: 0,
sourceTypes: new Set(),
targetTypes: new Set(),
};
}
relationStats[type].count++;
relationStats[type].sourceTypes.add(sourceType);
relationStats[type].targetTypes.add(targetType);
totalRelations++;
// Track flow patterns
const flowKey = `${sourceType}_${type}_${targetType}`;
if (!flowPatterns[flowKey]) {
flowPatterns[flowKey] = {
source: sourceType,
target: targetType,
relation: type,
count: 0,
percentage: 0,
};
}
flowPatterns[flowKey].count++;
});
}
} catch (error) {
console.warn("Error parsing graph_data:", error);
}
}
});
});
// Calculate percentages
Object.values(relationStats).forEach((stats) => {
stats.percentage =
totalRelations > 0
? Math.round((stats.count / totalRelations) * 100)
: 0;
});
Object.values(flowPatterns).forEach((pattern) => {
pattern.percentage =
totalRelations > 0
? Math.round((pattern.count / totalRelations) * 100)
: 0;
});
// Get top flow patterns
const topFlowPatterns = Object.values(flowPatterns)
.sort((a, b) => b.count - a.count)
.slice(0, 8);
const mostCommonRelation =
Object.entries(relationStats).sort(
([, a], [, b]) => b.count - a.count
)[0]?.[0] || "None";
const networkComplexity =
allEntityTypes.size > 0
? Math.round((totalRelations / allEntityTypes.size) * 10) / 10
: 0;
return {
totalRelations,
relationStats,
mostCommonRelation,
topFlowPatterns,
networkComplexity,
};
}, [state.traces]);
const getRelationColor = (index: number) => {
const colors = [
"bg-emerald-500",
"bg-cyan-500",
"bg-violet-500",
"bg-rose-500",
"bg-amber-500",
"bg-indigo-500",
];
return colors[index % colors.length];
};
const getImportanceIcon = (percentage: number) => {
if (percentage > 20)
return <TrendingUp className="h-4 w-4 text-green-600" />;
if (percentage > 10)
return <ArrowRight className="h-4 w-4 text-yellow-600" />;
return <GitBranch className="h-4 w-4 text-blue-600" />;
};
if (flowData.totalRelations === 0) {
return (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Network className="h-5 w-5" />
Relation Flow Analysis
</CardTitle>
</CardHeader>
<CardContent>
<div className="text-center py-8 text-muted-foreground">
<Network className="h-12 w-12 mx-auto mb-4 opacity-50" />
<p>No relation data available</p>
<p className="text-sm">
Generate some knowledge graphs to see relationship analysis
</p>
</div>
</CardContent>
</Card>
);
}
return (
<div className="space-y-6">
{/* Summary Stats */}
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
<Card>
<CardContent className="p-4">
<div className="flex items-center gap-3">
<div className="p-2 rounded-lg bg-emerald-100">
<GitBranch className="h-4 w-4 text-emerald-600" />
</div>
<div>
<p className="text-sm text-muted-foreground">Total Relations</p>
<p className="text-2xl font-bold">{flowData.totalRelations}</p>
</div>
</div>
</CardContent>
</Card>
<Card>
<CardContent className="p-4">
<div className="flex items-center gap-3">
<div className="p-2 rounded-lg bg-cyan-100">
<Network className="h-4 w-4 text-cyan-600" />
</div>
<div>
<p className="text-sm text-muted-foreground">Relation Types</p>
<p className="text-2xl font-bold">
{Object.keys(flowData.relationStats).length}
</p>
</div>
</div>
</CardContent>
</Card>
<Card>
<CardContent className="p-4">
<div className="flex items-center gap-3">
<div className="p-2 rounded-lg bg-violet-100">
<TrendingUp className="h-4 w-4 text-violet-600" />
</div>
<div>
<p className="text-sm text-muted-foreground">
Network Complexity
</p>
<p className="text-2xl font-bold">
{flowData.networkComplexity}
</p>
</div>
</div>
</CardContent>
</Card>
</div>
{/* Relation Types Distribution */}
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<GitBranch className="h-5 w-5" />
Relation Types Distribution
</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-3">
{Object.entries(flowData.relationStats)
.sort(([, a], [, b]) => b.count - a.count)
.map(([type, stats], index) => (
<div
key={type}
className="flex items-center justify-between p-3 bg-muted/30 rounded-lg"
>
<div className="flex items-center gap-3">
<div
className={`w-4 h-4 rounded-full ${getRelationColor(
index
)}`}
/>
<div>
<span className="font-medium">
{type.replace(/_/g, " ")}
</span>
<div className="text-xs text-muted-foreground mt-1">
{stats.sourceTypes.size} → {stats.targetTypes.size}{" "}
entity types
</div>
</div>
</div>
<div className="flex items-center gap-3">
{getImportanceIcon(stats.percentage)}
<Badge variant="secondary">{stats.count} relations</Badge>
<span className="text-sm text-muted-foreground w-12 text-right">
{stats.percentage}%
</span>
</div>
</div>
))}
</div>
</CardContent>
</Card>
{/* Top Flow Patterns */}
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<ArrowRight className="h-5 w-5" />
Top Flow Patterns
</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-4">
{flowData.topFlowPatterns.map((pattern, index) => (
<div
key={`${pattern.source}_${pattern.relation}_${pattern.target}`}
className="flex items-center justify-between p-4 border rounded-lg hover:bg-muted/20 transition-colors"
>
<div className="flex items-center gap-4">
<div
className={`w-3 h-3 rounded-full ${getRelationColor(
index
)}`}
/>
<div className="flex items-center gap-2">
<Badge variant="outline" className="text-xs">
{pattern.source}
</Badge>
<ArrowRight className="h-3 w-3 text-muted-foreground" />
<span className="text-sm font-medium px-2 py-1 bg-muted/50 rounded">
{pattern.relation.replace(/_/g, " ")}
</span>
<ArrowRight className="h-3 w-3 text-muted-foreground" />
<Badge variant="outline" className="text-xs">
{pattern.target}
</Badge>
</div>
</div>
<div className="flex items-center gap-3">
<Badge variant="secondary">{pattern.count} occurrences</Badge>
<span className="text-sm text-muted-foreground w-12 text-right">
{pattern.percentage}%
</span>
</div>
</div>
))}
</div>
</CardContent>
</Card>
</div>
);
}
|