Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
hughcrt committed Feb 5, 2025
1 parent 8ac60a6 commit 8beb7cf
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 4 deletions.
49 changes: 47 additions & 2 deletions packages/backend/src/api/v1/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1232,8 +1232,11 @@ analytics.get("/templates/top", async (ctx: Context) => {
analytics.get("/languages/top", async (ctx: Context) => {
const { projectId } = ctx.state;
ctx.query.granularity = "daily";
const { datesQuery, startDate, endDate, timeZone, filteredRunsQuery } =
parseQuery(projectId, ctx.querystring, ctx.query);
const { datesQuery, filteredRunsQuery } = parseQuery(
projectId,
ctx.querystring,
ctx.query,
);

const data = await sql`
with dates as (
Expand Down Expand Up @@ -1269,6 +1272,48 @@ analytics.get("/languages/top", async (ctx: Context) => {
ctx.body = { data };
});

analytics.get("/topics/top", async (ctx: Context) => {
const { projectId } = ctx.state;
ctx.query.granularity = "daily";
const { datesQuery, filteredRunsQuery } = parseQuery(
projectId,
ctx.querystring,
ctx.query,
);

const data = await sql`
with dates as (
${datesQuery}
),
filtered_runs as (
${filteredRunsQuery}
)
select
t.topic,
count(*) as count
from
dates d
left join filtered_runs r on d.date = r.local_created_at
join evaluation_result_v2 er on r.id = er.run_id
join evaluator e on er.evaluator_id = e.id
cross join lateral (
select distinct
elem #>> '{}' as topic
from
jsonb_array_elements(jsonb_path_query_array(er.result, '$.input[*].topic') || jsonb_path_query_array(er.result, '$.output[*].topic')) as elem
) t
where
e.type = 'topics'
and t.topic is not null
group by
t.topic
order by
count desc;
;`;

ctx.body = { data };
});

analytics.get("/custom-events", async (ctx: Context) => {
const { projectId } = ctx.state;
const { startDate, endDate, timeZone, filteredRunsQuery } = parseQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { Center, Flex, Loader, Overlay, Text } from "@mantine/core";
import { useMemo } from "react";
import { Chart, LogicNode } from "shared";
import { generateSeries } from "../ChartCreator";
import TopLanguages from "../TopLanguages";
import TopTemplates from "../TopTemplates";
import TopTopics from "../TopTopics";
import TopUsers from "../TopUsers";
import AreaChartComponent from "./AreaChartComponent";
import TopModels from "./TopModels";
import TopLanguages from "../TopLanguages";
import TopUsers from "../TopUsers";

interface ChartProps {
id: string;
Expand Down Expand Up @@ -134,6 +135,10 @@ export default function ChartComponent({
return <TopLanguages data={data} />;
}

if (dataKey === "topics/top") {
return <TopTopics data={data} />;
}

if (
[
"tokens",
Expand Down
65 changes: 65 additions & 0 deletions packages/frontend/components/analytics/TopTopics.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Box, Center, Overlay, Text } from "@mantine/core";
import BarList from "./BarList";

interface TopTopicsData {
topic: string;
count: number;
}

export default function TopTopics({ data }: { data: TopTopicsData[] }) {
data = data.filter((topic) => topic.count > 0);

if (data.length === 0) {
return (
<Center ta="center" h="100%" w="100%">
<Overlay blur={5} opacity={0.1} p="lg" zIndex={1} />
<Text>No data available for this period</Text>
</Center>
);
}

return (
<Box px="md">
<Box h="20px"></Box>
<BarList
filterZero={false}
data={data?.map((topic) => ({
value: topic.count,
barSections: [
{
value: "count",
tooltip: "Count",
count: topic.count,
color: "var(--mantine-color-blue-4)",
},
],
...topic,
}))}
columns={[
{
name: "Topics",
bar: true,
main: true,
key: "count",
},
{
name: "Name",
key: "topic",
render: (_, { topic }) => (
<Text
size="sm"
px="md"
style={{
overflow: "hidden",
textOverflow: "ellipsis",
}}
>
{topic}
</Text>
),
},
]}
/>
</Box>
);
}
8 changes: 8 additions & 0 deletions packages/shared/dashboards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ export const DEFAULT_CHARTS = {
dataKey: "languages/top",
aggregationMethod: null,
},
"topics/top": {
id: "topics/top",
name: "Top Topics",
description: "The number of topics occurrence in your project",
type: "Top",
dataKey: "topics/top",
aggregationMethod: null,
},
};

export const chartProps = Object.keys(DEFAULT_CHARTS);
Expand Down

0 comments on commit 8beb7cf

Please sign in to comment.