diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index b958e7b..b23777c 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -1,31 +1,35 @@ class ClientsController < ApplicationController before_action :set_client, only: :show before_action :fetch_logs # GET /clients def index @client_ids = Client.for_user(current_user.id).pluck(:ClientId) @clients = Client.where(ClientId: @client_ids).includes(:jobs) - @active_jobs = Job.running.where(ClientId: @client_ids).group(:ClientId).count @hosts = current_user.hosts.not_baculized + fetch_jobs_info get_charts end # GET /clients/1 def show @client_ids = [@client.id] get_charts end private def set_client @client = Client.for_user(current_user.id).find(params[:id]) end + def fetch_jobs_info + @stats = JobStats.new(@client_ids) + end + def get_charts days_ago = params.fetch(:days_back, 7).to_i rescue 7 @job_status = ChartGenerator.job_statuses(@client_ids, days_ago) @job_stats = ChartGenerator.job_stats(@client_ids, days_ago - 1) end end diff --git a/app/views/clients/_client.html.erb b/app/views/clients/_client.html.erb index 3e5337b..012e4b3 100644 --- a/app/views/clients/_client.html.erb +++ b/app/views/clients/_client.html.erb @@ -1,11 +1,11 @@ <%= link_to client.name, client %> <%= client.uname %> - <%= @active_jobs[client.id] || 0 %> - <%= client.last_job_date_formatted %> + <%= @stats.active_jobs[client.id] || 0 %> + <%= @stats.last_jobs[client.id] %> <%= client.file_retention_days %> <%= client.job_retention_days %> - <%= number_to_human_size(client.backup_jobs_size) %> - <%= number_by_magnitude(client.files_count) %> + <%= number_to_human_size(@stats.jobs_sizes[client.id]) %> + <%= number_by_magnitude(@stats.jobs_files[client.id]) %> <%= client.auto_prune_human %> diff --git a/lib/job_stats.rb b/lib/job_stats.rb new file mode 100644 index 0000000..0ea608c --- /dev/null +++ b/lib/job_stats.rb @@ -0,0 +1,36 @@ +# Helper class that fetches job stats for the desired clients +class JobStats + attr_reader :client_ids, :active_jobs, :last_jobs, :jobs_sizes, :jobs_files + + # Initializes the object. + def initialize(client_ids=nil) + @client_ids = client_ids + fetch_stats + end + + private + + # Assigns values to: + # + # * active_jobs + # * last_jobs + # * jobs_sizes + # * jobs_files + def fetch_stats + @active_jobs = Job.running + @active_jobs = @active_jobs.where(ClientId: client_ids) if client_ids + @active_jobs = @active_jobs.group(:ClientId).count + + @last_jobs = {} + @jobs_sizes = Hash.new { |h,k| h[k] = 0 } + @jobs_files = Hash.new { |h,k| h[k] = 0 } + + jobs = Job.backup_type.order(EndTime: :asc) + jobs = jobs.where(ClientId: client_ids) if client_ids + jobs.each do |job| + @last_jobs[job.client_id] = I18n.l(job.end_time, format: :long) if job.end_time + @jobs_sizes[job.client_id] += job.job_bytes + @jobs_files[job.client_id] += job.job_files + end + end +end