Skip to content

Commit

Permalink
refactor: add $workflow methods
Browse files Browse the repository at this point in the history
  • Loading branch information
imathews committed Dec 4, 2024
1 parent a9f13a3 commit a35817c
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 44 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: redivis
Type: Package
Title: R interface for Redivis
Version: 0.7.10
Version: 0.8.0
Author: Redivis Inc.
Maintainer: Redivis <[email protected]>
Description: Supports working with Redivis API through R.
Expand Down Expand Up @@ -38,7 +38,7 @@ Collate:
'Export.R'
'Variable.R'
'User.R'
'Project.R'
'Workflow.R'
'Table.R'
'Organization.R'
'Dataset.R'
Expand Down
6 changes: 3 additions & 3 deletions R/Notebook.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ Notebook <- setRefClass("Notebook",

base::file.remove(temp_file_path)

user_name <- unlist(strsplit(Sys.getenv("REDIVIS_DEFAULT_PROJECT"), "[.]"))[1]
project_name <- unlist(strsplit(Sys.getenv("REDIVIS_DEFAULT_PROJECT"), "[.]"))[2]
table <- User$new(name=user_name)$project(name=project_name)$table(name=res$name)
user_name <- unlist(strsplit(Sys.getenv("REDIVIS_DEFAULT_WORKFLOW"), "[.]"))[1]
workflow_name <- unlist(strsplit(Sys.getenv("REDIVIS_DEFAULT_WORKFLOW"), "[.]"))[2]
table <- User$new(name=user_name)$workflow(name=workflow_name)$table(name=res$name)

table$properties <- res

Expand Down
10 changes: 5 additions & 5 deletions R/Query.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#' @include Dataset.R Project.R api_request.R
#' @include Dataset.R Workflow.R api_request.R
Query <- setRefClass("Query",
fields = list(query="character", default_dataset="character", default_project="character", properties="list"),
fields = list(query="character", default_dataset="character", default_workflow="character", properties="list"),
methods = list(
show = function(){
print(str_interp("<Query ${.self$properties$id}>"))
},
initialize = function(query, default_dataset=NULL, default_project=NULL){
initialize = function(query, default_dataset=NULL, default_workflow=NULL){
payload <- list(query=query)

if (!is.null(default_project) && default_project != ""){
payload$defaultProject <- default_project
if (!is.null(default_workflow) && default_workflow != ""){
payload$defaultWorkflow <- default_workflow
}
if (!is.null(default_dataset) && default_dataset != "" ){
payload$defaultDataset <- default_dataset
Expand Down
12 changes: 6 additions & 6 deletions R/Table.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@

#' @include Dataset.R Project.R Variable.R Export.R util.R api_request.R
#' @include Dataset.R Workflow.R Variable.R Export.R util.R api_request.R
Table <- setRefClass("Table",
fields = list(name="character", dataset="ANY", project="ANY", properties="list", qualified_reference="character", scoped_reference="character", uri="character"),
fields = list(name="character", dataset="ANY", workflow="ANY", properties="list", qualified_reference="character", scoped_reference="character", uri="character"),

methods = list(
initialize = function(..., name="", dataset=NULL, project=NULL, properties=list()){
initialize = function(..., name="", dataset=NULL, workflow=NULL, properties=list()){
parent_reference <- ""
if (!is.null(dataset)) {
parent_reference <- str_interp("${dataset$qualified_reference}.")
} else if (!is.null(project)){
parent_reference <- str_interp("${project$qualified_reference}.")
} else if (!is.null(workflow)){
parent_reference <- str_interp("${workflow$qualified_reference}.")
}
scoped_reference_val <- if (length(properties$scopedReference)) properties$scopedReference else name
qualified_reference_val <- if (length(properties$qualifiedReference)) properties$qualifiedReference else str_interp("${parent_reference}${name}")
callSuper(...,
name=name,
dataset=dataset,
project=project,
workflow=workflow,
qualified_reference=qualified_reference_val,
scoped_reference=scoped_reference_val,
uri=str_interp("/tables/${URLencode(qualified_reference_val)}"),
Expand Down
2 changes: 1 addition & 1 deletion R/Upload.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Upload <- setRefClass("Upload",
)

get_upload_uri = function(upload){
str_interp("${project$user$name}.${project$name}")
str_interp("${upload$table$uri}.${upload$name}")
}

MAX_CHUNK_SIZE = 2 ** 23
Expand Down
23 changes: 18 additions & 5 deletions R/User.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' @include Dataset.R Project.R api_request.R
#' @include Dataset.R Workflow.R api_request.R
User <- setRefClass("User",
fields = list(name="character"),
methods = list(
Expand All @@ -9,8 +9,13 @@ User <- setRefClass("User",
Dataset$new(name=name, version=version, user=.self)
},

workflow = function(name) {
Workflow$new(name=name, user=.self)
},

project = function(name) {
Project$new(name=name, user=.self)
warning("Deprecation warning: Projects have been renamed to Workflows, please update your code to: user$workflow()")
Workflow$new(name=name, user=.self)
},

list_datasets = function(max_results=NULL) {
Expand All @@ -20,10 +25,18 @@ User <- setRefClass("User",
})
},

list_workflows = function(max_results=NULL) {
workflows <- make_paginated_request(path=str_interp("/users/${.self$name}/workflows"), page_size=100, max_results=max_results)
purrr::map(workflows, function(workflow) {
Workflow$new(name=workflow$name, properties=workflow, user=.self)
})
},

list_projects = function(max_results=NULL) {
projects <- make_paginated_request(path=str_interp("/users/${.self$name}/projects"), page_size=100, max_results=max_results)
purrr::map(projects, function(project) {
Project$new(name=project$name, properties=project, user=.self)
warning("Deprecation warning: Projects have been renamed to Workflows, please update your code to: user$list_workflows()")
workflows <- make_paginated_request(path=str_interp("/users/${.self$name}/workflows"), page_size=100, max_results=max_results)
purrr::map(workflows, function(workflow) {
Workflow$new(name=workflow$name, properties=workflow, user=.self)
})
}
)
Expand Down
16 changes: 8 additions & 8 deletions R/Project.R → R/Workflow.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' @include User.R api_request.R
Project <- setRefClass("Project",
Workflow <- setRefClass("Workflow",
fields = list(name="character",
user="ANY",
uri="character",
Expand All @@ -17,18 +17,18 @@ Project <- setRefClass("Project",
user=user,
qualified_reference=qualified_reference_val,
scoped_reference=scoped_reference_val,
uri=str_interp("/projects/${URLencode(qualified_reference_val)}"),
uri=str_interp("/workflows/${URLencode(qualified_reference_val)}"),
properties=properties
)
},

show = function(){
print(str_interp("<Project ${.self$qualified_reference}>"))
print(str_interp("<Workflow ${.self$qualified_reference}>"))
},

get = function(){
res <- make_request(path=.self$uri)
update_project_properties(.self, res)
update_workflow_properties(.self, res)
.self
},

Expand All @@ -46,11 +46,11 @@ Project <- setRefClass("Project",
},

table = function(name) {
Table(name=name, project=.self)
Table(name=name, workflow=.self)
},

query = function(query){
redivis::query(query, default_project = .self$qualified_reference)
redivis::query(query, default_workflow = .self$qualified_reference)
},

list_tables = function(max_results=NULL) {
Expand All @@ -60,13 +60,13 @@ Project <- setRefClass("Project",
max_results=max_results,
)
purrr::map(tables, function(table_properties) {
Table$new(name=table_properties$name, project=.self, properties=table_properties)
Table$new(name=table_properties$name, workflow=.self, properties=table_properties)
})
}
)
)

update_project_properties <- function(instance, properties){
update_workflow_properties <- function(instance, properties){
instance$properties = properties
instance$qualified_reference = properties$qualifiedReference
instance$scoped_reference = properties$scopedReference
Expand Down
47 changes: 33 additions & 14 deletions R/index.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' @importFrom stringr str_interp


#' @title query
#'
#' @description Create a Redivis SQL query
Expand All @@ -10,12 +11,12 @@
#' @examples
#' output_table <- redivis::query(query = 'SELECT 1 + 1 AS two')$to_tibble()
#' @export
query <- function(query="", default_project=NULL, default_dataset=NULL) {
if (is.null(default_project) && is.null(default_dataset)){
default_project <- Sys.getenv("REDIVIS_DEFAULT_PROJECT")
query <- function(query="", default_workflow=NULL, default_dataset=NULL) {
if (is.null(default_workflow) && is.null(default_dataset)){
default_workflow <- Sys.getenv("REDIVIS_DEFAULT_WORKFLOW")
default_dataset <- Sys.getenv("REDIVIS_DEFAULT_DATASET")
}
Query$new(query=query, default_project=default_project, default_dataset=default_dataset)
Query$new(query=query, default_workflow=default_workflow, default_dataset=default_dataset)
}


Expand All @@ -27,10 +28,10 @@ query <- function(query="", default_project=NULL, default_dataset=NULL) {
#'
#' @return class<user>
#' @examples
#' redivis::user('my_username')$project('my_project')$table('my_table')$to_tibble(max_results=100)
#' redivis::user('my_username')$workflow('my_workflow')$table('my_table')$to_tibble(max_results=100)
#'
#' We can also construct a query scoped to a particular project, removing the need to fully qualify table names
#' redivis::user('my_username')$project('my_project')$query("SELECT * FROM table_1 INNER JOIN table_2 ON id")$to_tibble()
#' We can also construct a query scoped to a particular workflow, removing the need to fully qualify table names
#' redivis::user('my_username')$workflow('my_workflow')$query("SELECT * FROM table_1 INNER JOIN table_2 ON id")$to_tibble()
#' @export
user <- function(name){
User$new(name=name)
Expand All @@ -48,15 +49,15 @@ user <- function(name){
#' redivis::organization('demo_organization')$dataset('some_dataset')$table('a_table')$to_tibble(max_results=100)
#'
#' We can also construct a query scoped to a particular dataset, removing the need to fully qualify table names
#' redivis::user('my_username')$project('my_project')$query("SELECT * FROM table_1 INNER JOIN table_2 ON id")$to_tibble()
#' redivis::user('my_username')$workflow('my_workflow')$query("SELECT * FROM table_1 INNER JOIN table_2 ON id")$to_tibble()
#' @export
organization <- function(name){
Organization$new(name=name)
}

#' @title table
#'
#' @description Reference a specific table when the REDIVIS_DEFAULT_PROJECT or REDIVIS_DEFAULT_DATASET env variable is set
#' @description Reference a specific table when the REDIVIS_DEFAULT_WORKFLOW or REDIVIS_DEFAULT_DATASET env variable is set
#'
#' @param name The table's username
#'
Expand All @@ -69,10 +70,10 @@ organization <- function(name){
table <- function(name){
if (Sys.getenv("REDIVIS_NOTEBOOK_JOB_ID") != ""){
Table$new(name=name)
} else if (Sys.getenv("REDIVIS_DEFAULT_PROJECT") != ""){
user_name <- unlist(strsplit(Sys.getenv("REDIVIS_DEFAULT_PROJECT"), "[.]"))[1]
project_name <- unlist(strsplit(Sys.getenv("REDIVIS_DEFAULT_PROJECT"), "[.]"))[2]
User$new(name=user_name)$project(name=project_name)$table(name=name)
} else if (Sys.getenv("REDIVIS_DEFAULT_WORKFLOW") != ""){
user_name <- unlist(strsplit(Sys.getenv("REDIVIS_DEFAULT_WORKFLOW"), "[.]"))[1]
workflow_name <- unlist(strsplit(Sys.getenv("REDIVIS_DEFAULT_WORKFLOW"), "[.]"))[2]
User$new(name=user_name)$workflow(name=workflow_name)$table(name=name)

}else if (Sys.getenv("REDIVIS_DEFAULT_DATASET") != ""){
user_name <- unlist(strsplit(Sys.getenv("REDIVIS_DEFAULT_DATASET"), "[.]"))[1]
Expand All @@ -81,7 +82,7 @@ table <- function(name){
User$new(name=user_name)$dataset(name=dataset_name)$table(name=name)
}
else{
stop("Cannot reference an unqualified table if the neither the REDIVIS_DEFAULT_PROJECT or REDIVIS_DEFAULT_DATASET environment variables are set.")
stop("Cannot reference an unqualified table if the neither the REDIVIS_DEFAULT_WORKFLOW or REDIVIS_DEFAULT_DATASET environment variables are set.")
}
}

Expand Down Expand Up @@ -132,4 +133,22 @@ authenticate <- function(force_reauthentication=FALSE) {
}


#' @title redivis
#'
#' @description Redivis wrapper, all primary methods accessible via $
#'
#' @return list
#' @examples
#' dataset <- redivis$user("username")$dataset("dataset_name")
#' @export
redivis <- list(
"query"=query,
"user"=user,
"organization"=organization,
"file"=file,
"table"=table,
"current_notebook"=current_notebook,
"authenticate"=authenticate
)


0 comments on commit a35817c

Please sign in to comment.