Class: Sleek::Queries::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/sleek/queries/query.rb

Overview

Public: The query.

Queries are performed on a set of events and usually return numeric values. You shouldn’t be using Sleek::Queries::Query directly, instead, you should subclass it and define #perform on it, which takes an events criteria and does its job.

Sleek::Queries::Query would take care of processing options, filtering events, handling series and groups.

Examples

class SomeQuery < Query
  def perform(events)
    ...
  end
end

Direct Known Subclasses

Average, Count, CountUnique, Maximum, Minimum, Sum

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, bucket, options = {}) ⇒ Query

Internal: Initialize the query.

namespace - the Sleek::Namespace object. bucket - the String bucket name. options - the optional Hash of options.

:timeframe - the optional timeframe description.
:interval  - the optional interval description.

Raises ArgumentError if passed options are invalid.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
# File 'lib/sleek/queries/query.rb', line 34

def initialize(namespace, bucket, options = {})
  @namespace = namespace
  @bucket = bucket
  @options = options
  @timeframe = options[:timeframe]

  raise ArgumentError, 'options are invalid' unless valid_options?
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



21
22
23
# File 'lib/sleek/queries/query.rb', line 21

def bucket
  @bucket
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



21
22
23
# File 'lib/sleek/queries/query.rb', line 21

def namespace
  @namespace
end

#optionsObject (readonly)

Returns the value of attribute options.



21
22
23
# File 'lib/sleek/queries/query.rb', line 21

def options
  @options
end

#timeframeObject (readonly)

Returns the value of attribute timeframe.



21
22
23
# File 'lib/sleek/queries/query.rb', line 21

def timeframe
  @timeframe
end

Class Method Details

.require_target_property!Object

Public: Indicate that the query requires target property.

Examples

class SomeQuery < Query
  require_target_property!

  def perform(events)
    ...
  end
end


127
128
129
# File 'lib/sleek/queries/query.rb', line 127

def require_target_property!
  @require_target_property = true
end

.require_target_property?Boolean

Public: Check if the query requires target property.

Returns:

  • (Boolean)


132
133
134
# File 'lib/sleek/queries/query.rb', line 132

def require_target_property?
  !!@require_target_property
end

Instance Method Details

#apply_filters(criteria) ⇒ Object

Internal: Apply all the filters to the criteria.



59
60
61
# File 'lib/sleek/queries/query.rb', line 59

def apply_filters(criteria)
  filters.reduce(criteria) { |crit, filter| filter.apply(crit) }
end

#eventsObject

Internal: Get Mongoid::Criteria for events to perform the query.

time_range - the optional range of Time objects.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sleek/queries/query.rb', line 46

def events
  evts = namespace.events(bucket)
  evts = evts.between('s.t' => timeframe) if timeframe?
  evts = apply_filters(evts) if filter?

  if group_by.present?
    evts = Sleek::GroupByCriteria.new(evts, "d.#{group_by}")
  end

  evts
end

#filter?Boolean

Internal: Check if options include filter.

Returns:

  • (Boolean)


77
78
79
# File 'lib/sleek/queries/query.rb', line 77

def filter?
  options[:filter].present?
end

#filtersObject

Internal: Get filters.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sleek/queries/query.rb', line 64

def filters
  filters = options[:filter]

  if filters.is_a?(Array) && filters.size == 3 && filters.none? { |f| f.is_a?(Array) }
    filters = [filters]
  elsif !filters.is_a?(Array) || !filters.all? { |f| f.is_a?(Array) && f.size == 3 }
    raise ArgumentError, "wrong filter - #{filters}"
  end

  filters.map { |f| Sleek::Filter.new(*f) }
end

#group_byObject

Internal: Get group_by property.



87
88
89
# File 'lib/sleek/queries/query.rb', line 87

def group_by
  options[:group_by]
end

#perform(events) ⇒ Object

Internal: Perform the query on a set of events.

Raises:

  • (NotImplementedError)


104
105
106
# File 'lib/sleek/queries/query.rb', line 104

def perform(events)
  raise NotImplementedError
end

#runObject

Internal: Run the query.



99
100
101
# File 'lib/sleek/queries/query.rb', line 99

def run
  perform(events)
end

#target_propertyObject

Internal: Get the target property.



92
93
94
95
96
# File 'lib/sleek/queries/query.rb', line 92

def target_property
  if options[:target_property].present?
    "d.#{options[:target_property]}"
  end
end

#timeframe?Boolean

Internal: Check if options include timeframe.

Returns:

  • (Boolean)


82
83
84
# File 'lib/sleek/queries/query.rb', line 82

def timeframe?
  timeframe.present?
end

#valid_options?Boolean

Internal: Validate the options.

Returns:

  • (Boolean)


109
110
111
112
113
# File 'lib/sleek/queries/query.rb', line 109

def valid_options?
  options.is_a?(Hash) &&
    (filter? ? options[:filter].is_a?(Array) : true) &&
    (require_target_property? ? options[:target_property].present? : true)
end