diff --git a/react/src/components/Products.jsx b/react/src/components/Products.jsx
index 565dae03..4802e510 100644
--- a/react/src/components/Products.jsx
+++ b/react/src/components/Products.jsx
@@ -134,13 +134,11 @@ function Products({ frontendSlowdown, backend, productsExtremelySlow, productsBe
{products.map((product, i) => {
- // Ensure reviews is an array and handle edge cases
- const reviews = Array.isArray(product.reviews) ? product.reviews : [];
const averageRating = (
- reviews.length > 0
- ? reviews.reduce((a, b) => a + (b['rating'] || 0), 0) / reviews.length
- : 0
+ product.reviews.reduce((a, b) => a + (b['rating'] || 0), 0) /
+ product.reviews.length
).toFixed(1);
+
let stars = [1, 2, 3, 4, 5].map((index) => {
if (index <= averageRating) {
return (
diff --git a/ruby-on-rails/app/controllers/api/v1/products_controller.rb b/ruby-on-rails/app/controllers/api/v1/products_controller.rb
index ace44196..69ef7197 100644
--- a/ruby-on-rails/app/controllers/api/v1/products_controller.rb
+++ b/ruby-on-rails/app/controllers/api/v1/products_controller.rb
@@ -3,18 +3,32 @@ def index
# get Sentry tags in application_controller.rb
set_Sentry_tags
-
transaction = Sentry.get_current_scope.get_transaction || Sentry.start_transaction(name: "custom transaction")
span_products_db = transaction.start_child(op: "custom.products_db_call")
- # Eager load reviews to prevent n+1 queries and ensure proper serialization
- products = Products.includes(:reviews)
- .select("id, title, description, descriptionfull, price, img, imgcropped")
+ sleep 0.25
+ products = Products.select("id, title, description, descriptionfull, price, img, imgcropped, Null as pg_sleep, Null as reviews")
+ sleep 0.25
span_products_db.finish
- # Use the serializer to properly format the response
- render json: products, each_serializer: ProductSerializer, status: 200
- end
+ # n+1 to db if done this way
+ products.each do |prod_slow|
+ span_products_slow_db = transaction.start_child(op: "custom.reviews_slow_db_call")
+ prod_slow["pg_sleep"] = ""
+ prod_slow["reviews"] = []
+ prod_slow["reviews"] = Reviews.select("id, productid, rating, customerid, description, created, Null as pg_sleep").where("productid="+prod_slow.id.to_s)
+ span_products_slow_db.finish
+ end
+
+ # fewer db calls this way -- done in products-join
+ # span_reviews_db = transaction.start_child(op: "custom.reviews_db_call")
+ # sleep 0.25
+ # reviews = Reviews.select("id, productid, rating, customerid, description, created, Null as pg_sleep")
+ # sleep 0.25
+ # span_reviews_db.finish
+
+ # span_response = transaction.start_child(op: "custom.construct_response_object")
+ # products.each do |prod|
# prod["pg_sleep"] = ""
# reviews_arr = []
# reviews.each do |review|
@@ -26,7 +40,7 @@ def index
# end
# span_response.finish
- render json: products.to_a, status: 200
+ render json: products, each_serializer: ProductSerializer, status: 200
end
# sent here if unexpected route was enterered