
# Instructions: 
#    Copy the template folder
#    make sure the new file_type is in the list below
#    Run this
#    move the files to the lookml folder
#    push to looker


setScience("Looker", subProj="rank_views")


clean_file_text <- function(file, orig, new) {
  raw <- readLines(file)

  add_s <- function(x) sprintf("%ss", x)
  add_s_upper <- function(x) toupper(add_s(x))

  funcs <- c("identity", "add_s", "add_s_upper", "toupper", "topropper")

  for (func in funcs) {
    func <- match.fun(func)
    pat <- sprintf("(_|\\b)%s(_|id|\\b)", func(orig))
    repl <- sprintf("\\1%s\\2", func(new))

    raw <- gsub(pat, repl, raw)
  }

  cat(raw, sep="\n", file=file)
}


original <- "artist"
file_types <- c("artist", "label", "release", "track", "country", "store")

folders <- as.path("~/development/looks", paste0("ranks_", file_types)) %>% setNames(nm=file_types, .)


file_model <-  extractFilesFromFolder(dirname(folders[[1]]), pat="^accounting", ext="model.lookml", full=TRUE)

for (nm in file_types) {
  if (nm == original)
    next;

  cat("~~~~~~~~~~~", nm, "~~~~~~~~~~\n")
  if (file.exists(folders[[nm]]) && length(dir(folders[[nm]]))) {
    files <- dir(folders[[nm]])
    files_new_name <- gsub(original, nm, files)

    files_full_path <- as.path(folders[[nm]], files)
    files__new_full_path <- as.path(folders[[nm]], files_new_name)
    file.rename(files_full_path, files__new_full_path)

    for (file in files__new_full_path)
      clean_file_text(file=file, orig=original, new=nm)

    file_join <- extractFilesFromFolder(folders[[nm]], pat="join_|\\b", ext="txt", full=TRUE)
    if (length(file_join) && length(file_model)) {
      readLines(file_join) %>% cat("", ., file=file_model, append=TRUE, sep="\n")
    }
    catn(readLines(file_model), sep="\n")
  }
}




## URLs for some of the looks.   Wondering if we could construct these? 
# https://theorchard.looker.com/explore/Accounting/accounting?dynamic_fields=%5B%7B%22table_calculation%22:%22change_in_rank_2015_v_2014%22,%22label%22:%22Change%20in%20Rank%202015%20v%202014%22,%22expression%22:%22$%7Brelease_rank_bi_accounting_by_revenue_2014.release_rank_for_2014%7D%20-%5Cn$%7Brelease_rank_bi_accounting_by_revenue_2015.release_rank_for_2015%7D%22,%22value_format%22:%22%22%7D,%7B%22table_calculation%22:%22change_in_rank_2014_v_2013%22,%22label%22:%22Change%20in%20Rank%202014%20v%202013%22,%22expression%22:%22$%7Brelease_rank_bi_accounting_by_revenue_2013.release_rank_for_2013%7D%20-%5Cn$%7Brelease_rank_bi_accounting_by_revenue_2014.release_rank_for_2014%7D%22%7D%5D&f%5Baccounting.rank_country_code_filter%5D=%22US%22&f%5Baccounting.rank_store_filter%5D=286&f%5Brelease_rank_bi_accounting_by_revenue_2015.release_rank_for_2015%5D=%3C%3D250&filter_config=%7B%22release_rank_bi_accounting_by_revenue_2015.release_rank_for_2015%22:%5B%7B%22type%22:%22%3C%3D%22,%22values%22:%5B%7B%22constant%22:%22250%22%7D,%7B%7D%5D,%22id%22:4%7D%5D,%22accounting.rank_country_code_filter%22:%5B%7B%22type%22:%22%3D%22,%22values%22:%5B%7B%22constant%22:%22US%22%7D,%7B%7D%5D,%22id%22:5%7D%5D,%22accounting.rank_store_filter%22:%5B%7B%22type%22:%22%3D%22,%22values%22:%5B%7B%22constant%22:%22286%22%7D,%7B%7D%5D,%22id%22:1%7D%5D%7D&look_id=156&query=J3YXtgc&show=data&title=Top%20Releases%20US,%20by%20Revenue%202015,%202014,%202013&vis=%7B%22type%22:%22looker_column%22,%22hidden_fields%22:%5B%22change_in_gross_2015_v_2014%22,%22change_in_gross_2014_v_2013%22%5D%7D
# https://theorchard.looker.com/explore/Accounting/accounting?dynamic_fields=%5B%7B%22table_calculation%22:%22change_in_rank_2015_v_2014%22,%22label%22:%22Change%20in%20Rank%202015%20v%202014%22,%22expression%22:%22$%7Btrack_rank_bi_accounting_by_revenue_2014.track_rank_for_2014%7D%20-%5Cn$%7Btrack_rank_bi_accounting_by_revenue_2015.track_rank_for_2015%7D%22,%22value_format%22:%22%22%7D,%7B%22table_calculation%22:%22change_in_rank_2014_v_2013%22,%22label%22:%22Change%20in%20Rank%202014%20v%202013%22,%22expression%22:%22$%7Btrack_rank_bi_accounting_by_revenue_2013.track_rank_for_2013%7D%20-%5Cn$%7Btrack_rank_bi_accounting_by_revenue_2014.track_rank_for_2014%7D%22%7D%5D&f%5Baccounting.rank_country_code_filter%5D=%22US%22&f%5Baccounting.rank_store_filter%5D=286&f%5Btrack_rank_bi_accounting_by_revenue_2015.track_rank_for_2015%5D=%3C%3D250&filter_config=%7B%22track_rank_bi_accounting_by_revenue_2015.track_rank_for_2015%22:%5B%7B%22type%22:%22%3C%3D%22,%22values%22:%5B%7B%22constant%22:%22250%22%7D,%7B%7D%5D,%22id%22:4%7D%5D,%22accounting.rank_country_code_filter%22:%5B%7B%22type%22:%22%3D%22,%22values%22:%5B%7B%22constant%22:%22US%22%7D,%7B%7D%5D,%22id%22:5%7D%5D,%22accounting.rank_store_filter%22:%5B%7B%22type%22:%22%3D%22,%22values%22:%5B%7B%22constant%22:%22286%22%7D,%7B%7D%5D,%22id%22:1%7D%5D%7D&look_id=156&query=J3YXtgc&show=data&title=Top%20Releases%20US,%20by%20Revenue%202015,%202014,%202013&vis=%7B%22type%22:%22looker_column%22,%22hidden_fields%22:%5B%22change_in_gross_2015_v_2014%22,%22change_in_gross_2014_v_2013%22%5D%7D

