Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby 3.2.0 test #91

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5.8
3.2.0
22 changes: 21 additions & 1 deletion lib/orbf/rules_engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,32 @@
require "dentaku"
require "json"
require "dhis2"

# TODO: bad
module Dhis2
module Api
class Analytic < Base
class << self
def fetch_values(client, args)
params = [
[:dimension, "ou:#{args[:organisation_units]}"],
[:dimension, "dx:#{args[:data_elements]}"],
[:dimension, "pe:#{args[:periods]}"]
]

client.get(resource_name, RestClient::ParamsArray.new(params))
end
end
end
end
end

require "set"
require "active_support/time"
require "active_support/core_ext/enumerable"

require_relative "./rules_engine/value_object"
require_relative "./rules_engine/assertions.rb"
require_relative "./rules_engine/assertions"
require_relative "./rules_engine/log"
require_relative "./rules_engine/services/tokenizer"
require_relative "./rules_engine/services/period_converter"
Expand Down
21 changes: 5 additions & 16 deletions lib/orbf/rules_engine/builders/activity_variables_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@ module RulesEngine
class ActivityVariablesBuilder
include VariablesBuilderSupport

class ValueLookup < Orbf::RulesEngine::ValueObject
attributes :value, :is_null

attr_reader :value, :is_null

def initialize(value:, is_null:)
@value = value
@is_null = is_null
freeze
end
end
ValueLookup = Data.define(:value, :is_null)

class << self
def to_variables(package_arguments, dhis2_values)
Expand Down Expand Up @@ -52,7 +42,8 @@ def convert(period)

if using_is_null?(flattened_dependencies, state)
express = expression.is_null ? "1" : "0"
array.push register_vars(package, activity.activity_code, suffix_is_null(suffixed_state), express, orgunit_id, period)
array.push register_vars(package, activity.activity_code, suffix_is_null(suffixed_state), express,
orgunit_id, period)
end
end
end
Expand Down Expand Up @@ -87,7 +78,7 @@ def register_vars(package, activity_code, state, expression, orgunit_id, period)
def de_values(activity_state, period, dependencies)
orgunits.each do |orgunit|
keys = build_keys_with_yearly([orgunit.ext_id, period, activity_state.ext_id])
if (dependencies.include?(activity_state.state+"_quarterly"))
if dependencies.include?(activity_state.state + "_quarterly")
quarter = package.calendar.periods(period, "quarterly").first
keys = [[orgunit.ext_id, quarter, activity_state.ext_id]] + keys
end
Expand Down Expand Up @@ -180,9 +171,7 @@ def build_keys_with_yearly(key)
[orgunit, package.calendar.periods(period, "financial_july").first, de]
]

if period.include?("Q")
keys << package.calendar.periods(period, "monthly").map { |pe| [orgunit, pe, de] }
end
keys << package.calendar.periods(period, "monthly").map { |pe| [orgunit, pe, de] } if period.include?("Q")

keys
end
Expand Down
14 changes: 3 additions & 11 deletions lib/orbf/rules_engine/builders/indicator_expression_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@

module Orbf
module RulesEngine
class IndicatorExpression < Orbf::RulesEngine::ValueObject
attributes :expression, :data_element, :category_combo
attr_reader :expression, :data_element, :category_combo
def initialize(expression: nil, data_element: nil, category_combo: nil)
@expression = expression
@data_element = data_element
@category_combo = category_combo
freeze
end
end
IndicatorExpression = Data.define(:expression, :data_element, :category_combo)

class UnsupportedFormulaException < StandardError
attr_reader :formula, :unsupported

def initialize(formula, unsupported)
@formula = formula
@unsupported = unsupported
Expand Down Expand Up @@ -46,7 +38,7 @@ def to_indicator_expression(expression)
data_element_category = expression.sub('#{', "").sub("}", "")
data_element, category = data_element_category.split(".").map(&:strip)
IndicatorExpression.new(
expression: '#{' + expression.strip + '}',
expression: '#{' + expression.strip + "}",
data_element: data_element,
category_combo: category
)
Expand Down
10 changes: 5 additions & 5 deletions lib/orbf/rules_engine/data/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class Activity < RulesEngine::ValueObject

attr_reader :name, :activity_code, :activity_states

def initialize(name:, activity_code:, activity_states:)
@name = name
@activity_code = activity_code
@activity_states = activity_states
def initialize(hash)
@name = hash[:name]
@activity_code = hash[:activity_code]
@activity_states = hash[:activity_states]
end

def states
def states
@states ||= activity_states.map(&:state).freeze
end
end
Expand Down
31 changes: 16 additions & 15 deletions lib/orbf/rules_engine/data/activity_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ def self.new_constant(state:, name:, formula:)

def self.new_data_element(state:, name:, ext_id:, origin:, category_combo_ext_id: nil)
with(
state: state,
name: name,
ext_id: ext_id,
kind: Kinds::KIND_DATA_ELEMENT,
origin: origin,
formula: nil,
state: state,
name: name,
ext_id: ext_id,
kind: Kinds::KIND_DATA_ELEMENT,
origin: origin,
formula: nil,
category_combo_ext_id: category_combo_ext_id
)
end
Expand All @@ -80,14 +80,15 @@ def self.new_indicator(state:, name:, ext_id:, expression:, origin:)
)
end

def initialize(state: nil, ext_id: nil, name: nil, kind: nil, formula: nil, origin: nil, category_combo_ext_id: nil)
@state = state
@ext_id = ext_id
@name = name
@kind = kind
@formula = formula
@origin = origin
@category_combo_ext_id = category_combo_ext_id
def initialize(*_args, **_kwargs)
hash = _args[0]
@state = hash[:state]
@ext_id = hash[:ext_id]
@name = hash[:name]
@kind = hash[:kind]
@formula = hash[:formula]
@origin = hash[:origin]
@category_combo_ext_id = hash[:category_combo_ext_id]

after_init
end
Expand Down Expand Up @@ -126,7 +127,7 @@ def after_init
raise "State is mandatory #{debug_info}" unless @state

@state = state.to_s
Kinds.assert_valid_kind_and_formula(kind, formula,self )
Kinds.assert_valid_kind_and_formula(kind, formula, self)
Origins.assert_valid_origin(origin, self)
end
end
Expand Down
26 changes: 13 additions & 13 deletions lib/orbf/rules_engine/data/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ class ActivityItem < Orbf::RulesEngine::ValueObject
attributes :activity, :solution, :problem, :substitued, :variables
attr_reader :activity, :solution, :problem, :substitued, :variables

def initialize(activity: nil, solution: nil, problem: nil, substitued: nil, variables: nil)
@activity = activity
@solution = solution
@problem = problem
@substitued = substitued
@variables = variables
def initialize(hash)
@activity = hash[:activity]
@solution = hash[:solution]
@problem = hash[:problem]
@substitued = hash[:substitued]
@variables = hash[:variables]
@indexed_variables = variables.index_by { |v| [v.state, v.activity_code] }
freeze
end
Expand All @@ -23,7 +23,7 @@ def not_exported?(code)
return false unless var&.formula&.exportable_formula_code

val = solution[var.formula.exportable_formula_code]
val == false || val == 0
[false, 0].include?(val)
end

def input?(code)
Expand All @@ -44,12 +44,12 @@ class TotalItem < Orbf::RulesEngine::ValueObject
attributes :key, :formula, :explanations, :value, :not_exported
attr_reader :key, :formula, :explanations, :value, :not_exported

def initialize(key:nil, formula: nil, explanations: nil, value: nil, not_exported:)
@key = key
@formula = formula
@explanations = explanations
@value = value
@not_exported = not_exported
def initialize(hash)
@key = hash[:key]
@formula = hash[:formula]
@explanations = hash[:explanations]
@value = hash[:value]
@not_exported = hash[:not_exported]
freeze
end

Expand Down
10 changes: 5 additions & 5 deletions lib/orbf/rules_engine/data/org_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class OrgUnit < Orbf::RulesEngine::ValueObject

attr_reader :ext_id, :name, :path, :group_ext_ids, :parent_ext_ids

def initialize(ext_id:, name:, path:, group_ext_ids:)
@ext_id = ext_id
@name = name
@path = path
@group_ext_ids = group_ext_ids || []
def initialize(hash)
@ext_id = hash[:ext_id]
@name = hash[:name]
@path = hash[:path]
@group_ext_ids = hash[:group_ext_ids] || []
end

def parent_ext_ids
Expand Down
9 changes: 5 additions & 4 deletions lib/orbf/rules_engine/data/org_unit_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ module RulesEngine
class OrgUnitGroup < Orbf::RulesEngine::ValueObject
attributes :ext_id, :name, :code
attr_reader :ext_id, :name, :code
def initialize(ext_id:, name:, code:)
@ext_id = ext_id
@name = name
@code = code

def initialize(hash)
@ext_id = hash[:ext_id]
@name = hash[:name]
@code = hash[:code]
end

def code_downcase
Expand Down
11 changes: 6 additions & 5 deletions lib/orbf/rules_engine/data/org_unit_groupset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ module RulesEngine
class OrgUnitGroupset < Orbf::RulesEngine::ValueObject
attributes :ext_id, :name, :group_ext_ids, :code
attr_reader :ext_id, :name, :group_ext_ids, :code
def initialize(ext_id:, name:, group_ext_ids:, code:)
@ext_id = ext_id
@name = name
@group_ext_ids = group_ext_ids
@code = code

def initialize(hash)
@ext_id = hash[:ext_id]
@name = hash[:name]
@group_ext_ids = hash[:group_ext_ids]
@code = hash[:code]
freeze
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/orbf/rules_engine/data/org_unit_with_facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class OrgUnitWithFacts < Orbf::RulesEngine::ValueObject

attr_reader :orgunit, :facts

def initialize(orgunit:, facts:)
@orgunit = orgunit
@facts = facts
def initialize(hash)
@orgunit = hash[:orgunit]
@facts = hash[:facts]
freeze
end

Expand Down
10 changes: 5 additions & 5 deletions lib/orbf/rules_engine/data/package_arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ class PackageArguments < Orbf::RulesEngine::ValueObject
attributes :periods, :orgunits, :datasets_ext_ids, :package
attr_reader :periods, :orgunits, :datasets_ext_ids, :package

def initialize(periods:, orgunits:, datasets_ext_ids:, package:)
@periods = periods
@orgunits = orgunits
@datasets_ext_ids = datasets_ext_ids
@package = package
def initialize(hash)
@periods = hash[:periods]
@orgunits = hash[:orgunits]
@datasets_ext_ids = hash[:datasets_ext_ids]
@package = hash[:package]
freeze
end
end
Expand Down
40 changes: 18 additions & 22 deletions lib/orbf/rules_engine/data/variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,30 +132,26 @@ module Types
].freeze
end

ATTRIBUTES = %i[key period expression type state activity_code
orgunit_ext_id formula package payment_rule exportable_variable_key
category_option_combo_ext_id
].freeze
ATTRIBUTES = %i[key period expression type state activity_code
orgunit_ext_id formula package payment_rule exportable_variable_key
category_option_combo_ext_id].freeze

attributes(*ATTRIBUTES)
attr_reader(*ATTRIBUTES)
attr_reader :dhis2_period

def initialize(key: nil, period: nil, expression: nil, type: nil, state: nil,
activity_code: nil, orgunit_ext_id: nil, formula: nil, package: nil, payment_rule: nil,
exportable_variable_key: nil, category_option_combo_ext_id: nil)
@key = key
@period = period
@expression = expression
@type = type
@state = state
@activity_code = activity_code
@orgunit_ext_id = orgunit_ext_id
@formula = formula
@package = package
@payment_rule = payment_rule
@exportable_variable_key = exportable_variable_key
@category_option_combo_ext_id = category_option_combo_ext_id
attr_reader(*ATTRIBUTES, :dhis2_period)

def initialize(hash)
@key = hash[:key]
@period = hash[:period]
@expression = hash[:expression]
@type = hash[:type]
@state = hash[:state]
@activity_code = hash[:activity_code]
@orgunit_ext_id = hash[:orgunit_ext_id]
@formula = hash[:formula]
@package = hash[:package]
@payment_rule = hash[:payment_rule]
@exportable_variable_key = hash[:exportable_variable_key]
@category_option_combo_ext_id = hash[:category_option_combo_ext_id]
after_init
end

Expand Down
13 changes: 5 additions & 8 deletions lib/orbf/rules_engine/fetch_data/fetch_data_analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@ def initialize(dhis2_connection, package_arguments)
def call
return [] if analytics_activity_states.none?

combined_response = Array(without_yearly_periods).each_slice(MAX_PERIODS_PER_FETCH).inject({}) do |result, period_slice|
puts({periods: period_slice.join(";"),
organisation_units: orgunit_ext_ids,
data_elements: data_elements}.to_json)

analytics_response = dhis2_connection.analytics.list(
combined_response = Array(without_yearly_periods).each_slice(MAX_PERIODS_PER_FETCH).each_with_object({}) do |period_slice, result|
params = {
periods: period_slice.join(";"),
organisation_units: orgunit_ext_ids,
data_elements: data_elements
)
}
analytics_response = dhis2_connection.analytics.fetch_values(params)
result["rows"] ||= []
result["rows"] += analytics_response["rows"]
result
end

map_to_data_values(combined_response).uniq
Expand All @@ -38,6 +34,7 @@ def call
def map_to_data_values(analytics_response)
analytics_response["rows"].each_with_object([]) do |v, array|
next if v[3] == "NaN"

array.push(
"dataElement" => data_element_mappings[v[0]] || v[0],
"period" => v[2],
Expand Down
Loading