<commit_before>require "rubygems"
require "bundler/setup"
require 'pusher-client'
require 'net/http'
require 'fileutils'
require './parse_options'


$log = Logger.new($options[:logfile])

$log.debug "LAUNCHING AN INSTANCE OF THE KITE REQUEST HANDLER"

$log.debug $options.inspect

class KiteRequestHandler
  def initialize(config)
    @config = config
    @socket = PusherClient::Socket.new(config[:key], config)
    krh_channel_name = "private-krh-#{$options[:kite_name]}"
    $log.debug "ADDING join/part HANDLERS: #{krh_channel_name}"
    add_listener(krh_channel_name, 'join')
    add_listener(krh_channel_name, 'part')
    add_listener(krh_channel_name, 'status')
    add_listener(krh_channel_name, 'fail')
    @channels = {}
    join_all()
  end
  
  def join_all()
    $log.debug 'Joining all previously joined channels.'
    channels = {}
    begin
      dir = Dir.new(@config[:joined_channels_path])
    rescue Errno::ENOENT
      FileUtils.mkdir_p(@config[:joined_channels_path])
      retry
    end
    $log.debug dir.inspect
    dir.each do |channel|
      if channel !~ /^(\.\.?)$/ && channels[channel].nil?
        $log.debug "Inspecting channel: #{channel}"
        $log.debug "This channel was not previously joined: #{channel}"
        join({'channel' => channel}, true)
        channels[channel] = true
      end
    end
  end
  
  def add_to_joined_channels_path(channel)
    $log.debug "[JOIN] Remembering that we are joined to a channel: #{channel}"
    FileUtils.touch(@config[:joined_channels_path]+channel)
  end
  
  def remove_from_joined_channels_path(channel)
    unless channel.nil?
      $log.debug "[PART] Forgetting about a channel that we were previously joined to: #{channel}"
      begin
        File.unlink(@config[:joined_channels_path]+channel)
      rescue Errno::ENOENT
        $log.warn "[WARN] could not remove tmp file for #{channel}"
      end
    end
  end
  
  def status(msg)
    $log.debug 'BEGINNING STATUS DUMP'
    $log.debug "REQUEST-HANDLER LEVEL RECORD: \n\t"+@channels.keys.join("\n\t")
    $log.debug "SOCKET LEVEL RECORD: \n\t"+@socket.channels.channels.keys.join("\n\t")
    $log.debug 'END STATUS DUMP'
  end
  
  def fail(msg)
    $log.warn 'Intentionally failing...'
    $log.debug "Intentionally failing means unsubscribing from the event on the socket without cleaning up the local state."
    @channels.each do |channel|
      @socket.unsubscribe(channel)
    end
  end

  def join(channel, silent=false)
    $log.debug "JOINING TO CHANNEL: #{channel['channel']}"
    $log.debug @channels.inspect
    $log.debug channel['channel']
    begin
      $log.debug @channels.has_key? channel['channel']
    rescue => err
      $log.error err
    end
    unless @channels.has_key? channel['channel']
      $log.debug "joining channel #{channel['channel']}"
      @channels[channel['channel']] = true
      add_listener(channel['channel'], 'client-message')
    end
    add_to_joined_channels_path(channel['channel']) unless silent
  end

  def part(channel, silent=false)
    $log.debug "PARTING FROM CHANNEL: #{channel['channel']}"
    @socket.unsubscribe(channel['channel'])
    @channels.delete channel['channel']
    remove_from_joined_channels_path(channel['channel']) unless silent
  end
  
  def add_listener(channel, event)
    @socket.subscribe(channel, 'khr')<commit_msg>iterate over the keys<commit_after>require "rubygems"
require "bundler/setup"
require 'pusher-client'
require 'net/http'
require 'fileutils'
require './parse_options'


$log = Logger.new($options[:logfile])

$log.debug "LAUNCHING AN INSTANCE OF THE KITE REQUEST HANDLER"

$log.debug $options.inspect

class KiteRequestHandler
  def initialize(config)
    @config = config
    @socket = PusherClient::Socket.new(config[:key], config)
    krh_channel_name = "private-krh-#{$options[:kite_name]}"
    $log.debug "ADDING join/part HANDLERS: #{krh_channel_name}"
    add_listener(krh_channel_name, 'join')
    add_listener(krh_channel_name, 'part')
    add_listener(krh_channel_name, 'status')
    add_listener(krh_channel_name, 'fail')
    @channels = {}
    join_all()
  end
  
  def join_all()
    $log.debug 'Joining all previously joined channels.'
    channels = {}
    begin
      dir = Dir.new(@config[:joined_channels_path])
    rescue Errno::ENOENT
      FileUtils.mkdir_p(@config[:joined_channels_path])
      retry
    end
    $log.debug dir.inspect
    dir.each do |channel|
      if channel !~ /^(\.\.?)$/ && channels[channel].nil?
        $log.debug "Inspecting channel: #{channel}"
        $log.debug "This channel was not previously joined: #{channel}"
        join({'channel' => channel}, true)
        channels[channel] = true
      end
    end
  end
  
  def add_to_joined_channels_path(channel)
    $log.debug "[JOIN] Remembering that we are joined to a channel: #{channel}"
    FileUtils.touch(@config[:joined_channels_path]+channel)
  end
  
  def remove_from_joined_channels_path(channel)
    unless channel.nil?
      $log.debug "[PART] Forgetting about a channel that we were previously joined to: #{channel}"
      begin
        File.unlink(@config[:joined_channels_path]+channel)
      rescue Errno::ENOENT
        $log.warn "[WARN] could not remove tmp file for #{channel}"
      end
    end
  end
  
  def status(msg)
    $log.debug 'BEGINNING STATUS DUMP'
    $log.debug "REQUEST-HANDLER LEVEL RECORD: \n\t"+@channels.keys.join("\n\t")
    $log.debug "SOCKET LEVEL RECORD: \n\t"+@socket.channels.channels.keys.join("\n\t")
    $log.debug 'END STATUS DUMP'
  end
  
  def fail(msg)
    $log.warn 'Intentionally failing...'
    $log.debug "Intentionally failing means unsubscribing from the event on the socket without cleaning up the local state."
    @channels.keys.each do |channel|
      @socket.unsubscribe(channel)
    end
  end

  def join(channel, silent=false)
    $log.debug "JOINING TO CHANNEL: #{channel['channel']}"
    $log.debug @channels.inspect
    $log.debug channel['channel']
    begin
      $log.debug @channels.has_key? channel['channel']
    rescue => err
      $log.error err
    end
    unless @channels.has_key? channel['channel']
      $log.debug "joining channel #{channel['channel']}"
      @channels[channel['channel']] = true
      add_listener(channel['channel'], 'client-message')
    end
    add_to_joined_channels_path(channel['channel']) unless silent
  end

  def part(channel, silent=false)
    $log.debug "PARTING FROM CHANNEL: #{channel['channel']}"
    @socket.unsubscribe(channel['channel'])
    @channels.delete channel['channel']
    remove_from_joined_channels_path(channel['channel']) unless silent
  end
  
  def add_listener(channel, event)
    @socket.subscribe(channel, 'khr')
{'fraction_non_alphanumeric': 0.1281591147001645, 'fraction_numerical': 0.0, 'mean_word_length': 3.6541405706332637, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '969750', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2151, 'n_words': 504}