perf: improve page loading times by limiting user fetching

This commit is contained in:
2026-02-03 10:07:26 +00:00
parent 07ab818d95
commit 471fea39c8

View File

@@ -36,11 +36,13 @@ const StatPage = () => {
const [topUserData, setTopUserData] = useState<TopUser[]>([]);
const [wordFrequencyData, setWordFrequencyData] = useState([]);
const inputRef = useRef<HTMLInputElement>(null);
const searchInputRef = useRef<HTMLInputElement>(null);
const beforeDateRef = useRef<HTMLInputElement>(null);
const afterDateRef = useRef<HTMLInputElement>(null);
const getStats = () => {
setError("");
//setLoading(true);
setLoading(true);
Promise.all([
axios.get("http://localhost:5000/stats/time"),
@@ -49,11 +51,11 @@ const StatPage = () => {
])
.then(([timeRes, userRes, wordsRes]) => {
const eventsPerDay = Array.isArray(timeRes.data?.events_per_day)
? timeRes.data.events_per_day
? timeRes.data.events_per_day.filter((d: any) => new Date(d.date) >= new Date("2026-01-10"))
: [];
const topUsers = Array.isArray(userRes.data?.top_users)
? userRes.data.top_users
? userRes.data.top_users.slice(0, 100)
: [];
const weekdayHourHeatmap = Array.isArray(timeRes.data?.weekday_hour_heatmap)
@@ -65,9 +67,7 @@ const StatPage = () => {
: [];
setPostsPerDay(
eventsPerDay.filter(
(d: any) => new Date(d.date) >= new Date("2026-01-10")
)
eventsPerDay
);
setTopUserData(topUsers);
@@ -78,22 +78,30 @@ const StatPage = () => {
value: d.count,
}))
);
})
.catch((e) => setError("Failed to load statistics: " + String(e)))
//.finally(() => setLoading(false));
.finally(() => setLoading(false));
};
const onSearch = () => {
const query = inputRef.current?.value ?? "";
axios.post("http://localhost:5000/filter/search", {
query: query
})
const onSubmitFilters = () => {
const query = searchInputRef.current?.value ?? "";
const startDate = beforeDateRef.current?.value;
const endDate = afterDateRef.current?.value;
Promise.all([
axios.post("http://localhost:5000/filter/search", {
query: query
}),
//axios.post("http://localhost:5000/filter/time", {
//start: startDate,
//end: endDate
//})
])
.then(() => {
getStats();
})
.catch(e => {
setError(e);
setError("Failed to load filters: " + e.response);
})
};
@@ -108,6 +116,7 @@ const StatPage = () => {
};
useEffect(() => {
setError("");
getStats();
}, [])
@@ -121,12 +130,26 @@ return (
<input
type="text"
id="query"
ref={inputRef}
ref={searchInputRef}
placeholder="Search events..."
style={styles.input}
/>
<button onClick={onSearch} style={styles.buttonPrimary}>
<input
type="date"
ref={beforeDateRef}
placeholder="Search before date"
style={styles.input}
/>
<input
type="date"
ref={afterDateRef}
placeholder="Search before date"
style={styles.input}
/>
<button onClick={onSubmitFilters} style={styles.buttonPrimary}>
Search
</button>