Class: Neo4jrb::TimeTree

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/outputs/timetree/timetree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ TimeTree

Returns a new instance of TimeTree.



9
10
11
12
# File 'lib/logstash/outputs/timetree/timetree.rb', line 9

def initialize(session)
  @session = session
  @root    = ::TimeTree::Root.first || create_node(::TimeTree::Root)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



7
8
9
# File 'lib/logstash/outputs/timetree/timetree.rb', line 7

def root
  @root
end

Instance Method Details

#add_event(ts, event) ⇒ Object



14
15
16
17
18
19
# File 'lib/logstash/outputs/timetree/timetree.rb', line 14

def add_event(ts, event)
  Neo4j::Transaction.run do
    start = refresh_tree(ts)
    append(start, event)
  end
end

#append(start, event) ⇒ Object



44
45
46
47
# File 'lib/logstash/outputs/timetree/timetree.rb', line 44

def append(start, event)
  event = create_node(::TimeTree::Event, {:message => event, :created_at => Time.now})
  ::TimeTree::Child.create(:from_node => start, :to_node => event)
end

#append_element(root, clazz, value) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/logstash/outputs/timetree/timetree.rb', line 49

def append_element(root, clazz, value)
  childs = has_child(root)
  if !childs.empty?
    selected = childs.select { |e| e.props[:value] == value }
    return selected.first if !selected.empty?
  end
  node = find_or_create(clazz, root, {:value => value})
  set_edge_to_first_node(root, node) if childs.empty?
  ::TimeTree::Child.create(:from_node => root, :to_node => node)
  set_edge_to_last_node(root, node)
  node
end

#create_node(clazz, props = {}) ⇒ Object



83
84
85
# File 'lib/logstash/outputs/timetree/timetree.rb', line 83

def create_node(clazz, props={})
  clazz.create(props)
end

#events_at(start_time) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/logstash/outputs/timetree/timetree.rb', line 29

def events_at(start_time)
  query = <<-QUERY
        MATCH (a:`TimeTree::Year`{value:#{start_time.year}})-[:`child`]->(b:`TimeTree::Month`{value:#{start_time.month}})-[:`child`]->(d:`TimeTree::Day`{value:#{start_time.day}})
        WITH d MATCH (e:`TimeTree::Event`)<-[:`child`]-(d)
        RETURN e
  QUERY
  run_query(query)
end

#events_between(start_time, end_time) ⇒ Object

Raises:

  • (Exception)


21
22
23
# File 'lib/logstash/outputs/timetree/timetree.rb', line 21

def events_between(start_time, end_time)
  raise Exception.new("Not implemented yet!")
end

#events_from(start_time) ⇒ Object

Raises:

  • (Exception)


25
26
27
# File 'lib/logstash/outputs/timetree/timetree.rb', line 25

def events_from(start_time)
  raise Exception.new("Not implemented yet!")
end

#find_or_create(clazz, root, criteria) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/logstash/outputs/timetree/timetree.rb', line 62

def find_or_create(clazz, root, criteria)
  nodes = clazz.where(criteria).map do |n|
     n.nodes(:dir => :incoming, :type => :child).select do |p|
       p.class == root.class && (!root.is_a?(::TimeTree::Root) && p.props[:value] == root.props[:value])
     end
  end.flatten
  ( nodes.empty? ? clazz.create(criteria) : nodes.first )
end

#has(base, dir) ⇒ Object



95
96
97
# File 'lib/logstash/outputs/timetree/timetree.rb', line 95

def has(base, dir)
  base.nodes(:dir => dir, :type => :child)
end

#has_child(base) ⇒ Object



87
88
89
# File 'lib/logstash/outputs/timetree/timetree.rb', line 87

def has_child(base)
  has(base, :outgoing)
end

#has_parent(base, value) ⇒ Object



91
92
93
# File 'lib/logstash/outputs/timetree/timetree.rb', line 91

def has_parent(base, value)
  has(base, :incoming)
end

#refresh_tree(ts) ⇒ Object



38
39
40
41
42
# File 'lib/logstash/outputs/timetree/timetree.rb', line 38

def refresh_tree(ts)
  year  = append_element(@root,  ::TimeTree::Year, ts.year)
  month = append_element(year,  ::TimeTree::Month, ts.month)
  append_element(month, ::TimeTree::Day, ts.day)
end

#set_edge_to_first_node(root, node) ⇒ Object



71
72
73
# File 'lib/logstash/outputs/timetree/timetree.rb', line 71

def set_edge_to_first_node(root, node)
  ::TimeTree::First.create(:from_node => root, :to_node => node)
end

#set_edge_to_last_node(root, node) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/logstash/outputs/timetree/timetree.rb', line 75

def set_edge_to_last_node(root, node)
  Neo4j::Transaction.run do
    ::TimeTree::Last.create(:from_node => root, :to_node => node)
  end
  rels = root.rel(:dir => :outgoing, :type => :last)
  rels.del
end