Skip to content

Commit f758df9

Browse files
committed
[react, rails] rollback some unnecessary changes in autofix-generate PR #655
1 parent d6c1a96 commit f758df9

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

react/src/components/Products.jsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,11 @@ function Products({ frontendSlowdown, backend, productsExtremelySlow, productsBe
134134
<div>
135135
<ul className="products-list">
136136
{products.map((product, i) => {
137-
// Ensure reviews is an array and handle edge cases
138-
const reviews = Array.isArray(product.reviews) ? product.reviews : [];
139137
const averageRating = (
140-
reviews.length > 0
141-
? reviews.reduce((a, b) => a + (b['rating'] || 0), 0) / reviews.length
142-
: 0
138+
product.reviews.reduce((a, b) => a + (b['rating'] || 0), 0) /
139+
product.reviews.length
143140
).toFixed(1);
141+
144142
let stars = [1, 2, 3, 4, 5].map((index) => {
145143
if (index <= averageRating) {
146144
return (

ruby-on-rails/app/controllers/api/v1/products_controller.rb

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,32 @@ def index
33
# get Sentry tags in application_controller.rb
44
set_Sentry_tags
55

6-
76
transaction = Sentry.get_current_scope.get_transaction || Sentry.start_transaction(name: "custom transaction")
87

98
span_products_db = transaction.start_child(op: "custom.products_db_call")
10-
# Eager load reviews to prevent n+1 queries and ensure proper serialization
11-
products = Products.includes(:reviews)
12-
.select("id, title, description, descriptionfull, price, img, imgcropped")
9+
sleep 0.25
10+
products = Products.select("id, title, description, descriptionfull, price, img, imgcropped, Null as pg_sleep, Null as reviews")
11+
sleep 0.25
1312
span_products_db.finish
1413

15-
# Use the serializer to properly format the response
16-
render json: products, each_serializer: ProductSerializer, status: 200
17-
end
14+
# n+1 to db if done this way
15+
products.each do |prod_slow|
16+
span_products_slow_db = transaction.start_child(op: "custom.reviews_slow_db_call")
17+
prod_slow["pg_sleep"] = ""
18+
prod_slow["reviews"] = []
19+
prod_slow["reviews"] = Reviews.select("id, productid, rating, customerid, description, created, Null as pg_sleep").where("productid="+prod_slow.id.to_s)
20+
span_products_slow_db.finish
21+
end
22+
23+
# fewer db calls this way -- done in products-join
24+
# span_reviews_db = transaction.start_child(op: "custom.reviews_db_call")
25+
# sleep 0.25
26+
# reviews = Reviews.select("id, productid, rating, customerid, description, created, Null as pg_sleep")
27+
# sleep 0.25
28+
# span_reviews_db.finish
29+
30+
# span_response = transaction.start_child(op: "custom.construct_response_object")
31+
# products.each do |prod|
1832
# prod["pg_sleep"] = ""
1933
# reviews_arr = []
2034
# reviews.each do |review|
@@ -26,7 +40,7 @@ def index
2640
# end
2741
# span_response.finish
2842

29-
render json: products.to_a, status: 200
43+
render json: products, each_serializer: ProductSerializer, status: 200
3044
end
3145

3246
# sent here if unexpected route was enterered

0 commit comments

Comments
 (0)