FIX: reports of topics with no response and time to first response should not include replies to your own topic

This commit is contained in:
Neil Lalonde 2015-06-25 18:45:11 -04:00
parent 2eb551e10a
commit 93d7edb810
2 changed files with 45 additions and 15 deletions

View File

@ -112,7 +112,10 @@ class Report
end end
def self.report_topics_with_no_response(report) def self.report_topics_with_no_response(report)
basic_report_about report, Topic, :with_no_response_per_day, report.start_date, report.end_date, report.category_id report.data = []
Topic.with_no_response_per_day(report.start_date, report.end_date, report.category_id).each do |r|
report.data << { x: Date.parse(r["date"]), y: r["count"] }
end
report.total = Topic.with_no_response_total(category_id: report.category_id) report.total = Topic.with_no_response_total(category_id: report.category_id)
report.prev30Days = Topic.with_no_response_total(start_date: report.start_date - 30.days, end_date: report.start_date, category_id: report.category_id) report.prev30Days = Topic.with_no_response_total(start_date: report.start_date - 30.days, end_date: report.start_date, category_id: report.category_id)
end end

View File

@ -878,12 +878,13 @@ class Topic < ActiveRecord::Base
opts ||= {} opts ||= {}
builder = SqlBuilder.new(sql) builder = SqlBuilder.new(sql)
builder.where("t.created_at >= :start_date", start_date: opts[:start_date]) if opts[:start_date] builder.where("t.created_at >= :start_date", start_date: opts[:start_date]) if opts[:start_date]
builder.where("t.created_at <= :end_date", end_date: opts[:end_date]) if opts[:end_date] builder.where("t.created_at < :end_date", end_date: opts[:end_date]) if opts[:end_date]
builder.where("t.category_id = :category_id", category_id: opts[:category_id]) if opts[:category_id] builder.where("t.category_id = :category_id", category_id: opts[:category_id]) if opts[:category_id]
builder.where("t.archetype <> '#{Archetype.private_message}'") builder.where("t.archetype <> '#{Archetype.private_message}'")
builder.where("t.deleted_at IS NULL") builder.where("t.deleted_at IS NULL")
builder.where("p.deleted_at IS NULL") builder.where("p.deleted_at IS NULL")
builder.where("p.post_number > 1") builder.where("p.post_number > 1")
builder.where("p.user_id != t.user_id")
builder.where("EXTRACT(EPOCH FROM p.created_at - t.created_at) > 0") builder.where("EXTRACT(EPOCH FROM p.created_at - t.created_at) > 0")
builder.exec builder.exec
end end
@ -897,22 +898,48 @@ class Topic < ActiveRecord::Base
total.first["hours"].to_f.round(2) total.first["hours"].to_f.round(2)
end end
WITH_NO_RESPONSE_SQL ||= <<-SQL
SELECT COUNT(*) as count, tt.created_at AS "date"
FROM (
SELECT t.id, t.created_at::date AS created_at, MIN(p.post_number) first_reply
FROM topics t
LEFT JOIN posts p ON p.topic_id = t.id AND p.user_id != t.user_id AND p.deleted_at IS NULL
/*where*/
GROUP BY t.id
) tt
WHERE tt.first_reply IS NULL
GROUP BY tt.created_at
ORDER BY tt.created_at
SQL
def self.with_no_response_per_day(start_date, end_date, category_id=nil) def self.with_no_response_per_day(start_date, end_date, category_id=nil)
result = listable_topics.where(highest_post_number: 1) builder = SqlBuilder.new(WITH_NO_RESPONSE_SQL)
result = result.where("created_at BETWEEN ? AND ?", start_date, end_date) builder.where("t.created_at >= :start_date", start_date: start_date) if start_date
result = result.where(category_id: category_id) if category_id builder.where("t.created_at < :end_date", end_date: end_date) if end_date
result.group("created_at::date") builder.where("t.category_id = :category_id", category_id: category_id) if category_id
.order("created_at::date") builder.where("t.archetype <> '#{Archetype.private_message}'")
.count builder.where("t.deleted_at IS NULL")
builder.exec
end end
def self.with_no_response_total(opts=nil) WITH_NO_RESPONSE_TOTAL_SQL ||= <<-SQL
opts ||= {} SELECT COUNT(*) as count
total = listable_topics.where(highest_post_number: 1) FROM (
total = total.where("created_at >= ?", opts[:start_date]) if opts[:start_date] SELECT t.id, MIN(p.post_number) first_reply
total = total.where("created_at <= ?", opts[:end_date]) if opts[:end_date] FROM topics t
total = total.where(category_id: opts[:category_id]) if opts[:category_id] LEFT JOIN posts p ON p.topic_id = t.id AND p.user_id != t.user_id AND p.deleted_at IS NULL
total.count /*where*/
GROUP BY t.id
) tt
WHERE tt.first_reply IS NULL
SQL
def self.with_no_response_total(opts={})
builder = SqlBuilder.new(WITH_NO_RESPONSE_TOTAL_SQL)
builder.where("t.category_id = :category_id", category_id: opts[:category_id]) if opts[:category_id]
builder.where("t.archetype <> '#{Archetype.private_message}'")
builder.where("t.deleted_at IS NULL")
builder.exec.first["count"]
end end
private private