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

Implement questions answered vs questions asked #4256

Merged
merged 4 commits into from
Dec 22, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def filter_questions_by_tag(questions, tagnames)
public

def index
@stats = helpers.filtering(params[:period])
@title = 'Questions and Answers'
set_sidebar
@questions = Node.questions
Expand Down
25 changes: 25 additions & 0 deletions app/helpers/questions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module QuestionsHelper
SORTING_OPTIONS = %w(All Week Month Year).freeze

def filtering(period)
Copy link
Member

Choose a reason for hiding this comment

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

This looks good but perhaps it could have a more descriptive name -- something describing how it's related to stats, or Q&A? Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Also, I'd love to see a simple test for these methods. Where would that live?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Let me try and figure out where the tests will go. Thanks

return if period.nil?

if period == 'All'
Rails.cache.fetch("all_stats", expires_in: 1.days) do
@asked = Node.questions.to_a.size
Copy link
Member

Choose a reason for hiding this comment

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

I think if you use Node.questions.count it should do a more efficient query, just a SQL SIZE query instead of fetching all the full records and counting them. Maybe same on line below! 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@jywarren tried .count was not working fine because its a hash returning results like so {82=>1, 85=>2, 86=>2, 87=>1}. Using .length instead. hope that's a better query?

@answered = Answer.all.map(&:node).uniq.size
"#{@asked} questions asked and #{@answered} questions answered"
end
else
Rails.cache.fetch("#{period}_stats", expires_in: 1.days) do
@asked = Node.questions.where('created >= ?', 1.send(period.downcase).ago.to_i).to_a.size
@answered = Answer.where("created_at >= ?", 1.send(period.downcase).ago).map(&:node).uniq.size
"#{@asked} questions asked and #{@answered} questions answered in the past #{period}"
end
end
end

def options
SORTING_OPTIONS
end
end
12 changes: 11 additions & 1 deletion app/views/questions/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<div class="col-md-12">
<div class ="row">
<div class="col-md-6" >
<h4><b> <%= @stats if params[:period].present? %></b></h4>
</div>
<div class="col-md-4">
<%= form_tag request.url, :method => 'get' do %>
<%= select_tag :period, options_for_select(options),onchange: "this.form.submit();", include_blank: "Filter stats", class: "form-control"%>
Copy link
Member

Choose a reason for hiding this comment

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

Ooh this is cool, could you share a screenshot? Thanks!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@jywarren a screenshot is ☝️ above. Thanks for the feedback.

<% end %>
</div>
</div>

<% if params[:action] == 'answered' %>
<h2>Recently Answered <small class="hidden-sm">Recently answered questions</small></h2>
Expand All @@ -14,7 +24,7 @@
<%= feature('questions-header') %>
<% end %>
</div>
<% end %>
<% end %>

<hr />

Expand Down