<commit_before>require 'btrack/query/criteria'
require 'btrack/query/lua'

module Btrack
  class Query
    attr_reader :criteria

    delegate :with_sha, :with_silent, to: Btrack::Redis

    class << self
      delegate :where, to: Criteria
    end

    def initialize(criteria = nil)
      @criteria = criteria.freeze
    end

    def count
      with_silent { with_sha { [lua(:count), *@criteria.realize!] } }
    end

    def exists?(id = nil)
      c = id ? @criteria.where([], id: id) : @criteria
      with_silent { with_sha { [lua(:exists), *c.realize!] }  == 1 }
    end

    def plot
      JSON.parse with_silent { with_sha { [plot_lua, *@criteria.realize!] } }
    rescue
      nil
    end

    class << self
      def method_missing(method, *args, &block)
        return unless method.to_s.end_with? '?'
        Criteria.where({method.to_s.chomp('?') => args[1]}.merge(args.extract_options!), id: args[0]).exists?
      end
    end
  end
end<commit_msg>changed plot to return the datetime part only<commit_after>require 'btrack/query/criteria'
require 'btrack/query/lua'

module Btrack
  class Query
    attr_reader :criteria

    delegate :with_sha, :with_silent, to: Btrack::Redis

    class << self
      delegate :where, to: Criteria
    end

    def initialize(criteria = nil)
      @criteria = criteria.freeze
    end

    def count
      with_silent { with_sha { [lua(:count), *@criteria.realize!] } }
    end

    def exists?(id = nil)
      c = id ? @criteria.where([], id: id) : @criteria
      with_silent { with_sha { [lua(:exists), *c.realize!] }  == 1 }
    end

    def plot
      JSON.parse(with_silent { with_sha { [plot_lua, *@criteria.realize!] } }).inject({}) do |n, (k, v)|
        n[k.rpartition(":").last] = v
        n
      end
    rescue
      nil
    end

    class << self
      def method_missing(method, *args, &block)
        return unless method.to_s.end_with? '?'
        Criteria.where({method.to_s.chomp('?') => args[1]}.merge(args.extract_options!), id: args[0]).exists?
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.13643031784841075, 'fraction_numerical': 0.00293398533007335, 'mean_word_length': 2.6470588235294117, 'pattern_counts': {'":': 1, '<': 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': '3350414', 'n_tokens_mistral': 720, 'n_tokens_neox': 705, 'n_words': 168}