-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'kb_stats' into 'master'
Display knowledge base stats in AI Assistant See merge request postgres-ai/database-lab!941
- Loading branch information
Showing
6 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,6 +200,7 @@ | |
"TSQL", | ||
"sparql", | ||
"SPARQL", | ||
"subtransactions" | ||
"subtransactions", | ||
"mbox" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React, { useMemo } from "react"; | ||
import { useKBStats } from "./hooks"; | ||
import Box from "@mui/material/Box/Box"; | ||
import { makeStyles, Typography } from "@material-ui/core"; | ||
import { Link } from "@postgres.ai/shared/components/Link2"; | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
container: { | ||
marginTop: 42, | ||
'& p': { | ||
margin: 0, | ||
lineHeight: 1.5, | ||
}, | ||
[theme.breakpoints.down(480)]: { | ||
marginTop: 24 | ||
}, | ||
[theme.breakpoints.down(360)]: { | ||
marginTop: 0 | ||
} | ||
}, | ||
headingLink: { | ||
fontSize: 16, | ||
[theme.breakpoints.down(330)]: { | ||
fontSize: 14 | ||
} | ||
}, | ||
})) | ||
|
||
export const KBStats = () => { | ||
const { data, loading, error } = useKBStats(); | ||
const classes = useStyles() | ||
|
||
const { totalSum, lastUpdate } = useMemo(() => { | ||
if (!data?.length) { | ||
return { totalSum: 0, lastUpdate: '' }; | ||
} | ||
|
||
const categoryTotals = new Map<string, number>(); | ||
let latestDate = data[0].last_document_date; | ||
|
||
data.forEach(({ category, total_count, last_document_date }) => { | ||
categoryTotals.set(category, total_count); | ||
if (new Date(last_document_date) > new Date(latestDate)) { | ||
latestDate = last_document_date; | ||
} | ||
}); | ||
|
||
latestDate = new Date(latestDate).toISOString().replace('T', ' ').split('.')[0] | ||
|
||
const totalSum = Array.from(categoryTotals.values()).reduce((sum, count) => sum + count, 0); | ||
return { totalSum, lastUpdate: latestDate }; | ||
}, [data]); | ||
|
||
if (error || loading || !data?.length) { | ||
return <div className={classes.container} style={{ height: 58.5 }}></div>; | ||
} | ||
|
||
return ( | ||
<Box className={classes.container}> | ||
<p>Knowledge base contains {totalSum.toLocaleString(navigator.language)} documents.</p> | ||
<p>Last updated: {lastUpdate}.</p> | ||
<Link | ||
external | ||
to={`https://postgres.ai/docs/reference-guides/postgres-ai-bot-reference#tool-rag_search`} | ||
target="_blank" | ||
title="Show full information" | ||
> | ||
Details | ||
</Link> | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useEffect, useState } from 'react' | ||
import { request } from "../../helpers/request"; | ||
|
||
export type KBStats = { | ||
category: 'articles' | 'docs' | 'src' | 'mbox', | ||
domain: string, | ||
total_count: number, | ||
count: number, | ||
last_document_date: string | ||
} | ||
|
||
type UseKBStats = { | ||
data: KBStats[] | null, | ||
error: string | null, | ||
loading: boolean | ||
} | ||
|
||
export const useKBStats = (): UseKBStats => { | ||
const [data, setData] = useState<KBStats[] | null>(null); | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState<string | null>(null); | ||
const apiServer = process.env.REACT_APP_API_URL_PREFIX || ''; | ||
useEffect(() => { | ||
const fetchData = async () => { | ||
setLoading(true) | ||
try { | ||
const response = await request("/kb_category_domain_counts", {}, apiServer) | ||
const result: KBStats[] = await response.json(); | ||
setData(result); | ||
} catch (err) { | ||
setError(err instanceof Error ? err.message : 'Unknown error'); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
fetchData().catch(console.error); | ||
}, []); | ||
|
||
return { data, loading, error }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters