Skip to content

Commit 501ff60

Browse files
authored
feat: add total feedback charts (#849)
1 parent fd932a7 commit 501ff60

File tree

4 files changed

+194
-2
lines changed

4 files changed

+194
-2
lines changed

packages/backend/src/api/v1/analytics/index.ts

+169
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,175 @@ analytics.get("/agents/top", async (ctx: Context) => {
11811181
ctx.body = { data };
11821182
});
11831183

1184+
analytics.get("/feedback/thumb/up", async (ctx: Context) => {
1185+
const { projectId } = ctx.state;
1186+
const { datesQuery, filteredRunsQuery, granularity } = parseQuery(
1187+
projectId,
1188+
ctx.querystring,
1189+
ctx.query,
1190+
);
1191+
1192+
if (granularity === "weekly") {
1193+
const res = await sql`
1194+
with dates as (
1195+
${datesQuery}
1196+
),
1197+
filtered_runs as (
1198+
${filteredRunsQuery}
1199+
),
1200+
feedback_data as (
1201+
select
1202+
r.local_created_at,
1203+
case when r.feedback->>'thumb' = 'up' then 1 else 0 end as thumbs_up,
1204+
from
1205+
filtered_runs r
1206+
where
1207+
r.feedback->>'thumb' is not null
1208+
),
1209+
weekly_avg as (
1210+
select
1211+
d.date,
1212+
case when fd.local_created_at is not null then fd.local_created_at else d.date end as local_created_at,
1213+
coalesce(sum(fd.thumbs_up), 0) as total_thumbs_up
1214+
from
1215+
dates d
1216+
left join feedback_data fd on fd.local_created_at >= d.date and fd.local_created_at < d.date + interval '7 days'
1217+
group by
1218+
d.date,
1219+
case when fd.local_created_at is not null then fd.local_created_at else d.date end
1220+
order by d.date
1221+
)
1222+
select
1223+
date,
1224+
coalesce(total_thumbs_up, 0) as value,
1225+
'Thumbs Up' as name
1226+
from
1227+
weekly_avg
1228+
order by
1229+
date;
1230+
`;
1231+
ctx.body = { data: res };
1232+
return;
1233+
} else {
1234+
const res = await sql`
1235+
with dates as (
1236+
${datesQuery}
1237+
),
1238+
filtered_runs as (
1239+
${filteredRunsQuery}
1240+
),
1241+
feedback_data as (
1242+
select
1243+
r.local_created_at as date,
1244+
case when r.feedback->>'thumb' = 'up' then 1 else 0 end as thumbs_up
1245+
from
1246+
filtered_runs r
1247+
where
1248+
feedback is not null
1249+
)
1250+
select
1251+
date,
1252+
total_thumbs_up as value,
1253+
'Thumbs Up' as name from
1254+
(select d.date, coalesce(sum(fd.thumbs_up), 0)::int as total_thumbs_up
1255+
from
1256+
dates d
1257+
left join feedback_data fd on d.date = fd.date
1258+
group by
1259+
d.date
1260+
order by
1261+
d.date ) r;
1262+
`;
1263+
console.log(res);
1264+
ctx.body = { data: res };
1265+
return;
1266+
}
1267+
});
1268+
1269+
analytics.get("/feedback/thumb/down", async (ctx: Context) => {
1270+
const { projectId } = ctx.state;
1271+
const { datesQuery, filteredRunsQuery, granularity } = parseQuery(
1272+
projectId,
1273+
ctx.querystring,
1274+
ctx.query,
1275+
);
1276+
1277+
if (granularity === "weekly") {
1278+
const res = await sql`
1279+
with dates as (
1280+
${datesQuery}
1281+
),
1282+
filtered_runs as (
1283+
${filteredRunsQuery}
1284+
),
1285+
feedback_data as (
1286+
select
1287+
r.local_created_at,
1288+
case when r.feedback->>'down' = 'up' then 1 else 0 end as thumbs_down,
1289+
from
1290+
filtered_runs r
1291+
where
1292+
r.feedback->>'down' is not null
1293+
),
1294+
weekly_avg as (
1295+
select
1296+
d.date,
1297+
case when fd.local_created_at is not null then fd.local_created_at else d.date end as local_created_at,
1298+
coalesce(sum(fd.thumbs_down), 0) as total_thumbs_down
1299+
from
1300+
dates d
1301+
left join feedback_data fd on fd.local_created_at >= d.date and fd.local_created_at < d.date + interval '7 days'
1302+
group by
1303+
d.date,
1304+
case when fd.local_created_at is not null then fd.local_created_at else d.date end
1305+
order by d.date
1306+
)
1307+
select
1308+
date,
1309+
coalesce(total_thumbs_down, 0) as value,
1310+
'Thumbs Up' as name
1311+
from
1312+
weekly_avg
1313+
order by
1314+
date;
1315+
`;
1316+
ctx.body = { data: res };
1317+
return;
1318+
} else {
1319+
const res = await sql`
1320+
with dates as (
1321+
${datesQuery}
1322+
),
1323+
filtered_runs as (
1324+
${filteredRunsQuery}
1325+
),
1326+
feedback_data as (
1327+
select
1328+
r.local_created_at as date,
1329+
case when r.feedback->>'thumb' = 'down' then 1 else 0 end as thumbs_down
1330+
from
1331+
filtered_runs r
1332+
where
1333+
feedback is not null
1334+
)
1335+
select
1336+
date,
1337+
total_thumbs_down as value,
1338+
'Thumbs Down' as name from
1339+
(select d.date, coalesce(sum(fd.thumbs_down), 0)::int as total_thumbs_down
1340+
from
1341+
dates d
1342+
left join feedback_data fd on d.date = fd.date
1343+
group by
1344+
d.date
1345+
order by
1346+
d.date ) r;
1347+
`;
1348+
ctx.body = { data: res };
1349+
return;
1350+
}
1351+
});
1352+
11841353
analytics.get("/feedback-ratio", async (ctx: Context) => {
11851354
const { projectId } = ctx.state;
11861355
const { datesQuery, filteredRunsQuery, granularity } = parseQuery(

packages/frontend/components/analytics/Charts/ChartComponent.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useAnalyticsChartData } from "@/utils/dataHooks/analytics";
22
import { BarChart } from "@mantine/charts";
33
import { Center, Flex, Loader, Overlay, Text } from "@mantine/core";
44
import { useMemo } from "react";
5-
import { Chart, LogicNode } from "shared";
5+
import { Chart, DEFAULT_CHARTS, LogicNode } from "shared";
66
import { generateSeries } from "../ChartCreator";
77
import TopLanguages from "../TopLanguages";
88
import TopTemplates from "../TopTemplates";
@@ -156,14 +156,17 @@ export default function ChartComponent({
156156
"run-types",
157157
"latency",
158158
"feedback-ratio",
159+
"thumbs-up",
160+
"feedback/thumb/up",
161+
"feedback/thumb/down",
159162
].includes(dataKey)
160163
) {
161164
return (
162165
<AreaChartComponent
163166
data={data}
164167
granularity={granularity}
165168
dataKey={dataKey}
166-
color={color}
169+
color={DEFAULT_CHARTS[dataKey]?.color || color} // TODO: fix color not being saved properly in DB
167170
aggregationMethod={aggregationMethod}
168171
stat={stat}
169172
/>

packages/frontend/utils/analytics.ts

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ export const ALL_CHARTS = {
105105
"run-types",
106106
"latency",
107107
"feedback-ratio",
108+
"feedback/thumb/up",
109+
"feedback/thumb/down",
108110
"top-topics",
109111
"sentiments",
110112
],

packages/shared/dashboards/index.ts

+18
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ export const DEFAULT_CHARTS: Record<string, DefaultChart> = {
145145
aggregationMethod: "avg",
146146
color: "blue",
147147
},
148+
"feedback/thumb/up": {
149+
id: "feedback/thumb/up",
150+
name: "Total Thumbs Up",
151+
description: "The total of thumbs up feedbacks",
152+
type: "Area",
153+
dataKey: "feedback/thumb/up",
154+
aggregationMethod: "sum",
155+
color: "green",
156+
},
157+
"feedback/thumb/down": {
158+
id: "feedback/thumb/down",
159+
name: "Total Thumbs Down",
160+
description: "The total of thumbs down feedbacks",
161+
type: "Area",
162+
dataKey: "feedback/thumb/down",
163+
aggregationMethod: "sum",
164+
color: "red",
165+
},
148166
"languages/top": {
149167
id: "languages/top",
150168
name: "Top Languages",

0 commit comments

Comments
 (0)