<commit_before>require 'mongrel'
require 'gem_plugin'

class Mongrel::Uploads
  include Singleton

  def initialize
    @guard = Mutex.new
    @counters = {}
  end

  def check(upid)
    @counters[upid]
  end

  def add(upid, size)
    stats = {'size' => size, 'received' => 0}
    @guard.synchronize do
      @counters[upid] = stats
    end
  end

  def mark(upid, len)
    upload = @counters[upid]
    recvd = upload['size'] - len
    @guard.synchronize do
      upload['received'] = recvd
    end
  end

  def finish(upid)
    upload = @counters[upid]
    recvd = upload['size']
    @guard.synchronize do
      upload['received'] = recvd
    end
  end
end

class Upload < GemPlugin::Plugin "/handlers"
  include Mongrel::HttpHandlerPlugin

  def initialize(options = {})
    @path_info      = options[:path_info]
    @request_notify = true
  end

  def request_begins(params)
    upload_notify(:add, params, params[Mongrel::Const::CONTENT_LENGTH].to_i) if params['PATH_INFO'] == @path_info
  end

  def request_progress(params, clen, total)
    upload_notify(:mark, params, clen) if params['PATH_INFO'] == @path_info
  end

  def process(request, response)
    upload_notify(:finish, request.params) if request.params['PATH_INFO'] == @path_info
  end

  private
  def upload_notify(action, params, *args)
    upload_id = Mongrel::HttpRequest.query_parse(params['QUERY_STRING'])['upload_id']
    if params[Mongrel::Const::REQUEST_METHOD] == 'POST' && upload_id
      Mongrel::Uploads.instance.send(action, upload_id, *args) if upload_id
    end
  end
end<commit_msg>changes to upload progress: use instance vars to keep from locking the main hash.  update the progress only every second.<commit_after>require 'mongrel'
require 'gem_plugin'

class Mongrel::Uploads
  include Singleton

  def initialize
    @guard    = Mutex.new
    @counters = {}
  end

  def check(upid)
    status = @counters[upid]
    puts "#{upid}: Checking" if @debug
    instance_variable_get(upload_var(status)) if status
  end
  
  def last_checked(upid)
    status = @counters[upid]
    instance_variable_get(checked_var(status)) if status
  end

  def add(upid, size)
    @guard.synchronize do
      @counters[upid] = rand(Time.now.to_i).to_s.intern
      instance_variable_set(upload_var(@counters[upid]), {:size => size, :received => 0})
      puts "#{upid}: Added" if @debug
    end
  end

  def mark(upid, len)
    last_checked_time = last_checked(upid)
    if last_checked_time.nil? || Time.now-last_checked_time > 3
      status = check(upid)
      status[:received] = status[:size] - len
      instance_variable_set(checked_var(@counters[upid]), Time.now)
      puts "#{upid}: #{status[:received]}" if @debug
    end
  end

  def finish(upid)
    @guard.synchronize do
      puts "#{upid}: Finished" if @debug
      counter = @counters.delete(upid)
      return unless counter
      instance_variable_set(upload_var(counter),  nil)
      instance_variable_set(checked_var(counter), nil)
    end
    true
  end
  
  private
    def upload_var(key)
      "@upload_#{key}"
    end
    
    def checked_var(key)
      "@checked_#{key}"
    end
end

class Upload < GemPlugin::Plugin "/handlers"
  include Mongrel::HttpHandlerPlugin

  def initialize(options = {})
    @path_info      = options[:path_info]
    @request_notify = true
    Mongrel::Uploads.instance.instance_variable_set(:@debug, true) if options[:debug]
  end

  def request_begins(params)
    upload_notify(:add, params, params[Mongrel::Const::CONTENT_LENGTH].to_i)
  end

  def request_progress(params, clen, total)
    upload_notify(:mark, params, clen)
  end

  def process(request, response)
    upload_notify(:finish, request.params)
  end

  private
    def upload_notify(action, params, *args)
      return unless params['PATH_INFO'] == @path_info && params[Mongrel::Const::REQUEST_METHOD] == 'POST'
      upload_id = Mongrel::HttpRequest.query_parse(params['QUERY_STRING'])['upload_id']
      Mongrel::Uploads.instance.send(action, upload_id, *args) if upload_id
    end
end
{'fraction_non_alphanumeric': 0.11814345991561181, 'fraction_numerical': 0.0007446016381236039, 'mean_word_length': 3.724501758499414, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '3835777', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1317, 'n_words': 303}