All files / src/apps/user/pages/knowledge/source/web WebInsights.tsx

93.75% Statements 15/16
100% Branches 12/12
80% Functions 4/5
93.33% Lines 14/15

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                                    2x 5x 5x 5x     5x   1x     5x   1x     5x 3x 5x 5x   5x                           10x                                          
import { ChartTitle, RankedList } from "@/components/charts";
import { SectionCard, SkeletonLines } from "@/components/detail";
import type { Knowledge, KnowledgeCrawl, WebPage } from "@/objects/Knowledge";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery } from "@tanstack/react-query";
import { Activity } from "lucide-react";
import React from "react";
 
import { freshnessBuckets, pageHosts } from "../../webSource";
import CrawlHistoryChart from "./CrawlHistoryChart";
 
/**
 * How a website source is kept: what each crawl found, how recently its
 * pages changed, and which sites they come from. Read from the same crawls
 * and pages the website section lists (the same queries, so no second
 * request), and drawn only from what is stored.
 */
const WebInsights: React.FC<{ knowledge: Knowledge }> = ({ knowledge }) => {
  const { getApiHandler } = useApiHandler();
  const api = getApiHandler().knowledge_router;
  const hasUrls = Array.isArray(knowledge.source_config?.urls)
    ? (knowledge.source_config.urls as unknown[]).length > 0
    : false;
  const { data: crawlsData, isLoading: loadingCrawls } = useQuery<KnowledgeCrawl[] | null>({
    queryKey: [QUERY_KEYS.GET.KNOWLEDGE_WEB_CRAWLS, knowledge.id],
    queryFn: () => api.listWebCrawls(knowledge.id),
    enabled: hasUrls,
  });
  const { data: pagesData, isLoading: loadingPages } = useQuery<WebPage[] | null>({
    queryKey: [QUERY_KEYS.GET.KNOWLEDGE_WEB_PAGES, knowledge.id],
    queryFn: () => api.listWebPages(knowledge.id),
    enabled: hasUrls,
  });
  if (!hasUrls) return null;
  const crawls: KnowledgeCrawl[] = crawlsData ?? [];
  const pages: WebPage[] = pagesData ?? [];
  const loading = loadingCrawls || loadingPages;
 
  return (
    <SectionCard icon={Activity} title="How it is kept" subtitle="What its crawls found, and how current its pages are">
      {loading ? (
        <SkeletonLines count={4} />
      ) : (
        <div className="space-y-6">
          <div>
            <ChartTitle title="Crawls" note="Each crawl's pages, oldest left" />
            <CrawlHistoryChart crawls={crawls} />
          </div>
          <div className="grid gap-6 md:grid-cols-2">
            <div>
              <ChartTitle title="Last changed" note="When each page's text last changed" />
              <RankedList
                rows={freshnessBuckets(pages).map((b) => ({ key: b.key, label: b.label, value: b.count }))}
                unit="pages"
                empty="No pages stored yet."
              />
            </div>
            <div>
              <ChartTitle title="Sites" note="Where its pages come from" />
              <RankedList
                rows={pageHosts(pages).map((h) => ({ key: h.host, label: h.host, value: h.count }))}
                unit="pages"
                empty="No pages stored yet."
              />
            </div>
          </div>
        </div>
      )}
    </SectionCard>
  );
};
 
export default WebInsights;