<commit_before>require 'forwardable'

class Chef
  module Zabbix
    module API

      class GraphItem
        extend Forwardable
        def_delegators :@options, :[], :[]=, :delete
        def initialize(options)
          Chef::Application.fatal! ":item_template is required" if options[:item_template].to_s.empty?
          Chef::Application.fatal! ":item_key is required" if options[:item_key].to_s.empty?
          Chef::Application.fatal! ":calc_function must be a Zabbix::API::GraphItemCalcFunction" unless options[:calc_function].kind_of?(GraphItemCalcFunction)
          Chef::Application.fatal! ":type must be a Zabbix::API::GraphItemType" unless options[:type].kind_of?(GraphItemType)
          @options = options
        end

        def to_hash
          unless @options[:itemid]
            Chef::Application.fatal! ":itemid was never set. This probably means that an item with key '#{@options[:item_key]}' couldn't be found on template '#{@options[:item_template]}'"
          end
          {
            :itemid => @options[:itemid],
            :color => @options[:color],
            :calc_fnc => @options[:calc_function].value,
            :type => @options[:type].value,
            :periods_cnt => @options[:period_count]
          }
        end
      end

      class << self
        def find_hostgroup_ids(connection, hostgroup)
          group_id_request = {
            :method => "hostgroup.get",
            :params => { 
              :filter => {
                :name => hostgroup
              }
            }
          }
          connection.query(group_id_request)
        end

        def find_template_ids(connection, template)
          get_template_request = {
            :method => "template.get",
            :params => {
              :filter => {
                :host => template,
              }
            }
          }
          connection.query(get_template_request) 
        end

        def find_application_ids(connection, application, template_id)
          request = {
            :method => "application.get",
            :params => {
              :hostids => template_id,
              :filter => { 
                :name => application
              }
            }
          }
          connection.query(request)
        end

        def find_item_ids(connection, template_id, key, name=nil)
          request = {
            :method => "item.get",
            :params => {
              :hostids => template_id,
              :search => {
                :key_ => key
              }
            }
          }
          unless name.to_s.empty?
            request[:filter] = {
              :name => name
            }
          end

          connection.query(request)
        end

        def find_graph_ids(connection, template_id, name)
          request = {
            :method => "graph.get",
            :params => {
              :filter => {
                :name => name
              },
              :search => {
                :hostid => template_id
              }
            }
          }
          connection.query(request)
        end
      end

      module  Enumeration
        class << self
          def included(base)
            base.extend(ClassMethods)
            base.send(:include, ClassMethods)
          end 

          def extended(base)
            base.send(:include, ClassMethods)
          end 
        end 

        module ClassMethods
          attr_reader :enumeration_values
          def enum(name, val)
            @enumeration_values ||= {}
            @enumeration_values[name] ||= new(val)
            eigen_class = class << self; self; end
            eigen_class.send(:define_method, name) do
              @enumeration_values[name]
            end
          end
        end

        attr_reader :value
        def initialize(value)
          @value = value
        end 
      end 

      class ItemType
        include Enumeration
        enum :zabbix_agent,               0
        enum :snmp_v1_agent,              1
        enum :zabbix_trapper,             2
        enum :simple_check,               3<commit_msg>comment about 1.8.x compat<commit_after>require 'forwardable'

class Chef
  module Zabbix
    module API

      class GraphItem
        extend Forwardable
        def_delegators :@options, :[], :[]=, :delete
        def initialize(options)
          Chef::Application.fatal! ":item_template is required" if options[:item_template].to_s.empty?
          Chef::Application.fatal! ":item_key is required" if options[:item_key].to_s.empty?
          Chef::Application.fatal! ":calc_function must be a Zabbix::API::GraphItemCalcFunction" unless options[:calc_function].kind_of?(GraphItemCalcFunction)
          Chef::Application.fatal! ":type must be a Zabbix::API::GraphItemType" unless options[:type].kind_of?(GraphItemType)
          @options = options
        end

        def to_hash
          unless @options[:itemid]
            Chef::Application.fatal! ":itemid was never set. This probably means that an item with key '#{@options[:item_key]}' couldn't be found on template '#{@options[:item_template]}'"
          end
          {
            :itemid => @options[:itemid],
            :color => @options[:color],
            :calc_fnc => @options[:calc_function].value,
            :type => @options[:type].value,
            :periods_cnt => @options[:period_count]
          }
        end
      end

      class << self
        def find_hostgroup_ids(connection, hostgroup)
          group_id_request = {
            :method => "hostgroup.get",
            :params => { 
              :filter => {
                :name => hostgroup
              }
            }
          }
          connection.query(group_id_request)
        end

        def find_template_ids(connection, template)
          get_template_request = {
            :method => "template.get",
            :params => {
              :filter => {
                :host => template,
              }
            }
          }
          connection.query(get_template_request) 
        end

        def find_application_ids(connection, application, template_id)
          request = {
            :method => "application.get",
            :params => {
              :hostids => template_id,
              :filter => { 
                :name => application
              }
            }
          }
          connection.query(request)
        end

        def find_item_ids(connection, template_id, key, name=nil)
          request = {
            :method => "item.get",
            :params => {
              :hostids => template_id,
              :search => {
                :key_ => key
              }
            }
          }
          unless name.to_s.empty?
            request[:filter] = {
              :name => name
            }
          end

          connection.query(request)
        end

        def find_graph_ids(connection, template_id, name)
          request = {
            :method => "graph.get",
            :params => {
              :filter => {
                :name => name
              },
              :search => {
                :hostid => template_id
              }
            }
          }
          connection.query(request)
        end
      end

      module  Enumeration
        class << self
          def included(base)
            base.extend(ClassMethods)
            base.send(:include, ClassMethods)
          end 

          def extended(base)
            base.send(:include, ClassMethods)
          end 
        end 

        module ClassMethods
          attr_reader :enumeration_values
          def enum(name, val)
            @enumeration_values ||= {}
            @enumeration_values[name] ||= new(val)
            # The better solution would be to just call define_singleton_method
            # but that doesn't work in 1.8.x and we want 1.8.x compatibility
            eigen_class = class << self; self; end
            eigen_class.send(:define_method, name) do
              @enumeration_values[name]
            end
          end
        end

        attr_reader :value
        def initialize(value)
          @value = value
        end 
      end 

      class ItemType
        include Enumeration
        enum :zabbix_agent,               0
        enum :snmp_v1_agent,              1
        enum :zabbix_trapper,             2
        enum :simple_check,               3
{'fraction_non_alphanumeric': 0.09674738841405509, 'fraction_numerical': 0.001899335232668566, 'mean_word_length': 1.411276473955352, 'pattern_counts': {'":': 10, '<': 15, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1436002', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2159, 'n_words': 476}