<commit_before>module Mystro
  module Connect
    class Base
      class << self
        attr_accessor :model
        attr_accessor :collection

        def cname
          self.name.split("::").last.downcase
        end

        def fog
          Mystro.send(cname).fog || nil rescue nil
        end

        def after(event, &block)
          @hooks ||= {}
          @hooks[event] ||= []
          @hooks[event] << block
        end

        def hooks(event, *args)
          Mystro::Log.debug "#{cname}:hooks:#{event} #{@hooks[event].count}"
          @hooks[event].each do |e|
            Mystro::Log.debug "#{cname}:hooks:#{event} #{e}"
            e.call(*args) if e
          end
        end
      end

      attr_reader :cfg
      attr_reader :opt
      attr_reader :fog

      def initialize
        a = Mystro.account.dup
        c = cname

        # opt is used to initialize fog in connect classes
        defaults = a.connect!.fog? ? a.connect.fog : {}
        overrides = a[c] && a[c].fog? ? a[c].fog : {}
        raw = defaults.deep_merge(overrides)
        @opt = Hashie::Mash.new(raw)

        # cfg is the additional configuration parms (aside from the fog block)
        raw = {}
        if a[c]
          raw = a[c].to_hash
          raw.delete(:fog) if raw[:fog]
        end
        @cfg = Hashie::Mash.new(raw)

        Mystro::Log.info "#{@cfg.inspect}"

        self.class.after :create do |compute, model|
          Mystro::Plugin.run "#{cname}:create", compute, model.fog_tags
        end

        self.class.after :destroy do |e, tags|
          Mystro::Plugin.run "#{cname}:destroy", e, tags
        end

        connect
      end

      def connect
        @fog ||= model.new(opt) if opt
      rescue => e
        Mystro::Log.error "#{cname} connect failed: #{e.message} at #{e.backtrace.first}"
      end

      def connected?
        fog.nil? ? false : true
      end

      def find(id)
        fog.send(collection).get(id)
      end

      def all
        fog.send(collection).all
      end

      def create(model)
        Mystro::Log.debug "#{cname}#create #{model.inspect}"
        e = fog.send(collection).create(model.fog_options)
        hooks :create, e, model
        e
      end

      def destroy(list)
        list = [*list]
        list.map {|e| e.kind_of?(String) ? find(e) : e}.each do |e|
          Mystro::Log.debug "#{cname}#destroy #{e.id}"
          e.destroy
          hooks :destroy, e, e.tags<commit_msg>fixes for mystro-server dns worker<commit_after>module Mystro
  module Connect
    class Base
      class << self
        attr_accessor :model
        attr_accessor :collection

        def cname
          self.name.split("::").last.downcase
        end

        def fog
          Mystro.send(cname).fog || nil rescue nil
        end

        def after(event, &block)
          @hooks ||= {}
          @hooks[event] ||= []
          @hooks[event] << block
        end

        def hooks(event, *args)
          Mystro::Log.debug "#{cname}:hooks:#{event} #{@hooks[event].count}"
          @hooks[event].each do |e|
            Mystro::Log.debug "#{cname}:hooks:#{event} #{e}"
            e.call(*args) if e
          end
        end
      end

      attr_reader :cfg
      attr_reader :opt
      attr_reader :fog

      def initialize
        a = Mystro.account.dup
        c = cname

        # opt is used to initialize fog in connect classes
        defaults = a.connect!.fog? ? a.connect.fog : {}
        overrides = a[c] && a[c].fog? ? a[c].fog : {}
        raw = defaults.deep_merge(overrides)
        @opt = Hashie::Mash.new(raw)

        # cfg is the additional configuration parms (aside from the fog block)
        raw = {}
        if a[c]
          raw = a[c].to_hash
          raw.delete(:fog) if raw[:fog]
        end
        @cfg = Hashie::Mash.new(raw)

        self.class.after :create do |e, model|
          Mystro::Plugin.run "#{cname}:create", e, model
        end

        self.class.after :destroy do |e, tags|
          Mystro::Plugin.run "#{cname}:destroy", e, tags
        end

        connect
      end

      def connect
        @fog ||= model.new(opt) if opt
      rescue => e
        Mystro::Log.error "#{cname} connect failed: #{e.message} at #{e.backtrace.first}"
      end

      def connected?
        fog.nil? ? false : true
      end

      def find(id)
        fog.send(collection).get(id)
      end

      def all
        fog.send(collection).all
      end

      def create(model)
        Mystro::Log.debug "#{cname}#create #{model.inspect}"
        e = fog.send(collection).create(model.fog_options)
        hooks :create, e, model
        e
      end

      def destroy(models)
        list = [*models].flatten
        list.each do |m|
          Mystro::Log.debug "#{cname}#destroy #{m.rid}"
          e = find(m.rid)
          e.destroy
          hooks :destroy, e, e.respond_to?(:tags) ? e.tags : []
{'fraction_non_alphanumeric': 0.13005308289097592, 'fraction_numerical': 0.0, 'mean_word_length': 1.712624584717608, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 5, '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': '1164619', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1558, 'n_words': 378}