<commit_before>module Rackdis
  class API < Grape::API
      
    rescue_from :all do |e|
      error = process_error(e)
      Rack::Response.new({ success: false, error: error.message }.to_json, error.code)
    end

    version 'v1'
    format :json

    helpers do
      include Rack::Stream::DSL
      
      def redis
        @redis ||= RedisFacade.new(Rackdis.redis_client, Rackdis.config, Rackdis.logger)
      end
      
      def do_subscribe
        after_open do
          Rackdis.logger.debug "Someone subscribed to #{params[:channel]}"
          redis.redis.subscribe params[:channel] do |on|
            on.message do |channel, msg|
              Rackdis.logger.debug "Pushing: #{msg}"
              chunk msg
            end
          end
        end
        
        status 200
        header 'Content-Type', 'application/json'
        ""
      end
      
      def args
        params[:args].split("/")
      end

      def process_error(e)
        Rackdis.logger.e("#{e.class.name}: #{e.message}")
        Rackdis.log_backtrace(e)

        error = {
          code: 500,
          message: e.message<commit_msg>Added an EM timer to ping the client with an empty chunk every 10secs<commit_after>module Rackdis
  class API < Grape::API
      
    rescue_from :all do |e|
      error = process_error(e)
      Rack::Response.new({ success: false, error: error.message }.to_json, error.code)
    end

    version 'v1'
    format :json

    helpers do
      include Rack::Stream::DSL
      
      def redis
        @redis ||= RedisFacade.new(Rackdis.redis_client, Rackdis.config, Rackdis.logger)
      end
      
      def do_subscribe
        after_open do
          @timer = EM.add_periodic_timer(10) { chunk "" }
          Rackdis.logger.debug "Someone subscribed to #{params[:channel]}"
          redis.redis.subscribe params[:channel] do |on|
            on.message do |channel, msg|
              Rackdis.logger.debug "Pushing: #{msg}"
              chunk msg
            end
          end
        end
        
        before_close do
          @timer.cancel
        end

        status 200
        header 'Content-Type', 'application/json'
        ""
      end
      
      def args
        params[:args].split("/")
      end

      def process_error(e)
        Rackdis.logger.e("#{e.class.name}: #{e.message}")
        Rackdis.log_backtrace(e)

        error = {
          code: 500,
          message: e.message
{'fraction_non_alphanumeric': 0.10242698477992596, 'fraction_numerical': 0.007404360345536816, 'mean_word_length': 1.720357941834452, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '3992463', 'n_tokens_mistral': 766, 'n_tokens_neox': 739, 'n_words': 177}