Skip to content

Commit

Permalink
Download .CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
vitoriamr committed Jan 18, 2018
1 parent 38e21fc commit 960f288
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 11 deletions.
7 changes: 7 additions & 0 deletions app/assets/stylesheets/app/manage/statistics.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ body.manage.statistics {
.starts-at-wrapper {
margin-bottom: 16px;

.csv {
padding-top: 0;
margin-top: 0;
border-top: 0;
float: right;
}

.btn-group {
white-space: nowrap;
display: table-cell;
Expand Down
18 changes: 14 additions & 4 deletions app/controllers/manage_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,29 @@ def recordings
end
end

def statistics
def check_statistics_params
if params[:statistics].present?
from = params[:statistics][:starts_on_time]
to = params[:statistics][:ends_on_time]

from.present? ? from_date = Date.strptime(from, '%m/%d/%Y') : from_date =nil
to.present? ? to_date = Date.strptime(to, '%m/%d/%Y') : to_date =nil
from.present? ? @from_date = Date.strptime(from, '%m/%d/%Y') : @from_date =nil
to.present? ? @to_date = Date.strptime(to, '%m/%d/%Y') : @to_date =nil
end
end

@data = Mconf::StatisticsModule.generate(from_date, to_date)
def statistics
check_statistics_params
@data = Mconf::StatisticsModule.generate(@from_date, @to_date)
end

def statistics_filter
render layout: false
end

def statistics_csv
check_statistics_params
respond_to do |format|
format.csv { send_data Mconf::StatisticsModule.generate_csv(@from_date, @to_date), type: Mime::CSV, disposition: "attachment", filename: "overview-#{@from_date}-#{@to_date}.csv" }
end
end
end
8 changes: 5 additions & 3 deletions app/views/manage/statistics.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
.row
.col-xs-12.col-sm-9
.starts-at-wrapper
.csv
= link_to t('.csv'), manage_statistics_csv_path(format: :csv), class: 'btn btn-primary'
.btn-group{ 'data-toggle': "buttons" }

%label.btn.btn-default.all{ 'data-attr-value': '0' }
%input{ type: 'radio' }
= t('.filter.all')
= t('.all')

%label.btn.btn-default{ 'data-attr-value': '1' }
%input{ type: 'radio' }
= link_to manage_statistics_filter_path, class: "open-modal tooltipped", title: t('.filter.date') do
= link_to manage_statistics_filter_path, class: "open-modal tooltipped", title: t('.date') do
= icon_later
= t('.filter.date')
= t('.date')

#statistics-list
= render partial: 'statistics_list'
6 changes: 3 additions & 3 deletions config/locales/en/mconf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1045,9 +1045,9 @@ en:
filter_count_empty: "Found %{count} spaces"
no_space_found: "No space found"
statistics:
filter:
all: "All"
date: "Pick date"
all: "All"
date: "Pick date"
csv: "Download .csv"
statistics_filter:
from: "From"
to: "To"
Expand Down
8 changes: 8 additions & 0 deletions config/locales/pt-br/mconf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,14 @@ pt-br:
filter_count_empty: "%{count} comunidades encontradas"
no_space_found: "Nenhuma comunidade encontrada"
statistics:
all: "Mostrar tudo"
date: "Escolher data"
csv: "Baixar .csv"
statistics_filter:
from: "De"
to: "Até"
title: "Período"
statistics_list:
users:
title: "Estatísticas de usuários"
all: "Total de usuários: %{value}"
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
scope 'manage' do
resource :site, only: [:show, :edit, :update]

['users', 'spaces', 'recordings', 'statistics', 'statistics_filter'].each do |resource|
['users', 'spaces', 'recordings', 'statistics', 'statistics_filter', 'statistics_csv'].each do |resource|
get resource, to: "manage##{resource}", as: "manage_#{resource}"
end

Expand Down
22 changes: 22 additions & 0 deletions lib/mconf/statistics_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,27 @@ def self.generate(from =nil, to =nil)

statistics
end

def self.flatten_hash(hash)
hash.each_with_object({}) do |(k, v), h|
if v.is_a? Hash
flatten_hash(v).map do |h_k, h_v|
h["#{k}.#{h_k}".to_sym] = h_v
end
else
h[k] = v
end
end
end

def self.generate_csv(from =nil, to =nil)
data = self.generate(from, to)
csv_data = self.flatten_hash(data)

CSV.generate(headers: true) do |csv|
csv << csv_data.keys
csv << csv_data.values
end
end
end
end

0 comments on commit 960f288

Please sign in to comment.