From 52e45b89dbac7e64f60ede22ee9bda04d2777313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikko=20Pyykk=C3=B6?= Date: Mon, 13 Dec 2021 12:58:04 +0200 Subject: [PATCH 1/4] try streaming user app datum json --- .../api/v8/user_app_datum_controller.rb | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v8/user_app_datum_controller.rb b/app/controllers/api/v8/user_app_datum_controller.rb index 52303c8a2..79f910b01 100644 --- a/app/controllers/api/v8/user_app_datum_controller.rb +++ b/app/controllers/api/v8/user_app_datum_controller.rb @@ -1,5 +1,4 @@ # frozen_string_literal: true - module Api module V8 class UserAppDatumController < Api::V8::BaseController @@ -13,7 +12,28 @@ def index return render json: data end - render json: UserAppDatum.all + headers['X-Accel-Buffering'] = 'no' + headers['Cache-Control'] = 'no-cache' + headers['Content-Type'] = 'application/json' + # headers['Transfer-Encoding'] = 'chunked' + headers.delete('Content-Length') + + self.response_body = build_json_enumerator(-> { UserAppDatum.all }) + # render json: UserAppDatum.all + end + + private + def build_json_enumerator(query) + first = true + Enumerator.new do |yielder| + yielder << '[' + query.call.each do |datum| + yielder << ',' unless first + yielder << datum.to_json + first = false + end + yielder << ']' + end end end end From b6df0e0ad4061bbbbb7444f99dfede92772c49c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikko=20Pyykk=C3=B6?= Date: Mon, 13 Dec 2021 13:06:46 +0200 Subject: [PATCH 2/4] fix offenses --- app/controllers/api/v8/user_app_datum_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v8/user_app_datum_controller.rb b/app/controllers/api/v8/user_app_datum_controller.rb index 79f910b01..ce7ee0553 100644 --- a/app/controllers/api/v8/user_app_datum_controller.rb +++ b/app/controllers/api/v8/user_app_datum_controller.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module Api module V8 class UserAppDatumController < Api::V8::BaseController @@ -15,7 +16,7 @@ def index headers['X-Accel-Buffering'] = 'no' headers['Cache-Control'] = 'no-cache' headers['Content-Type'] = 'application/json' - # headers['Transfer-Encoding'] = 'chunked' + # headers['Transfer-Encoding'] = 'chunked' headers.delete('Content-Length') self.response_body = build_json_enumerator(-> { UserAppDatum.all }) From 3906844b6e7df4fdca2e1ff35dd8d41b56396fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikko=20Pyykk=C3=B6?= Date: Mon, 13 Dec 2021 14:06:38 +0200 Subject: [PATCH 3/4] use find_each instead of all --- app/controllers/api/v8/user_app_datum_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v8/user_app_datum_controller.rb b/app/controllers/api/v8/user_app_datum_controller.rb index ce7ee0553..061409db9 100644 --- a/app/controllers/api/v8/user_app_datum_controller.rb +++ b/app/controllers/api/v8/user_app_datum_controller.rb @@ -19,7 +19,7 @@ def index # headers['Transfer-Encoding'] = 'chunked' headers.delete('Content-Length') - self.response_body = build_json_enumerator(-> { UserAppDatum.all }) + self.response_body = build_json_enumerator(-> { UserAppDatum.find_each }) # render json: UserAppDatum.all end From c514424ea3ee6f37fcd53413668ab08047f2badb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikko=20Pyykk=C3=B6?= Date: Mon, 13 Dec 2021 14:10:36 +0200 Subject: [PATCH 4/4] remove lambda thingy, use find_each inside numerator --- app/controllers/api/v8/user_app_datum_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v8/user_app_datum_controller.rb b/app/controllers/api/v8/user_app_datum_controller.rb index 061409db9..a1891e1b7 100644 --- a/app/controllers/api/v8/user_app_datum_controller.rb +++ b/app/controllers/api/v8/user_app_datum_controller.rb @@ -19,7 +19,7 @@ def index # headers['Transfer-Encoding'] = 'chunked' headers.delete('Content-Length') - self.response_body = build_json_enumerator(-> { UserAppDatum.find_each }) + self.response_body = build_json_enumerator(UserAppDatum) # render json: UserAppDatum.all end @@ -28,7 +28,7 @@ def build_json_enumerator(query) first = true Enumerator.new do |yielder| yielder << '[' - query.call.each do |datum| + query.find_each do |datum| yielder << ',' unless first yielder << datum.to_json first = false