Class: Reader::Watchman

Inherits:
Object
  • Object
show all
Includes:
Renderer
Defined in:
lib/city_watch/reader/watchman.rb

Instance Method Summary collapse

Methods included from Renderer

#render, #render_bare

Constructor Details

#initialize(host, name) ⇒ Watchman

Returns a new instance of Watchman.



7
8
9
10
# File 'lib/city_watch/reader/watchman.rb', line 7

def initialize(host,name)
	@host = host
	@name = name
end

Instance Method Details

#data_set_key(data_set) ⇒ Object



42
43
44
# File 'lib/city_watch/reader/watchman.rb', line 42

def data_set_key(data_set)
	key("data_set::#{data_set}")
end

#data_setsObject



46
47
48
# File 'lib/city_watch/reader/watchman.rb', line 46

def data_sets
	CityWatch.redis.smembers(data_sets_key)
end

#data_sets_keyObject



50
51
52
# File 'lib/city_watch/reader/watchman.rb', line 50

def data_sets_key
	@data_sets_key ||= key(:data_sets)
end

#get_data_set(data_set_name, s_time = (Time.now - (60*60*4)), e_time = Time.now) ⇒ Object



35
36
37
38
39
40
# File 'lib/city_watch/reader/watchman.rb', line 35

def get_data_set(data_set_name,s_time=(Time.now - (60*60*4)),e_time=Time.now)
	CityWatch.redis.zrevrangebyscore(data_set_key(data_set_name), e_time.to_i, s_time.to_i, :with_scores => true).map do |(val,score)|
		timestamp,value = val.split(",")
		[Time.at(timestamp.to_i), Float(value).to_i]
	end.reverse
end

#key(more) ⇒ Object



83
84
85
# File 'lib/city_watch/reader/watchman.rb', line 83

def key(more)
	"#{key_prefix}::#{more}"
end

#key_prefixObject



87
88
89
# File 'lib/city_watch/reader/watchman.rb', line 87

def key_prefix
	@key_prefix ||= "#{CityWatch.config[:prefix]}::#{@host}::#{@name}"
end

#render_from_summary(data) ⇒ Object



12
13
14
15
# File 'lib/city_watch/reader/watchman.rb', line 12

def render_from_summary(data)
	@current_data = data
	render_bare "watchmen/#{@name}/summary", "watchmen/summary"
end

#sparkline(dat) ⇒ Object



54
55
56
57
58
# File 'lib/city_watch/reader/watchman.rb', line 54

def sparkline(dat)
	require 'base64'
	require 'city_watch/util/spark_pr'
	Base64.encode64(Spark.smooth(dat, :height => 14, :step => 4).to_png).gsub("\n",'')
end

#sparkline_for(set) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/city_watch/reader/watchman.rb', line 64

def sparkline_for(set)
	dat = get_data_set(set)
	if dat.length > 1
		puts dat.inspect
		require 'base64'
		require 'city_watch/util/spark_pr'
		Base64.encode64(Spark.smooth(dat.map {|(tm,val)| val }, :height => 14, :step => 4).to_png).gsub("\n",'')
	end
end

#sparkline_image_tagsObject



27
28
29
30
31
32
33
# File 'lib/city_watch/reader/watchman.rb', line 27

def sparkline_image_tags
	data_sets.map do |set|
		dat = get_data_set(set)
		puts dat.inspect
		[set.to_sym, dat.length > 1 ? sparkline_img_tag(dat.map {|(tm,val)| val }, "#{set}") : nil]
	end.select {|(name,tag)| !tag.nil? }
end

#sparkline_img_tag(dat, alt = "") ⇒ Object



60
61
62
# File 'lib/city_watch/reader/watchman.rb', line 60

def sparkline_img_tag(dat,alt="")
	"<img src=\"data:image/png;base64,#{sparkline(dat)}\" alt=\"#{alt}\" title=\"#{alt} max: #{dat.max} min: #{dat.min}\"/>"
end

#sparkline_img_tag_for(set) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/city_watch/reader/watchman.rb', line 74

def sparkline_img_tag_for(set)
	dat = get_data_set(set)
	if dat.length > 1
		sparkline_img_tag(dat.map {|(tm,val)| val },set)
	else
		""
	end
end

#sparklinesObject



21
22
23
24
25
# File 'lib/city_watch/reader/watchman.rb', line 21

def sparklines
	data_sets.map do |set|
		[set.to_sym,sparkline(get_data_set(set).map {|(tm,val)| val })]
	end
end

#watchman_classObject



17
18
19
# File 'lib/city_watch/reader/watchman.rb', line 17

def watchman_class
	@klass ||= Object.const_get(@name.to_sym)
end