#' monitoraggio avanzamento progetto
#'
#' @param plot plot a file in /tmp/
#' @param view use a viewer
#'
#' @export
monitoring <- function(prj, plot = TRUE, view = TRUE){
prj_dir <- subs_dir(prj)
av_f <- avanz_f(prj)
if (!file.exists(av_f)) stop("File avanzamento inesistente")
db <- avanz_import(avanz_f(prj))
## directory and files on the disk
all_srt <- list.files(path = prj_dir, pattern = '.srt$')
trn_f <- grep("^subs", all_srt, value = TRUE)
rev2_f <- grep("^revs", all_srt, value = TRUE)
## colors
alpha <- 0.6
cols <- list(compl = lbmisc::col2hex("green4", alpha = alpha),
iniz = lbmisc::col2hex("yellow3", alpha = alpha),
non_iniz = lbmisc::col2hex("red2", alpha = alpha))
col_gen <- function(assigned, completed){
ifelse(assigned,
ifelse(completed, cols$compl, cols$iniz),
cols$non_iniz)
}
## ## check che il database sia in sync con i file rilevati su disco o dai errori e stoppa
## correct_trn <- all(db$trn_filename %in% trn_f) && all(trn_f %in% db$trn_filename)
## if (!correct_trn) stop("traduzioni: filenames in directory e avanzamento.csv non coincidono")
## correct_rev2 <- all(db$rev2_filename %in% rev2_f) && all(rev2_f %in% db$rev2_filename)
## if (!correct_rev2) stop("traduzioni: filenames in directory e avanzamento.csv non coincidono")
## translations and revision1
db$trn_starting_digits <- gsub(trn_starting_time_ptrn, '\\1', db$trn_filename)
db$trn_starting_time <- digits_to_time(db$trn_starting_digits)
db$trn_color <- with(db, col_gen(trn_assigned, trn_completed))
db$rev1_color <- with(db, col_gen(rev1_assigned, rev1_completed))
db$rev2_color <- with(db, col_gen(rev2_assigned, rev2_completed))
## db$rev2_starting_digits <- gsub(rev2_starting_time_ptrn, '\\1', db$rev2_filename)
## db$rev2_ending_digits <- gsub(rev2_ending_time_ptrn, '\\1', db$rev2_filename)
## trn_in_rev <- function(trn_st_dg, rev_st_dg, rev_end_dg){
## ## trn_st_dg è il digits di inizio della traduzione (dal filename)
## ## rev_st_dg è il digits di inizio della revisione
## ## rev_end_dg è il digits di inizio dell'ultimo pezzo della revisione
## ## esempi
## ## trn_in_rev('000500', '000000', '000500') da TRUE
## ## trn_in_rev('005500', '000000', '000500') da FALSE
## rev_content_id <- seq(which(db$trn_starting_digits %in% rev_st_dg),
## which(db$trn_starting_digits %in% rev_end_dg))
## trn_st_dg %in% db$trn_starting_digits[rev_content_id]
## }
## trn_rev_file <- function(trn_st_dg){# trn_st_dg è una cosa tipo '000000'
## ## fai tutto se ci sono revisioni, se no restituisci ''
## if (nrow(rev2) > 0L) {
## res <- Map(trn_in_rev,
## list(trn_st_dg),
## as.list(rev2$rev2_starting_digits),
## as.list(rev2$rev2_ending_digits))
## ## vettore di logici per ogni trn (che lega ad ogni rev)
## res <- unlist(res)
## names(res) <- rev2$rev2_filename
## fname <- names(which(res))
## ## normalize output
## if (length(fname) == 0L) '' else fname
## } else ''
## }
## ## Merge di TRN e REV2
## db$rev2_filename <- unlist(lapply(db$trn_starting_digits, trn_rev_file))
## db <- merge(trn, rev2, by = 'rev2_filename', all.x = TRUE)
## var_order <- c(
## "trn_filename",
## # "trn_starting_digits",
## "trn_starting_time",
## "trn_assigned", "trn_completed", "trn_color",
## "rev1_assigned", "rev1_completed", "rev1_color",
## "rev2_filename",
## ## "rev2_starting_digits", "rev2_ending_digits",
## "rev2_assigned",
## "rev2_completed", "rev2_color")
db <- db[order(db$trn_filename), ]
rownames(db) <- NULL
## db$rev2_color[is.na(db$rev2_color)] <- cols$non_iniz
## -----
## plot
## -----
if (plot) {
## graphical parames
data_y <- -seq_len(nrow(db))
headers <- c('Spezzone', 'Traduzione', 'Rev1', 'Rev2')
headers_y <- 1L
headers_x <- 0:3
## first column: text
text_x <- rep(0, nrow(db))
text_y <- data_y
adj <- c(0,0.5)
## second column: rectangles regarding translation
trn_x <- rep(1, nrow(db))
trn_y <- data_y
trn_xleft <- trn_x - 0.5
trn_ytop <- trn_y + 0.5
trn_xright <- trn_x + 0.5
trn_ybottom <- trn_y - 0.5
## third column: rectangles regarding revision 1
rev1_x <- rep(2, nrow(db))
rev1_y <- data_y
rev1_xleft <- rev1_x - 0.5
rev1_ytop <- rev1_y + 0.5
rev1_xright <- rev1_x + 0.5
rev1_ybottom <- rev1_y - 0.5
## fourth column: rectangles regarding revision 2
rev2_x <- rep(3, nrow(db))
rev2_y <- data_y
rev2_xleft <- rev2_x - 0.5
rev2_ytop <- rev2_y + 0.5
rev2_xright <- rev2_x + 0.5
rev2_ybottom <- rev2_y - 0.5
## base graph
filename <- sprintf("/tmp/%s_%s.png",
prj,
format(Sys.Date(), '%Y_%m_%d'))
png(filename = filename, width = 380)
par(mar = c(0,0,3,0), oma = rep(0, 4))
plot(x = c(-0.5, 5), y = c(0, -nrow(db)),
pch = NA,
main = sprintf("%s: avanzamento al %s", prj, Sys.Date()),
ylab = '',
xlab = '',
axes = FALSE,
frame.plot = FALSE)
## headers
tmp <- Map(text,
x = as.list(headers_x),
y = as.list(headers_y),
labels = as.list(headers))
## starting times
tmp <- Map(text,
x = as.list(text_x),
y = as.list(text_y),
labels = as.list(db$trn_starting_time))
## translation rectangles
tmp <- Map(rect,
xleft = as.list(trn_xleft),
xright = as.list(trn_xright),
ytop = as.list(trn_ytop),
ybottom = as.list(trn_ybottom),
col = as.list(db$trn_color))
## revision1 rectangles
tmp <- Map(rect,
xleft = as.list(rev1_xleft),
xright = as.list(rev1_xright),
ytop = as.list(rev1_ytop),
ybottom = as.list(rev1_ybottom),
col = as.list(db$rev1_color))
## revision2 rectangles
tmp <- Map(rect,
xleft = as.list(rev2_xleft),
xright = as.list(rev2_xright),
ytop = as.list(rev2_ytop),
ybottom = as.list(rev2_ybottom),
col = as.list(db$rev2_color))
## legenda
legend(x = 3.6, y = -0.5,
legend = c('Non iniziata', 'Iniziata', 'Completata'),
fill = c(cols$non_iniz, cols$iniz, cols$compl))
tmp <- dev.off()
if (view) system(sprintf("feh %s &", filename))
}
invisible(db)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.