All files / src/apps/user/pages/knowledge/source DocumentsBreakdown.tsx

82.6% Statements 19/23
70.58% Branches 12/17
75% Functions 6/8
85.71% Lines 18/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                  3x                     3x                                         7x 7x 7x       1x 1x 3x 3x   1x 1x 2x     3x       27x 27x   8x   27x 27x                                          
import { ChartTitle, RankedList } from "@/components/charts";
import { SectionCard } from "@/components/detail";
import type { KnowledgeDocument, KnowledgeStats } from "@/objects/Knowledge";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery } from "@tanstack/react-query";
import { PieChart } from "lucide-react";
import React from "react";
 
import { documentsKey } from "../figures";
 
/**
 * What a file source holds: its documents by where their indexing stands,
 * and by kind — read from the figures and the document list its Documents
 * section already fetched (the same query).
 */
 
/** Indexing states, in the order a reader scans them, and their words. */
export const INDEX_STATE_LABEL: [string, string][] = [
  ["indexed", "Indexed"],
  ["pending", "Waiting to index"],
  ["held", "Waiting for budget"],
  ["indexing", "Indexing"],
  ["importing", "Downloading"],
  ["not_indexed", "No text to index"],
  ["failed", "Could not be indexed"],
];
 
/** A document's kind, from its file name. */
const KIND_BY_EXTENSION: Record<string, string> = {
  pdf: "PDF",
  doc: "Word",
  docx: "Word",
  xls: "Spreadsheet",
  xlsx: "Spreadsheet",
  csv: "Spreadsheet",
  ppt: "Slides",
  pptx: "Slides",
  md: "Text",
  txt: "Text",
  rst: "Text",
  html: "Web page",
  htm: "Web page",
  json: "Data",
  xml: "Data",
  yaml: "Data",
  yml: "Data",
};
 
export function documentKind(doc: KnowledgeDocument): string {
  const name = doc.document_metadata?.original_filename ?? doc.source_id ?? "";
  const extension = /\.([a-z0-9]+)$/i.exec(name)?.[1]?.toLowerCase();
  return extension ? (KIND_BY_EXTENSION[extension] ?? extension.toUpperCase()) : "Other";
}
 
export function kindsOf(documents: KnowledgeDocument[]) {
  const counts = new Map<string, number>();
  for (const doc of documents) {
    const kind = documentKind(doc);
    counts.set(kind, (counts.get(kind) ?? 0) + 1);
  }
  return [...counts.entries()]
    .sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
    .map(([kind, count]) => ({ key: kind, label: kind, value: count }));
}
 
const DocumentsBreakdown: React.FC<{
  knowledgeId: string;
  stats: KnowledgeStats | null;
}> = ({ knowledgeId, stats }) => {
  const { getApiHandler } = useApiHandler();
  const { data } = useQuery<KnowledgeDocument[] | null>({
    queryKey: documentsKey(knowledgeId),
    queryFn: () => getApiHandler().knowledge_router.listDocuments(knowledgeId),
  });
  const documents: KnowledgeDocument[] = data ?? [];
  Eif (!stats || stats.documents.total === 0) return null;
  const states = INDEX_STATE_LABEL.filter(([key]) => stats.documents.by_state[key])
    .map(([key, label]) => ({ key, label, value: stats.documents.by_state[key] }));
 
  return (
    <SectionCard icon={PieChart} title="What it holds">
      <div className="grid gap-6 md:grid-cols-2">
        <div>
          <ChartTitle title="Indexing" note="Whether agents can search each document" />
          <RankedList rows={states} unit="documents" />
        </div>
        <div>
          <ChartTitle title="Kinds" note="By file type" />
          <RankedList rows={kindsOf(documents).slice(0, 8)} unit="documents" />
        </div>
      </div>
    </SectionCard>
  );
};
 
export default DocumentsBreakdown;