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

100% Statements 10/10
100% Branches 2/2
100% Functions 4/4
100% Lines 10/10

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                        3x               3x       6x 31x     24x                             3x 4x 4x 3x       1x                    
import { StackedBars, type StackedSeries } from "@/components/charts";
import type { KnowledgeCrawl } from "@/objects/Knowledge";
import { parseServerDate } from "@/util/time";
import React from "react";
 
/**
 * A website source's crawls, oldest left: each one's pages by what became of
 * them. Categorical series, checked with the dataviz palette validator on
 * both card surfaces (each clears 3:1 and the adjacent pairs separate under
 * every colour-vision deficiency); "Unchanged" is the neutral, and "Failed"
 * carries the danger meaning it has everywhere else.
 */
export const CRAWL_SERIES: StackedSeries[] = [
  { key: "unchanged", label: "Unchanged", className: "bg-gray-500" },
  { key: "changed", label: "Changed", className: "bg-orange-600" },
  { key: "new", label: "New", className: "bg-blue-600 dark:bg-blue-500" },
  { key: "failed", label: "Failed", className: "bg-red-600 dark:bg-red-500" },
];
 
/** How many crawls the chart reads back to. */
export const CRAWL_HISTORY = 20;
 
/** The finished crawls, oldest first, as the chart's columns. */
export function crawlColumns(crawls: KnowledgeCrawl[]) {
  return crawls
    .filter((c) => c.status !== "running")
    .slice(0, CRAWL_HISTORY)
    .reverse()
    .map((c) => ({
      key: c.id,
      label: parseServerDate(c.started_at).toLocaleDateString(undefined, {
        day: "numeric",
        month: "short",
      }),
      values: {
        unchanged: c.pages_unchanged,
        changed: c.pages_changed,
        new: c.pages_new,
        failed: c.pages_failed,
      },
    }));
}
 
const CrawlHistoryChart: React.FC<{ crawls: KnowledgeCrawl[] }> = ({ crawls }) => {
  const columns = crawlColumns(crawls);
  if (!columns.length) {
    return (
      <p className="text-sm text-gray-400 dark:text-gray-500 italic">No crawl has finished yet.</p>
    );
  }
  return (
    <StackedBars
      columns={columns}
      series={CRAWL_SERIES}
      caption="Pages each crawl read, by what became of them"
    />
  );
};
 
export default CrawlHistoryChart;