Skip to content

Commit

Permalink
refact(admin disk-usage): show data folder tree
Browse files Browse the repository at this point in the history
  • Loading branch information
juliendiot42 committed Aug 13, 2024
1 parent dd8b8ed commit 3fe1045
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 125 deletions.
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
GenomicRanges
bsicons
uuid
shinyTree
prettyunits

(pkgs.rPackages.buildRPackage {
name = "rutilstimflutre";
Expand Down
57 changes: 57 additions & 0 deletions src/fun/func_dbRequests.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,60 @@ delGameSession <- function(id) {
db_execute_request_safe(query, id = id)
}


get_folder_size <- function(dir, as_string = FALSE) {
size <- as.numeric(strsplit(system(paste("du -s --block-size=1", dir), intern = TRUE), "\t")[[1]][1])
if (as_string) {
return(prettyunits::pretty_bytes(size, style = "6"))
}
return(size)
}

get_files_size <- function(files, as_string = FALSE) {
size <- file.info(files)$size
if (as_string) {
return(prettyunits::pretty_bytes(size))
}
return(size)
}

get_folder_tree <- function(dir, exclude_files = FALSE, excluded_files_ext = NULL) {
dirs_fullnames <- list.dirs(path = dir, recursive = F)
dirs_list <- lapply(dirs_fullnames, function(dir) {
structure(get_folder_tree(dir,
exclude_files = exclude_files,
excluded_files_ext = excluded_files_ext),
sttype = "directory")
})
dirs <- basename(dirs_fullnames)
if (length(dirs) > 0) {
dir_sizes <- sapply(dirs_fullnames, get_folder_size, as_string = TRUE)
names(dirs_list) <- paste0(dirs, " (", dir_sizes, ")")
}

if (exclude_files) {
file_list <- list()
} else {
files_fullnames <- list.files(path = dir, full.names = TRUE, include.dirs = F)
files_fullnames <- setdiff(files_fullnames, dirs_fullnames)

if (!is.null(excluded_files_ext)) {
excl_file_regex <- paste0("\\.(", paste0(excluded_files_ext, collapse = "|"), ")$")
excl_files <- grepl(excl_file_regex, files_fullnames)
files_fullnames <- files_fullnames[!excl_files]
}

files <- basename(files_fullnames)
file_list <- lapply(files, function(f){
structure("", sttype="file")
})
if (length(files) > 0) {
file_sizes <- get_files_size(files_fullnames, as_string = TRUE)
names(file_list) <- paste0(files, " (", file_sizes, ")")
}
}

tree_list <- c(dirs_list, file_list)
return(tree_list)
}

103 changes: 16 additions & 87 deletions src/server/server_admin.R
Original file line number Diff line number Diff line change
Expand Up @@ -344,95 +344,24 @@ observeEvent(input$admin_button_const_initialBudget, {


## Disk usage managment ----
output$sizeDataFolder <- renderTable({
# data frame containing the size of all subfolder of "data"
output$dataFolderTree <- shinyTree::renderTree({
invalidateLater(60000)
withProgress(
{
if (breederStatus() == "game master") {
# get list of all subfolders in "truth" and "shared"
folderShared <- list.dirs(path = DATA_SHARED, full.names = TRUE, recursive = TRUE)[-1]
folderTruth <- list.dirs(path = DATA_TRUTH, full.names = TRUE, recursive = TRUE)[-1]
subFolders <- c(folderShared, folderTruth)

# get size of each subfolders
funApply <- function(folder) {
files <- list.files(folder, all.files = TRUE, recursive = TRUE, full.names = T)
sum(file.info(files)$size)
}
infoDataFolder <- as.data.frame(sapply(subFolders, FUN = funApply, USE.NAMES = FALSE),
col.names = c("size")
)
names(infoDataFolder) <- c("size")
infoDataFolder$path <- subFolders

# clac size of "shared"
infoDataFolder <- rbind(
infoDataFolder,
data.frame(
path = DATA_SHARED,
size = sum(infoDataFolder$size[infoDataFolder$path %in% folderShared])
)
)

# clac size of "truth"
# 1.sum of all subfolders
infoDataFolder <- rbind(
infoDataFolder,
data.frame(
path = DATA_TRUTH,
size = sum(infoDataFolder$size[infoDataFolder$path %in% folderTruth])
)
)
# 2. sum of all initial collection
sizeInitCol <- sum(file.info(list.files(DATA_TRUTH, recursive = FALSE, full.names = TRUE))$size)
infoDataFolder[infoDataFolder$path == DATA_TRUTH, "size"] <- infoDataFolder[infoDataFolder$path == DATA_TRUTH, "size"] + sizeInitCol

# get size of the database
infoDataFolder <- rbind(
infoDataFolder,
data.frame(
path = DATA_DB,
size = file.info(DATA_DB)$size
)
)

# clac size of all "data" folder
sizeData <- sum(infoDataFolder[
infoDataFolder$path %in% c(
DATA_SHARED,
DATA_TRUTH,
DATA_DB
),
"size"
])
infoDataFolder <- rbind(
infoDataFolder,
data.frame(
path = "data",
size = sizeData
)
)

# order table
infoDataFolder <- infoDataFolder[order(infoDataFolder$size, decreasing = T), ]

# convert in Mo
infoDataFolder$size <- infoDataFolder$size / 10^6
infoDataFolder <- rev(infoDataFolder)

# var names
names(infoDataFolder) <- c("path", "size (Mo)")

return(infoDataFolder)
} else {
return(NULL)
}
},
message = "Calculating... Please wait."
)
get_folder_tree("data", exclude_files = FALSE, excluded_files_ext = "RData")
})

output$currentDataUsage <- renderUI({
invalidateLater(60000)
current_size <- get_folder_size(DATA_ROOT)
max_usage <- getBreedingGameConstants()$max.disk.usage
current_usage <- round(current_size / (max_usage * 10^9) * 100, 0)
p(paste0("Current usage: ",
prettyunits::pretty_bytes(current_size),
" / ",
max_usage,
" GB (",
current_usage,
"%)"))
})

observeEvent(input$updateMaxDiskUsage, {
# save maximum disk usage value in the database
Expand All @@ -452,7 +381,7 @@ currentMaxDiskUsage <- reactive({
})

output$InfoCurrentMaxDiskUsage <- renderText({
paste("Current maximum disk usage:", currentMaxDiskUsage(), "Gb")
paste("Current maximum disk usage:", currentMaxDiskUsage(), "GB")
})


Expand Down
5 changes: 2 additions & 3 deletions src/server/server_id.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ accessGranted <- eventReactive(input$submitPSW,
maxDiskUsage <- getBreedingGameConstants()$max.disk.usage

allDataFiles <- list.files(DATA_SESSION, all.files = TRUE, recursive = TRUE, full.names = FALSE)
currentSize <- sum(na.omit(file.info(allDataFiles)$size)) /
10^9 # in Gb
currentSize <- get_folder_size(DATA_ROOT) / 10^9

if (currentSize < maxDiskUsage) {
goodDiskUsage <- TRUE
Expand All @@ -102,7 +101,7 @@ accessGranted <- eventReactive(input$submitPSW,
goodDiskUsage <- TRUE
alert(paste0(
"Warning! The size of the \"data\" folder exceeds the specified limit\n",
paste("of", round(currentSize, 2), "Gb (maximum size allowed:", maxDiskUsage, "Gb).\n"),
paste("of", round(currentSize, 2), "GB (maximum size allowed:", maxDiskUsage, "GB).\n"),
"To preserve your server, players can't log in anymore (but connected users can still play).\n",
"If you want to resume the game, please raise the maximum disk usage limit.\n",
"Go to the Admin tab, then \"Disk usage\", and raise the threshold."
Expand Down
84 changes: 50 additions & 34 deletions src/ui/ui_admin_loggedIn.R
Original file line number Diff line number Diff line change
Expand Up @@ -264,45 +264,61 @@ if (gameInitialised()) {
)

disk_usage_tab_content <- div(
div(
id = "admin_diskU_data",
style = "display: inline-block;
vertical-align:top;
width: 33%;
min-width:300px;",
h3("Disk usage:"),
tableOutput("sizeDataFolder")
),
div(
id = "admin_diskU_input",
style = "display: inline-block;
vertical-align:top;
width: 66%;",
p("To prevent over disk usage on your server, you can specifiy here the maximum size for all game data.",
style = "margin-top:20px;"
),
p("In case the size of all data exceeds this threshold, players will not be allowed to connect any more, and you will have to delete haplotypes of some breeders."),
p(textOutput("InfoCurrentMaxDiskUsage")),
div(style = "display: flex;",
div(
style = "width: 50%;
display: inline-block;
vertical-align: top;",
numericInput("admin_maxDiskUsage",
label = "Maximum disk usage (in Gb)",
value = 10,
min = 2
)
id = "admin_diskU_data",
style = "flex: 1; margin-right:10px;",
h3("Disk usage:"),
uiOutput("currentDataUsage"),
p("Current session: ", CURRENT_SESSION_ID),
p(code("RData"), "files are not shown."),
shinyTree::shinyTree(
"dataFolderTree",
stripes = TRUE,
multiple = FALSE,
animation = FALSE,
types =
"{
'directory' : { 'icon' : 'glyphicon glyphicon-folder-open' },
'default' : { 'icon' : 'glyphicon glyphicon-file', 'valid_children' : [] }
}"
) %>% withSpinner()
),

div(
style = "width: 30%;
padding-top: 26px;
display: inline-block;
vertical-align: top;",
actionButton("updateMaxDiskUsage",
label = "Update"
id = "admin_diskU_input",
style = "flex: 2;",
p(style = "margin-top:20px;",
"To prevent over disk usage on your server,",
"you can specifiy here the maximum size for all game data."
),
p("In case the size of all data exceeds this threshold,",
"players will not be allowed to connect any more, and you will",
"have to delete haplotypes of some breeders."),
p("Deletion of data must be made manually on the host machine."),
p(textOutput("InfoCurrentMaxDiskUsage")),

div(
style = "width: 50%;
display: inline-block;
vertical-align: top;",
numericInput("admin_maxDiskUsage",
label = "Maximum disk usage (in GB)",
value = 10,
min = 2)
),
div(
style = "width: 30%;
padding-top: 26px;
display: inline-block;
vertical-align: top;",
actionButton("updateMaxDiskUsage",
label = "Update")
)


)
) # end div "admin_diskU_input"
),
)


Expand Down
4 changes: 3 additions & 1 deletion tools/installDeps.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ install.packages(
"vistime",
"shinyvalidate",
"bsicons",
"uuid"
"uuid",
"shinyTree",
"prettyunits"
)
)

Expand Down

0 comments on commit 3fe1045

Please sign in to comment.