Skip to content

Commit ee50da6

Browse files
authored
feat: ratings API fixes and UI in place. (#67)
* refactor: post rating api * dep: added recharts to the dependencies * fix: added the UI for the charts * fix: fetch the count details for graph from backend * fix: exception when count key is not present * fix: exception due to updated_at typo * fix: fallback condition for ui if no ratings available
1 parent 4485488 commit ee50da6

File tree

6 files changed

+78
-23
lines changed

6 files changed

+78
-23
lines changed

backend/packages.py

+36-21
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ def get_package(namespace_name, package_name):
516516
"description": package_obj.description,
517517
"ratings": round(sum(package_obj.ratings['users'].values())/len(package_obj.ratings['users']),3) if len(package_obj.ratings['users']) > 0 else 0,
518518
"downloads": package_obj.downloads_stats,
519+
"ratings_count": package_obj.ratings["counts"] if "counts" in package_obj.ratings else {},
519520
}
520521

521522
return jsonify({"data": package_response_data, "code": 200})
@@ -892,35 +893,49 @@ def post_ratings(namespace, package):
892893
}
893894
return jsonify({"message": error_message}), 404
894895

895-
if user["_id"] in package_doc["ratings"]["users"] and package_doc["ratings"][
896-
"users"
897-
][user["_id"]] == int(rating):
898-
return jsonify({"message": "Ratings Submitted Successfully", "code": 200}), 200
899-
900-
if user["_id"] in package_doc["ratings"]["users"] and package_doc["ratings"][
901-
"users"
902-
][user["_id"]] != int(rating):
903-
package_version_doc = db.packages.update_one(
904-
{"name": package, "namespace": namespace_doc["_id"]},
905-
{
906-
"$set": {
907-
f"ratings.users.{user['_id']}": int(rating),
908-
},
909-
},
910-
)
911-
return jsonify({"message": "Ratings Updated Successfully", "code": 200}), 200
912-
913-
package_version_doc = db.packages.update_one(
896+
db.packages.update_one(
914897
{"name": package, "namespace": namespace_doc["_id"]},
915898
{
916899
"$set": {
917900
f"ratings.users.{user['_id']}": int(rating),
918901
},
919-
"$inc": {
920-
"ratings.total_count": 1,
902+
},
903+
)
904+
905+
# Iterate through ratings and calculate how many users rated 5, 4, 3, 2, 1.
906+
ratings = db.packages.find_one(
907+
{"name": package, "namespace": namespace_doc["_id"]}
908+
)["ratings"]["users"]
909+
910+
ratings_count = {
911+
"5": 0,
912+
"4": 0,
913+
"3": 0,
914+
"2": 0,
915+
"1": 0,
916+
}
917+
918+
for user_id, user_rating in ratings.items():
919+
if user_rating == 5:
920+
ratings_count["5"] += 1
921+
elif user_rating == 4:
922+
ratings_count["4"] += 1
923+
elif user_rating == 3:
924+
ratings_count["3"] += 1
925+
elif user_rating == 2:
926+
ratings_count["2"] += 1
927+
elif user_rating == 1:
928+
ratings_count["1"] += 1
929+
930+
db.packages.update_one(
931+
{"name": package, "namespace": namespace_doc["_id"]},
932+
{
933+
"$set": {
934+
"ratings.counts": ratings_count,
921935
},
922936
},
923937
)
938+
924939
return jsonify({"message": "Ratings Submitted Successfully", "code": 200}), 200
925940

926941

backend/user.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def profile(username):
118118
"name": package["name"],
119119
"namespace": namespace["namespace"],
120120
"description": package["description"],
121-
"updatedAt": package["updatedAt"],
121+
"updatedAt": package["updated_at"],
122122
"isNamespaceMaintainer": isNamespaceMaintainer,
123123
"isPackageMaintainer": isPackageMaintainer,
124124
}

frontend/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"react-router-dom": "^6.11.2",
2121
"react-scripts": "5.0.1",
2222
"react-toastify": "^9.1.3",
23+
"recharts": "^2.12.0",
2324
"redux": "^4.2.1",
2425
"redux-persist": "^6.0.0",
2526
"styled-components": "^5.3.10",

frontend/src/pages/package.js

+14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
import ShowUserListDialog from "./showUserListDialog";
2525
import ReportPackageForm from "./reportPackageForm";
2626
import { Button } from "react-bootstrap";
27+
import PackageRatingGraph from "./packageRatingGraph";
2728

2829
const PackagePage = () => {
2930
const [iconsActive, setIconsActive] = useState("readme");
@@ -106,6 +107,14 @@ const PackagePage = () => {
106107
<MDBIcon fas icon="tag" className="me-2" /> Versions
107108
</MDBTabsLink>
108109
</MDBTabsItem>
110+
<MDBTabsItem>
111+
<MDBTabsLink
112+
onClick={() => handleIconsClick("stats")}
113+
active={iconsActive === "stats"}
114+
>
115+
<MDBIcon fas icon="chart-bar" className="me-2" /> Stats
116+
</MDBTabsLink>
117+
</MDBTabsItem>
109118
</MDBTabs>
110119

111120
<MDBTabsContent sm={4}>
@@ -179,6 +188,11 @@ const PackagePage = () => {
179188
</MDBRow>
180189
</MDBContainer>
181190
</MDBTabsPane>
191+
<MDBTabsPane show={iconsActive === "stats"}>
192+
<MDBContainer>
193+
<PackageRatingGraph data={data.ratings_count} />
194+
</MDBContainer>
195+
</MDBTabsPane>
182196
</MDBTabsContent>
183197
<ReportPackageForm
184198
namespace={namespace_name}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from "react";
2+
import { BarChart, Bar, XAxis, Tooltip } from "recharts";
3+
4+
const PackageRatingGraph = ({ data }) => {
5+
const graphData = data;
6+
7+
const parsedArray = Object.entries(graphData).map(([key, value]) => ({
8+
name: `${key} ⭐`,
9+
star: value,
10+
}));
11+
12+
return parsedArray.length === 0 ? (
13+
<div>
14+
No stats available right now. This will update as soon as the package gets
15+
rated.
16+
</div>
17+
) : (
18+
<BarChart width={600} height={500} data={parsedArray}>
19+
<XAxis dataKey="name" />
20+
<Bar dataKey="star" fill="#8884d8" />
21+
<Tooltip />
22+
</BarChart>
23+
);
24+
};
25+
26+
export default PackageRatingGraph;

frontend/src/pages/reportPackageForm.js

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const ReportPackageForm = (props) => {
2727
};
2828

2929
useEffect(() => {
30-
console.log("Inside use effect");
3130
if (statusCode === 200) {
3231
toast.success(message);
3332
} else {

0 commit comments

Comments
 (0)