File size: 2,836 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
import { KnowledgeGraph } from "@/types";

export type KGDisplayMode = "single" | "multiple";

/**
 * Select the best knowledge graph from a list of KGs for single display mode
 */
export function selectBestKnowledgeGraph(
  knowledgeGraphs: KnowledgeGraph[]
): KnowledgeGraph | null {
  if (!knowledgeGraphs || knowledgeGraphs.length === 0) {
    return null;
  }

  if (knowledgeGraphs.length === 1) {
    return knowledgeGraphs[0] || null;
  }

  // Sort by priority criteria
  const sortedKGs = [...knowledgeGraphs].sort((a, b) => {
    // 1. Prefer by status (analyzed > perturbed > enriched > created)
    const statusPriority = {
      analyzed: 4,
      perturbed: 3,
      enriched: 2,
      created: 1,
    };
    const statusDiff =
      (statusPriority[b.status as keyof typeof statusPriority] || 0) -
      (statusPriority[a.status as keyof typeof statusPriority] || 0);
    if (statusDiff !== 0) return statusDiff;

    // 2. Prefer production method over baseline methods
    const aMethod = a.processing_metadata?.method_name || "";
    const bMethod = b.processing_metadata?.method_name || "";
    if (aMethod === "production" && bMethod !== "production") return -1;
    if (bMethod === "production" && aMethod !== "production") return 1;

    // 3. Prefer more recent creation timestamp
    const aTime = a.created_at ? new Date(a.created_at).getTime() : 0;
    const bTime = b.created_at ? new Date(b.created_at).getTime() : 0;
    return bTime - aTime;
  });

  return sortedKGs[0] || null;
}

/**
 * Filter knowledge graphs based on display mode
 */
export function filterKnowledgeGraphsByMode(
  knowledgeGraphs: KnowledgeGraph[],
  mode: KGDisplayMode
): KnowledgeGraph[] {
  if (mode === "multiple") {
    return knowledgeGraphs;
  }

  // Group KGs by trace_id, then select the best one from each group
  const kgsByTrace = new Map<string, KnowledgeGraph[]>();

  knowledgeGraphs.forEach((kg) => {
    // Use a fallback key if trace_id is not available
    const traceKey =
      kg.processing_metadata?.method_name || kg.kg_id || "unknown";
    if (!kgsByTrace.has(traceKey)) {
      kgsByTrace.set(traceKey, []);
    }
    kgsByTrace.get(traceKey)!.push(kg);
  });

  // Select the best KG from each trace group
  const selectedKGs: KnowledgeGraph[] = [];
  kgsByTrace.forEach((kgsForTrace) => {
    const bestKG = selectBestKnowledgeGraph(kgsForTrace);
    if (bestKG !== null) {
      selectedKGs.push(bestKG);
    }
  });

  return selectedKGs;
}

/**
 * Get display mode description for UI
 */
export function getDisplayModeDescription(mode: KGDisplayMode): string {
  switch (mode) {
    case "single":
      return "Showing the most recent/best knowledge graph per trace";
    case "multiple":
      return "Showing all knowledge graphs for research and comparison";
    default:
      return "";
  }
}