Skip to content

Commit

Permalink
Revert "Improve Sync To User performance (batch 3) (#1898)"
Browse files Browse the repository at this point in the history
This reverts commit 8ff7ba1.
  • Loading branch information
kitallis committed Dec 24, 2020
1 parent 24feffe commit 7b61d43
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 30 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ gem "kaminari"
gem "lodash-rails"
gem "lograge"
gem "ougai"
gem "oj"
gem "parallel", require: false
gem "passenger"
gem "pg", ">= 0.18", "< 2.0"
Expand Down
1 change: 0 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,6 @@ DEPENDENCIES
memery
memory_profiler
mock_redis
oj
ougai
parallel
parallel_tests
Expand Down
10 changes: 4 additions & 6 deletions app/controllers/api/v3/sync_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ def __sync_from_user__(params)
end

def __sync_to_user__(response_key)
records = records_to_sync

AuditLog.create_logs_async(current_user, records, "fetch", Time.current) unless disable_audit_logs?
AuditLog.create_logs_async(current_user, records_to_sync, "fetch", Time.current) unless disable_audit_logs?
render(
json: Oj.dump({
response_key => records.map { |record| transform_to_response(record) },
json: {
response_key => records_to_sync.map { |record| transform_to_response(record) },
"process_token" => encode_process_token(response_process_token)
}, mode: :compat),
},
status: :ok
)
end
Expand Down
4 changes: 1 addition & 3 deletions app/transformers/api/v3/facility_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ def to_response(facility)
"enable_teleconsultation",
"teleconsultation_phone_number",
"teleconsultation_isd_code",
"teleconsultation_phone_numbers",
"organization_name",
"facility_group_name")
"teleconsultation_phone_numbers")
.merge(config: {enable_diabetes_management: facility.enable_diabetes_management,
enable_teleconsultation: facility.enable_teleconsultation},
protocol_id: facility.protocol.try(:id))
Expand Down
8 changes: 4 additions & 4 deletions app/transformers/api/v3/patient_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def from_nested_request(payload_attributes)

def to_nested_response(patient)
Api::V3::Transformer.to_response(patient)
.except("address_id",
"registration_user_id",
"test_data",
"deleted_by_user_id")
.except("address_id")
.except("registration_user_id")
.except("test_data")
.except("deleted_by_user_id")
.merge(
"address" => Api::V3::Transformer.to_response(patient.address),
"phone_numbers" => patient.phone_numbers.map do |phone_number|
Expand Down
26 changes: 12 additions & 14 deletions app/transformers/api/v3/transformer.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
class Api::V3::Transformer
class << self
def from_request(payload_attributes)
rename_attributes(payload_attributes, from_request_key_mapping)
def from_request(attributes_of_payload)
rename_attributes(attributes_of_payload, key_mapping)
end

def to_response(model)
rename_attributes(model.attributes, to_response_key_mapping).as_json
rename_attributes(model.attributes, inverted_key_mapping).as_json
end

def rename_attributes(attributes, mapping)
replace_keys(attributes.to_hash, mapping).with_indifferent_access
mapping = mapping.with_indifferent_access
attributes
.to_hash
.except(*mapping.values)
.transform_keys! { |key| mapping[key] || key }
.with_indifferent_access
end

def replace_keys(hsh, mapping)
mapping.each do |k, v|
hsh[v] = hsh.delete(k)
end
hsh
end

def from_request_key_mapping
def key_mapping
{
"created_at" => "device_created_at",
"updated_at" => "device_updated_at"
}
end

def to_response_key_mapping
from_request_key_mapping.invert
def inverted_key_mapping
key_mapping.invert
end
end
end
2 changes: 1 addition & 1 deletion spec/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def to_json_and_back

def with_payload_keys
Api::V3::Transformer.rename_attributes(
self, Api::V3::Transformer.to_response_key_mapping
self, Api::V3::Transformer.inverted_key_mapping
)
end
end

0 comments on commit 7b61d43

Please sign in to comment.