Class: DruidConfig::Entities::Rule

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @type = type
  @time_type = time_type
  @datasource = options[:datasource]
  @replicants = options[:replicants]
  if period?
    @period = ISO8601::Duration.new(options[:period])
  elsif interval?
    # TODO: https://github.com/arnau/ISO8601/issues/15
    @interval = options[:interval]
  end
end

Instance Attribute Details

#datasourceObject (readonly)

Variables



8
9
10
# File 'lib/druid_config/entities/rule.rb', line 8

def datasource
  @datasource
end

#intervalObject (readonly)

Variables



8
9
10
# File 'lib/druid_config/entities/rule.rb', line 8

def interval
  @interval
end

#periodObject (readonly)

Variables



8
9
10
# File 'lib/druid_config/entities/rule.rb', line 8

def period
  @period
end

#replicantsObject (readonly)

Variables



8
9
10
# File 'lib/druid_config/entities/rule.rb', line 8

def replicants
  @replicants
end

#time_typeObject (readonly)

Variables



8
9
10
# File 'lib/druid_config/entities/rule.rb', line 8

def time_type
  @time_type
end

#typeObject (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'])
  options = { replicants: data['tieredReplicants'] }
  options.merge!(datasource: datasource) if datasource
  if time_type == :period
    options.merge!(period: data['period'])
  elsif time_type == :interval
    options.merge!(interval: data['interval'])
  end
  # Instance the class
  new(type, time_type, options)
end

Instance Method Details

#to_hObject

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_jsonObject

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