Consultation on how to obtain user information

I have created a form as a plugin that searches for a user from a user field that I have defined independently.

I used Web API to store user information in an external DB (AWS DynamoDB) and extract data from it for searching.

class DiscourseUserSearchController < ApplicationController
  def index
  end

  def search
    dynamodb = Aws::DynamoDB::Client.new(
      access_key_id: ENV['ACCESS_KEY_ID'],
      secret_access_key: ENV['SECRET_ACCESS_KEY'],
      region: AWS_REGION
    )

    scan_condition = {
      table_name: "DiscourseUserInformation"
    }
    result = dynamodb.scan(scan_condition)

    result = result.items
    Rails.logger.info result
    data = result.map do |user_data|
      user = User.find(user_data['UserID'])
      user_data['avatar_template'] = user.avatar_template
      user_data['username'] = user.username
      next user_data

    end

    render status: 200, json: { status: 200, data: data }
  end
end

The above code gets all users when the page is opened and passes them to angular.

This works fine, but the response takes 3500 milliseconds, which is not very desirable. (about 1400 search users).

I think the problem is that I am doing User.find(user_data[‘UserID’]) in a loop, but I can’t think of a way to make it better.

I would like to display user cards, so I think it is necessary to acquire users on Ruby, but I would be happy to get suggestions on how to make it faster.

 User.find_by_sql("select * from users where id in (#{user_ids.join(',')})")
1 Like

Thanks. It looks like it will be resolved.

1 Like

So that was enough? You might also also do something like

user_ids=user_data.pluck(:UserID)
2 Likes

Thank you!
I wasn’t familiar with Ruby so I accomplished that with dirty code, but I’ll try to improve it the way you’re presenting it!

1 Like