Skip to content
This repository was archived by the owner on Jan 5, 2025. It is now read-only.

Commit 3df25e4

Browse files
authored
Merge branch 'main' into ui/sessions-can-review-the-response
2 parents 1a9c314 + 9fcfbc9 commit 3df25e4

File tree

10 files changed

+206
-56
lines changed

10 files changed

+206
-56
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { HeaderShell } from '@/components/domain/HeaderShell'
2+
import { SimpleCard } from '@/components/domain/simple-card'
3+
import { Stack } from '@/components/ui/Stack'
4+
import React from 'react'
5+
import { EChart } from '@kbox-labs/react-echarts'
6+
7+
type Props = {
8+
params: {
9+
copilot_id: string
10+
}
11+
}
12+
export default async function AnalyticsPage(props: Props) {
13+
return (
14+
<Stack direction='column' fluid className='h-full'>
15+
<HeaderShell>
16+
<h1 className="text-lg font-bold text-secondary-foreground">
17+
Analytics
18+
</h1>
19+
</HeaderShell>
20+
<main className='flex-1 overflow-auto p-4 w-full flex flex-col gap-4'>
21+
<div className='grid grid-cols-3 gap-4'>
22+
<SimpleCard title="Knowledgebase Calls" description="number of knowledgebase calls">
23+
20
24+
</SimpleCard>
25+
<SimpleCard title="Api Calls" description="number of api calls">
26+
20
27+
</SimpleCard>
28+
</div>
29+
<div className='flex-1 w-full rounded-md grid grid-cols-2 overflow-hidden gap-5'>
30+
<SimpleCard title="Messages Per session">
31+
<EChart
32+
renderer='svg'
33+
className='size-full'
34+
xAxis={{
35+
type: "value",
36+
}}
37+
yAxis={{
38+
type: "value",
39+
}}
40+
series={[{
41+
type: "scatter",
42+
symbolSize: 20,
43+
data: [
44+
[10.0, 8.04],
45+
[8.0, 6.95],
46+
[13.0, 7.58],
47+
[9.0, 8.81],
48+
[11.0, 8.33],
49+
[14.0, 9.96],
50+
[6.0, 7.24],
51+
[4.0, 4.26],
52+
[12.0, 10.84],
53+
[7.0, 4.82],
54+
[5.0, 5.68],
55+
],
56+
}]}
57+
/>
58+
</SimpleCard>
59+
<SimpleCard title="Action Calls">
60+
<EChart
61+
renderer='svg'
62+
className='size-full'
63+
xAxis={{
64+
type: 'category'
65+
}}
66+
yAxis={{
67+
type: 'value',
68+
boundaryGap: [0, '30%']
69+
}}
70+
series={[
71+
{
72+
type: "line",
73+
data: [120, 200, 150, 80, 70, 110, 130],
74+
}
75+
]}
76+
/>
77+
</SimpleCard>
78+
</div>
79+
</main>
80+
</Stack>
81+
)
82+
}

dashboard/app/(copilot)/copilot/[copilot_id]/layout.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
MessagesSquare,
1212
Settings,
1313
Workflow,
14-
SquareCode, Codesandbox, Route, Zap
14+
SquareCode, Codesandbox, Route, Zap, PieChart
1515
} from "lucide-react";
1616
import React from "react";
1717
import { CopilotLayoutNavLink } from "../../_parts/CopilotNavLink";
@@ -52,7 +52,7 @@ export default function CopilotLayout({ children, params }: Props) {
5252
</div>
5353
<Separator className="my-2" />
5454
<div className="flex flex-col items-center gap-4">
55-
<CopilotLayoutNavLink
55+
<CopilotLayoutNavLink
5656
href={copilotBase + "/actions"}
5757
IconComponent={Zap}
5858
label="Actions"
@@ -79,6 +79,11 @@ export default function CopilotLayout({ children, params }: Props) {
7979
IconComponent={Settings}
8080
label="Settings"
8181
/>
82+
<CopilotLayoutNavLink
83+
href={copilotBase + "/analytics"}
84+
IconComponent={PieChart}
85+
label="Analytics"
86+
/>
8287
</div>
8388
</div>
8489
<div className="mx-auto pb-5 flex flex-col items-center gap-2">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from "react";
2+
import { Card, CardHeader, CardTitle, CardDescription, CardContent } from "../ui/card";
3+
4+
export function SimpleCard({ children, title, description }: {
5+
children: React.ReactNode
6+
title: React.ReactNode
7+
description?: React.ReactNode
8+
}) {
9+
return <Card className="flex flex-col">
10+
<CardHeader className="w-full">
11+
<CardTitle>
12+
{title}
13+
</CardTitle>
14+
{description && <CardDescription>
15+
{description}
16+
</CardDescription>}
17+
</CardHeader>
18+
<CardContent className="flex-1 w-full">
19+
{children}
20+
</CardContent>
21+
</Card>
22+
}

dashboard/components/ui/card.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const Card = React.forwardRef<
99
<div
1010
ref={ref}
1111
className={cn(
12-
"rounded-lg border bg-card text-card-foreground shadow-sm",
12+
"rounded-lg border bg-card text-accent-foreground shadow-sm transition-colors hover:border-primary",
1313
className
1414
)}
1515
{...props}
@@ -23,7 +23,7 @@ const CardHeader = React.forwardRef<
2323
>(({ className, ...props }, ref) => (
2424
<div
2525
ref={ref}
26-
className={cn("flex flex-col space-y-1.5 p-6", className)}
26+
className={cn("flex flex-col space-y-1.5 p-5", className)}
2727
{...props}
2828
/>
2929
))
@@ -36,7 +36,7 @@ const CardTitle = React.forwardRef<
3636
<h3
3737
ref={ref}
3838
className={cn(
39-
"text-2xl font-semibold leading-none tracking-tight",
39+
"text-xl font-semibold leading-none tracking-tight",
4040
className
4141
)}
4242
{...props}
@@ -60,7 +60,7 @@ const CardContent = React.forwardRef<
6060
HTMLDivElement,
6161
React.HTMLAttributes<HTMLDivElement>
6262
>(({ className, ...props }, ref) => (
63-
<div ref={ref} className={cn("p-6 pt-0", className)} {...props} />
63+
<div ref={ref} className={cn("p-5 pt-0", className)} {...props} />
6464
))
6565
CardContent.displayName = "CardContent"
6666

@@ -70,7 +70,7 @@ const CardFooter = React.forwardRef<
7070
>(({ className, ...props }, ref) => (
7171
<div
7272
ref={ref}
73-
className={cn("flex items-center p-6 pt-0", className)}
73+
className={cn("flex items-center p-5 pt-0", className)}
7474
{...props}
7575
/>
7676
))

dashboard/data/copilot.ts

+9
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,12 @@ export async function createVariable(id: string, variables: VariableType[]) {
7676
export async function deleteVariableByKey(id: string, name: string) {
7777
return await instance.delete<{ message: string }>(`/${id}/variable/${name}`)
7878
}
79+
80+
// analytics data
81+
export async function getAnalyticsData(id: string) {
82+
return (await instance.get<{
83+
api_called_count: number;
84+
knowledgebase_called_count:number;
85+
other_count: number;
86+
}[]>(`/${id}/analytics`)).data
87+
}

dashboard/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"dependencies": {
1414
"@hookform/resolvers": "^3.3.1",
1515
"@openchatai/copilot-widget": "^2.3.0",
16+
"@kbox-labs/react-echarts": "^1.0.3",
1617
"@radix-ui/react-accordion": "^1.1.2",
1718
"@radix-ui/react-alert-dialog": "^1.0.5",
1819
"@radix-ui/react-avatar": "^1.0.4",
@@ -40,6 +41,7 @@
4041
"class-variance-authority": "^0.7.0",
4142
"clsx": "^2.0.0",
4243
"cmdk": "^0.2.0",
44+
"echarts": "^5.4.3",
4345
"framer-motion": "^10.16.16",
4446
"immer": "^10.0.3",
4547
"jotai": "^2.4.3",

dashboard/pnpm-lock.yaml

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

llm-server/models/migrations/versions/b9828e70d171_add_api_call_made_and_knowledgebase_.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@
2323

2424
# Add two new boolean columns
2525
def upgrade():
26-
op.add_column("chat_history", Column("api_called", Boolean, default=False))
27-
op.add_column(
28-
"chat_history", Column("knowledgebase_called", Boolean, default=False)
29-
)
26+
if not op.get_bind().execute(
27+
sa.text("SHOW COLUMNS FROM chat_history LIKE 'api_called'")
28+
).fetchone():
29+
op.add_column("chat_history", Column("api_called", Boolean, default=False))
30+
31+
if not op.get_bind().execute(
32+
sa.text("SHOW COLUMNS FROM chat_history LIKE 'knowledgebase_called'")
33+
).fetchone():
34+
op.add_column(
35+
"chat_history", Column("knowledgebase_called", Boolean, default=False)
36+
)
3037

3138

3239
# Remove the two boolean columns

llm-server/models/repository/chat_history_repo.py

+31-27
Original file line numberDiff line numberDiff line change
@@ -279,37 +279,41 @@ def create_chat_histories(
279279
return chat_histories
280280

281281

282-
async def get_analytics(email: str):
282+
async def get_analytics(chatbot_id: str):
283283
with Session() as session:
284284
chat_histories = []
285285

286-
# Step 1: Get chatbot_ids associated with the given email
287-
chatbot_ids = session.query(Chatbot.id).filter(Chatbot.email == email).all()
288-
289-
if chatbot_ids:
290-
chatbot_ids = [chatbot_id[0] for chatbot_id in chatbot_ids]
291-
292-
# Step 2: Get analytics data in a single query
293-
analytics_data = (
294-
session.query(
295-
func.sum(func.cast(ChatHistory.api_called, Integer)).label("api_called_count"),
296-
func.sum(func.cast(ChatHistory.knowledgebase_called, Integer)).label("knowledgebase_called_count"),
297-
func.count().label("total"),
298-
func.sum(func.cast(
299-
~ChatHistory.api_called & ~ChatHistory.knowledgebase_called, Integer)).label("other_count")
300-
)
301-
.filter(ChatHistory.chatbot_id.in_(chatbot_ids))
302-
.one()
286+
# Step 2: Get analytics data in a single query
287+
analytics_data = (
288+
session.query(
289+
func.sum(func.cast(ChatHistory.api_called, Integer)).label(
290+
"api_called_count"
291+
),
292+
func.sum(func.cast(ChatHistory.knowledgebase_called, Integer)).label(
293+
"knowledgebase_called_count"
294+
),
295+
func.count().label("total"),
296+
func.sum(
297+
func.cast(
298+
~ChatHistory.api_called & ~ChatHistory.knowledgebase_called,
299+
Integer,
300+
)
301+
).label("other_count"),
303302
)
303+
.filter(ChatHistory.chatbot_id == chatbot_id)
304+
.one()
305+
)
304306

305-
# Append the results to chat_histories
306-
chat_histories.append(
307-
{
308-
"api_called_count": analytics_data.api_called_count or 0,
309-
"knowledgebase_called_count": analytics_data.knowledgebase_called_count or 0,
310-
"total": analytics_data.total or 0,
311-
"other_count": analytics_data.other_count or 0,
312-
}
313-
)
307+
# Append the results to chat_histories
308+
chat_histories.append(
309+
{
310+
"api_called_count": int(analytics_data.api_called_count or 0),
311+
"knowledgebase_called_count": int(
312+
analytics_data.knowledgebase_called_count or 0
313+
),
314+
"total": int(analytics_data.total or 0),
315+
"other_count": int(analytics_data.other_count or 0),
316+
}
317+
)
314318

315319
return chat_histories

0 commit comments

Comments
 (0)