1+ import React , { useMemo } from "react" ;
2+ import { useKBStats } from "./hooks" ;
3+ import Box from "@mui/material/Box/Box" ;
4+ import { makeStyles , Typography } from "@material-ui/core" ;
5+ import { Link } from "@postgres.ai/shared/components/Link2" ;
6+
7+ const useStyles = makeStyles ( ( theme ) => ( {
8+ container : {
9+ marginTop : 42 ,
10+ '& p' : {
11+ margin : 0 ,
12+ lineHeight : 1.5 ,
13+ } ,
14+ [ theme . breakpoints . down ( 480 ) ] : {
15+ marginTop : 24
16+ } ,
17+ [ theme . breakpoints . down ( 360 ) ] : {
18+ marginTop : 0
19+ }
20+ } ,
21+ headingLink : {
22+ fontSize : 16 ,
23+ [ theme . breakpoints . down ( 330 ) ] : {
24+ fontSize : 14
25+ }
26+ } ,
27+ } ) )
28+
29+ export const KBStats = ( ) => {
30+ const { data, loading, error } = useKBStats ( ) ;
31+ const classes = useStyles ( )
32+
33+ const { totalSum, lastUpdate } = useMemo ( ( ) => {
34+ if ( ! data ?. length ) {
35+ return { totalSum : 0 , lastUpdate : '' } ;
36+ }
37+
38+ const categoryTotals = new Map < string , number > ( ) ;
39+ let latestDate = data [ 0 ] . last_document_date ;
40+
41+ data . forEach ( ( { category, total_count, last_document_date } ) => {
42+ categoryTotals . set ( category , total_count ) ;
43+ if ( new Date ( last_document_date ) > new Date ( latestDate ) ) {
44+ latestDate = last_document_date ;
45+ }
46+ } ) ;
47+
48+ latestDate = new Date ( latestDate ) . toISOString ( ) . replace ( 'T' , ' ' ) . split ( '.' ) [ 0 ]
49+
50+ const totalSum = Array . from ( categoryTotals . values ( ) ) . reduce ( ( sum , count ) => sum + count , 0 ) ;
51+ return { totalSum, lastUpdate : latestDate } ;
52+ } , [ data ] ) ;
53+
54+ if ( error || loading || ! data ?. length ) {
55+ return < div className = { classes . container } style = { { height : 58.5 } } > </ div > ;
56+ }
57+
58+ return (
59+ < Box className = { classes . container } >
60+ < p > Knowledge base contains { totalSum . toLocaleString ( navigator . language ) } documents.</ p >
61+ < p > Last updated: { lastUpdate } .</ p >
62+ < Link
63+ external
64+ to = { `https://postgres.ai/docs/reference-guides/postgres-ai-bot-reference#tool-rag_search` }
65+ target = "_blank"
66+ title = "Show full information"
67+ >
68+ Details
69+ </ Link >
70+ </ Box >
71+ ) ;
72+ }
0 commit comments