class Btrack::Query

Attributes

criteria[R]

Public Class Methods

method_missing(method, *args, &block) click to toggle source
# File lib/btrack/query.rb, line 39
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
new(criteria = nil) click to toggle source
# File lib/btrack/query.rb, line 14
def initialize(criteria = nil)
  @criteria = criteria.freeze
end

Public Instance Methods

count() click to toggle source
# File lib/btrack/query.rb, line 18
def count
  with_silent { with_sha { [lua(:count), *@criteria.realize!] } }
end
exists?(id = nil) click to toggle source
# File lib/btrack/query.rb, line 22
def exists?(id = nil)
  c = id ? @criteria.where([], id: id) : @criteria
  with_silent { with_sha { [lua(:exists), *c.realize!] }  == 1 }
end
lua(f) click to toggle source
# File lib/btrack/query/lua.rb, line 3
def lua(f)
  %Q{
    local index = 1

    for i, count in ipairs(ARGV) do
      if count == '0' then
        break
      end

      local bitop = {}

      for c = index, index * count do
        table.insert(bitop, KEYS[c])
        index = index + 1
      end

      if i == 1 then
        redis.call('bitop', 'or', 'tmp', unpack(bitop))
      else
        redis.call('bitop', 'or', 'tmp:or', unpack(bitop))
        redis.call('bitop', 'and', 'tmp', 'tmp', 'tmp:or')
        redis.call('del', 'tmp:or')
      end
    end

    #{send('lua_' + f.to_s)}

    redis.call('del', 'tmp')
    return results
  }          
end
lua_count() click to toggle source
# File lib/btrack/query/lua.rb, line 35
def lua_count
  "local results = redis.call('bitcount', 'tmp')"
end
lua_exists() click to toggle source
# File lib/btrack/query/lua.rb, line 39
def lua_exists
  "local results = redis.call('getbit', 'tmp', KEYS[#KEYS])"
end
plot() click to toggle source
# File lib/btrack/query.rb, line 27
def plot
  JSON.parse(with_silent { with_sha { [plot_lua, *@criteria.realize!] } }).inject({}) do |n, (k, v)|
    g = @criteria.criteria.first[:granularity] || Criteria.default_granularity
    key = Time.strptime(k.rpartition(":").last, Helper.format(g))
    n[key] = v
    n
  end
rescue
  nil
end
plot_lua() click to toggle source

lua script for plotting please note - it cannot be used with the prefixed 'lua' like count and exists this is a standalone script ment for plotting and allowing for cohort analysis all series must be of the same size

# File lib/btrack/query/lua.rb, line 47
def plot_lua
  %Q{
    local series_count = #ARGV
    local length = ARGV[1]

    -- run over the first series
    -- all series must be of the same size
    local plot = {}
    for i = 1, length do
      local bitop = {}
      for j = 1, series_count do
        table.insert(bitop, KEYS[i*j])
      end

      -- make sure 'tmp' is populated with the first key (so the 'and' op would work as expected)
      redis.call('bitop', 'or', 'tmp', 'tmp', bitop[1])

      redis.call('bitop', 'and', 'tmp', unpack(bitop))
      -- table.insert(plot, redis.call('bitcount', 'tmp'))
      plot[KEYS[i]] = redis.call('bitcount', 'tmp')
      redis.call('del', 'tmp')
    end

    return cjson.encode(plot)
  }          
end