Class: Manifold::API::Workspace

Inherits:
Object
  • Object
show all
Defined in:
lib/manifold/api/workspace.rb

Overview

Encapsulates a single manifold.

Constant Summary collapse

DEFAULT_TEMPLATE_PATH =
File.expand_path(
  "../templates/workspace_template.yml", __dir__
).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, template_path: DEFAULT_TEMPLATE_PATH, logger: Logger.new($stdout)) ⇒ Workspace

Returns a new instance of Workspace.



36
37
38
39
40
41
# File 'lib/manifold/api/workspace.rb', line 36

def initialize(name, template_path: DEFAULT_TEMPLATE_PATH, logger: Logger.new($stdout))
  @name = name
  @template_path = template_path
  @logger = logger
  @vector_service = Services::VectorService.new(logger)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



30
31
32
# File 'lib/manifold/api/workspace.rb', line 30

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/manifold/api/workspace.rb', line 30

def name
  @name
end

#template_pathObject (readonly)

Returns the value of attribute template_path.



30
31
32
# File 'lib/manifold/api/workspace.rb', line 30

def template_path
  @template_path
end

Class Method Details

.from_directory(directory, logger: Logger.new($stdout)) ⇒ Object



43
44
45
# File 'lib/manifold/api/workspace.rb', line 43

def self.from_directory(directory, logger: Logger.new($stdout))
  new(directory.basename.to_s, logger:)
end

Instance Method Details

#addObject



47
48
49
50
# File 'lib/manifold/api/workspace.rb', line 47

def add
  [tables_directory, routines_directory].each(&:mkpath)
  FileUtils.cp(template_path, manifold_path)
end

#dimensions_merge_sql_pathObject



122
123
124
# File 'lib/manifold/api/workspace.rb', line 122

def dimensions_merge_sql_path
  routines_directory.join("merge_dimensions.sql")
end

#generate(with_terraform: false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/manifold/api/workspace.rb', line 52

def generate(with_terraform: false)
  return nil unless manifold_exists? && any_vectors?

  write_schemas
  logger.info("Generated BigQuery dimensions table schema for workspace '#{name}'.")

  return unless with_terraform

  write_manifold_merge_sql
  write_dimensions_merge_sql
  generate_terraform
  logger.info("Generated Terraform configuration for workspace '#{name}'.")
end

#manifold_exists?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/manifold/api/workspace.rb', line 80

def manifold_exists?
  manifold_path.file?
end

#manifold_fileObject



74
75
76
77
78
# File 'lib/manifold/api/workspace.rb', line 74

def manifold_file
  return nil unless manifold_exists?

  File.new(manifold_path)
end

#manifold_pathObject



84
85
86
# File 'lib/manifold/api/workspace.rb', line 84

def manifold_path
  directory.join("manifold.yml")
end

#routines_directoryObject



70
71
72
# File 'lib/manifold/api/workspace.rb', line 70

def routines_directory
  directory.join("routines")
end

#tables_directoryObject



66
67
68
# File 'lib/manifold/api/workspace.rb', line 66

def tables_directory
  directory.join("tables")
end

#terraform_main_pathObject



88
89
90
# File 'lib/manifold/api/workspace.rb', line 88

def terraform_main_path
  directory.join("main.tf.json")
end

#valid_dimensions_config?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
# File 'lib/manifold/api/workspace.rb', line 111

def valid_dimensions_config?
  return false unless manifold_yaml

  !manifold_yaml["dimensions"]&.dig("merge", "source").nil?
end

#write_dimensions_merge_sqlObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/manifold/api/workspace.rb', line 100

def write_dimensions_merge_sql
  return unless valid_dimensions_config?

  source_sql = File.read(Pathname.pwd.join(manifold_yaml["dimensions"]["merge"]["source"]))
  sql_builder = Terraform::SQLBuilder.new(name, manifold_yaml)
  sql = sql_builder.build_dimensions_merge_sql(source_sql)
  return unless sql

  write_dimensions_merge_sql_file(sql)
end

#write_dimensions_merge_sql_file(sql) ⇒ Object



117
118
119
120
# File 'lib/manifold/api/workspace.rb', line 117

def write_dimensions_merge_sql_file(sql)
  routines_directory.mkpath
  dimensions_merge_sql_path.write(sql)
end

#write_manifold_merge_sqlObject



92
93
94
95
96
97
98
# File 'lib/manifold/api/workspace.rb', line 92

def write_manifold_merge_sql
  return unless manifold_file

  sql_builder = Terraform::SQLBuilder.new(name, manifold_yaml)
  sql = sql_builder.build_manifold_merge_sql
  routines_directory.join("merge_manifold.sql").write(sql)
end