Class: StripChart::App

Inherits:
Object
  • Object
show all
Defined in:
lib/stripchart/app.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ App

Returns a new instance of App.



21
22
23
24
# File 'lib/stripchart/app.rb', line 21

def initialize(argv)
  @command = argv
  @start_time = Time.now
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



26
27
28
# File 'lib/stripchart/app.rb', line 26

def command
  @command
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



26
27
28
# File 'lib/stripchart/app.rb', line 26

def start_time
  @start_time
end

Class Method Details

.run!(argv) ⇒ Object



17
18
19
# File 'lib/stripchart/app.rb', line 17

def self.run!(argv)
  new(argv).run!
end

Instance Method Details

#pump_fh(data_channel, fh = $stdin) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/stripchart/app.rb', line 44

def pump_fh(data_channel, fh=$stdin)
  @buffer ||= ""
  data = @buffer + fh.read_nonblock(100_000)
  samples = {}
  data.lines.each do |line|
    if line.end_with?("\n")
      if line =~ /^(.*\S)\s+([\d.]+)$/
        samples[$1] = $2.to_f
      end
    else
      # This should be the last, partial line.
      @buffer = line
    end
  end

  offset = Time.now - start_time
  samples = samples.map { |name,value| { :name => name, :value => value } }

  data_channel.push :offset => offset, :samples => samples

rescue EOFError
  EM.stop

rescue IO::WaitReadable
  # nothing to read
end

#run!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/stripchart/app.rb', line 28

def run!
  @timers = []
  EventMachine.run do
    data_channel = EM::Channel.new
    EM::PeriodicTimer.new(0.2) { pump_fh(data_channel) }
    WebSocket.new(data_channel).run!
    Thread.new do
      puts "Reading data from stdin..."
      Web.new(data_channel).run! # This doesn't return until sinatra exits. (Sinatra handles SIGINT.)
      puts "Exit..."
      EM.stop
      exit
    end
  end
end