-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathmodel.rb
More file actions
99 lines (82 loc) · 3 KB
/
model.rb
File metadata and controls
99 lines (82 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true
# == Schema Information
#
# Table name: models
#
# id :bigint not null, primary key
# name :string
# workspace_id :integer
# connector_id :integer
# query :text
# query_type :integer
# primary_key :string
# created_at :datetime not null
# updated_at :datetime not null
#
class Model < ApplicationRecord
AI_ML_CONFIG_JSON_SCHEMA = Rails.root.join("app/models/schema_validations/models/configuration_aiml.json")
DYNAMIC_SQL_CONFIG_JSON_SCHEMA = Rails.root.join(
"app/models/schema_validations/models/configuration_dynamic_sql.json"
)
UNSTRUCTURED_CONFIG_JSON_SCHEMA = Rails.root.join(
"app/models/schema_validations/models/configuration_unstructured.json"
)
VECTOR_SEARCH_CONFIG_JSON_SCHEMA = Rails.root.join(
"app/models/schema_validations/models/configuration_vector_search.json"
)
validates :workspace_id, presence: true
validates :connector_id, presence: true
validates :name, presence: true
enum :query_type, %i[raw_sql dbt soql table_selector ai_ml dynamic_sql unstructured vector_search]
validates :query, presence: true, if: :requires_query?
# Havesting configuration
validates :configuration, presence: true, if: :requires_configuration?
validates :configuration, presence: true, json: { schema: lambda {
configuration_schema_validation
} }, if: :requires_configuration?
belongs_to :workspace
belongs_to :connector
has_many :syncs, dependent: :destroy
has_many :visual_components, as: :configurable, dependent: :destroy
scope :data, -> { where(query_type: %i[raw_sql dbt soql table_selector dynamic_sql]) }
scope :ai_ml, -> { where(query_type: :ai_ml) }
scope :unstructured, -> { where(query_type: :unstructured) }
scope :vector_search, -> { where(query_type: :vector_search) }
default_scope { order(updated_at: :desc) }
def to_protocol
Multiwoven::Integrations::Protocol::Model.new(
name:,
query: query || "",
query_type:,
primary_key: primary_key || ""
)
end
def requires_query?
%w[raw_sql dbt soql table_selector].include?(query_type)
end
def requires_configuration?
%w[ai_ml dynamic_sql unstructured vector_search].include?(query_type)
end
def json_schema
configuration["json_schema"]
end
def masked_configuration
return configuration if configuration.blank?
schema_path = configuration_schema_validation
return configuration if schema_path.nil?
schema = JSON.parse(File.read(schema_path)).with_indifferent_access
Utils::SecretMasking.mask_by_keys(configuration.deep_dup, schema)
end
private
def configuration_schema_validation
if ai_ml?
AI_ML_CONFIG_JSON_SCHEMA
elsif dynamic_sql?
DYNAMIC_SQL_CONFIG_JSON_SCHEMA
elsif unstructured?
UNSTRUCTURED_CONFIG_JSON_SCHEMA
elsif vector_search?
VECTOR_SEARCH_CONFIG_JSON_SCHEMA
end
end
end