diff --git a/app/controllers/super_admin/dashboard_controller.rb b/app/controllers/super_admin/dashboard_controller.rb index 56d19d563..db38d033b 100644 --- a/app/controllers/super_admin/dashboard_controller.rb +++ b/app/controllers/super_admin/dashboard_controller.rb @@ -1,11 +1,41 @@ class SuperAdmin::DashboardController < SuperAdmin::ApplicationController include ActionView::Helpers::NumberHelper + ALLOWED_CHART_DAYS = [7, 30].freeze + def index - @data = Conversation.unscoped.group_by_day(:created_at, range: 30.days.ago..2.seconds.ago).count.to_a - @accounts_count = number_with_delimiter(Account.count) - @users_count = number_with_delimiter(User.count) - @inboxes_count = number_with_delimiter(Inbox.count) - @conversations_count = number_with_delimiter(Conversation.count) + @chart_days = parse_chart_days + @data = conversations_by_day + @accounts_count = format_count(Account.count) + @users_count = format_count(User.count) + @inboxes_count = format_count(Inbox.count) + @conversations_count = format_count(recent_conversations.count) + end + + private + + def parse_chart_days + days = params[:chart_period].to_i + ALLOWED_CHART_DAYS.include?(days) ? days : 7 + end + + def chart_date_range + (@chart_days - 1).days.ago.beginning_of_day..Time.current + end + + def last_30_days + 29.days.ago.beginning_of_day..Time.current + end + + def recent_conversations + Conversation.unscoped.where(created_at: last_30_days) + end + + def conversations_by_day + Conversation.unscoped.group_by_day(:created_at, range: chart_date_range, default_value: 0).count.to_a + end + + def format_count(count) + number_with_delimiter(count) end end diff --git a/app/dashboards/account_dashboard.rb b/app/dashboards/account_dashboard.rb index 9be674f11..dd7236692 100644 --- a/app/dashboards/account_dashboard.rb +++ b/app/dashboards/account_dashboard.rb @@ -31,8 +31,8 @@ class AccountDashboard < Administrate::BaseDashboard updated_at: Field::DateTime, users: CountField, conversations: CountField, - locale: Field::Select.with_options(collection: LANGUAGES_CONFIG.map { |_x, y| y[:iso_639_1_code] }), - status: Field::Select.with_options(collection: [%w[Active active], %w[Suspended suspended]]), + locale: LocaleField, + status: StatusField, account_users: Field::HasMany, custom_attributes: Field::String }.merge(enterprise_attribute_types).freeze @@ -45,10 +45,9 @@ class AccountDashboard < Administrate::BaseDashboard COLLECTION_ATTRIBUTES = %i[ id name - locale - users - conversations status + users + locale ].freeze # SHOW_PAGE_ATTRIBUTES diff --git a/app/fields/locale_field.rb b/app/fields/locale_field.rb new file mode 100644 index 000000000..60dd28289 --- /dev/null +++ b/app/fields/locale_field.rb @@ -0,0 +1,12 @@ +require 'administrate/field/base' + +class LocaleField < Administrate::Field::Base + def to_s + language = LANGUAGES_CONFIG.find { |_key, val| val[:iso_639_1_code] == data } + language ? language[1][:name] : data + end + + def selectable_options + LANGUAGES_CONFIG.map { |_key, val| [val[:name], val[:iso_639_1_code]] } + end +end diff --git a/app/fields/status_field.rb b/app/fields/status_field.rb new file mode 100644 index 000000000..e06df55fc --- /dev/null +++ b/app/fields/status_field.rb @@ -0,0 +1,15 @@ +require 'administrate/field/base' + +class StatusField < Administrate::Field::Base + def to_s + data.to_s.capitalize + end + + def active? + data.to_s == 'active' + end + + def selectable_options + [%w[Active active], %w[Suspended suspended]] + end +end diff --git a/app/javascript/superadmin_pages/views/dashboard/Index.vue b/app/javascript/superadmin_pages/views/dashboard/Index.vue index 0c03a846c..579aff64f 100644 --- a/app/javascript/superadmin_pages/views/dashboard/Index.vue +++ b/app/javascript/superadmin_pages/views/dashboard/Index.vue @@ -1,6 +1,8 @@