Class: Dasht::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dasht/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



17
18
19
20
21
22
23
# File 'lib/dasht/base.rb', line 17

def initialize
  @boards      = {}
  @log_threads = {}
  @metrics     = Metrics.new(self)
  @reloader    = Reloader.new(self)
  @rack_app    = RackApp.new(self)
end

Instance Attribute Details

#backgroundObject

Returns the value of attribute background.



10
11
12
# File 'lib/dasht/base.rb', line 10

def background
  @background
end

#boardsObject

Returns the value of attribute boards.



6
7
8
# File 'lib/dasht/base.rb', line 6

def boards
  @boards
end

#default_heightObject

Returns the value of attribute default_height.



14
15
16
# File 'lib/dasht/base.rb', line 14

def default_height
  @default_height
end

#default_refreshObject

Returns the value of attribute default_refresh.



12
13
14
# File 'lib/dasht/base.rb', line 12

def default_refresh
  @default_refresh
end

#default_resolutionObject

Returns the value of attribute default_resolution.



11
12
13
# File 'lib/dasht/base.rb', line 11

def default_resolution
  @default_resolution
end

#default_widthObject

Returns the value of attribute default_width.



13
14
15
# File 'lib/dasht/base.rb', line 13

def default_width
  @default_width
end

#historyObject

Returns the value of attribute history.



15
16
17
# File 'lib/dasht/base.rb', line 15

def history
  @history
end

#metricsObject

Returns the value of attribute metrics.



3
4
5
# File 'lib/dasht/base.rb', line 3

def metrics
  @metrics
end

#portObject

Settings.



9
10
11
# File 'lib/dasht/base.rb', line 9

def port
  @port
end

#rack_appObject

Returns the value of attribute rack_app.



4
5
6
# File 'lib/dasht/base.rb', line 4

def rack_app
  @rack_app
end

#reloaderObject

Returns the value of attribute reloader.



5
6
7
# File 'lib/dasht/base.rb', line 5

def reloader
  @reloader
end

Instance Method Details

#board(name = "default") {|board| ... } ⇒ Object

Yields:



74
75
76
77
78
79
# File 'lib/dasht/base.rb', line 74

def board(name = "default", &block)
  name = name.to_s
  board = @boards[name] = Board.new(self, name)
  yield(board) if block
  board
end

#interval(metric, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dasht/base.rb', line 46

def interval(metric, &block)
  Thread.new do
    begin
      while true
        value = block.call
        metrics.set(metric, value, :last, Time.now.to_i) if value
      end
    rescue => e
      log e
      raise e
    end
  end
end

#log(s) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/dasht/base.rb', line 25

def log(s)
  if s.class < Exception
    print "\n#{s}\n"
    print s.backtrace.join("\n")
  else
    print "\r#{s}\n"
  end
end

#reload(&block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dasht/base.rb', line 102

def reload(&block)
  @boards = {}
  @log_threads.values.map(&:terminate)
  @log_threads = {}

  begin
    block.call(self)
  rescue => e
    log e
    raise e
  end

  @log_threads.values.map(&:run)
end

#run(&block) ⇒ Object

RUN & RELOAD ###



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dasht/base.rb', line 83

def run(&block)
  if @already_running
    begin
      reload(&block)
    rescue => e
      log e
    end
    return
  end

  @already_running = true
  @reloader.run

  block.call(self)

  @log_threads.values.map(&:run)
  @rack_app.run(port)
end

#start(command) {|log_thread| ... } ⇒ Object

DATA INGESTION ###

Yields:

  • (log_thread)


36
37
38
39
40
# File 'lib/dasht/base.rb', line 36

def start(command, &block)
  log_thread = @log_threads[command] = LogThread.new(self, command)
  yield(log_thread) if block
  log_thread
end

#system_plugins_pathObject



66
67
68
# File 'lib/dasht/base.rb', line 66

def system_plugins_path
  File.join(File.dirname(__FILE__), "..", "..", "assets", "plugins")
end

#tail(path) ⇒ Object



42
43
44
# File 'lib/dasht/base.rb', line 42

def tail(path)
  start("tail -F -n 0 \"#{path}\"")
end

#user_plugins_pathObject



70
71
72
# File 'lib/dasht/base.rb', line 70

def user_plugins_path
  File.join(File.dirname($PROGRAM_NAME), "plugins")
end

#views_pathObject

DASHBOARD ###



62
63
64
# File 'lib/dasht/base.rb', line 62

def views_path
  File.join(File.dirname(__FILE__), "..", "..", "views")
end