Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify inputs (arguments) to the list_user_domains routine #8016

Merged
merged 2 commits into from
Nov 30, 2023

Conversation

yevgenko
Copy link
Contributor

Relevant issue(s)

N/A

What does this do?

This tidying makes easier to read and understand the inputs of list_user_domains routine.

Why was this needed?

Making code easier to understand makes it easier to work with.


[skip changelog]

Easier to read what is expected in params
Copy link
Member

@garethrees garethrees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much! Little code cleanup commits like this are always appreciated 💖 .

I'm wondering whether we could make a slight tweak? I'm not sure we need both list_user_domains and list_user_domains_since, since the former now doesn't really do anything.

I think the key improvement here is moving to keyword arguments, which I don't see a reason not to do in the existing list_user_domains method. Essentially, ending up with:

def self.list_user_domains(start_date: nil, limit: nil)
  sql = if start_date
    <<~SQL
    SELECT lower(substring(email, position('@' in email)+1)) AS domain,
    COUNT(id) AS count
    FROM users
    WHERE created_at >= '#{start_date}'
    GROUP BY domain
    ORDER BY count DESC
    SQL
  else
    <<~SQL
    SELECT lower(substring(email, position('@' in email)+1)) AS domain,
    COUNT(id) AS count
    FROM users
    GROUP BY domain
    ORDER BY count DESC
    SQL
  end
  sql = "#{sql} LIMIT #{limit}" if limit

  User.connection.select_all(sql).to_a
end

That looks like it should work for existing uses of list_user_domains. What do you think?

Easier to see what data is expected and harder to mis-use.
@yevgenko
Copy link
Contributor Author

I'm wondering whether we could make a slight tweak? I'm not sure we need both list_user_domains and list_user_domains_since, since the former now doesn't really do anything.

I do agree, I see the method is used in rake task and it relies on the "ruby magic" to interpret keyword arguments, so, the client won't be affected by the change. I'll push update in a minute.

@yevgenko yevgenko force-pushed the tidying-user-stats-arguments branch from 421e9ee to d0d56ca Compare November 25, 2023 12:35
Copy link
Member

@garethrees garethrees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks so much!

@gbp gbp merged commit 9e4e43a into mysociety:develop Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants