Skip to content

Commit

Permalink
Add project directory size to project show page via AJAX (#3334)
Browse files Browse the repository at this point in the history
  • Loading branch information
abujeda authored Feb 6, 2024
1 parent e6178f0 commit d03d2c5
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 4 deletions.
4 changes: 4 additions & 0 deletions apps/dashboard/app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def show
alert_messages << I18n.t('dashboard.jobs_project_invalid_configuration_clusters') unless @valid_project
alert_messages << I18n.t('dashboard.jobs_project_invalid_configuration_scripts') if @scripts.any? && !@valid_scripts
flash.now[:alert] = alert_messages.join(' ') if alert_messages.any?
respond_to do |format|
format.html
format.json { render :show }
end
end
end

Expand Down
28 changes: 27 additions & 1 deletion apps/dashboard/app/javascript/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ jQuery(function() {
$('[data-job-poller="true"]').each((_index, ele) => {
pollForJobInfo(ele);
});

$("[data-toggle='project']").each((_index, ele) => {
updateProjectSize(ele);
});
});

function pollForJobInfo(element) {
Expand Down Expand Up @@ -50,4 +54,26 @@ function jobInfoDiv(jobId, state, stateTitle='', stateDescription='') {
<span class="job-info-title badge ${cssBadgeForState(state)}" title="${stateTitle}">${state.toUpperCase()}</span>
<span class="job-info-description text-muted">${stateDescription}</span>
</div>`;
}
}

function updateProjectSize(element) {
const UNDETERMINED = 'Undetermined Size';
const $container = $(element);

const projectPath = $container.data('url');
$.ajax({
url: projectPath,
type: 'GET',
headers: {
'Accept': 'application/json'
},
success: function (projectData) {
const projectSize = projectData.size === 0 ? UNDETERMINED : projectData.human_size;
$container.text(`(${projectSize})`);
},
error: function (request, status, error) {
console.log("An error occurred getting project size!\n" + error);
$container.text(`(${UNDETERMINED})`);
}
});
}
7 changes: 7 additions & 0 deletions apps/dashboard/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ def collect_errors
errors.map(&:message).join(', ')
end

def size
if Dir.exist? project_dataroot
o, e, s = Open3.capture3('timeout', "#{Configuration.project_size_timeout}s", 'du', '-s', '-b', project_dataroot.to_s)
o.split('/')[0].to_i
end
end

private

def update_attrs(attributes)
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/views/projects/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<div class="row my-2">
<a id="new-dir-btn" class="btn btn-outline-dark" href="<%= files_path(fs: 'fs', filepath: @project.directory ) %>">
<i class="fas fa-folder-open" aria-hidden="true"></i>
<%= t('dashboard.project') %> <%= t('dashboard.directory') %>
<%= t('dashboard.project') %> <%= t('dashboard.directory') %> <span data-toggle="project" data-url="<%= project_path(@project.id) %>"></span>
</a>
</div>

Expand Down
8 changes: 8 additions & 0 deletions apps/dashboard/app/views/projects/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
json.id @project.id
json.name @project.name
json.description @project.description
json.icon @project.icon
json.directory @project.directory
project_size = @project.size
json.size project_size
json.human_size ::ApplicationController.helpers.number_to_human_size(project_size)
3 changes: 2 additions & 1 deletion apps/dashboard/config/configuration_singleton.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def string_configs
:google_analytics_tag_id => nil,
:project_template_dir => "#{config_root}/projects",
:rclone_extra_config => nil,
:default_profile => nil
:default_profile => nil,
:project_size_timeout => '15'
}.freeze
end

Expand Down
3 changes: 2 additions & 1 deletion apps/dashboard/test/fixtures/config/ondemand.d/string.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ bc_clean_old_dirs_days: 'string from file'
google_analytics_tag_id: 'string from file'
project_template_dir: 'string from file'
rclone_extra_config: 'string from file'
default_profile: 'string from file'
default_profile: 'string from file'
project_size_timeout: 'string from file'
1 change: 1 addition & 0 deletions apps/dashboard/test/system/jobs_app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def setup
stub_user
stub_sacctmgr
stub_scontrol
stub_du

# Stub Time.now for created_at field
@expected_now = 1_679_943_564
Expand Down
8 changes: 8 additions & 0 deletions apps/dashboard/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ def stub_sacctmgr
.returns([File.read('test/fixtures/cmd_output/sacctmgr_show_accts.txt'), '', exit_success])
end

def stub_du(directory = nil)
directory ||= anything
Open3
.stubs(:capture3)
.with('timeout', "#{Configuration.project_size_timeout}s", 'du', '-s', '-b', directory)
.returns(['2097152 /directory/path', '', exit_success])
end

def output_fixture(file)
File.read("#{Rails.root}/test/fixtures/file_output/#{file}")
end
Expand Down

0 comments on commit d03d2c5

Please sign in to comment.