From 364c890cd3040292615fed4949180ba3a6b932e5 Mon Sep 17 00:00:00 2001 From: kyg516 Date: Tue, 27 Sep 2016 12:14:39 +0900 Subject: [PATCH] Update graph and table --- app/controllers/application_controller.rb | 9 +- app/helpers/application_helper.rb | 95 ++++----- app/helpers/home_helper.rb | 88 +++++---- app/views/home/index.html.erb | 226 ++++++++++++---------- 4 files changed, 230 insertions(+), 188 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 639be94..0369412 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,6 +3,8 @@ class ApplicationController < ActionController::Base # For APIs, you may want to use :null_session instead. # protect_from_forgery with: :exception include ApplicationHelper + include HomeHelper + include VolumeHelper def require_login unless user_signed_in? @@ -36,7 +38,12 @@ class ApplicationController < ActionController::Base def chdir @current_dir = params[:next_dir] puts "current_dir : " + @current_dir - render :json => {:dir => @current_dir} + render :json => { + :dir => @current_dir, + :file_manager_table => file_manager_table(@current_dir), + :disk_usage_table => disk_usage_table(@current_dir), + :du => get_du(@current_dir), + } end def rmdir diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 28f86df..f8461dc 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -26,6 +26,54 @@ module ApplicationHelper return df end + def get_du(dir = @current_dir) + du = Array.new + du_each = Hash.new + command = String.new + avail = 0.0 + if dir.eql? "/" + command = "sudo df /" + s = `#{command}`.split("\n")[1].split(" ") + avail = s[2].to_f + s[3].to_f + dir = "" + else + command << "sudo du -s #{dir}" + begin + avail = `#{command}`.split(" ")[0].to_f + rescue + # some directory is not connected + avail = `#{command}`.split(" ")[1].to_f + end + end + + command = "" + command << "sudo du -s #{dir}/*" + puts command + output = `#{command}`.split("\n") + output.each do |t| + begin + du_each['usage'] = t.split(" ")[0].to_f / avail + du_each['file_name'] = t.split(" ")[1].split("/").last + du << du_each + du_each = Hash.new + rescue + # directory is not connected + du_each['usage'] = 0.0 + du_each['file_name'] = t.split(" ")[1].split("/").last.split("'")[0] + du << du_each + du_each = Hash.new + end + end + + if du.length == 0 + du_each['usage'] = 1.0 + du_each['file_name'] = "empty" + du << du_each + end + + return du + end + def volumes volumes = Array.new volume = Hash.new @@ -83,51 +131,4 @@ module ApplicationHelper return files end - def file_manager_table(dir = "/mnt", id = "datatable", class_option = "table table-striped table-bordered jambo_table") - html = String.new - html << "" - html << "" - html << "" - html << "" - html << "" - html << "" - html << "" - html << "" - html << "" - html << "" - html << "" - html << "" - html << "" - html << "" - html << "" - html << "" - - files(dir).each do |file| - html << "" - if file["auth"][0]=='d' - html << "" - else - conv_name = (dir + '/' + file['name']).gsub("/", "+") - html << "" - end - html << "" - html << "" - html << "" - html << "" - end - - html << "" - html << "
NameauthSizeDate
" - html << "" - html << " #{dir}" - html << "" - html << "
" - html << " #{file['name']}" - html << "" - html << " #{file['name']}" - html << "#{file['auth']}#{file['size']}#{file["date"]}" - html << "" - html << "
" - return raw html - end end diff --git a/app/helpers/home_helper.rb b/app/helpers/home_helper.rb index 01a1cfc..21df71e 100644 --- a/app/helpers/home_helper.rb +++ b/app/helpers/home_helper.rb @@ -1,46 +1,54 @@ module HomeHelper - def get_du(dir) - du = Array.new - du_each = Hash.new - command = String.new - avail = 0.0 - if dir.eql? "/" - command = "sudo df /" - s = `#{command}`.split("\n")[1].split(" ") - avail = s[2].to_f + s[3].to_f - else - command << "sudo du -s #{dir}" - begin - avail = `#{command}`.split(" ")[0].to_f - rescue - # some directory is not connected - avail = `#{command}`.split(" ")[1].to_f + def file_manager_table(dir = @current_dir, id = "file_manager_table", class_option = "table table-striped table-bordered jambo_table") + html = String.new + html << "" + html << "" + html << "" + html << "" + html << "" + html << "" + html << "" + html << "" + html << "" + html << "" + html << "" + html << "" + html << "" + html << "" + html << "" + html << "" + + files(dir).each do |file| + html << "" + if file["auth"][0]=='d' + html << "" + else + conv_name = (dir + '/' + file['name']).gsub("/", "+") + html << "" end + html << "" + html << "" + html << "" + html << "" end - command = "" - command << "sudo du -s #{dir}/*" - puts command - output = `#{command}`.split("\n") - output.each do |t| - begin - du_each['usage'] = t.split(" ")[0].to_f / avail - du_each['file_name'] = t.split(" ")[1].split("/").last - du << du_each - du_each = Hash.new - rescue - # directory is not connected - du_each['usage'] = 0.0 - du_each['file_name'] = t.split(" ")[1].split("/").last.split("'")[0] - du << du_each - du_each = Hash.new - end - end - return du + html << "" + html << "
NameauthSizeDate
" + html << "" + html << " #{dir}" + html << "" + html << "
" + html << " #{file['name']}" + html << "" + html << " #{file['name']}" + html << "#{file['auth']}#{file['size']}#{file["date"]}" + html << "" + html << "
" + return html end - def disk_usage_table(dir = "/mnt") + def disk_usage_table(dir = @current_dir) html = String.new html << "" html << "" @@ -53,18 +61,18 @@ module HomeHelper html << "

Usage

" html << "" html << "" - html << "" + html << "" html << "
" html << "" - get_du(dir).each_with_index do |t, index| + get_du(dir).each_with_index do |file, index| color = ['blue', 'green', 'red', 'purple', 'grey'][index % 5] html << "" html << "" html << "" end diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index 71cca8f..6edd6a3 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -65,8 +65,8 @@
<%= users.nil? ? 0 : users.length %>
<%= today_user.nil? ? 0 : today_user.length %> - user signed Today - + user signed Today +
@@ -76,8 +76,8 @@
<%= nodes.nil? ? 0 : nodes.length %>
<%= today_node.nil? ? 0 : today_node.length %> - Node added Today - + Node added Today +
@@ -126,7 +126,7 @@
- <%= file_manager_table @current_dir %> + <%= raw file_manager_table %>
@@ -259,107 +259,30 @@ - - + - - - - +} - - + + + +
" html << "

" - html << t['file_name'] + html << file['file_name'] html << "

" - html << format("%.2f", t['usage']*100) + "%" + html << format("%.2f", file['usage']*100) + "%" html << "