-
Notifications
You must be signed in to change notification settings - Fork 3
/
timer.coffee
153 lines (116 loc) · 4.89 KB
/
timer.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
class Dashing.Timer extends Dashing.Widget
displayError:(msg) ->
$(@node).find(".error").show()
$(@node).find(".error").html(msg)
displayMissingDependency:(name,url) ->
error_html = "<h1>Missing #{name}</h1><p>Download <a href='#{url}'>#{name}</a> and place it in the <span class='highlighted'>assets/javascripts</span> folder"
@displayError(error_html)
ready: ->
@displayMissingDependency("moment.js","http://momentjs.com/downloads/moment.min.js") if (!window.moment)
@displayMissingDependency("lodash.js","https://raw.githubusercontent.com/lodash/lodash/2.4.1/dist/lodash.min.js") if (!window._)
@displayMissingDependency("jQuery Sparkline","http://omnipotent.net/jquery.sparkline/#s-about") if (!$.fn.sparkline)
if @get('debug')
@debug = (@get('debug'))
else
@debug = false
# use the data-unit property in the widget tag to indicate the unit to display (Default:ms)
if typeof @get('unit') isnt "undefined"
@unit = (@get('unit'))
else
@unit = "ms"
# use the graphite_host property in the widget tag to indicate the graphite host (Default:our P2 graphite host)
if @get('graphite_host')
@graphite_host = (@get('graphite_host'))
else
@graphite_host = "http://graphite"
$n = $(@node)
# The widget looks at 24 hours worth of data in 10 minutes increment and compares it to the same day a week ago
targets= ["summarize(#{@get('metric')},'10min','avg')",
"timeShift(summarize(#{@get('metric')},'10min','avg'),'7d')"]
console.dir targets if @debug
@encoded_target = _.reduce(targets, (memo,target,key) ->
memo += "&target=#{target}"
,"")
if @get('interval')
interval = parseInt(@get('interval'))
else
interval = 60000
self = this
console.dir self if @debug
setInterval ->
self.updateGraph()
, interval
@updateGraph()
setInterval ->
self.updateSparkline()
, interval*100
@updateSparkline()
updateGraph: ->
graph_data_url = "#{@graphite_host}/render?format=json#{@encoded_target}"
console.log graph_data_url if @debug
$.getJSON graph_data_url,
from: '-1d'
until: 'now',
renderResults.bind(@)
updateSparkline: ->
metric = @get('metric')
target = "&target=summarize(#{metric},'8h','avg')"
graph_data_url = "#{@graphite_host}/render?format=json#{target}"
console.log "Month worth of data in 8 hours increments for #{metric} grabbed from: #{graph_data_url}" if @debug
$.getJSON graph_data_url,
from: '-30d'
until: 'now',
renderSparkline.bind(@)
renderSparkline = (data) ->
console.dir(data) if @debug
dataset = _.compact(roundUpArrayValues(removeTimestampFromTuple(data[0].datapoints)))
console.log(dataset.length)
if dataset.length>1
$(@node).find(".sparkline-chart").sparkline(dataset, {
type: 'line',
chartRangeMin: 0,
drawNormalOnTop: true,
normalRangeMax: 3000,
width:'12em',
normalRangeColor: '#336699'})
else
$(@node).find(".sparkline").hide()
renderResults = (data) ->
dataAverage = Math.floor(array_values_average(_.compact(removeTimestampFromTuple(data[0].datapoints))))
dataAverage_minus1w = Math.floor(array_values_average(_.compact(removeTimestampFromTuple(data[1].datapoints))))
change_rate = Math.floor(dataAverage/dataAverage_minus1w*100) - 100
$(@node).find(".change-rate i").removeClass("icon-arrow-up").removeClass("icon-arrow-down")
if isNaN change_rate
change_rate = "No data for -7d"
$(@node).find(".change-rate").css("font-size","1em")
$(@node).find(".change-rate").css("line-height","40px")
else if change_rate>0
$(@node).find(".change-rate").css("color","red")
change_rate=change_rate+"%"
$(@node).find(".change-rate i").addClass("icon-arrow-up")
else if change_rate==0
$(@node).find(".change-rate").css("color","white")
change_rate="no change"
$(@node).find(".change-rate").css("font-size","1em")
$(@node).find(".change-rate").css("line-height","40px")
else
$(@node).find(".change-rate").css("color","green")
change_rate=change_rate+"%"
$(@node).find(".change-rate i").addClass("icon-arrow-down")
unit = @unit
if isNaN dataAverage
$(@node).find(".value").text("N/A").fadeOut().fadeIn()
else
$(@node).find(".value").html("#{dataAverage}<span style='font-size:.3em;'>#{unit}</span>").fadeOut().fadeIn()
$(@node).find(".change-rate span").text("#{change_rate}")
$(@node).find(".change-rate span").fadeOut().fadeIn()
$(@node).find(".updated-at").text(moment().format('MMMM Do YYYY, h:mmA')).fadeOut().fadeIn()
return
removeTimestampFromTuple = (arr) ->
_.map(arr, (num) -> num[0])
roundUpArrayValues = (arr) ->
_.map(arr, (num) -> Math.floor(num))
array_values_average = (arr) ->
_.reduce(arr, (memo, num) ->
memo + num
, 0) / arr.length