Skip to content

Commit 4973c76

Browse files
committed
Add stats on search modal and change average length stat to float
1 parent ed9d5c8 commit 4973c76

4 files changed

Lines changed: 58 additions & 5 deletions

File tree

backend/circles/handler.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,41 @@ func SearchTextHandler(w http.ResponseWriter, r *http.Request) {
277277
json.NewEncoder(w).Encode("Failed to search circles\n")
278278
return
279279
}
280+
type Stats struct {
281+
TotalMatches int `json:"total_matches"`
282+
AverageLength float32 `json:"average_length"`
283+
MostFoundAuthor string `json:"most_found_author"`
284+
}
285+
var stats Stats
286+
stats.TotalMatches = len(results)
287+
if stats.TotalMatches > 0 {
288+
var totalLength int
289+
authorMap := make(map[string]int)
290+
for _, result := range results {
291+
totalLength += len(result.Content)
292+
authorMap[result.AuthorUsername]++
293+
}
294+
stats.AverageLength = float32(totalLength) / float32(stats.TotalMatches)
295+
var maxCount int
296+
var mostFoundAuthor string
297+
for author, count := range authorMap {
298+
if count > maxCount {
299+
maxCount = count
300+
mostFoundAuthor = author
301+
}
302+
}
303+
stats.MostFoundAuthor = mostFoundAuthor
304+
}
305+
306+
fmt.Printf("Search stats: %v\n", stats)
307+
280308
fmt.Printf("Search results: %v\n", results)
281309
w.WriteHeader(http.StatusOK)
282-
json.NewEncoder(w).Encode(results)
310+
json.NewEncoder(w).Encode(struct {
311+
Results []models.SearchMessage `json:"results"`
312+
Stats Stats `json:"stats"`
313+
}{
314+
Results: results,
315+
Stats: stats,
316+
})
283317
}

frontend/app/components/Dashboard.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
.modal {
2222
width: 24rem;
23-
max-height: 50vh;
2423
background: white;
2524
border-radius: 10px;
2625
padding: 2rem;

frontend/app/components/SearchModal.css

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.search-modal {
2-
height: 80vh;
2+
height: 52vh;
33
}
44

55
.search-result-content {
@@ -17,7 +17,13 @@
1717
.search-results {
1818
padding: 1rem;
1919
overflow-y: scroll;
20-
height: 34vh;
20+
height: 35vh;
2121
border: 1px solid hsl(0, 0%, 90%);
2222
border-radius: 5px;
23+
}
24+
25+
.search-stats {
26+
color: hsl(0, 0%, 40%);
27+
font-size: 0.85rem;
28+
margin-top: 0.6rem;
2329
}

frontend/app/components/SearchModal.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ function SearchModal({ isOpen, setOpen, circleId }: SearchModalProps) {
1717
const { getAccessToken } = authContext;
1818

1919
const [results, setResults] = useState<any[]>([]);
20+
const [totalMatches, setTotalMatches] = useState(0);
21+
const [averageLength, setAverageLength] = useState(0);
22+
const [mostFoundAuthor, setMostFoundAuthor] = useState("");
23+
2024
async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
2125
e.preventDefault();
2226
const token = await getAccessToken();
@@ -35,12 +39,17 @@ function SearchModal({ isOpen, setOpen, circleId }: SearchModalProps) {
3539
})
3640
.then(async (response) => {
3741
const data = await response.json();
38-
const mappedResults = data.map((result: any) => ({
42+
const mappedResults = data.results.map((result: any) => ({
3943
content: result.content,
4044
created_at: result.created_at,
4145
author: result.author_username
4246
}));
4347
setResults(mappedResults);
48+
setTotalMatches(data.stats.total_matches);
49+
setAverageLength(data.stats.average_length);
50+
setMostFoundAuthor(data.stats.most_found_author
51+
? data.stats.most_found_author
52+
: "No author found");
4453
if (!response.ok) {
4554
console.log("Error:", data);
4655
}
@@ -84,6 +93,11 @@ function SearchModal({ isOpen, setOpen, circleId }: SearchModalProps) {
8493
</div>
8594
))}
8695
</div>
96+
<div className="search-stats">
97+
Total matches: {totalMatches ? totalMatches : "N/A"} <br />
98+
Average match length: {averageLength ? averageLength : "N/A"} <br />
99+
Most found author: {mostFoundAuthor ? mostFoundAuthor : "N/A"}
100+
</div>
87101
</div>
88102
);
89103
}

0 commit comments

Comments
 (0)