app/controllers/cohortly/reports_controller.rb in cohortly-0.0.9.1 vs app/controllers/cohortly/reports_controller.rb in cohortly-0.0.92
- old
+ new
@@ -1,25 +1,58 @@
class Cohortly::ReportsController < Cohortly::CohortlyController
def index
Cohortly::Metric.send :attr_accessor, :groups
- @metric_search = Cohortly::Metric.new(params[:cohortly_metric])
- tags = @metric_search.tags.any? ? @metric_search.tags : nil
- groups = @metric_search.groups
+ Cohortly::Metric.send :attr_accessor, :groups_intersect
+ @metric_search = Cohortly::Metric.new(params[:cohortly_metric])
+ json_res = { }
+
+ if params[:cohortly_metric]
+ tags = @metric_search.tags.any? ? @metric_search.tags : ['upload']
+ groups = @metric_search.groups
+ groups_intersect = @metric_search.groups_intersect
+
+ reports = Cohortly::TagReport.where(:tags => tags).all
+
+ @tag_report = reports.inject(Cohortly::TagReport.new) { |accum, tag_report| accum.merge(tag_report) }
+
+ user_base_func = lambda { |user_ids| user_ids.length }
+
+ if groups || groups_intersect
+ users_constraint = false
+ if groups && groups.any?
+ users_constraint = Cohortly::UserCohort.union(Cohortly::GroupCohort.where(:name => groups).all)
+ end
+ if groups_intersect && groups_intersect.any?
+ group_intersect_users = Cohortly::UserCohort.intersect(Cohortly::GroupCohort.where(:name => groups_intersect).all)
+ users_constraint = users_constraint ? users_constraint & group_intersect_users : group_intersect_users
+ end
+ if users_constraint
+ user_base_func = lambda { |user_ids| (users_constraint & user_ids).length }
+ @tag_report.intersect(users_constraint.map(&:to_s))
+ end
+ end
- @report_name = Cohortly::Metric.report_table_name(tags, groups, true)
- # run this in background would be better
+ @base_n = Cohortly::PeriodCohort.all.inject({ }) do |base_n, per_coh|
+ base_n.merge( per_coh.name => {
+ :count => user_base_func.call(per_coh.user_ids),
+ :pretty_date => DateTime.strptime( per_coh.name, '%Y-%W').beginning_of_week.strftime('%m-%d-%Y') } )
+ end
- if Cohortly::Metric.respond_to? :delay
- Cohortly::Metric.delay.weekly_cohort_chart_for_tag(tags, groups)
- else
- Cohortly::Metric.weekly_cohort_chart_for_tag(tags, groups)
+ json_res = {
+ :groups => groups,
+ :groups_intersect => groups_intersect,
+ :tags => tags,
+ :weekly => true,
+ :data => @tag_report.data_without_empty_rows,
+ :base_n => @base_n
+ }
end
- @report = Cohortly::Report.new( tags, groups, true )
-
+
respond_to do |format|
format.html
format.js {
- render :json => @report
+ render :json => json_res
}
end
end
+
end