From f934a266197c794e781e3fca0076acbbc88f4830 Mon Sep 17 00:00:00 2001 From: prabhanshuguptagit Date: Thu, 17 Dec 2020 17:24:30 +0530 Subject: [PATCH 1/5] Use OJ to serialize instead of default as_json --- Gemfile | 1 + Gemfile.lock | 1 + app/controllers/api/v3/sync_controller.rb | 6 +++--- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index d335b17917..d10cf17438 100644 --- a/Gemfile +++ b/Gemfile @@ -43,6 +43,7 @@ gem "kaminari" gem "lodash-rails" gem "lograge" gem "ougai" +gem "oj" gem "parallel", require: false gem "passenger" gem "pg", ">= 0.18", "< 2.0" diff --git a/Gemfile.lock b/Gemfile.lock index 467bb82dee..69205f1ca5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -606,6 +606,7 @@ DEPENDENCIES memery memory_profiler mock_redis + oj ougai parallel parallel_tests diff --git a/app/controllers/api/v3/sync_controller.rb b/app/controllers/api/v3/sync_controller.rb index 039cc2bf41..05c7dd4e2c 100644 --- a/app/controllers/api/v3/sync_controller.rb +++ b/app/controllers/api/v3/sync_controller.rb @@ -13,10 +13,10 @@ def __sync_from_user__(params) def __sync_to_user__(response_key) AuditLog.create_logs_async(current_user, records_to_sync, "fetch", Time.current) unless disable_audit_logs? render( - json: { - response_key => records_to_sync.map { |record| transform_to_response(record) }, + json: Oj.dump({ + response_key => records.map { |record| transform_to_response(record) }, "process_token" => encode_process_token(response_process_token) - }, + }, mode: :compat), status: :ok ) end From 934f650cced7de15baa3b729422939cd2eac0965 Mon Sep 17 00:00:00 2001 From: prabhanshuguptagit Date: Thu, 17 Dec 2020 17:24:49 +0530 Subject: [PATCH 2/5] Remove redundant records_to_sync call --- app/controllers/api/v3/sync_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v3/sync_controller.rb b/app/controllers/api/v3/sync_controller.rb index 05c7dd4e2c..a25742f911 100644 --- a/app/controllers/api/v3/sync_controller.rb +++ b/app/controllers/api/v3/sync_controller.rb @@ -11,7 +11,9 @@ def __sync_from_user__(params) end def __sync_to_user__(response_key) - AuditLog.create_logs_async(current_user, records_to_sync, "fetch", Time.current) unless disable_audit_logs? + records = records_to_sync + + AuditLog.create_logs_async(current_user, records, "fetch", Time.current) unless disable_audit_logs? render( json: Oj.dump({ response_key => records.map { |record| transform_to_response(record) }, From 9f0d19b1c0ad4d4ca5ef80a58ffdc9887bbc9610 Mon Sep 17 00:00:00 2001 From: prabhanshuguptagit Date: Thu, 17 Dec 2020 17:38:27 +0530 Subject: [PATCH 3/5] Replace selected keys only in v3 transformer --- app/transformers/api/v3/transformer.rb | 26 ++++++++++++++------------ spec/utils.rb | 2 +- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/transformers/api/v3/transformer.rb b/app/transformers/api/v3/transformer.rb index 0e4c1db30c..256816fc80 100644 --- a/app/transformers/api/v3/transformer.rb +++ b/app/transformers/api/v3/transformer.rb @@ -1,31 +1,33 @@ class Api::V3::Transformer class << self - def from_request(attributes_of_payload) - rename_attributes(attributes_of_payload, key_mapping) + def from_request(payload_attributes) + rename_attributes(payload_attributes, from_request_key_mapping) end def to_response(model) - rename_attributes(model.attributes, inverted_key_mapping).as_json + rename_attributes(model.attributes, to_response_key_mapping).as_json end def rename_attributes(attributes, mapping) - mapping = mapping.with_indifferent_access - attributes - .to_hash - .except(*mapping.values) - .transform_keys! { |key| mapping[key] || key } - .with_indifferent_access + replace_keys(attributes.to_hash, mapping).with_indifferent_access end - def key_mapping + def replace_keys(hsh, mapping) + mapping.each do |k, v| + hsh[v] = hsh.delete(k) + end + hsh + end + + def from_request_key_mapping { "created_at" => "device_created_at", "updated_at" => "device_updated_at" } end - def inverted_key_mapping - key_mapping.invert + def to_response_key_mapping + from_request_key_mapping.invert end end end diff --git a/spec/utils.rb b/spec/utils.rb index 30d484bf13..fd9030c540 100644 --- a/spec/utils.rb +++ b/spec/utils.rb @@ -20,7 +20,7 @@ def to_json_and_back def with_payload_keys Api::V3::Transformer.rename_attributes( - self, Api::V3::Transformer.inverted_key_mapping + self, Api::V3::Transformer.to_response_key_mapping ) end end From e0a5f5b0da5e77b2d69a96efdbebd4e3666797c7 Mon Sep 17 00:00:00 2001 From: prabhanshuguptagit Date: Thu, 17 Dec 2020 17:50:10 +0530 Subject: [PATCH 4/5] Merge except calls --- app/transformers/api/v3/patient_transformer.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/transformers/api/v3/patient_transformer.rb b/app/transformers/api/v3/patient_transformer.rb index e1a559d0ff..07e41b801a 100644 --- a/app/transformers/api/v3/patient_transformer.rb +++ b/app/transformers/api/v3/patient_transformer.rb @@ -59,10 +59,10 @@ def from_nested_request(payload_attributes) def to_nested_response(patient) Api::V3::Transformer.to_response(patient) - .except("address_id") - .except("registration_user_id") - .except("test_data") - .except("deleted_by_user_id") + .except("address_id", + "registration_user_id", + "test_data", + "deleted_by_user_id") .merge( "address" => Api::V3::Transformer.to_response(patient.address), "phone_numbers" => patient.phone_numbers.map do |phone_number| From ea389c87faacdaac8d53223ec146fd7872b33cab Mon Sep 17 00:00:00 2001 From: prabhanshuguptagit Date: Thu, 17 Dec 2020 17:50:20 +0530 Subject: [PATCH 5/5] Don't send facility virtual attributes These are used for CSV upload, no need to serialize them. --- app/transformers/api/v3/facility_transformer.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/transformers/api/v3/facility_transformer.rb b/app/transformers/api/v3/facility_transformer.rb index 3bc9f67b74..860f549785 100644 --- a/app/transformers/api/v3/facility_transformer.rb +++ b/app/transformers/api/v3/facility_transformer.rb @@ -7,7 +7,9 @@ def to_response(facility) "enable_teleconsultation", "teleconsultation_phone_number", "teleconsultation_isd_code", - "teleconsultation_phone_numbers") + "teleconsultation_phone_numbers", + "organization_name", + "facility_group_name") .merge(config: {enable_diabetes_management: facility.enable_diabetes_management, enable_teleconsultation: facility.enable_teleconsultation}, protocol_id: facility.protocol.try(:id))