Spaces:
Running
Running
File size: 9,163 Bytes
16b7924 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
import {
Badge,
Card,
Center,
Group,
Progress,
SimpleGrid,
Stack,
Text,
ThemeIcon,
Title,
} from "@mantine/core";
import { IconSearch } from "@tabler/icons-react";
import { useMemo } from "react";
import { useSearchHistory } from "../../hooks/useSearchHistory";
import type { SearchEntry } from "../../modules/history";
import { formatRelativeTime } from "../../modules/stringFormatters";
interface SearchStatsProps {
period?: "today" | "week" | "month" | "all";
compact?: boolean;
}
interface StatsData {
totalSearches: number;
avgPerDay: number;
mostActiveHour: number;
topSources: { source: string; count: number; percentage: number }[];
recentActivity: SearchEntry[];
searchTrends: { date: string; count: number }[];
}
export default function SearchStats({
period = "week",
compact = false,
}: SearchStatsProps) {
const { recentSearches, isLoading } = useSearchHistory({ limit: 1000 });
const stats = useMemo((): StatsData => {
if (!recentSearches.length) {
return {
totalSearches: 0,
avgPerDay: 0,
mostActiveHour: 0,
topSources: [],
recentActivity: [],
searchTrends: [],
};
}
const now = new Date();
const filterDate = new Date();
switch (period) {
case "today":
filterDate.setHours(0, 0, 0, 0);
break;
case "week":
filterDate.setDate(now.getDate() - 7);
break;
case "month":
filterDate.setDate(now.getDate() - 30);
break;
default:
filterDate.setFullYear(2000);
}
const filteredSearches = recentSearches.filter(
(search) => search.timestamp >= filterDate.getTime(),
);
const totalSearches = filteredSearches.length;
const daysDiff = Math.max(
1,
Math.ceil((now.getTime() - filterDate.getTime()) / (1000 * 60 * 60 * 24)),
);
const avgPerDay = Math.round(totalSearches / daysDiff);
const hourCounts = new Array(24).fill(0);
filteredSearches.forEach((search) => {
const hour = new Date(search.timestamp).getHours();
hourCounts[hour]++;
});
const mostActiveHour = hourCounts.indexOf(Math.max(...hourCounts));
const sourceCounts = filteredSearches.reduce(
(acc, search) => {
if (compact && search.source.toLowerCase() === "user") {
return acc;
}
acc[search.source] = (acc[search.source] || 0) + 1;
return acc;
},
{} as Record<string, number>,
);
const sourcesTotal = Object.values(sourceCounts).reduce(
(sum, n) => sum + n,
0,
);
const topSources = Object.entries(sourceCounts)
.map(([source, count]) => ({
source: source.charAt(0).toUpperCase() + source.slice(1),
count,
percentage:
sourcesTotal > 0 ? Math.round((count / sourcesTotal) * 100) : 0,
}))
.sort((a, b) => b.count - a.count);
const recentActivity = filteredSearches
.sort((a, b) => b.timestamp - a.timestamp)
.slice(0, 10);
const trends = new Map<string, number>();
filteredSearches.forEach((search) => {
const date = new Date(search.timestamp).toISOString().split("T")[0];
trends.set(date, (trends.get(date) || 0) + 1);
});
const searchTrends = Array.from(trends.entries())
.map(([date, count]) => ({ date, count }))
.sort((a, b) => a.date.localeCompare(b.date));
return {
totalSearches,
avgPerDay,
mostActiveHour,
topSources,
recentActivity,
searchTrends,
};
}, [recentSearches, period, compact]);
const getSourceColor = (source: string) => {
const colors = {
User: "blue",
Followup: "green",
Suggestion: "orange",
};
return colors[source as keyof typeof colors] || "gray";
};
const formatHour = (hour: number) => {
const period = hour >= 12 ? "PM" : "AM";
const displayHour = hour === 0 ? 12 : hour > 12 ? hour - 12 : hour;
return `${displayHour}:00 ${period}`;
};
if (isLoading) {
return (
<Card withBorder>
<Center h={200}>
<Text c="dimmed">Loading analytics...</Text>
</Center>
</Card>
);
}
if (stats.totalSearches === 0) {
return (
<Card withBorder>
<Center h={200}>
<Stack align="center" gap="xs">
<ThemeIcon size="xl" variant="light" color="gray">
<IconSearch size={24} />
</ThemeIcon>
<Text c="dimmed">No search data available</Text>
<Text size="sm" c="dimmed">
Start searching to see analytics
</Text>
</Stack>
</Center>
</Card>
);
}
function MetricCard({
title,
value,
}: {
title: string;
value: string | number;
}) {
return (
<Card withBorder p={compact ? "sm" : "md"}>
<Stack gap="xs" align="flex-start">
<Text size="xs" tt="uppercase" fw={700} c="dimmed">
{title}
</Text>
<Text fw={700} size={compact ? "lg" : "xl"}>
{value}
</Text>
</Stack>
</Card>
);
}
return (
<Stack gap={compact ? "sm" : "md"}>
<SimpleGrid cols={compact ? 2 : { base: 1, sm: 2, lg: 4 }}>
<MetricCard
title="Total Searches"
value={stats.totalSearches.toLocaleString()}
/>
<MetricCard title="Daily Average" value={stats.avgPerDay} />
<MetricCard
title="Most Active Hour"
value={formatHour(stats.mostActiveHour)}
/>
<MetricCard
title="Last Search"
value={
stats.recentActivity.length > 0
? formatRelativeTime(stats.recentActivity[0].timestamp)
: "Never"
}
/>
</SimpleGrid>
<SimpleGrid cols={compact ? 1 : { base: 1, md: 2 }}>
{!(compact && stats.topSources.length === 0) && (
<Card withBorder>
<Card.Section p={compact ? "sm" : "md"}>
<Title order={compact ? 5 : 4} mb={compact ? "xs" : "md"}>
Search Sources
</Title>
{stats.topSources.length > 0 ? (
<Stack gap="xs">
{stats.topSources.map((source) => (
<Group key={source.source} justify="space-between">
<Group gap="xs">
<Badge
color={getSourceColor(source.source)}
variant="dot"
size="sm"
>
{source.source}
</Badge>
<Text size={compact ? "xs" : "sm"}>
{source.count} searches
</Text>
</Group>
<Group gap="xs">
<Progress
value={source.percentage}
size="sm"
w={compact ? 40 : 60}
color={getSourceColor(source.source)}
/>
<Text size="xs" c="dimmed" w={30}>
{source.percentage}%
</Text>
</Group>
</Group>
))}
</Stack>
) : (
<Text c="dimmed" size="sm">
No data available
</Text>
)}
</Card.Section>
</Card>
)}
</SimpleGrid>
{stats.searchTrends.length > 1 && (
<Card withBorder>
<Card.Section p="md">
<Title order={4} mb="md">
Search Trends
</Title>
<Text size="sm" c="dimmed">
Daily search activity over the selected period
</Text>
<Stack gap="xs" mt="md">
{stats.searchTrends.slice(-7).map((trend) => {
const maxCount = Math.max(
...stats.searchTrends.map((t) => t.count),
);
const percentage =
maxCount > 0 ? (trend.count / maxCount) * 100 : 0;
return (
<Group key={trend.date} justify="space-between">
<Text size="xs" c="dimmed" w={80}>
{new Date(trend.date).toLocaleDateString("en", {
month: "short",
day: "numeric",
})}
</Text>
<Group gap="xs" style={{ flex: 1 }}>
<Progress
value={percentage}
size="sm"
style={{ flex: 1 }}
color="blue"
/>
<Text size="xs" w={20}>
{trend.count}
</Text>
</Group>
</Group>
);
})}
</Stack>
</Card.Section>
</Card>
)}
</Stack>
);
}
|