From 7ad05ef0343315662e59357a44debad202388472 Mon Sep 17 00:00:00 2001 From: Manson Wang Date: Mon, 4 Nov 2013 17:45:01 +0800 Subject: [PATCH] Dashboard with Google Chart --- Gemfile | 3 +- app/helpers/application_helper.rb | 36 +- .../admin/dashboards/_server_loading.erb | 412 ++---------------- .../admin/dashboards/_traffic_loading.erb | 76 +--- app/views/admin/dashboards/index.html.erb | 13 +- config/locales/en.yml | 3 + config/locales/zh_tw.yml | 3 + 7 files changed, 94 insertions(+), 452 deletions(-) diff --git a/Gemfile b/Gemfile index a1c2c94e..54f32939 100644 --- a/Gemfile +++ b/Gemfile @@ -71,8 +71,7 @@ gem 'redis-search' gem 'syslog-logger' gem "recaptcha", :require => "recaptcha/rails" -gem 'puma' -gem 'request-log-analyzer' +gem "chartkick" gem 'usagewatch' # Gems used only for assets and not required diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f28f15a6..2f41f71d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,5 +1,5 @@ module ApplicationHelper - + Time.zone = ActiveSupport::TimeZone[+8.hours] FLASH_NOTICE_KEYS = [:error, :notice, :warning] @@ -223,25 +223,49 @@ module ApplicationHelper end def display_visitors(options={}) - Impression.where(options).and(:referrer.ne => nil).distinct(:session_hash).count + Impression.where(options).count end def display_visitors_today - display_visitors(created_at: {'$gte' => Date.today.beginning_of_day, '$lte' => Date.today.end_of_day}) + display_visitors(created_at: {'$gte' => Time.now.beginning_of_day, '$lte' => Time.now}) end def display_visitors_this_week - display_visitors(created_at: {'$gte' => Date.today.beginning_of_week, '$lte' => Date.today.end_of_week}) + display_visitors(created_at: {'$gte' => Time.now-7.days, '$lte' => Time.now}) end def display_visitors_this_month - display_visitors(created_at: {'$gte' => Date.today.beginning_of_month, '$lte' => Date.today.end_of_month}) + display_visitors(created_at: {'$gte' => Time.now-30.days, '$lte' => Time.now}) end def display_visitors_this_year - display_visitors(created_at: {'$gte' => Date.today.beginning_of_year, '$lte' => Date.today.end_of_year}) + display_visitors(created_at: {'$gte' => Time.now-365.days, '$lte' => Time.now}) end + def get_month_traffic + result = [] + (0..30).each do |i| + visits = Impression.where( created_at: { + '$gte' => Time.now.beginning_of_day-i.days, + '$lte' => Time.now.end_of_day-i.days} + ).count + result.push([ Time.now.beginning_of_day-i.days, visits]) + end + [:name=> t(:visitors_count),:data=>result] + end + + # def get_today_traffic + # result = [] + # (0..30).each do |i| + # the_day = i.day.ago + # visits = Impression.where( created_at: {'$gte' => the_day.beginning_of_day, '$lte' => the_day.end_of_day}).count + # # result.push([ the_day.to_time.to_i, visits]) + # result.push([ the_day.strftime("%m %d"), visits]) + # # result.push({'x'=> i, 'y'=> visits}) + # end + # result + # end + def display_date_time(object) object.strftime("%Y-%m-%d %H:%M") end diff --git a/app/views/admin/dashboards/_server_loading.erb b/app/views/admin/dashboards/_server_loading.erb index 6ff3fa00..c2122cc3 100644 --- a/app/views/admin/dashboards/_server_loading.erb +++ b/app/views/admin/dashboards/_server_loading.erb @@ -2,399 +2,45 @@

- Server Loading + <%= t(:server_usage) %>

-
-
-
+ + +
\ No newline at end of file diff --git a/app/views/admin/dashboards/_traffic_loading.erb b/app/views/admin/dashboards/_traffic_loading.erb index 0dee91c6..2a705a5e 100644 --- a/app/views/admin/dashboards/_traffic_loading.erb +++ b/app/views/admin/dashboards/_traffic_loading.erb @@ -2,61 +2,33 @@

- Traffic Loading + <%= t(:monthly_traffic)%>

-
+
+ <%= line_chart get_month_traffic, :height => "200px", :name => 'Visit', + :library => { + legend: 'none', + chartArea:{ width: '95%', left: 50, right: 0}, + hAxis:{format:'MMM-d',gridlines:{color: '#CCC', count: 31}}, + vAxis:{minValue:-100,viewWindowMode: 'maximized'} + } %> +
\ No newline at end of file diff --git a/app/views/admin/dashboards/index.html.erb b/app/views/admin/dashboards/index.html.erb index 1d700791..18c29dd2 100644 --- a/app/views/admin/dashboards/index.html.erb +++ b/app/views/admin/dashboards/index.html.erb @@ -1,20 +1,15 @@ - - - - - - - - +<%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
+ <% if is_admin? %>
<%= render 'server_loading' %>
-
+ <% end %> +
<%= render 'traffic' %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index 061feef9..3715ebef 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -289,6 +289,7 @@ en: menu_enabled_for: Menu enabled for module: Module module_authorization: Module Authorization + monthly_traffic: Monthly Traffic more_plus: more+ most_visited_page: Most Visited Page multilingual: Multilingual @@ -377,6 +378,7 @@ en: search_: Search search_google: Search Google setup_member: Member setup + server_usage: Server Usage show: Show show_mode: index: Index @@ -516,6 +518,7 @@ en: vertical: Vertical view: View view_count: View count + visitors_count: Visits visitors_this_month: This month's visitors visitors_this_week: This week's visitors visitors_this_year: This year's visitors diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 048bfdf7..5cfbec91 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -290,6 +290,7 @@ zh_tw: menu_enabled_for: 選單啟用 module: 模組 module_authorization: 模組授權 + monthly_traffic: 本月流量 more_plus: 更多+ most_visited_page: 最多瀏覽頁面 multilingual: 多語系 @@ -378,6 +379,7 @@ zh_tw: search_: 搜尋 search_google: 搜尋Google setup_member: 會員設定 + server_usage: 主機負載 show: 顯示 show_mode: index: 檢索 @@ -517,6 +519,7 @@ zh_tw: vertical: 垂直的 view: 檢視 view_count: 查看次數 + visitors_count: 造訪人次 visitors_this_month: 本月造訪人次 visitors_this_week: 本星期造訪人次 visitors_this_year: 今年造訪人次