Class: Redistat::Event

Inherits:
Object
  • Object
show all
Includes:
Database, Options
Defined in:
lib/redistat/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Options

included, #options, #parse_options, #raw_options

Methods included from Database

#db, included

Constructor Details

#initialize(scope, label = nil, date = nil, stats = {}, opts = {}, meta = {}, is_new = true) ⇒ Event

Returns a new instance of Event.



20
21
22
23
24
25
26
# File 'lib/redistat/event.rb', line 20

def initialize(scope, label = nil, date = nil, stats = {}, opts = {}, meta = {}, is_new = true)
  parse_options(opts)
  @key = Key.new(scope, label, date, @options)
  @stats = stats ||= {}
  @meta = meta ||= {}
  @new = is_new
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/redistat/event.rb', line 6

def id
  @id
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/redistat/event.rb', line 7

def key
  @key
end

#metaObject

Returns the value of attribute meta.



10
11
12
# File 'lib/redistat/event.rb', line 10

def meta
  @meta
end

#statsObject

Returns the value of attribute stats.



9
10
11
# File 'lib/redistat/event.rb', line 9

def stats
  @stats
end

Class Method Details

.create(*args) ⇒ Object



86
87
88
# File 'lib/redistat/event.rb', line 86

def self.create(*args)
  self.new(*args).save
end

.find(scope, id) ⇒ Object



90
91
92
93
94
95
# File 'lib/redistat/event.rb', line 90

def self.find(scope, id)
  event = db.hgetall "#{scope}#{KEY_EVENT}#{id}"
  return nil if event.size == 0
  self.new( event["scope"], event["label"], event["date"], JSON.parse(event["stats"]),
            JSON.parse(event["options"]), JSON.parse(event["meta"]), false )
end

Instance Method Details

#dateObject



32
33
34
# File 'lib/redistat/event.rb', line 32

def date
  @key.date
end

#date=(input) ⇒ Object



36
37
38
# File 'lib/redistat/event.rb', line 36

def date=(input)
  @key.date = input
end

#default_optionsObject



12
13
14
15
16
17
18
# File 'lib/redistat/event.rb', line 12

def default_options
  { :depth => :hour,
    :store_event => false,
    :connection_ref => nil,
    :enable_grouping => true,
    :label_indexing => true }
end

#depth_limitObject



82
83
84
# File 'lib/redistat/event.rb', line 82

def depth_limit
  @options[:depth] ||= @key.depth
end

#labelObject



48
49
50
# File 'lib/redistat/event.rb', line 48

def label
  @key.label
end

#label=(input) ⇒ Object



56
57
58
# File 'lib/redistat/event.rb', line 56

def label=(input)
  @key.label = input
end

#label_hashObject



52
53
54
# File 'lib/redistat/event.rb', line 52

def label_hash
  @key.label_hash
end

#new?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/redistat/event.rb', line 28

def new?
  @new
end

#next_idObject



60
61
62
# File 'lib/redistat/event.rb', line 60

def next_id
  db.incr("#{self.scope}#{KEY_NEXT_ID}")
end

#saveObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/redistat/event.rb', line 64

def save
  return false if !self.new?
  Summary.update_all(@key, @stats, depth_limit, @options)
  if @options[:store_event]
    @id = self.next_id
    db.hmset("#{self.scope}#{KEY_EVENT}#{@id}",
             "scope", self.scope,
             "label", self.label,
             "date", self.date.to_time.to_s,
             "stats", self.stats.to_json,
             "meta", self.meta.to_json,
             "options", self.options.to_json)
    db.sadd("#{self.scope}#{KEY_EVENT_IDS}", @id)
  end
  @new = false
  self
end

#scopeObject



40
41
42
# File 'lib/redistat/event.rb', line 40

def scope
  @key.scope
end

#scope=(input) ⇒ Object



44
45
46
# File 'lib/redistat/event.rb', line 44

def scope=(input)
  @key.scope = input
end