Skip to content

Commit

Permalink
[react, rails] rollback some unnecessary changes in autofix-generate PR
Browse files Browse the repository at this point in the history
  • Loading branch information
realkosty authored Jan 7, 2025
1 parent d6c1a96 commit d27844a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
8 changes: 3 additions & 5 deletions react/src/components/Products.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,11 @@ function Products({ frontendSlowdown, backend, productsExtremelySlow, productsBe
<div>
<ul className="products-list">
{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 (
Expand Down
30 changes: 22 additions & 8 deletions ruby-on-rails/app/controllers/api/v1/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Expand Down

0 comments on commit d27844a

Please sign in to comment.