class Orchestrate::EventType::TimeSlice
Manages events in a specified duration of time.
Attributes
@return [KeyValue] The associated KeyValue
@return [EventType] The associated EventType
@return [String] The associated event type name
Public Class Methods
Instantiates a new TimeSlice
@param type [EventType] The associated EventType
@param bounds [nil, String, Integer, Time, Date, Hash] Boundaries for the Time
If `nil`, no boundaries are set. Used to enumerate over all events. If `String`, `Integer`, `Time`, `Date`, used as `:start` option, below. If `Hash`, see options.
@option bounds [nil, String, Integer, Time, Date] :start Used as the ‘startEvent’ key. @option bounds [nil, String, Integer, Time, Date] :after Used as the ‘afterEvent’ key. @option bounds [nil, String, Integer, Time, Date] :before Used as the ‘beforeEvent’ key. @option bounds [nil, String, Integer, Time, Date] :end Used as the ‘endEvent’ key. @option bounds [Integer] :limit (100) The number of results to return.
# File lib/orchestrate/event_source.rb, line 193 def initialize(type, bounds=nil) @type = type @type_name = type.type @kv_item = type.kv_item if bounds.kind_of?(Hash) @bounds = bounds else @bounds = {} @bounds[:start] = bounds if bounds end @bounds[:limit] ||= 100 end
Public Instance Methods
Creates a new Event
of the given type for the associated KeyValue
. @param body [#to_json] The value for the event. @return [Orchestrate::Event] The event that was created.
# File lib/orchestrate/event_source.rb, line 218 def <<(body) response = perform(:post_event, body, @bounds[:start]) Event.from_bodyless_response(type, body, response) end
Retrieves a single ID by timestamp and ordinal. Uses the timestamp value from the associated bounds, by ‘start`, `before`, `after`, or `end` in that order. @param ordinal [Integer, to_s] The ordinal value for the event to retrieve. @return [Orchestrate::Event]
# File lib/orchestrate/event_source.rb, line 229 def [](ordinal) begin timestamp = @bounds[:start] || @bounds[:before] || @bounds[:after] || @bounds[:end] response = perform(:get_event, timestamp, ordinal) Event.new(type, response) rescue API::NotFound nil end end
Sets the exclusive start boundary for enumeration over events. Overwrites any value given to start
@param bound [Orchestrate::Event, Time, Date, Integer, to_s] The exclusive start of the event range. @return [EventType::TimeSlice]
# File lib/orchestrate/event_source.rb, line 295 def after(bound) @bounds[:after] = extract_bound_from(bound) @bounds.delete(:start) self end
Sets the exclusive end boundary for enumeration over events. Overwrites any value given to end
@param bound [Orchestrate::Event, Time, Date, Integer, to_s] The exclusive end of the event range. @return [EventType::TimeSlice]
# File lib/orchestrate/event_source.rb, line 305 def before(bound) @bounds[:before] = extract_bound_from(bound) @bounds.delete(:end) self end
Iterates over events belonging to the KeyValue
of the specified Type within the given bounds. Used as the basis for enumerable methods. Events are provided in reverse chronological order by timestamp and ordinal value. @overlaod each
@return Enumerator
@overload each(&block)
@yieldparam [Orchestrate::Event] event The Event item
# File lib/orchestrate/event_source.rb, line 249 def each(&block) @response = perform(:list_events, @bounds) return enum_for(:each) unless block_given? raise ResultsNotReady.new if type.kv_item.collection.app.inside_parallel? loop do @response.results.each do |listing| yield Event.from_listing(type, listing, @response) end break unless @response.next_link @response = @response.next_results end @response = nil end
Sets the inclusive end boundary for enumeration over events. Overwrites any value given to before
@param bound [Orchestrate::Event, Time, Date, Integer, to_s] The inclusive end of the event range. @return [EventType::TimeSlice]
# File lib/orchestrate/event_source.rb, line 315 def end(bound) @bounds[:end] = extract_bound_from(bound) @bounds.delete(:before) self end
Creates a Lazy Enumerator for the EventList. If called inside the app’s ‘#in_parallel` block, will prefetch results. @return Enumerator::Lazy
# File lib/orchestrate/event_source.rb, line 266 def lazy return each.lazy if type.kv_item.collection.app.inside_parallel? super end
Calls a method on the associated API
Client
, with collection, key and event_type being provided by EventType
. @param api_method [Symbol] The method to call @param args [#to_s, to_json, Hash] The arguments for the method being called. @return [API::Response]
# File lib/orchestrate/event_source.rb, line 211 def perform(api_method, *args) type.perform(api_method, *args) end
Sets the inclusive start boundary for enumeration over events. Overwrites any value given to before
. @param bound [Orchestrate::Event, Time, Date, Integer, to_s] The inclusive start of the event range. @return [EventType::TimeSlice]
# File lib/orchestrate/event_source.rb, line 285 def start(bound) @bounds[:start] = extract_bound_from(bound) @bounds.delete(:after) self end
Returns the first n items. Equivalent to Enumerable#take. Sets the ‘limit` parameter on the query to Orchestrate
, so we don’t ask for more than is needed. @param count [Integer] The number of events to limit to. @return [Array]
# File lib/orchestrate/event_source.rb, line 275 def take(count) count = 1 if count < 1 @bounds[:limit] = count > 100 ? 100 : count super(count) end
Private Instance Methods
# File lib/orchestrate/event_source.rb, line 322 def extract_bound_from(bound) if bound.kind_of?(Event) "#{bound.timestamp}/#{bound.ordinal}" else bound end end