class DruidConfig::Entities::Rule

Rule class

Constants

FOREVER_DRUID_STRING

Identifier for type

INTERVAL_DRUID_STRING
PERIOD_DRUID_STRING

Attributes

datasource[R]

Variables

interval[R]

Variables

period[R]

Variables

replicants[R]

Variables

time_type[R]

Variables

type[R]

Variables

Public Class Methods

detect_type(type_to_parse) click to toggle source

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

# 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
new(type, time_type, options = {}) click to toggle source

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
# 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
parse(data, datasource = nil) click to toggle source

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

# 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

Public Instance Methods

to_h() click to toggle source

Return the rule as Hash format

Returns:

Hash

# 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() click to toggle source

Return the rule as valid JSON for Druid

Returns:

JSON String

# File lib/druid_config/entities/rule.rb, line 119
def to_json
  to_h.to_json
end

Private Instance Methods

type_to_druid() click to toggle source

Convert the type to an String Druid can identify

Returns:

String with the type of the rule

# File lib/druid_config/entities/rule.rb, line 152
def type_to_druid
  time = if period?
           PERIOD_DRUID_STRING
         elsif interval?
           INTERVAL_DRUID_STRING
         else
           FOREVER_DRUID_STRING
         end
  "#{@type}#{time}"
end