Skip to content
Browse files

download list of users

  • Loading branch information...
1 parent 77f0a32 commit e5889a195328b03a95854aa4661f6cb93915dbb3 @decioferreira decioferreira committed May 3, 2012
Showing with 43 additions and 22 deletions.
  1. +3 −1 README.rdoc
  2. +20 −9 app/controllers/admin_controller.rb
  3. +4 −6 app/models/download_job.rb
  4. +15 −6 app/views/admin/index.html.erb
  5. +1 −0 config/routes.rb
View
4 README.rdoc
@@ -26,7 +26,9 @@ You need redis-server installed and running. Default redis config is in config/r
To get the workers running:
- QUEUE=statused rake resque:work
+ QUEUE=download_serve rake resque:work
+
+With debug and verbose:
VVERBOSE=1 QUEUE=download_serve bundle exec rake resque:work
View
29 app/controllers/admin_controller.rb
@@ -4,20 +4,16 @@ class AdminController < ApplicationController
def index
@layer_downloads = LayerDownload.order(:id)
- @users = User.all
+ @users = User.order(:email)
end
-# def get_job_status
-# render :json => Resque::Plugins::Status::Hash.get(params[:job_id])
-# end
-
# Generates download file from CartoDB
def generate
if params[:layer]
layer_download = LayerDownload.find(params[:layer])
layer_download.update_attributes(generated_at: Time.now, finished: false)
Resque.enqueue(DownloadJob, {:layer => layer_download.id})
- else
+ else # user
user = User.find(params[:user])
user.update_attributes(generated_at: Time.now, finished: false)
Resque.enqueue(DownloadJob, {:user => user.id})
@@ -29,10 +25,25 @@ def generate
# Downloads file generated from CartoDB
def download
if params[:layer]
- send_file DownloadJob.zip_path(:layer, params[:layer]), type: 'application/zip'
- else
- send_file DownloadJob.zip_path(:user, params[:user]), type: 'application/zip'
+ layer_download = LayerDownload.find(params[:layer])
+ send_file DownloadJob.zip_path(:layer, params[:layer]), filename: "#{layer_download.name}.zip", type: 'application/zip'
+ else # user
+ user = User.find(params[:user])
+ send_file DownloadJob.zip_path(:user, params[:user]), filename: "#{user.email}.zip", type: 'application/zip'
+ end
+ end
+
+ def download_users
+ require 'csv'
+
+ users = CSV.generate do |csv|
+ csv << ['EMAIL']
+ User.order(:email).each do |user|
+ csv << [user.email]
+ end
end
+
+ send_data users, filename: 'users.csv', type: 'text/csv; charset=utf-8; header=present'
end
private
View
10 app/models/download_job.rb
@@ -23,12 +23,12 @@ def self.perform(options)
# Result count
count = JSON.parse(res.body)["rows"].first["count"]
- Tempfile.open('download') do |file|
+ Tempfile.open(['download', '.json']) do |file|
0.upto (count/APP_CONFIG['admin_user_edits_limit']).round do |offset|
query = "SELECT * FROM #{APP_CONFIG['cartodb_table']} WHERE #{where_clause} LIMIT #{APP_CONFIG['admin_user_edits_limit']} OFFSET #{offset * APP_CONFIG['admin_user_edits_limit']}"
uri = URI.parse(URI.escape("http://carbon-tool.cartodb.com/api/v1/sql?q=#{query}&format=geojson"))
res = Net::HTTP.get_response(uri)
- file << "#{res.body}\n"
+ file << res.body
end
file.rewind
@@ -38,7 +38,6 @@ def self.perform(options)
# Debug
# system "cp #{file.path} ~/Desktop"
- p "ogr2ogr -overwrite -skipfailures -f 'ESRI Shapefile' #{ogr2ogr_dir} #{Rails.root}#{file.path}"
system "ogr2ogr -overwrite -skipfailures -f 'ESRI Shapefile' #{ogr2ogr_dir} #{Rails.root}#{file.path}"
system "zip -j #{self.zip_path(:layer, layer_download.id)} #{ogr2ogr_dir}/*"
@@ -60,12 +59,12 @@ def self.perform(options)
# Result count
count = JSON.parse(res.body)["rows"].first["count"]
- Tempfile.open('download') do |file|
+ Tempfile.open(['download', '.json']) do |file|
0.upto (count/APP_CONFIG['admin_user_edits_limit']).round do |offset|
query = "SELECT * FROM #{APP_CONFIG['cartodb_table']} WHERE #{where_clause} LIMIT #{APP_CONFIG['admin_user_edits_limit']} OFFSET #{offset * APP_CONFIG['admin_user_edits_limit']}"
uri = URI.parse(URI.escape("http://carbon-tool.cartodb.com/api/v1/sql?q=#{query}&format=geojson"))
res = Net::HTTP.get_response(uri)
- file << "#{res.body}\n"
+ file << res.body
end
file.rewind
@@ -75,7 +74,6 @@ def self.perform(options)
# Debug
# system "cp #{file.path} ~/Desktop"
- p "ogr2ogr -overwrite -skipfailures -f 'ESRI Shapefile' #{ogr2ogr_dir} #{Rails.root}#{file.path}"
system "ogr2ogr -overwrite -skipfailures -f 'ESRI Shapefile' #{ogr2ogr_dir} #{Rails.root}#{file.path}"
system "zip -j #{self.zip_path(:user, user.id)} #{ogr2ogr_dir}/*"
View
21 app/views/admin/index.html.erb
@@ -1,9 +1,11 @@
-<h2>Community layer files</h2>
+<div class="page-header">
+ <h1>Community layer files</h1>
+</div>
<table class="table table-bordered table-striped">
<thead>
<tr>
- <th>File</th>
+ <th>Name</th>
<th>Generated at</th>
<th>Finished</th>
<th></th>
@@ -23,22 +25,29 @@
</tbody>
</table>
-<h2>Individual user layers</h2>
+<div class="page-header">
+ <%= link_to 'Download users information', admin_download_users_path, :class => 'btn' %>
+ <h1>Individual user layers</h1>
+</div>
<table class="table table-bordered table-striped">
<thead>
<tr>
- <th>User</th>
+ <th>Email</th>
<th>Edits</th>
- <th>Mangrove</th>
- <th>Coral</th>
+ <th>Generated at</th>
+ <th>Finished</th>
+ <th></th>
+ <th></th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.email %></td>
<td><%= user.layers.count %></td>
+ <td><%= user.generated_at %></td>
+ <td><%= user.finished %></td>
<td><%= link_to 'Generate', admin_generate_path(user: user) %></td>
<td><%= user.finished? ? link_to('Download', admin_download_path(user: user)) : 'Download' %></td>
</tr>
View
1 config/routes.rb
@@ -9,6 +9,7 @@
match 'admin' => "admin#index"
match 'admin/generate' => "admin#generate"
match 'admin/download' => "admin#download"
+ match 'admin/download_users' => "admin#download_users"
# The priority is based upon order of creation:
# first created -> highest priority.

0 comments on commit e5889a1

Please sign in to comment.
Something went wrong with that request. Please try again.