Class: DruidConfig::Entities::Rule
- Inherits:
-
Object
- Object
- DruidConfig::Entities::Rule
- Defined in:
- lib/druid_config/entities/rule.rb
Overview
Rule class
Constant Summary collapse
- FOREVER_DRUID_STRING =
Identifier for type
'Forever'
- INTERVAL_DRUID_STRING =
'ByInterval'
- PERIOD_DRUID_STRING =
'ByPeriod'
Instance Attribute Summary collapse
-
#datasource ⇒ Object
readonly
Variables.
-
#interval ⇒ Object
readonly
Variables.
-
#period ⇒ Object
readonly
Variables.
-
#replicants ⇒ Object
readonly
Variables.
-
#time_type ⇒ Object
readonly
Variables.
-
#type ⇒ Object
readonly
Variables.
Class Method Summary collapse
-
.detect_type(type_to_parse) ⇒ Object
Detect the type of the rule based on ‘type’ field.
-
.parse(data, datasource = nil) ⇒ Object
Parse data from a Druid API response an initialize an object of Rule class.
Instance Method Summary collapse
-
#initialize(type, time_type, options = {}) ⇒ Rule
constructor
Initialize a Rule object.
-
#to_h ⇒ Object
Return the rule as Hash format.
-
#to_json ⇒ Object
Return the rule as valid JSON for Druid.
Constructor Details
#initialize(type, time_type, options = {}) ⇒ Rule
Initialize a Rule object. This constructor accepts a Hash with format defined in:
http://druid.io/docs/latest/operations/rule-configuration.html
Parameters:
- datasource
-
String with the name of the data source
- type
-
Type of the rule, it can be :drop or :load
- time_type
-
Time reference. It can be :forever, :period or :interval
- options
-
Hash with extra data to the rules.
- replicants: Hash with format { 'tier' => NumberOfReplicants, 'tier' => ... } - period: String with a period in ISO8601 format. Only available when type is :period. - interval: String with a interval in ISO8601 format. Only available when type is :interval. - datasource: Name of the datasource
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/druid_config/entities/rule.rb', line 64 def initialize(type, time_type, = {}) @type = type @time_type = time_type @datasource = [:datasource] @replicants = [:replicants] if period? @period = ISO8601::Duration.new([:period]) elsif interval? # TODO: https://github.com/arnau/ISO8601/issues/15 @interval = [:interval] end end |
Instance Attribute Details
#datasource ⇒ Object (readonly)
Variables
8 9 10 |
# File 'lib/druid_config/entities/rule.rb', line 8 def datasource @datasource end |
#interval ⇒ Object (readonly)
Variables
8 9 10 |
# File 'lib/druid_config/entities/rule.rb', line 8 def interval @interval end |
#period ⇒ Object (readonly)
Variables
8 9 10 |
# File 'lib/druid_config/entities/rule.rb', line 8 def period @period end |
#replicants ⇒ Object (readonly)
Variables
8 9 10 |
# File 'lib/druid_config/entities/rule.rb', line 8 def replicants @replicants end |
#time_type ⇒ Object (readonly)
Variables
8 9 10 |
# File 'lib/druid_config/entities/rule.rb', line 8 def time_type @time_type end |
#type ⇒ Object (readonly)
Variables
8 9 10 |
# File 'lib/druid_config/entities/rule.rb', line 8 def type @type end |
Class Method Details
.detect_type(type_to_parse) ⇒ Object
Detect the type of the rule based on ‘type’ field. This method will detect if is a drop/load rule and how it defines time.
Parameters:
- type_to_parse
-
String with the content of type field
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/druid_config/entities/rule.rb', line 131 def self.detect_type(type_to_parse) type = type_to_parse.starts_with?('drop') ? :drop : :load time_type = case type_to_parse.gsub(type.to_s, '') when INTERVAL_DRUID_STRING :interval when FOREVER_DRUID_STRING :forever when PERIOD_DRUID_STRING :period end [type, time_type] end |
.parse(data, datasource = nil) ⇒ Object
Parse data from a Druid API response an initialize an object of Rule class
Parameters:
- datasource
-
String with the name of the datsource
- data
-
Hash provided by the API
Returns:
Rule instance
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/druid_config/entities/rule.rb', line 29 def self.parse(data, datasource = nil) type, time_type = detect_type(data['type']) = { replicants: data['tieredReplicants'] } .merge!(datasource: datasource) if datasource if time_type == :period .merge!(period: data['period']) elsif time_type == :interval .merge!(interval: data['interval']) end # Instance the class new(type, time_type, ) end |
Instance Method Details
#to_h ⇒ Object
Return the rule as Hash format
Returns:
Hash
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/druid_config/entities/rule.rb', line 101 def to_h base = { type: type_to_druid } base.merge!(tieredReplicants: @replicants) if @replicants if period? base.merge(period: @period.to_s) elsif interval? base.merge(interval: @interval.to_s) else base end end |
#to_json ⇒ Object
Return the rule as valid JSON for Druid
Returns:
JSON String
119 120 121 |
# File 'lib/druid_config/entities/rule.rb', line 119 def to_json to_h.to_json end |