Created
July 20, 2010 20:35
-
-
Save camwest/483534 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ReportsController < ApplicationController | |
| rescue_from ArgumentError, :with => :invalid_report | |
| before_filter :require_user | |
| def show | |
| respond_to do |wants| | |
| wants.json { render :json => UserReportFactory.get_report( current_user, params[:id] ), :status => :ok } | |
| wants.csv { | |
| filename = "woople_#{params[:id]}.csv" | |
| headers.merge!( | |
| 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', | |
| 'Content-Type' => 'text/csv', | |
| 'Content-Disposition' => "attachment; filename=\"#{filename}\"", | |
| 'Content-Transfer-Encoding' => 'binary' | |
| ) | |
| render :status => 200, :text => DetailedReportFactory.get_report( current_user, params[:id], current_account ) | |
| } | |
| end | |
| end | |
| class DetailedReportFactory | |
| def self.get_report(report_on, report_type, current_account) | |
| if report_on.class.to_s != "Array" | |
| report_ids = report_on.to_user_ids | |
| else | |
| report_ids = report_on | |
| end | |
| if (report_ids.length == 0) | |
| return [] | |
| end | |
| case report_type | |
| when "week" | |
| start_date = 7.days.ago | |
| when "month" | |
| start_date = 1.month.ago | |
| when "annual" | |
| start_date = 1.year.ago | |
| else | |
| raise ArgumentError, "Invalid report type" | |
| end | |
| Proc.new do |response, output| | |
| begin | |
| # create the header | |
| header = ["Date", "Course/Channel", "Unit", "Video Name", "Learner Name", "Account", "Packages", "Relearning"] | |
| output.write FasterCSV.generate_line(header) | |
| number_of_reports_per_page = 5000 | |
| report_id_pages = (report_ids.length.to_f / number_of_reports_per_page.to_f).ceil | |
| report_id_pages.times do |index| | |
| start_report_index = index * number_of_reports_per_page | |
| page_of_report_ids = report_ids.slice(start_report_index, number_of_reports_per_page) | |
| viewings = Viewing.report(page_of_report_ids.join(","), start_date, current_account.id) | |
| lines = "" | |
| viewings.each do |viewing| | |
| lines << FasterCSV.generate_line(viewing) | |
| end | |
| output.write lines | |
| end | |
| rescue => exception | |
| RAILS_DEFAULT_LOGGER.error(exception.message) | |
| RAILS_DEFAULT_LOGGER.error(caller.join("\n")) | |
| NewRelic::Agent.notice_error(exception) | |
| end | |
| end | |
| end | |
| def each(&callback) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment