Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/finders/data_types_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def execute
data_types = base_scope
data_types = by_data_type(data_types)
data_types = by_runtime_function_definition(data_types)
data_types = by_function_definition(data_types)
data_types = by_flow_type(data_types)
data_types = by_flow(data_types)

Expand Down Expand Up @@ -35,6 +36,16 @@ def by_runtime_function_definition(data_types)
data_types.where(id: referenced_data_types_ids)
end

def by_function_definition(data_types)
return data_types unless params[:function_definition]

referenced_data_types_ids = FunctionDefinitionDataTypeLink
.where(function_definition: params[:function_definition])
.select(:referenced_data_type_id)

data_types.where(id: referenced_data_types_ids)
end

def by_flow_type(data_types)
return data_types unless params[:flow_type]

Expand Down
33 changes: 12 additions & 21 deletions app/graphql/types/function_definition_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class FunctionDefinitionType < Types::BaseObject

authorize :read_function_definition

field :identifier, String, null: false, description: 'Identifier of the function'
field :identifier, String, null: false,
description: 'Identifier of the function',
method: :runtime_name

field :parameter_definitions, Types::ParameterDefinitionType.connection_type,
null: true,
Expand All @@ -33,6 +35,14 @@ class FunctionDefinitionType < Types::BaseObject
field :throws_error, Boolean,
null: false, description: 'Indicates if the function can throw an error'

field :version, String,
null: false,
description: 'Version of the runtime function definition'

field :definition_source, String,
null: true,
description: 'The source that defines this definition'

# rubocop:disable GraphQL/ExtractType
field :display_icon, String,
null: true, description: 'Display icon of the function'
Expand All @@ -45,27 +55,8 @@ class FunctionDefinitionType < Types::BaseObject
id_field FunctionDefinition
timestamps

def identifier
object.runtime_function_definition&.runtime_name
end

def signature
object.runtime_function_definition&.signature
end

def throws_error
object.runtime_function_definition&.throws_error
end

def display_icon
object.runtime_function_definition&.display_icon
end

def linked_data_types
return [] unless object.runtime_function_definition

DataTypesFinder.new({ runtime_function_definition: object.runtime_function_definition,
expand_recursively: true }).execute
DataTypesFinder.new({ function_definition: object, expand_recursively: true }).execute
end
end
end
6 changes: 1 addition & 5 deletions app/graphql/types/parameter_definition_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ParameterDefinitionType < Types::BaseObject

authorize :read_parameter_definition

field :identifier, String, null: false, description: 'Identifier of the parameter'
field :identifier, String, null: false, description: 'Identifier of the parameter', method: :runtime_name

field :descriptions, [Types::TranslationType], null: true, description: 'Description of the parameter'
field :names, [Types::TranslationType], null: true, description: 'Name of the parameter'
Expand All @@ -23,9 +23,5 @@ class ParameterDefinitionType < Types::BaseObject

id_field ParameterDefinition
timestamps

def identifier
object.runtime_parameter_definition&.runtime_name
end
end
end
19 changes: 19 additions & 0 deletions app/grpc/function_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class FunctionHandler < Tucana::Sagittarius::FunctionDefinitionService::Service
include GrpcHandler
include Code0::ZeroTrack::Loggable

def update(request, _call)
current_runtime = Runtime.find(Code0::ZeroTrack::Context.current[:runtime][:id])

response = Runtimes::Grpc::FunctionDefinitions::UpdateService.new(
current_runtime,
request.functions
).execute

logger.debug("FunctionHandler#update response: #{response.inspect}")

Tucana::Sagittarius::FunctionDefinitionUpdateResponse.new(success: response.success?)
end
end
19 changes: 12 additions & 7 deletions app/models/function_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
class FunctionDefinition < ApplicationRecord
belongs_to :runtime_function_definition

has_one :runtime, through: :runtime_function_definition

has_many :function_definition_data_type_links, inverse_of: :function_definition
has_many :referenced_data_types, through: :function_definition_data_type_links, source: :referenced_data_type

has_many :node_functions, inverse_of: :function_definition
has_many :parameter_definitions, inverse_of: :function_definition

Expand All @@ -19,20 +24,20 @@ class FunctionDefinition < ApplicationRecord

def to_grpc
Tucana::Shared::FunctionDefinition.new(
runtime_name: runtime_function_definition.runtime_name,
runtime_name: runtime_name,
parameter_definitions: parameter_definitions.map(&:to_grpc),
signature: runtime_function_definition.signature,
throws_error: runtime_function_definition.throws_error,
signature: signature,
throws_error: throws_error,
name: names.map(&:to_grpc),
description: descriptions.map(&:to_grpc),
documentation: documentations.map(&:to_grpc),
deprecation_message: deprecation_messages.map(&:to_grpc),
display_message: display_messages.map(&:to_grpc),
alias: aliases.map(&:to_grpc),
linked_data_type_identifiers: runtime_function_definition.referenced_data_types.map(&:identifier),
version: runtime_function_definition.version,
display_icon: runtime_function_definition.display_icon,
definition_source: runtime_function_definition.definition_source,
linked_data_type_identifiers: referenced_data_types.map(&:identifier),
version: version,
display_icon: display_icon,
definition_source: definition_source,
runtime_definition_name: runtime_function_definition.runtime_name
)
end
Expand Down
6 changes: 6 additions & 0 deletions app/models/function_definition_data_type_link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class FunctionDefinitionDataTypeLink < ApplicationRecord
belongs_to :function_definition, inverse_of: :function_definition_data_type_links
belongs_to :referenced_data_type, class_name: 'DataType'
end
3 changes: 2 additions & 1 deletion app/models/parameter_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def function_definition_matches_definition

def to_grpc
Tucana::Shared::ParameterDefinition.new(
runtime_name: runtime_parameter_definition.runtime_name,
runtime_name: runtime_name,
runtime_definition_name: runtime_parameter_definition.runtime_name,
default_value: Tucana::Shared::Value.from_ruby(default_value),
name: names.map(&:to_grpc),
description: descriptions.map(&:to_grpc),
Expand Down
5 changes: 5 additions & 0 deletions app/services/error_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def self.error_codes
invalid_runtime_status: { description: 'The runtime status is invalid because of active model errors' },
invalid_runtime_status_configuration: { description: 'The runtime status configuration is invalid because of active model errors' },
invalid_runtime_feature: { description: 'The runtime feature is invalid because of active model errors' },
invalid_function_definition: { description: 'The function definition is invalid because of active model errors' },
parent_runtime_function_definition_not_found: { description: 'The parent runtime function definition could not be found' },
parent_runtime_parameter_definition_not_found: { description: 'The parent runtime parameter definition could not be found' },
invalid_parameter_definition: { description: 'The parameter definition is invalid because of active model errors' },
parameter_definition_count_mismatch: { description: 'The number of parameter definitions does not match the number of runtime parameter definitions' },

primary_level_not_found: { description: '', deprecation_reason: 'Outdated concept' },
secondary_level_not_found: { description: '', deprecation_reason: 'Outdated concept' },
Expand Down
154 changes: 154 additions & 0 deletions app/services/runtimes/grpc/function_definitions/update_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# frozen_string_literal: true

module Runtimes
module Grpc
module FunctionDefinitions
class UpdateService
include Sagittarius::Database::Transactional
include Code0::ZeroTrack::Loggable
include Runtimes::Grpc::TranslationUpdateHelper
include Runtimes::Grpc::DataTypeHelper

attr_reader :current_runtime, :function_definitions

def initialize(current_runtime, function_definitions)
@current_runtime = current_runtime
@function_definitions = function_definitions
end

def execute
transactional do |t|
# rubocop:disable Rails/SkipsModelValidations -- when marking definitions as removed, we don't care about validations
FunctionDefinition
.joins(:runtime)
.where(runtimes: { id: current_runtime })
.update_all(removed_at: Time.zone.now)
# rubocop:enable Rails/SkipsModelValidations
function_definitions.each do |function_definition|
response = update_function_definition(function_definition, t)
next if response.persisted?

logger.error(message: 'Failed to update function definition',
runtime_id: current_runtime.id,
definition_identifier: function_definition.runtime_name,
errors: response.errors.full_messages)

t.rollback_and_return! ServiceResponse.error(message: 'Failed to update function definition',
error_code: :invalid_function_definition,
details: response.errors)
end

UpdateRuntimeCompatibilityJob.perform_later({ runtime_id: current_runtime.id })

logger.info(message: 'Updated function definitions for runtime', runtime_id: current_runtime.id)

ServiceResponse.success(message: 'Updated function definition',
payload: function_definitions)
end
end

protected

def update_function_definition(function_definition, t)
parent_runtime_function_definition = RuntimeFunctionDefinition.find_by(
runtime: current_runtime,
runtime_name: function_definition.runtime_definition_name
)
if parent_runtime_function_definition.nil?
t.rollback_and_return! ServiceResponse.error(
message: 'Could not find parent runtime function definition for function definition',
error_code: :parent_runtime_function_definition_not_found,
details: { runtime_definition_name: function_definition.runtime_definition_name }
)
end

db_object = FunctionDefinition.find_or_initialize_by(
runtime_function_definition_id: parent_runtime_function_definition.id,
runtime_name: function_definition.runtime_name,
runtime_definition_name: function_definition.runtime_definition_name
)
db_object.runtime_function_definition = parent_runtime_function_definition
db_object.removed_at = nil
db_object.signature = function_definition.signature
db_object.throws_error = function_definition.throws_error
db_object.version = function_definition.version
db_object.definition_source = function_definition.definition_source
db_object.display_icon = function_definition.display_icon
db_object.names = update_translations(function_definition.name, db_object.names)
db_object.descriptions = update_translations(function_definition.description, db_object.descriptions)
db_object.documentations = update_translations(function_definition.documentation, db_object.documentations)
db_object.deprecation_messages = update_translations(function_definition.deprecation_message,
db_object.deprecation_messages)
db_object.display_messages = update_translations(function_definition.display_message,
db_object.display_messages)
db_object.aliases = update_translations(function_definition.alias, db_object.aliases)

db_object.save

db_object.parameter_definitions = update_parameters(db_object, function_definition.parameter_definitions,
db_object.parameter_definitions, t)

link_data_types(db_object, function_definition.linked_data_type_identifiers, t)
db_object
end

def update_parameters(function_definition, parameters, db_parameters, t)
# rubocop:disable Rails/SkipsModelValidations -- when marking definitions as removed, we don't care about validations
db_parameters.update_all(removed_at: Time.zone.now)
# rubocop:enable Rails/SkipsModelValidations

parameters.each do |real_param|
db_param = db_parameters.find do |current_param|
current_param.runtime_definition_name == real_param.runtime_definition_name
end
if db_param.nil?
db_param = ParameterDefinition.new
db_parameters << db_param
end

runtime_parameter_definition = RuntimeParameterDefinition.find_by(
runtime_function_definition_id: function_definition.runtime_function_definition_id,
runtime_name: real_param.runtime_definition_name
)
if runtime_parameter_definition.nil?
t.rollback_and_return! ServiceResponse.error(
message: 'Could not find parent runtime parameter definition for function definition parameter',
error_code: :parent_runtime_parameter_definition_not_found,
details: { runtime_definition_name: real_param.runtime_definition_name }
)
end

db_param.function_definition = function_definition
db_param.runtime_parameter_definition = runtime_parameter_definition
db_param.runtime_name = real_param.runtime_name
db_param.runtime_definition_name = real_param.runtime_definition_name
db_param.removed_at = nil

db_param.names = update_translations(real_param.name, db_param.names)
db_param.descriptions = update_translations(real_param.description, db_param.descriptions)
db_param.documentations = update_translations(real_param.documentation, db_param.documentations)

db_param.default_value = real_param.default_value&.to_ruby(true)

next if db_param.save

t.rollback_and_return! ServiceResponse.error(
message: 'Could not save parameter definition',
error_code: :invalid_parameter_definition,
details: db_param.errors
)
end

if db_parameters.reload.where(removed_at: nil).count !=
function_definition.runtime_function_definition.parameters.size
t.rollback_and_return! ServiceResponse.error(
message: 'Number of parameter definitions does not match number of runtime parameter definitions',
error_code: :parameter_definition_count_mismatch
)
end
db_parameters
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,6 @@ def update_runtime_function_definition(runtime_function_definition, t)

db_object.save

if db_object.function_definitions.empty?
definition = FunctionDefinition.new
definition.names = update_translations(runtime_function_definition.name, definition.names)
definition.descriptions = update_translations(runtime_function_definition.description,
definition.descriptions)
definition.documentations = update_translations(runtime_function_definition.documentation,
definition.documentations)
definition.display_messages = update_translations(runtime_function_definition.display_message,
definition.display_messages)
definition.aliases = update_translations(runtime_function_definition.alias, definition.aliases)

db_object.function_definitions << definition
end

db_object.parameters = update_parameters(db_object, runtime_function_definition.runtime_parameter_definitions,
db_object.parameters, t)

Expand Down Expand Up @@ -111,24 +97,13 @@ def update_parameters(runtime_function_definition, parameters, db_parameters, t)

db_param.default_value = real_param.default_value&.to_ruby(true)

unless db_param.save
t.rollback_and_return! ServiceResponse.error(
message: 'Could not save runtime parameter definition',
error_code: :invalid_runtime_parameter_definition,
details: db_param.errors
)
end

next unless db_param.parameter_definitions.empty?

definition = ParameterDefinition.new
definition.names = update_translations(real_param.name, definition.names)
definition.descriptions = update_translations(real_param.description, definition.descriptions)
definition.documentations = update_translations(real_param.documentation, definition.documentations)
definition.default_value = db_param.default_value
definition.function_definition = runtime_function_definition.function_definitions.first
next if db_param.save

db_param.parameter_definitions << definition
t.rollback_and_return! ServiceResponse.error(
message: 'Could not save runtime parameter definition',
error_code: :invalid_runtime_parameter_definition,
details: db_param.errors
)
end

db_parameters
Expand Down
Loading