Browse Source

Added get_domain. Changed set_project with add_project and added

delete_project and list_projects
master
Maxime Wack 6 years ago
parent
commit
8c6cdf6a35
4 changed files with 95 additions and 48 deletions
  1. +4
    -1
      NAMESPACE
  2. +84
    -17
      R/pm.R
  3. +7
    -5
      man/set_domain.Rd
  4. +0
    -25
      man/set_project.Rd

+ 4
- 1
NAMESPACE View File

@@ -9,6 +9,7 @@ export(add_ont)
export(add_ontology)
export(add_patients_demodata)
export(add_patients_imdata)
export(add_project)
export(add_user)
export(add_user_roles)
export(add_users)
@@ -24,12 +25,15 @@ export(delete_concept)
export(delete_modifier)
export(delete_ont)
export(delete_ontology)
export(delete_project)
export(delete_users)
export(fresh_install)
export(get_domain)
export(get_ont)
export(import_data)
export(list_concepts)
export(list_ont)
export(list_projects)
export(list_schemes)
export(list_user_roles)
export(list_users)
@@ -41,5 +45,4 @@ export(secure_db)
export(send_msg)
export(set_domain)
export(set_permissions)
export(set_project)
importFrom(dplyr,"%>%")

+ 84
- 17
R/pm.R View File

@@ -4,17 +4,18 @@
#'
#' Set the domain id and domain name in the databases
#' and set the domain name in the webclient'
#'
#' @param admin Name of the database admin account
#' @param pass Password of the database admin account
#'
#' @param domain_id The desired domain_id
#' @param domain_name The desired domain_name
#' @param host The host to connect to
#' @param admin Name of the database admin account
#' @param pass Password of the database admin account
#' @export
set_domain <- function(admin, pass, domain_id, domain_name)
set_domain <- function(domain_id, domain_name, host = "", admin = "", pass = "")
{
# Connect to the db
hive <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host = "127.0.0.1", dbname = "i2b2hive", user = admin, password = pass)
pm <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host = "127.0.0.1", dbname = "i2b2pm", user = admin, password = pass)
hive <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host = host, dbname = "i2b2hive", user = admin, password = pass)
pm <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host = host, dbname = "i2b2pm", user = admin, password = pass)

# Set the domain id to all cells in i2b2hive
c("crc", "im", "ont", "work") %>%
@@ -36,35 +37,101 @@ set_domain <- function(admin, pass, domain_id, domain_name)
RPostgreSQL::dbDisconnect(pm)
}

#' Set the project
#'
#' Set the project id, project path and project name of the instance
#' Get the domain
#'
#' Set the project id, project path and project name in the databases
#' Get the domain id of the instance
#'
#' @param host Address of the host, defaults to 127.0.0.1
#' @param host The host to connect to
#' @param admin Name of the database admin account
#' @param pass Password of the database admin account
#' @return The domain id and name
#' @export
get_domain <- function(host = "", admin = "", pass = "")
{
dplyr::src_postgres("i2b2pm", host = host, user = admin, password = pass) %>%
dplyr::tbl("pm_hive_data") %>%
dplyr::collect()
}

#' Add a project
#'
#' Add a project with its id, path and name in the hive
#'
#' Also create a specific database for this project
#'
#' @param project_id The desired project id
#' @param project_name The desired project name
#' @param host Address of the host, defaults to 127.0.0.1
#' @param admin Name of the database admin account
#' @param pass Password of the database admin account
#' @export
set_project <- function(project_id, project_name, host = "", admin = "", pass = "")
add_project <- function(project_id, project_name, host = "", admin = "", pass = "")
{
# Get the current domain id
domain_id <- get_domain(host, admin, pass)$domain_id

# Connect to the db
hive <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host, dbname = "i2b2hive", user = admin, password = pass)
pm <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host, dbname = "i2b2pm", user = admin, password = pass)

# Set the project id to all cells in i2b2hive
RPostgreSQL::dbGetQuery(hive, stringr::str_c("INSERT INTO im_db_lookup (c_domain_id, c_project_path, c_owner_id, c_db_fullschema, c_db_datasource, c_db_servertype, c_db_nicename) VALUES ('", domain_id, "', '", project_id, "/', '@', 'public', 'java:/IMDemoDS', 'POSTGRESQL', 'IM');"))
RPostgreSQL::dbGetQuery(hive, stringr::str_c("INSERT INTO ont_db_lookup (c_domain_id, c_project_path, c_owner_id, c_db_fullschema, c_db_datasource, c_db_servertype, c_db_nicename) VALUES ('", domain_id, "', '", project_id, "/', '@', 'public', 'java:/OntologyDemoDS', 'POSTGRESQL', 'Metadata');"))
RPostgreSQL::dbGetQuery(hive, stringr::str_c("INSERT INTO work_db_lookup (c_domain_id, c_project_path, c_owner_id, c_db_fullschema, c_db_datasource, c_db_servertype, c_db_nicename) VALUES ('", domain_id, "', '", project_id, "/', '@', 'public', 'java:/WorkplaceDemoDS', 'POSTGRESQL', 'Workplace');"))
RPostgreSQL::dbGetQuery(hive, stringr::str_c("INSERT INTO crc_db_lookup (c_domain_id, c_project_path, c_owner_id, c_db_fullschema, c_db_datasource, c_db_servertype, c_db_nicename) VALUES ('", domain_id, "', '/", project_id, "/', '@', 'public', 'java:/QueryTool", project_id, "DS', 'POSTGRESQL', '", project_name,"');"))

# Set the project id and name in pm_hive_data
RPostgreSQL::dbGetQuery(pm, stringr::str_c("INSERT INTO pm_project_data (project_id, project_name, project_wiki, project_path) VALUES ('", project_id, "', '", project_name, "', 'http://www.i2b2.org', '/", project_id, "');"))

RPostgreSQL::dbGetQuery(hive, stringr::str_c("CREATE DATABASE i2b2", project_id, "data WITH TEMPLATE i2b2demodata owner i2b2demodata;"))

# Disconnect the db
RPostgreSQL::dbDisconnect(hive)
RPostgreSQL::dbDisconnect(pm)
}

#' List projects
#'
#' List available projects in the hive
#'
#' @param host Address of the host, defaults to 127.0.0.1
#' @param admin Name of the database admin account
#' @param pass Password of the database admin account
#' @return The list of projects
#' @export
list_projects <- function(host = "", admin = "", pass = "")
{
dplyr::src_postgres(dbname = "i2b2pm", host = host, user = admin, password = pass) %>%
dplyr::tbl("pm_project_data") %>%
dplyr::collect()
}


#' Delete a project
#'
#' Delete a project from the hive
#'
#' @param project_id The project to delete
#' @param host Address of the host, defaults to 127.0.0.1
#' @param admin Name of the database admin account
#' @param pass Password of the database admin account
#' @export
delete_project <- function(project_id, host = "", admin = "", pass = "")
{
# Connect to the db
hive <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host = host, dbname = "i2b2hive", user = admin, password = pass)
pm <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host, dbname = "i2b2pm", user = admin, password = pass)

c("im", "ont", "work") %>%
purrr::walk(~RPostgreSQL::dbGetQuery(hive, stringr::str_c("UPDATE ", .x, "_db_lookup SET c_project_path = '", project_id, "/';")))
purrr::walk(~RPostgreSQL::dbGetQuery(hive, stringr::str_c("DELETE FROM ", .x, "_db_lookup WHERE c_project_path = '", project_id, "/';")))
RPostgreSQL::dbGetQuery(hive, stringr::str_c("DELETE FROM crc_db_lookup WHERE c_project_path = '/", project_id, "/';"))

RPostgreSQL::dbGetQuery(hive, stringr::str_c("UPDATE crc_db_lookup SET c_project_path = '/", project_id, "/';"))
RPostgreSQL::dbGetQuery(pm, stringr::str_c("DELETE FROM pm_project_data WHERE project_id = '", project_id, "';"))

# Set the project id and name in pm_hive_data
RPostgreSQL::dbGetQuery(pm, stringr::str_c("UPDATE pm_project_data SET project_id = '", project_id, "', project_name = '", project_name, "', project_path = '/", project_id, "';"))
RPostgreSQL::dbGetQuery(pm, stringr::str_c("UPDATE pm_project_user_roles SET project_id = '", project_id, "' WHERE (project_id = 'Demo');"))
RPostgreSQL::dbGetQuery(pm, stringr::str_c("DROP DATABASE i2b2", project_id, "demodata;"))

# Disconnect the db
RPostgreSQL::dbDisconnect(hive)
RPostgreSQL::dbDisconnect(pm)
}


+ 7
- 5
man/set_domain.Rd View File

@@ -4,16 +4,18 @@
\alias{set_domain}
\title{Set the domain}
\usage{
set_domain(admin, pass, domain_id, domain_name)
set_domain(domain_id, domain_name, host = "", admin = "", pass = "")
}
\arguments{
\item{admin}{Name of the database admin account}

\item{pass}{Password of the database admin account}

\item{domain_id}{The desired domain_id}

\item{domain_name}{The desired domain_name}

\item{host}{The host to connect to}

\item{admin}{Name of the database admin account}

\item{pass}{Password of the database admin account}
}
\description{
Set the domain id and domain name of the instance


+ 0
- 25
man/set_project.Rd View File

@@ -1,25 +0,0 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/pm.R
\name{set_project}
\alias{set_project}
\title{Set the project}
\usage{
set_project(host = "127.0.0.1", admin, pass, project_id, project_name)
}
\arguments{
\item{host}{Address of the host, defaults to 127.0.0.1}

\item{admin}{Name of the database admin account}

\item{pass}{Password of the database admin account}

\item{project_id}{The desired project id}

\item{project_name}{The desired project name}
}
\description{
Set the project id, project path and project name of the instance
}
\details{
Set the project id, project path and project name in the databases
}

Loading…
Cancel
Save