module Motor::Configs::LoadFromCache

Constants

CACHE_HASH

Public Instance Methods

cache_keys_sql() click to toggle source
# File lib/motor/configs/load_from_cache.rb, line 93
def cache_keys_sql
  [
    Motor::Config.select("'configs', MAX(updated_at)").to_sql,
    Motor::Resource.select("'resources', MAX(updated_at)").to_sql,
    Motor::Dashboard.select("'dashboards', MAX(updated_at)").to_sql,
    Motor::Alert.select("'alerts', MAX(updated_at)").to_sql,
    Motor::Query.select("'queries', MAX(updated_at)").to_sql,
    Motor::Form.select("'forms', MAX(updated_at)").to_sql
  ].join(' UNION ')
end
call() click to toggle source
# File lib/motor/configs/load_from_cache.rb, line 10
def call
  cache_keys = load_cache_keys

  {
    configs: load_configs(cache_key: cache_keys[:configs]),
    resources: load_resources(cache_key: cache_keys[:resources]),
    queries: load_queries(cache_key: cache_keys[:queries]),
    dashboards: load_dashboards(cache_key: cache_keys[:dashboards]),
    alerts: load_alerts(cache_key: cache_keys[:alerts]),
    forms: load_forms(cache_key: cache_keys[:forms])
  }
end
load_alerts(cache_key: nil, current_ability: nil) click to toggle source
# File lib/motor/configs/load_from_cache.rb, line 53
def load_alerts(cache_key: nil, current_ability: nil)
  maybe_fetch_from_cache('alerts', cache_key) do
    rel = Motor::Alert.all.active.preload(:tags)
    rel = rel.accessible_by(current_ability) if current_ability

    rel.load
  end
end
load_cache_keys() click to toggle source
# File lib/motor/configs/load_from_cache.rb, line 85
def load_cache_keys
  result = ActiveRecord::Base.connection.execute(cache_keys_sql).to_a

  result = result.map(&:values) if result.first.is_a?(Hash)

  result.to_h.with_indifferent_access
end
load_configs(cache_key: nil) click to toggle source
# File lib/motor/configs/load_from_cache.rb, line 23
def load_configs(cache_key: nil)
  maybe_fetch_from_cache('configs', cache_key) do
    Motor::Config.all.load
  end
end
load_dashboards(cache_key: nil, current_ability: nil) click to toggle source
# File lib/motor/configs/load_from_cache.rb, line 44
def load_dashboards(cache_key: nil, current_ability: nil)
  maybe_fetch_from_cache('dashboards', cache_key) do
    rel = Motor::Dashboard.all.active.preload(:tags)
    rel = rel.accessible_by(current_ability) if current_ability

    rel.load
  end
end
load_forms(cache_key: nil, current_ability: nil) click to toggle source
# File lib/motor/configs/load_from_cache.rb, line 62
def load_forms(cache_key: nil, current_ability: nil)
  maybe_fetch_from_cache('forms', cache_key) do
    rel = Motor::Form.all.active.preload(:tags)
    rel = rel.accessible_by(current_ability) if current_ability

    rel.load
  end
end
load_queries(cache_key: nil, current_ability: nil) click to toggle source
# File lib/motor/configs/load_from_cache.rb, line 35
def load_queries(cache_key: nil, current_ability: nil)
  maybe_fetch_from_cache('queries', cache_key) do
    rel = Motor::Query.all.active.preload(:tags)
    rel = rel.accessible_by(current_ability) if current_ability

    rel.load
  end
end
load_resources(cache_key: nil) click to toggle source
# File lib/motor/configs/load_from_cache.rb, line 29
def load_resources(cache_key: nil)
  maybe_fetch_from_cache('resources', cache_key) do
    Motor::Resource.all.load
  end
end
maybe_fetch_from_cache(type, cache_key) { || ... } click to toggle source
# File lib/motor/configs/load_from_cache.rb, line 71
def maybe_fetch_from_cache(type, cache_key)
  return yield unless cache_key

  if CACHE_HASH[type] && CACHE_HASH[type][:key] == cache_key
    CACHE_HASH[type][:value]
  else
    result = yield

    CACHE_HASH[type] = { key: cache_key, value: result }

    result
  end
end